@Test public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException { String exceptionMsg = TestUtils.getRandomString(10); responseHandler.shutdown(); try { RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null); assertTrue("RestRequest channel is not open", restRequest.isOpen()); MockRestResponseChannel restResponseChannel = new MockRestResponseChannel(); frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg)); assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage()); restRequest = createRestRequest(RestMethod.GET, "/", null, null); assertTrue("RestRequest channel is not open", restRequest.isOpen()); restResponseChannel = new MockRestResponseChannel(); ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0)); assertTrue("Response channel is not open", response.isOpen()); frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null); assertNotNull("There is no cause of failure", restResponseChannel.getException()); assertFalse("Response channel is not cleaned up", response.isOpen()); } finally { frontendRestRequestService.setupResponseHandler(responseHandler); responseHandler.start(); } RestRequest restRequest; MockRestResponseChannel restResponseChannel; for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) { restRequest = createRestRequest(RestMethod.GET, "/", null, null); restResponseChannel = new MockRestResponseChannel(); frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null); assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null); } restRequest = createRestRequest(RestMethod.GET, "/", null, null); restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true)); restResponseChannel = new MockRestResponseChannel(); frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null); assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME)); assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME)); }
void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) { try { if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs() .get(InternalKeys.SEND_TRACKING_INFO)) { restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName); restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname); } if (exception instanceof RouterException) { exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode())); } responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception); } catch (Exception e) { frontendMetrics.responseSubmissionError.inc(); if (exception != null) { logger.error("Error submitting response to response handler", e); } else { exception = e; } logger.error("Handling of request {} failed", restRequest.getUri(), exception); restResponseChannel.onResponseComplete(exception); if (responseBody != null) { try { responseBody.close(); } catch (IOException ioe) { frontendMetrics.resourceReleaseError.inc(); logger.error("Error closing ReadableStreamChannel", e); } } } }
FrontendRestRequestService implements RestRequestService { void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) { try { if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs() .get(InternalKeys.SEND_TRACKING_INFO)) { restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName); restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname); } if (exception instanceof RouterException) { exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode())); } responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception); } catch (Exception e) { frontendMetrics.responseSubmissionError.inc(); if (exception != null) { logger.error("Error submitting response to response handler", e); } else { exception = e; } logger.error("Handling of request {} failed", restRequest.getUri(), exception); restResponseChannel.onResponseComplete(exception); if (responseBody != null) { try { responseBody.close(); } catch (IOException ioe) { frontendMetrics.resourceReleaseError.inc(); logger.error("Error closing ReadableStreamChannel", e); } } } } }
FrontendRestRequestService implements RestRequestService { void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) { try { if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs() .get(InternalKeys.SEND_TRACKING_INFO)) { restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName); restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname); } if (exception instanceof RouterException) { exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode())); } responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception); } catch (Exception e) { frontendMetrics.responseSubmissionError.inc(); if (exception != null) { logger.error("Error submitting response to response handler", e); } else { exception = e; } logger.error("Handling of request {} failed", restRequest.getUri(), exception); restResponseChannel.onResponseComplete(exception); if (responseBody != null) { try { responseBody.close(); } catch (IOException ioe) { frontendMetrics.resourceReleaseError.inc(); logger.error("Error closing ReadableStreamChannel", e); } } } } FrontendRestRequestService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics, Router router,
      ClusterMap clusterMap, IdConverterFactory idConverterFactory, SecurityServiceFactory securityServiceFactory,
      UrlSigningService urlSigningService, IdSigningService idSigningService, AccountService accountService,
      AccountAndContainerInjector accountAndContainerInjector, String datacenterName, String hostname,
      String clusterName); }
FrontendRestRequestService implements RestRequestService { void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) { try { if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs() .get(InternalKeys.SEND_TRACKING_INFO)) { restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName); restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname); } if (exception instanceof RouterException) { exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode())); } responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception); } catch (Exception e) { frontendMetrics.responseSubmissionError.inc(); if (exception != null) { logger.error("Error submitting response to response handler", e); } else { exception = e; } logger.error("Handling of request {} failed", restRequest.getUri(), exception); restResponseChannel.onResponseComplete(exception); if (responseBody != null) { try { responseBody.close(); } catch (IOException ioe) { frontendMetrics.resourceReleaseError.inc(); logger.error("Error closing ReadableStreamChannel", e); } } } } FrontendRestRequestService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics, Router router,
      ClusterMap clusterMap, IdConverterFactory idConverterFactory, SecurityServiceFactory securityServiceFactory,
      UrlSigningService urlSigningService, IdSigningService idSigningService, AccountService accountService,
      AccountAndContainerInjector accountAndContainerInjector, String datacenterName, String hostname,
      String clusterName); @Override void setupResponseHandler(RestResponseHandler responseHandler); @Override void start(); @Override void shutdown(); @Override void handleGet(final RestRequest restRequest, final RestResponseChannel restResponseChannel); @Override void handlePost(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handlePut(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleDelete(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleHead(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleOptions(RestRequest restRequest, RestResponseChannel restResponseChannel); }
FrontendRestRequestService implements RestRequestService { void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) { try { if (restRequest.getArgs().containsKey(InternalKeys.SEND_TRACKING_INFO) && (Boolean) restRequest.getArgs() .get(InternalKeys.SEND_TRACKING_INFO)) { restResponseChannel.setHeader(TrackingHeaders.DATACENTER_NAME, datacenterName); restResponseChannel.setHeader(TrackingHeaders.FRONTEND_NAME, hostname); } if (exception instanceof RouterException) { exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode())); } responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception); } catch (Exception e) { frontendMetrics.responseSubmissionError.inc(); if (exception != null) { logger.error("Error submitting response to response handler", e); } else { exception = e; } logger.error("Handling of request {} failed", restRequest.getUri(), exception); restResponseChannel.onResponseComplete(exception); if (responseBody != null) { try { responseBody.close(); } catch (IOException ioe) { frontendMetrics.resourceReleaseError.inc(); logger.error("Error closing ReadableStreamChannel", e); } } } } FrontendRestRequestService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics, Router router,
      ClusterMap clusterMap, IdConverterFactory idConverterFactory, SecurityServiceFactory securityServiceFactory,
      UrlSigningService urlSigningService, IdSigningService idSigningService, AccountService accountService,
      AccountAndContainerInjector accountAndContainerInjector, String datacenterName, String hostname,
      String clusterName); @Override void setupResponseHandler(RestResponseHandler responseHandler); @Override void start(); @Override void shutdown(); @Override void handleGet(final RestRequest restRequest, final RestResponseChannel restResponseChannel); @Override void handlePost(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handlePut(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleDelete(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleHead(RestRequest restRequest, RestResponseChannel restResponseChannel); @Override void handleOptions(RestRequest restRequest, RestResponseChannel restResponseChannel);  }