org.apache.http.client.utils.URIUtils Java Examples

The following examples show how to use org.apache.http.client.utils.URIUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PentahoParameterRefreshHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * HttpClient does not support preemptive authentication out of the box, because if misused or used incorrectly the
 * preemptive authentication can lead to significant security issues, such as sending user credentials in clear text
 * to an unauthorized third party. Therefore, users are expected to evaluate potential benefits of preemptive
 * authentication versus security risks in the context of their specific application environment.
 *
 * Nonetheless one can configure HttpClient to authenticate preemptively by prepopulating the authentication data cache.
 *
 * @see https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
 *
 * @param target target URI
 * @param auth login data
 * @return
 */
private HttpClientContext buildPreemptiveAuthRequestContext( final URI target, final AuthenticationData auth ) {

  if ( target == null || auth == null || StringUtils.isEmpty( auth.getUsername() ) ) {
    return null; // nothing to do here; if no credentials were passed, there's no need to create a preemptive auth Context
  }

  HttpHost targetHost = URIUtils.extractHost( target );

  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials( new AuthScope( targetHost.getHostName(), targetHost.getPort() ),
      new UsernamePasswordCredentials( auth.getUsername(), auth.getPassword() ) );

  // Create AuthCache instance
  AuthCache authCache = new BasicAuthCache();

  // Generate BASIC scheme object and add it to the local auth cache
  BasicScheme basicAuth = new BasicScheme();
  authCache.put( targetHost, basicAuth );

  HttpClientContext context = HttpClientContext.create();
  context.setCredentialsProvider( credsProvider );
  context.setAuthCache( authCache );

  return context;
}
 
Example #2
Source File: BitlyUrlService.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Make a GET request and append the Map of parameters onto the query string.
 * @param address		the fully qualified URL to make the request to
 * @param parameters	the Map of parameters, ie key,value pairs
 * @return
 */
private String doGet(String address, Map<String, String> parameters){
	try {
		
		List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
		
		for (Map.Entry<String,String> entry : parameters.entrySet()) {
			queryParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		
		URI uri = URIUtils.createURI(null, address, -1, null, URLEncodedUtils.format(queryParams, "UTF-8"), null);
		
		log.info(uri.toString());
		
		return doGet(uri.toString());
	
	} catch (URISyntaxException e) {
		log.error(e.getClass() + ":" + e.getMessage());
	}
	return null;
}
 
Example #3
Source File: HttpClientManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates absolute request URI with full path from passed in context.
 */
@Nonnull
private URI getRequestURI(final HttpContext context) {
  final HttpClientContext clientContext = HttpClientContext.adapt(context);
  final HttpRequest httpRequest = clientContext.getRequest();
  final HttpHost target = clientContext.getTargetHost();
  try {
    URI uri;
    if (httpRequest instanceof HttpUriRequest) {
      uri = ((HttpUriRequest) httpRequest).getURI();
    }
    else {
      uri = URI.create(httpRequest.getRequestLine().getUri());
    }
    return uri.isAbsolute() ? uri : URIUtils.resolve(URI.create(target.toURI()), uri);
  }
  catch (Exception e) {
    log.warn("Could not create absolute request URI", e);
    return URI.create(target.toURI());
  }
}
 
Example #4
Source File: FormatClientSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
protected CloseableHttpResponse execute(final HttpUriRequest request, String username, String password)
    throws IOException
{
  log.debug("Authorizing request for {} using credentials provided for username: {}",
      request.getURI(), username);
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

  HttpHost host = URIUtils.extractHost(request.getURI());

  AuthCache authCache = new BasicAuthCache();
  authCache.put(host, new BasicScheme());

  HttpClientContext clientContext = new HttpClientContext(httpClientContext);
  clientContext.setAuthCache(authCache);
  clientContext.setCredentialsProvider(credsProvider);

  return execute(request, clientContext);
}
 
Example #5
Source File: FormatClientSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
protected CloseableHttpResponse execute(final HttpUriRequest request, String username, String password)
    throws IOException
{
  log.debug("Authorizing request for {} using credentials provided for username: {}",
      request.getURI(), username);
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

  HttpHost host = URIUtils.extractHost(request.getURI());

  AuthCache authCache = new BasicAuthCache();
  authCache.put(host, new BasicScheme());

  HttpClientContext clientContext = new HttpClientContext(httpClientContext);
  clientContext.setAuthCache(authCache);
  clientContext.setCredentialsProvider(credsProvider);

  return execute(request, clientContext);
}
 
Example #6
Source File: MDSInterface2.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected static MDSResult doPost(String scheme, String host, int port,
                                  String path,
                                  List<NameValuePair> postData) throws UnsupportedEncodingException {
    URI uri = null;
    try {
        uri = URIUtils.createURI(scheme, host, port, path, null, null);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IllegalArgumentException(String.format("Can not post to mds: %s, %s, %d, %s", scheme, host, port, path), e);
    }
    Log.d(TAG, "doPost() uri: " + uri.toASCIIString());
    HttpPost post = new HttpPost(uri);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData, "UTF-8");
    post.setEntity(entity);
    return MDSInterface2.doExecute(post);

}
 
