下面列出了org.apache.http.impl.client.RedirectLocations#org.apache.http.client.utils.URIUtils 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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);
}
}
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;
}
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);
}
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);
}
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);
}
/**
* 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());
}
}
/**
* 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;
}
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);
}
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);
}
/**
* 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;
}
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;
}
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();
}
/**
* 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;
}
/**
* 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();
}
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"));
}
/**
* 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;
}
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;
}
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);
}
}
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);
}
/**
* 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;
}
/**
*
* @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);
}
}
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());
}
/** 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);
}
}
}
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;
}
/**
* 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);
}
}
}
}
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");
}
}
/**
* 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);
}
}