本文整理汇总了Java中org.apache.http.protocol.ExecutionContext类的典型用法代码示例。如果您正苦于以下问题:Java ExecutionContext类的具体用法?Java ExecutionContext怎么用?Java ExecutionContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExecutionContext类属于org.apache.http.protocol包,在下文中一共展示了ExecutionContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public HttpResponse execute(HttpRequest request) throws IOException, HttpException {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProcessor processor = new ImmutableHttpProcessor(new RequestContent());
HttpRequestExecutor executor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, connection);
if (!connection.isOpen()) {
Socket socket = new Socket(address.getAddress(), address.getPort());
connection.bind(socket, params);
}
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
request.setParams(params);
executor.preProcess(request, processor, context);
HttpResponse response = executor.execute(request, connection, context);
executor.postProcess(response, processor, context);
return response;
}
示例2: isRedirectRequested
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
HttpRequest request = (HttpRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
String method = request.getRequestLine().getMethod();
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
示例3: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme avaialble yet, try to initialize it preemptively
if (authState.getAuthScheme() == null) {
AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authScheme != null) {
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
示例4: submitRequest
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public HttpRequest submitRequest(final HttpContext context) {
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
Object flag = context.getAttribute(REQUEST_SENT);
if (flag == null) {
// Stick some object into the context
context.setAttribute(REQUEST_SENT, Boolean.TRUE);
System.out.println("--------------");
System.out.println("Sending request to " + targetHost);
System.out.println("--------------");
return new BasicHttpRequest("GET", "/");
} else {
// No new request to submit
return null;
}
}
示例5: submitRequest
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public HttpRequest submitRequest(final HttpContext context) {
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
Object token = context.getAttribute(REQUEST_SENT);
if (token == null) {
// Stick some object into the context
context.setAttribute(REQUEST_SENT, Boolean.TRUE);
System.out.println("--------------");
System.out.println("Sending request to " + targetHost);
System.out.println("--------------");
return new BasicHttpRequest("GET", "/");
} else {
// No new request to submit
return null;
}
}
示例6: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
Credentials credentials = credentialsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (credentials != null) {
authState.setAuthScheme(new DiadocAuthScheme());
authState.setCredentials(credentials);
}
}
}
示例7: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例8: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例9: setupClient
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
/**
* This method is used to capture Location headers after HttpClient redirect handling.
*/
private void setupClient(final AbstractHttpClient client) {
this.client.addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
Header header = response.getFirstHeader("Location");
if (header!=null) {
String location = header.getValue();
/*
* Append the base name to the Location header
*/
if (location.startsWith("/")) {
String baseUrl = context.getAttribute(ExecutionContext.HTTP_TARGET_HOST).toString();
location = baseUrl + location;
}
context.setAttribute("Location", location);
}
}
});
}
示例10: handleCacheHit
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
private HttpResponse handleCacheHit(final HttpHost target, final HttpRequestWrapper request,
final HttpContext context, final HttpCacheEntry entry)
throws ClientProtocolException, IOException {
recordCacheHit(target, request);
HttpResponse out = null;
final Date now = getCurrentDate();
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
log.debug("Cache hit");
out = generateCachedResponse(request, context, entry, now);
} else if (!mayCallBackend(request)) {
log.debug("Cache entry not suitable but only-if-cached requested");
out = generateGatewayTimeout(context);
} else {
log.debug("Revalidating cache entry");
return revalidateCacheEntry(target, request, context, entry, now);
}
if (context != null) {
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
context.setAttribute(ExecutionContext.HTTP_RESPONSE, out);
context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
}
return out;
}
示例11: isRedirectRequested
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
Args.notNull(response, "HTTP response");
final int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
final HttpRequest request = (HttpRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
final String method = request.getRequestLine().getMethod();
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
示例12: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public void process( final HttpRequest request, final HttpContext context )
throws HttpException, IOException
{
AuthState authState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE );
CredentialsProvider credsProvider = (CredentialsProvider) context
.getAttribute( ClientContext.CREDS_PROVIDER );
HttpHost targetHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST );
// If not auth scheme has been initialized yet
if( authState.getAuthScheme() == null )
{
AuthScope authScope = new AuthScope( targetHost.getHostName(),
targetHost.getPort() );
// Obtain credentials matching the target host
Credentials creds = credsProvider.getCredentials( authScope );
// If found, generate BasicScheme preemptively
if( creds != null )
{
authState.setAuthScheme( new BasicScheme() );
authState.setCredentials( creds );
}
}
}
示例13: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState) context
.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(),
targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例14: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() != null || authState.hasAuthOptions()) {
return;
}
// If no authState has been established and this is a PUT or POST request, add preemptive authorisation
String requestMethod = request.getRequestLine().getMethod();
if (requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
Credentials credentials = credentialsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (credentials == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.update(authScheme, credentials);
}
}
示例15: process
import org.apache.http.protocol.ExecutionContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme available yet, try to initialize it preemptively
if (authState.getAuthScheme() == null) {
AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authScheme != null) {
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (creds != null) {
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
}