Example #7
Source File: OTSUri.java    From aliyun-tablestore-java-sdk with Apache License 2.0 6 votes vote down vote up
public OTSUri(String endpoint, String action) {
    this.action = action;

    final String delimiter = "/";
    if (!endpoint.endsWith(delimiter)) {
        endpoint += delimiter;
    }

    // keep only one '/' in the end
    int index = endpoint.length() - 1;
    while (index > 0 && endpoint.charAt(index - 1) == '/') {
        index--;
    }

    endpoint = endpoint.substring(0, index + 1);

    try {
        this.uri = new URI(endpoint + action);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("The endpoint is invalid.", e);
    }

    this.host = URIUtils.extractHost(uri);
}
 
Example #8
Source File: MDSInterface2.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected static MDSResult doPost(String scheme,
                                  String host,
                                  int port,
                                  String path,
                                  HttpEntity entity) {
    URI uri = null;
    try {
        uri = URIUtils.createURI(scheme, host, port, path, null, null);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IllegalArgumentException(String.format("Can not post to mds: %s, %s, %d, %s", scheme, host, port, path), e);
    }
    Log.d(TAG, "doPost() uri: " + uri.toASCIIString());
    HttpPost post = new HttpPost(uri);
    post.setEntity(entity);
    return MDSInterface2.doExecute(post);
}
 
Example #9
Source File: BitlyUrlService.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Make a GET request and append the Map of parameters onto the query string.
 * @param address		the fully qualified URL to make the request to
 * @param parameters	the Map of parameters, ie key,value pairs
 * @return
 */
private String doGet(String address, Map<String, String> parameters){
	try {
		
		List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
		
		for (Map.Entry<String,String> entry : parameters.entrySet()) {
			queryParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		
		URI uri = URIUtils.createURI(null, address, -1, null, URLEncodedUtils.format(queryParams, "UTF-8"), null);
		
		log.info(uri.toString());
		
		return doGet(uri.toString());
	
	} catch (URISyntaxException e) {
		log.error(e.getClass() + ":" + e.getMessage());
	}
	return null;
}
 
Example #10
Source File: jr.java    From letv with Apache License 2.0 6 votes vote down vote up
public static String c(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    URI i = i(str);
    if (i == null) {
        return str;
    }
    i = i.normalize();
    if (i.isOpaque()) {
        return str;
    }
    i = URIUtils.resolve(i, "./");
    if (i != null) {
        return i.toString();
    }
    return str;
}
 
Example #11
Source File: AsyncInternalClient.java    From fc-java-sdk with MIT License 6 votes vote down vote up
protected <Res> void asyncSend(HttpRequest request, AbstractResponseConsumer<Res> consumer, FutureCallback<Res> callback,
                               String contentType, HttpMethod method, boolean httpInvoke)
        throws ClientException {
    try{
        // try refresh credentials if CredentialProvider set
        config.refreshCredentials();

        // Add all needed headers
        if (!httpInvoke
                || !ANONYMOUS.equals(((HttpInvokeFunctionRequest) request).getAuthType())) {
            FcUtil.signRequest(config, request, contentType, method, httpInvoke);
        }

        // Construct HttpRequest
        PrepareUrl prepareUrl = FcUtil.prepareUrl(request.getPath(), request.getQueryParams(), this.config);
        RequestBuilder requestBuilder = RequestBuilder.create(method.name())
                .setUri(prepareUrl.getUrl());
        copyToRequest(request, requestBuilder);
        HttpUriRequest httpRequest = requestBuilder.build();

        HttpHost httpHost = URIUtils.extractHost(httpRequest.getURI());
        httpClient.execute(new FcRequestProducer(httpHost, httpRequest), consumer, callback);
    } catch (Exception e) {
        throw new ClientException(e);
    }
}
 
Example #12
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 6 votes vote down vote up
protected void rewriteRequestURI(
        final RequestWrapper request,
        final HttpRoute route) throws ProtocolException {
    try {

        URI uri = request.getURI();
        if (route.getProxyHost() != null && !route.isTunnelled()) {
            // Make sure the request URI is absolute
            if (!uri.isAbsolute()) {
                HttpHost target = route.getTargetHost();
                uri = URIUtils.rewriteURI(uri, target);
                request.setURI(uri);
            }
        } else {
            // Make sure the request URI is relative
            if (uri.isAbsolute()) {
                uri = URIUtils.rewriteURI(uri, null);
                request.setURI(uri);
            }
        }

    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid URI: "
        		+ request.getRequestLine().getUri(), ex);
    }
}
 
