org.apache.http.impl.client.RedirectLocations#org.apache.http.client.utils.URIUtils源码实例Demo

下面列出了org.apache.http.impl.client.RedirectLocations#org.apache.http.client.utils.URIUtils 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: davmail   文件: HC4DavExchangeSession.java
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;
    }
}
 
源代码2 项目: fc-java-sdk   文件: AsyncInternalClient.java
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);
    }
}
 
源代码3 项目: letv   文件: jr.java
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;
}
 
源代码4 项目: aliyun-tablestore-java-sdk   文件: OTSUri.java
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);
}
 
源代码5 项目: nexus-public   文件: FormatClientSupport.java
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);
}
 
源代码6 项目: nexus-public   文件: FormatClientSupport.java
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);
}
 
源代码7 项目: nexus-public   文件: HttpClientManagerImpl.java
/**
 * 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());
  }
}
 
源代码8 项目: sakai   文件: BitlyUrlService.java
/**
 * 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;
}
 
源代码9 项目: sana.mobile   文件: MDSInterface2.java
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);

}
 
源代码10 项目: sana.mobile   文件: MDSInterface2.java
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);
}
 
源代码11 项目: sakai   文件: BitlyUrlService.java
/**
 * 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;
}
 
源代码12 项目: YiBo   文件: LibRequestDirector.java
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);
    }
}
 
/**
 * 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;
}
 
源代码14 项目: service-now-plugin   文件: ServiceNowExecution.java
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();
}
 
源代码15 项目: davmail   文件: HC4DavExchangeSession.java
/**
 * 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;
}
 
源代码16 项目: davmail   文件: HttpClientAdapter.java
/**
 * 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();
}
 
源代码17 项目: davmail   文件: TestHC4DavExchangeSession.java
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"));
}
 
源代码18 项目: Selenium-Foundation   文件: GridUtility.java
/**
 * 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;
}
 
源代码19 项目: aws-xray-sdk-java   文件: TracedHttpClient.java
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;
}
 
源代码20 项目: mycore   文件: MCRHttpUtils.java
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;
}
 
/**
 *  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);
    }
}
 
源代码22 项目: onboard   文件: ApiProxyServlet.java
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);
}
 
源代码23 项目: CloverETL-Engine   文件: HttpConnector.java
/**
 * 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;
}
 
源代码24 项目: sana.mobile   文件: HttpRequestFactory.java
/**
 *
 * @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);
    }
}
 
源代码25 项目: cs-actions   文件: FinalLocationConsumer.java
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());
}
 
源代码26 项目: aem-solr-search   文件: ProxyServlet.java
/** 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);
        }
    }
}
 
源代码27 项目: ribbon   文件: NFHttpClient.java
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;
}
 
源代码28 项目: prerender-java   文件: PrerenderSeoService.java
/**
 * 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);
            }
        }
    }
}
 
源代码29 项目: geoportal-server-harvester   文件: AgpClient.java
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");
  }
}
 
源代码30 项目: esigate   文件: UriUtils.java
/**
 * 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);
    }
}