/** * Finds the {@link Discoverable} for the given {@link HttpRequest} to route to. */ private Discoverable getDiscoverable(HttpRequest httpRequest) { EndpointStrategy strategy = serviceLookup.getDiscoverable(httpRequest); if (strategy == null) { throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No endpoint strategy found for request " + getRequestLine(httpRequest)); } // Do a non-blocking pick first. If the service has been discovered before, this should return an endpoint // immediately. Discoverable discoverable = strategy.pick(); if (discoverable != null) { return discoverable; } // Do a blocking pick for up to 1 second. It is for the case where a service is being discovered for the first time, // in which population of the cache might take time. discoverable = strategy.pick(1, TimeUnit.SECONDS); if (discoverable == null) { throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No discoverable found for request " + getRequestLine(httpRequest)); } return discoverable; }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { HttpResponse response = cause instanceof HandlerException ? ((HandlerException) cause).createFailureResponse() : createErrorResponse(cause); HttpUtil.setKeepAlive(response, false); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
/** * Executes an admin operation on a dataset instance. * * @param namespaceId namespace of the dataset instance * @param name name of the dataset instance * @param method the admin operation to execute (e.g. "exists", "truncate", "upgrade") * @throws Exception */ @POST @Path("/data/datasets/{name}/admin/{method}") public void executeAdmin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name, @PathParam("method") String method) throws Exception { logCallReceived(request); DatasetId instance = ConversionHelpers.toDatasetInstanceId(namespaceId, name); try { responder.sendJson(HttpResponseStatus.OK, GSON.toJson(instanceService.executeAdmin(instance, method))); } catch (HandlerException e) { responder.sendStatus(e.getFailureStatus()); } logCallResponded(request); }
/** * Creates a new dataset instance. * * @param namespaceId namespace of the new dataset instance * @param name name of the new dataset instance */ @PUT @Path("/data/datasets/{name}") @AuditPolicy(AuditDetail.REQUEST_BODY) public void create(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) throws Exception { logCallReceived(request); DatasetInstanceConfiguration creationProperties = ConversionHelpers.getInstanceConfiguration(request); try { instanceService.create(namespaceId, name, creationProperties); responder.sendStatus(HttpResponseStatus.OK); } catch (DatasetAlreadyExistsException e) { responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage()); } catch (IllegalArgumentException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (DatasetTypeNotFoundException e) { responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (HandlerException e) { responder.sendString(e.getFailureStatus(), e.getMessage()); } logCallResponded(request); }
/** * Creates a new dataset instance. * * @param namespaceId namespace of the new dataset instance * @param name name of the new dataset instance */ @PUT @Path("/data/datasets/{name}") @AuditPolicy(AuditDetail.REQUEST_BODY) public void create(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) throws Exception { logCallReceived(request); DatasetInstanceConfiguration creationProperties = ConversionHelpers.getInstanceConfiguration(request); try { instanceService.create(namespaceId, name, creationProperties); responder.sendStatus(HttpResponseStatus.OK); } catch (DatasetAlreadyExistsException e) { responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage()); } catch (IllegalArgumentException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (DatasetTypeNotFoundException e) { responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (HandlerException e) { responder.sendString(e.getFailureStatus(), e.getMessage()); } logCallResponded(request); }
private void verifyResponse(HttpResponse httpResponse) throws ConflictException { if (httpResponse.getResponseCode() == 409) { throw new ConflictException(httpResponse.getResponseBodyAsString(Charsets.UTF_8)); } if (httpResponse.getResponseCode() != 200) { throw new HandlerException(HttpResponseStatus.valueOf(httpResponse.getResponseCode()), httpResponse.getResponseBodyAsString(Charsets.UTF_8)); } } }
/** * Executes an admin operation on a dataset instance. * * @param namespaceId namespace of the dataset instance * @param name name of the dataset instance * @param method the admin operation to execute (e.g. "exists", "truncate", "upgrade") * @throws Exception */ @POST @Path("/data/datasets/{name}/admin/{method}") public void executeAdmin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name, @PathParam("method") String method) throws Exception { logCallReceived(request); DatasetId instance = ConversionHelpers.toDatasetInstanceId(namespaceId, name); try { responder.sendJson(HttpResponseStatus.OK, GSON.toJson(instanceService.executeAdmin(instance, method))); } catch (HandlerException e) { responder.sendStatus(e.getFailureStatus()); } logCallResponded(request); }
private void verifyResponse(HttpResponse httpResponse) throws ConflictException { if (httpResponse.getResponseCode() == 409) { throw new ConflictException(httpResponse.getResponseBodyAsString(Charsets.UTF_8)); } if (httpResponse.getResponseCode() != 200) { throw new HandlerException(HttpResponseStatus.valueOf(httpResponse.getResponseCode()), httpResponse.getResponseBodyAsString(Charsets.UTF_8)); } } }
break; default: throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);
break; default: throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);