Example #13
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
protected void checkPublicFolder() {
    // check public folder access
    try {
        publicFolderUrl = URIUtils.resolve(httpClientAdapter.getUri(), PUBLIC_ROOT).toString();
        DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet();
        davPropertyNameSet.add(Field.getPropertyName("displayname"));

        HttpPropfind httpPropfind = new HttpPropfind(publicFolderUrl, davPropertyNameSet, 0);
        httpClientAdapter.executeDavRequest(httpPropfind);
        // update public folder URI
        publicFolderUrl = httpPropfind.getURI().toString();

    } catch (IOException e) {
        LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage()));
        // default public folder path
        publicFolderUrl = PUBLIC_ROOT;
    }
}
 
Example #14
Source File: FinalLocationConsumer.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
public void consume(Map<String, String> returnResult) {
    URI location;
    try {
        location = URIUtils.resolve(uri, targetHost, redirectLocations);
    } catch (URISyntaxException e) {
        //this is not a fatal error
        throw new IllegalArgumentException("could not determine '" + HttpClientService.FINAL_LOCATION
                + "': " + e.getMessage(), e);
    }
    returnResult.put(HttpClientService.FINAL_LOCATION, location.toASCIIString());
}
 
Example #15
Source File: ProxyServlet.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
/** Copy request headers from the servlet client to the proxy request. */
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
    // Get an Enumeration of all of the header names sent by the client
    Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames();
    while (enumerationOfHeaderNames.hasMoreElements()) {
        String headerName = (String) enumerationOfHeaderNames.nextElement();
        //Instead the content-length is effectively set via InputStreamEntity
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH))
            continue;
        if (hopByHopHeaders.containsHeader(headerName))
            continue;

        Enumeration headers = servletRequest.getHeaders(headerName);
        while (headers.hasMoreElements()) {//sometimes more than one value
            String headerValue = (String) headers.nextElement();
            // In case the proxy host is running multiple virtual servers,
            // rewrite the Host header to ensure that we get content from
            // the correct virtual server
            if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) {
                HttpHost host = URIUtils.extractHost(this.targetUri);
                headerValue = host.getHostName();
                if (host.getPort() != -1)
                    headerValue += ":"+host.getPort();
            }
            proxyRequest.addHeader(headerName, headerValue);
        }
    }
}
 
Example #16
Source File: NFHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException {
	// A null target may be acceptable if there is a default target.
	// Otherwise, the null target is detected in the director.
	HttpHost target = null;
	URI requestURI = request.getURI();
	if (requestURI.isAbsolute()) {
		target = URIUtils.extractHost(requestURI);
		if (target == null) {
			throw new ClientProtocolException(
					"URI does not specify a valid host name: " + requestURI);
		}
	}
	return target;
}
 
Example #17
Source File: PrerenderSeoService.java    From prerender-java with MIT License 5 votes vote down vote up
/**
 * Copy request headers from the servlet client to the proxy request.
 *
 * @throws java.net.URISyntaxException
 */
