本文整理汇总了Java中org.apache.http.protocol.HttpContext.getAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java HttpContext.getAttribute方法的具体用法?Java HttpContext.getAttribute怎么用?Java HttpContext.getAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.protocol.HttpContext
的用法示例。
在下文中一共展示了HttpContext.getAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createOkResponseWithCookie
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
private Answer<HttpResponse> createOkResponseWithCookie() {
return new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
HttpContext context = (HttpContext) invocation.getArguments()[1];
if (context.getAttribute(ClientContext.COOKIE_STORE) != null) {
BasicCookieStore cookieStore =
(BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie");
cookieStore.addCookie(cookie);
}
return OK_200_RESPONSE;
}
};
}
示例2: process
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.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 (alwaysSendAuth || requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.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);
}
}
示例3: getModSlug0
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Nullable
private String getModSlug0(int id)
{
try
{
log.debug("Getting mod slug from server...");
URI uri = getURI(CURSEFORGE_URL, String.format(PROJECT_PATH, id), null);
HttpGet request = new HttpGet(uri.toURL().toString());
HttpContext context = new BasicHttpContext();
HttpResponse response = http.execute(request, context);
EntityUtils.consume(response.getEntity());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
HttpHost currentHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
Splitter splitter = Splitter.on('/').omitEmptyStrings();
List<String> pathParts = splitter.splitToList(currentUrl);
return pathParts.get(pathParts.size() - 1);
}
catch (Exception e)
{
log.error("Failed to perform request from CurseForge site.", e);
return null;
}
}
示例4: authFailed
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
if (authhost == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache != null) {
if (this.log.isDebugEnabled()) {
this.log.debug("Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
示例5: createLayeredSocket
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public Socket createLayeredSocket(final Socket socket, final String target, final int port,
final HttpContext context) throws IOException {
Boolean enableSniValue = (Boolean) context.getAttribute(ENABLE_SNI);
boolean enableSni = enableSniValue == null || enableSniValue;
return super.createLayeredSocket(socket, enableSni ? target : "", port, context);
}
示例6: requestReady
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public void requestReady(NHttpClientConnection conn) throws IOException, HttpException {
try {
super.requestReady(conn);
} catch (Exception ex) {
LOGGER.error("", ex);
}
// 需要自动关闭连接
if (this.liveTime > 0) {
HttpRequest httpRequest = conn.getHttpRequest();
if (httpRequest == null) {
return;
}
HttpContext context = conn.getContext();
long currentTimeMillis = System.currentTimeMillis();
Object oldTimeMillisObj = context.getAttribute("t");
if (oldTimeMillisObj == null) {
context.setAttribute("t", currentTimeMillis);
} else {
long oldTimeMillis = (Long) oldTimeMillisObj;
long dt = currentTimeMillis - oldTimeMillis;
if (dt > 1000 * liveTime) { // 超时,重连
tryCloseConnection(httpRequest);
context.setAttribute("t", currentTimeMillis);
}
}
}
}
示例7: retryRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
boolean sent;
boolean retry = true;
Boolean b = (Boolean) context.getAttribute("http.request_sent");
if (b == null || !b.booleanValue()) {
sent = false;
} else {
sent = true;
}
if (executionCount > this.maxRetries) {
retry = false;
} else if (isInList(exceptionBlacklist, exception)) {
retry = false;
} else if (isInList(exceptionWhitelist, exception)) {
retry = true;
} else if (!sent) {
retry = true;
}
if (retry) {
if (((HttpUriRequest) context.getAttribute("http.request")).getMethod().equals("POST")) {
retry = false;
} else {
retry = true;
}
}
if (retry) {
SystemClock.sleep(1500);
} else {
exception.printStackTrace();
}
return retry;
}
示例8: createAsyncRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient asyncClient = getHttpAsyncClient();
startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
config = RequestConfig.DEFAULT;
}
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
示例9: createRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
CloseableHttpClient client = (CloseableHttpClient) getHttpClient();
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
if (this.socketTimeout > 0 || this.connectTimeout > 0) {
config = RequestConfig.custom()
.setConnectTimeout(this.connectTimeout)
.setSocketTimeout(this.socketTimeout)
.build();
}
else {
config = RequestConfig.DEFAULT;
}
}
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
}
}
示例10: process
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT")) {
request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
return;
}
// Obtain the client connection (required)
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();
if (route.getHopCount() == 1 || route.isTunnelled()) {
if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) {
request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
}
}
if (route.getHopCount() == 2 && !route.isTunnelled()) {
if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) {
request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
}
}
}
示例11: process
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
return;
}
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();
if (route.isTunnelled()) {
return;
}
// Obtain authentication state
AuthState authState = (AuthState) context.getAttribute(
ClientContext.PROXY_AUTH_STATE);
if (authState == null) {
this.log.debug("Proxy auth state not set in the context");
return;
}
if (this.log.isDebugEnabled()) {
this.log.debug("Proxy auth state: " + authState.getState());
}
process(authState, request, context);
}
示例12: authSucceeded
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (isCachable(authScheme)) {
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
示例13: retryRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
boolean retry = true;
Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
boolean sent = (b != null && b);
if (executionCount > maxRetries) {
// Do not retry if over max retry count
retry = false;
} else if (isInList(exceptionWhitelist, exception)) {
// immediately retry if error is whitelisted
retry = true;
} else if (isInList(exceptionBlacklist, exception)) {
// immediately cancel retry if the error is blacklisted
retry = false;
} else if (!sent) {
// for most other errors, retry only if request hasn't been fully sent yet
retry = true;
}
if (retry) {
// resend all idempotent requests
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
if (currentReq == null) {
return false;
}
}
if (retry) {
SystemClock.sleep(retrySleepTimeMS);
} else {
exception.printStackTrace();
}
return retry;
}
示例14: retryRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
boolean retry = true;
Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
boolean sent = (b != null && b);
if (executionCount > maxRetries) {
// Do not retry if over max retry count
retry = false;
} else if (isInList(exceptionBlacklist, exception)) {
// immediately cancel retry if the error is blacklisted
retry = false;
} else if (isInList(exceptionWhitelist, exception)) {
// immediately retry if error is whitelisted
retry = true;
} else if (!sent) {
// for most other errors, retry only if request hasn't been fully sent yet
retry = true;
}
if (retry) {
// resend all idempotent requests
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
if (currentReq == null) {
return false;
}
String requestType = currentReq.getMethod();
retry = !requestType.equals("POST");
}
if (retry) {
SystemClock.sleep(retrySleepTimeMS);
} else {
exception.printStackTrace();
}
return retry;
}
示例15: retryRequest
import org.apache.http.protocol.HttpContext; //导入方法依赖的package包/类
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
boolean sent;
boolean retry = true;
Boolean b = (Boolean) context.getAttribute("http.request_sent");
if (b == null || !b.booleanValue()) {
sent = false;
} else {
sent = true;
}
if (executionCount > this.maxRetries) {
retry = false;
} else if (isInList(exceptionWhitelist, exception)) {
retry = true;
} else if (isInList(exceptionBlacklist, exception)) {
retry = false;
} else if (!sent) {
retry = true;
}
if (retry && ((HttpUriRequest) context.getAttribute("http.request")) == null) {
return false;
}
if (retry) {
SystemClock.sleep((long) this.retrySleepTimeMS);
} else {
exception.printStackTrace();
}
return retry;
}