/**
   * Get the URL of the file to download. This has not been modified from the original version.
   *
   * <p>For more information, please see: <a href=
   * "https://collegereadiness.collegeboard.org/educators/higher-ed/reporting-portal-help#features">
   * https://collegereadiness.collegeboard.org/educators/higher-ed/reporting-
   * portal-help#features</a>
   *
   * <p>Original code can be accessed at: <a href=
   * "https://collegereadiness.collegeboard.org/zip/pascoredwnld-java-sample.zip">
   * https://collegereadiness.collegeboard.org/zip/pascoredwnld-java-sample.zip </a>
   *
   * @see #login(String, String)
   * @see org.collegeboard.scoredwnld.client.FileInfo
   * @author CollegeBoard
   * @param accessToken Access token obtained from {@link #login(String, String)}
   * @param filePath File to download
   * @return FileInfo descriptor of file to download
   */
  private FileInfo getFileUrlByToken(String accessToken, String filePath) {

    Client client = getClient();
    WebResource webResource =
        client.resource(
            scoredwnldUrlRoot + "/pascoredwnld/file?tok=" + accessToken + "&filename=" + filePath);
    ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
    if (response.getStatus() != 200) {
      throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    try {
      JSONObject json = new JSONObject(response.getEntity(String.class));
      FileInfo fileInfo = new FileInfo();
      fileInfo.setFileName(filePath);
      fileInfo.setFileUrl(String.valueOf(json.get("fileUrl")));
      return fileInfo;
    } catch (ClientHandlerException e) {
      log("Error: " + e.getMessage());
      e.printStackTrace();
    } catch (UniformInterfaceException e) {
      log("Error: " + e.getMessage());
      e.printStackTrace();
    } catch (JSONException e) {
      log("Error: " + e.getMessage());
      e.printStackTrace();
    }

    return null;
  }
  private List<AuthGroup> getAuthGroups(AuthToken token) {
    if (token != null && requestGroups) {

      AuthGroups authGroups = checkGroupCache(token);

      if (authGroups == null) {
        try {
          authGroups = getGroups(token.getUserId());
          cacheGroupInfo(token, authGroups);
        } catch (ClientHandlerException ex) {
          LOG.error(
              "Failure communicating with the auth service when retrieving groups: "
                  + ex.getMessage(),
              ex);
          LOG.error("X-PP-Groups will not be set.");
        } catch (Exception ex) {
          LOG.error("Failure in auth when retrieving groups: " + ex.getMessage(), ex);
          LOG.error("X-PP-Groups will not be set.");
        }
      }

      if (authGroups != null && authGroups.getGroups() != null) {
        return authGroups.getGroups();
      }
    }
    return new ArrayList<AuthGroup>();
  }
  private FilterDirector authenticate(HttpServletRequest request) {
    final FilterDirector filterDirector = new FilterDirectorImpl();
    filterDirector.setResponseStatus(HttpStatusCode.UNAUTHORIZED);
    filterDirector.setFilterAction(FilterAction.RETURN);

    final String authToken = request.getHeader(CommonHttpHeader.AUTH_TOKEN.toString());
    ExtractorResult<String> account = null;
    AuthToken token = null;

    if (tenanted) {
      account = extractAccountIdentification(request);
    }

    final boolean allow = allowAccount(account);

    if ((!StringUtilities.isBlank(authToken) && allow)) {
      token = checkToken(account, authToken);

      if (token == null) {
        try {
          token = validateToken(account, StringUriUtilities.encodeUri(authToken));
          cacheUserInfo(token);
        } catch (ClientHandlerException ex) {
          LOG.error("Failure communicating with the auth service: " + ex.getMessage(), ex);
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (AuthServiceException ex) {
          LOG.error("Failure in Auth-N: " + ex.getMessage());
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (IllegalArgumentException ex) {
          LOG.error("Failure in Auth-N: " + ex.getMessage());
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (Exception ex) {
          LOG.error("Failure in auth: " + ex.getMessage(), ex);
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        }
      }
    }

    List<AuthGroup> groups = getAuthGroups(token);

    setFilterDirectorValues(
        authToken,
        token,
        delegable,
        filterDirector,
        account == null ? "" : account.getResult(),
        groups);

    return filterDirector;
  }
  @Override
  public void deployPackage(
      PackageModel packageRN,
      String token,
      InputStream stream,
      Map<String, DatabaseAction> associatedDatabases) {
    try {
      install(stream, packageRN, associatedDatabases);

      String name = packageRN.getUrlPath();
      if (packageRN.getUrlPath() == null || packageRN.getUrlPath().isEmpty()) name = "ROOT";

      name = name + PackageFileType.APP_WAR.getDotExtension();
      String file = packageRN.getName() + PackageFileType.APP_WAR.getDotExtension();
      progress.status("... deploying application: " + name, 1, LogLevel.INFO);
      requestTaskService.deploy(packageRN.getId(), name, file);
      progress.status("Archive version", 2, LogLevel.INFO);
      OPFEngine.RegistryService.archive(packageRN.getId());
      progress.status("Installation process has been completed", 2, LogLevel.INFO);
    } catch (ClientHandlerException e) {
      if (e.getCause() instanceof FileNotFoundException) {
        progress.status("Package archive has not been found", 1, LogLevel.ERROR);
      } else if (e.getCause() instanceof ConnectException) {
        progress.status("Connection refused", 1, LogLevel.ERROR);
      } else {
        progress.status("Occurred error", 1, LogLevel.ERROR);
      }
      logger.error(e.getMessage(), e);
    } catch (Exception e) {
      progress.status("Occurred error", 1, LogLevel.ERROR);
      logger.error(e.getMessage(), e);
    } finally {
      if (stream != null) {
        IOUtils.closeQuietly(stream);
      }
    }
  }
  @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());
            }
          }
        }
      }
    }
  }