private void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest)
        throws URISyntaxException {
    // Get an Enumeration of all of the header names sent by the client
    Enumeration<?> enumerationOfHeaderNames = servletRequest.getHeaderNames();
    while (enumerationOfHeaderNames.hasMoreElements()) {
        String headerName = (String) enumerationOfHeaderNames.nextElement();
        //Instead the content-length is effectively set via InputStreamEntity
        if (!headerName.equalsIgnoreCase(CONTENT_LENGTH) && !hopByHopHeaders.containsHeader(headerName)) {
            Enumeration<?> headers = servletRequest.getHeaders(headerName);
            while (headers.hasMoreElements()) {//sometimes more than one value
                String headerValue = (String) headers.nextElement();
                // In case the proxy host is running multiple virtual servers,
                // rewrite the Host header to ensure that we get content from
                // the correct virtual server
                if (headerName.equalsIgnoreCase(HOST)) {
                    HttpHost host = URIUtils.extractHost(new URI(prerenderConfig.getPrerenderServiceUrl()));
                    headerValue = host.getHostName();
                    if (host.getPort() != -1) {
                        headerValue += ":" + host.getPort();
                    }
                }
                proxyRequest.addHeader(headerName, headerValue);
            }
        }
    }
}
 
Example #18
Source File: HttpRequestFactory.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 * @param scheme
 * @param host
 * @param port
 * @param path
 * @param queryParams
 * @return
 * @throws IllegalArgumentException
 */
public static HttpGet getHttpGetRequest(String scheme, String host, int port,
                                        String path, List<NameValuePair> queryParams, Header header) {
    try {
        URI uri = URIUtils.createURI(scheme, host, port, path,
                URLEncodedUtils.format(queryParams, "UTF-8"), null);
        HttpGet get = new HttpGet(uri);
        get.addHeader(header);
        return get;
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #19
Source File: ServiceNowExecution.java    From service-now-plugin with MIT License 5 votes vote down vote up
private CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpRequestBase requestBase, HttpContext httpContext) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(
            new AuthScope(requestBase.getURI().getHost(), requestBase.getURI().getPort()),
            CredentialsUtil.readCredentials(credentials, vaultConfiguration));
    clientBuilder.setDefaultCredentialsProvider(provider);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(URIUtils.extractHost(requestBase.getURI()), new BasicScheme());
    httpContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);

    return clientBuilder.build();
}
 
Example #20
Source File: HttpConnector.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Changes the query string in given URI and returns the updated URI.
 * 
 * @param uri
 * @param newQuery
 * @return the updated URI.
 */
private static URI changeQueryString(URI uri, String newQuery) {
	try {
		URI newURI = URIUtils.createURI(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), newQuery, uri.getFragment());
		return newURI;
	} catch (URISyntaxException e) {
	}
	return null;
}
 
Example #21
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 5 votes vote down vote up
protected void initTarget() throws ServletException {
    targetUri = getConfigParam(P_TARGET_URI);
    if (targetUri == null)
        throw new ServletException(P_TARGET_URI + " is required.");
    // test it's valid
    try {
        targetUriObj = new URI(targetUri);
    } catch (Exception e) {
        throw new ServletException("Trying to process targetUri init parameter: " + e, e);
    }
    targetHost = URIUtils.extractHost(targetUriObj);
}
 
Example #22
Source File: ApacheCloudStackClient.java    From apache-cloudstack-java-client with Apache License 2.0 5 votes vote down vote up
/**
 *  It configures the cookie domain with the domain of the Apache CloudStack that is being accessed.
 *  The domain is extracted from {@link #url} variable.
 */
protected void configureDomainForCookie(BasicClientCookie cookie) {
    try {
        HttpHost httpHost = URIUtils.extractHost(new URI(url));
        String domain = httpHost.getHostName();
        cookie.setDomain(domain);
    } catch (URISyntaxException e) {
        throw new ApacheCloudStackClientRuntimeException(e);
    }
}
 
Example #23
Source File: MCRHttpUtils.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static HttpHost getHttpHost(String serverUrl) {
    HttpHost host = null;
    //determine host name
    HttpGet serverGet = new HttpGet(serverUrl);
    final URI requestURI = serverGet.getURI();
    if (requestURI.isAbsolute()) {
        host = URIUtils.extractHost(requestURI);
    }
    return host;
}
 
Example #24
Source File: TracedHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
public static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException {
    // A null target may be acceptable if there is a default target.
    // Otherwise, the null target is detected in the director.
    HttpHost target = null;

    final URI requestUri = request.getURI();
    if (requestUri.isAbsolute()) {
        target = URIUtils.extractHost(requestUri);
        if (target == null) {
            throw new ClientProtocolException("URI does not specify a valid host name: "
                    + requestUri);
        }
    }
    return target;
}
 
