@Override public void configurationUpdated(SystemModel configurationObject) { SystemModelInterrogator interrogator = new SystemModelInterrogator(ports); Optional<ReposeCluster> cluster = interrogator.getLocalCluster(configurationObject); Optional<Node> node = interrogator.getLocalNode(configurationObject); if (cluster.isPresent() && node.isPresent()) { localDomain = cluster.get(); localHost = node.get(); List<Destination> destinations = new ArrayList<>(); destinations.addAll(localDomain.getDestinations().getEndpoint()); destinations.addAll(localDomain.getDestinations().getTarget()); for (Destination powerApiHost : destinations) { configuredHosts.put(powerApiHost.getId(), powerApiHost); } isInitialized = true; healthCheckServiceProxy.resolveIssue(SYSTEM_MODEL_CONFIG_HEALTH_REPORT); } else { LOG.error( "Unable to identify the local host in the system model - please check your system-model.cfg.xml"); healthCheckServiceProxy.reportIssue( SYSTEM_MODEL_CONFIG_HEALTH_REPORT, "Unable to identify the " + "local host in the system model - please check your system-model.cfg.xml", Severity.BROKEN); } }
@Override public void route( MutableHttpServletRequest servletRequest, MutableHttpServletResponse servletResponse) throws IOException, ServletException, URISyntaxException { DestinationLocation location = null; if (!StringUtilities.isBlank(defaultDst)) { servletRequest.addDestination(defaultDst, servletRequest.getRequestURI(), -1); } RouteDestination routingDestination = servletRequest.getDestination(); String rootPath = ""; if (routingDestination != null) { Destination configDestinationElement = destinations.get(routingDestination.getDestinationId()); if (configDestinationElement == null) { LOG.warn( "Invalid routing destination specified: " + routingDestination.getDestinationId() + " for domain: " + domain.getId()); ((HttpServletResponse) servletResponse).setStatus(HttpStatusCode.NOT_FOUND.intValue()); } else { location = locationBuilder.build( configDestinationElement, routingDestination.getUri(), servletRequest); rootPath = configDestinationElement.getRootPath(); } } if (location != null) { // According to the Java 6 javadocs the routeDestination passed into getContext: // "The given path [routeDestination] must begin with /, is interpreted relative to the // server's document root // and is matched against the context roots of other web applications hosted on this // container." final ServletContext targetContext = context.getContext(location.getUri().toString()); if (targetContext != null) { // Capture this for Location header processing final HttpServletRequest originalRequest = (HttpServletRequest) servletRequest.getRequest(); String uri = new DispatchPathBuilder(location.getUri().getPath(), targetContext.getContextPath()) .build(); final RequestDispatcher dispatcher = targetContext.getRequestDispatcher(uri); servletRequest.setRequestUrl(new StringBuffer(location.getUrl().toExternalForm())); servletRequest.setRequestUri(location.getUri().getPath()); requestHeaderService.setVia(servletRequest); requestHeaderService.setXForwardedFor(servletRequest); if (dispatcher != null) { LOG.debug("Attempting to route to " + location.getUri()); LOG.debug("Request URL: " + ((HttpServletRequest) servletRequest).getRequestURL()); LOG.debug("Request URI: " + ((HttpServletRequest) servletRequest).getRequestURI()); LOG.debug("Context path = " + targetContext.getContextPath()); final long startTime = System.currentTimeMillis(); try { reportingService.incrementRequestCount(routingDestination.getDestinationId()); dispatcher.forward(servletRequest, servletResponse); final long stopTime = System.currentTimeMillis(); reportingService.recordServiceResponse( routingDestination.getDestinationId(), servletResponse.getStatus(), (stopTime - startTime)); responseHeaderService.fixLocationHeader( originalRequest, servletResponse, routingDestination, location.getUri().toString(), rootPath); } catch (ClientHandlerException e) { if (e.getCause() instanceof ReadLimitReachedException) { LOG.error("Error reading request content", e); servletResponse.sendError( HttpStatusCode.REQUEST_ENTITY_TOO_LARGE.intValue(), "Error reading request content"); servletResponse.setLastException(e); } else { LOG.error("Connection Refused to " + location.getUri() + " " + e.getMessage(), e); ((HttpServletResponse) servletResponse) .setStatus(HttpStatusCode.SERVICE_UNAVAIL.intValue()); } } } } } }
private void addDestinations(List<? extends Destination> destList) { for (Destination dest : destList) { destinations.put(dest.getId(), dest); } }