private Map<String, ExtractorResult<Node>> getLocalReposeInstances(SystemModel systemModel) {

    Map<String, ExtractorResult<Node>> updatedSystem = new HashMap<String, ExtractorResult<Node>>();

    for (ReposeCluster cluster : systemModel.getReposeCluster()) {
      for (Node node : cluster.getNodes().getNode()) {
        if (NetUtilities.isLocalHost(node.getHostname())) {
          updatedSystem.put(
              cluster.getId()
                  + node.getId()
                  + node.getHostname()
                  + node.getHttpPort()
                  + node.getHttpsPort(),
              new ExtractorResult<Node>(cluster.getId(), node));
        }
      }
    }

    return updatedSystem;
  }
  @Override
  public void initialize(
      ReposeCluster domain, Node localhost, ServletContext context, String defaultDst)
      throws PowerFilterChainException {
    if (localhost == null || domain == null) {
      throw new PowerFilterChainException("Domain and localhost cannot be null");
    }

    LOG.info("Initializing Repose Router");
    this.domain = domain;
    this.context = context;
    this.defaultDst = defaultDst;
    this.destinations.clear();
    this.locationBuilder.init(localhost);

    if (domain.getDestinations() != null) {
      addDestinations(domain.getDestinations().getEndpoint());
      addDestinations(domain.getDestinations().getTarget());
    }
  }
 @Override
 public void configurationUpdated(SystemModel config) {
   ReposeCluster serviceDomain = interrogator.getLocalServiceDomain(config);
   proxyService.setRewriteHostHeader(serviceDomain.isRewriteHostHeader());
   isInitialized = true;
 }
  @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());
            }
          }
        }
      }
    }
  }