Example #25
Source File: GridUtility.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * Extract HTTP host object from the specified URL.
 * 
 * @param url {@link URL} from which to extract HTTP host
 * @return {@link HttpHost} object
 */
public static HttpHost extractHost(URL url) {
    if (url != null) {
        try {
            return URIUtils.extractHost(url.toURI());
        } catch (URISyntaxException e) {
            throw UncheckedThrow.throwUnchecked(e);
        }
    }
    return null;
}
 
Example #26
Source File: TestHC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void testOpenSession() throws IOException {
    assertEquals("https://" + server + "/owa/", authenticator.getExchangeUri().toString());
    assertNotNull(authenticator.getHttpClientAdapter());
    initSession();
    assertEquals(email, session.getEmail());
    assertEquals(username, session.getAlias());
    assertEquals(URIUtils.resolve(authenticator.getExchangeUri(), "/public/").toString(), ((HC4DavExchangeSession) session).getCmdBasePath());

    assertNotNull(((HC4DavExchangeSession) session).getFolderPath("/users/" + email + "/inbox"));
    assertNotNull(((HC4DavExchangeSession) session).getFolderPath("/users/" + email + "/calendar"));
}
 
Example #27
Source File: HttpClientAdapter.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * fix relative uri and update current uri.
 *
 * @param request http request
 */
private void handleURI(HttpRequestBase request) {
    URI requestURI = request.getURI();
    if (!requestURI.isAbsolute()) {
        request.setURI(URIUtils.resolve(uri, requestURI));
    }
    uri = request.getURI();
}
 
Example #28
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * sometimes permanenturis inside items are wrong after an Exchange version migration
 * need to restore base uri to actual public Exchange uri
 *
 * @param url input uri
 * @return fixed uri
 * @throws IOException on error
 */
protected String encodeAndFixUrl(String url) throws IOException {
    String fixedurl = URIUtil.encodePath(url);
    // sometimes permanenturis inside items are wrong after an Exchange version migration
    // need to restore base uri to actual public Exchange uri
    if (restoreHostName && fixedurl.startsWith("http")) {
        try {
            return URIUtils.rewriteURI(new java.net.URI(fixedurl), URIUtils.extractHost(httpClientAdapter.getUri())).toString();
        } catch (URISyntaxException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
    return fixedurl;
}
 
Example #29
Source File: AgpClient.java    From geoportal-server-harvester with Apache License 2.0 4 votes vote down vote up
private String execute(HttpUriRequest req, Integer redirectDepth) throws IOException {
  // Determine if we've reached the limit of redirection attempts
  if (redirectDepth > this.maxRedirects) {
    throw new HttpResponseException(HttpStatus.SC_GONE, "Too many redirects, aborting");
  }

  try (CloseableHttpResponse httpResponse = httpClient.execute(req); InputStream contentStream = httpResponse.getEntity().getContent();) {
    if (httpResponse.getStatusLine().getStatusCode()>=400) {
      throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
    } else if (httpResponse.getStatusLine().getStatusCode() >= 300) {
      // See if we can redirect the command
      Header locationHeader = httpResponse.getFirstHeader("Location");
      if (locationHeader != null) {
        try {
          HttpRequestWrapper newReq = HttpRequestWrapper.wrap(req);

          // Determine if this is a relataive redirection
          URI redirUrl = new URI(locationHeader.getValue());
          if (!redirUrl.isAbsolute()) {
            HttpHost target = URIUtils.extractHost(newReq.getURI());

            redirUrl = URI.create(
              String.format(
                "%s://%s%s",
                target.getSchemeName(),
                target.toHostString(),
                locationHeader.getValue()
              )
            );
          }
          
          newReq.setURI(redirUrl);

          return execute(newReq, ++redirectDepth);
        } catch (IOException | URISyntaxException e) {
          LOG.debug("Error executing request", e);
          throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
        }
      }
    }
    return IOUtils.toString(contentStream, "UTF-8");
  }
}
 
Example #30
Source File: UriUtils.java    From esigate with Apache License 2.0 3 votes vote down vote up
/**
 * Replaces the scheme, host and port in a URI.
 * 
 * @param uri
 *            the URI
 * @param targetHost
 *            the target host
 * @return the rewritten URI
 */
public static String rewriteURI(String uri, HttpHost targetHost) {
    try {
        return URIUtils.rewriteURI(createURI(uri), targetHost).toString();
    } catch (URISyntaxException e) {
        throw new InvalidUriException(e);
    }
}