Пример #1
0
  /*
   * Creates the URL based on the configured host, port, and services context root path.
   */
  private UriBuilder generateEndpointUrl(String path, UriBuilder uriBuilder)
      throws UnknownHostException {
    UriBuilder builder = uriBuilder;
    if (host != null && port != null && servicesContextRoot != null) {
      builder = builder.host(host);

      try {
        int portInt = Integer.parseInt(port);
        builder = builder.port(portInt);
      } catch (NumberFormatException nfe) {
        LOGGER.debug(
            "Cannot convert the current DDF port: {} to an integer."
                + " Defaulting to port in invocation.",
            port);
        throw new UnknownHostException("Unable to determine port DDF is using.");
      }

      builder = builder.replacePath(path);
    } else {
      LOGGER.debug("DDF Port is null, unable to determine host DDF is running on.");
      throw new UnknownHostException("Unable to determine port DDF is using.");
    }

    return builder;
  }
Пример #2
0
  /**
   * Extracts an UriBuilder for the current request, taking into account the possibility of
   * header-based URI override.
   *
   * @param uriInfo
   * @param httpHeaders
   * @return
   * @throws URISyntaxException
   */
  public static UriBuilder getUriBuilder(final UriInfo uriInfo, final HttpHeaders httpHeaders)
      throws URISyntaxException {
    final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();

    final List<String> hosts = httpHeaders.getRequestHeader(HttpHeaders.HOST);
    if ((hosts != null) && (!hosts.isEmpty())) {
      final String host = hosts.get(0);
      uriBuilder.host(StringUtils.substringBefore(host, ":"));

      final String port = StringUtils.substringAfter(host, ":");
      if (StringUtils.isNotBlank(port)) {
        uriBuilder.port(Integer.valueOf(port));
      }
    }

    final String protocol = getSingleHeader(httpHeaders, Constants.FORWARDED_PROTOCOL_HTTP_HEADER);
    if (StringUtils.isNotBlank(protocol)) {
      uriBuilder.scheme(protocol);
    }

    return uriBuilder;
  }