private String createHttpUrl(String host, String port, String path) {
   UriBuilder ub = UriBuilder.fromPath(path);
   ub.scheme("http").host(host);
   if (port != null && port.isEmpty()) {
     ub.port(Integer.parseInt(port));
   }
   return ub.build().toString();
 }
Ejemplo n.º 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;
  }