001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.client.protocol; 029 030import java.net.URI; 031import java.util.List; 032 033import org.apache.http.auth.AuthSchemeProvider; 034import org.apache.http.auth.AuthState; 035import org.apache.http.client.AuthCache; 036import org.apache.http.client.CookieStore; 037import org.apache.http.client.CredentialsProvider; 038import org.apache.http.client.config.RequestConfig; 039import org.apache.http.config.Lookup; 040import org.apache.http.conn.routing.HttpRoute; 041import org.apache.http.conn.routing.RouteInfo; 042import org.apache.http.cookie.CookieOrigin; 043import org.apache.http.cookie.CookieSpec; 044import org.apache.http.cookie.CookieSpecProvider; 045import org.apache.http.protocol.BasicHttpContext; 046import org.apache.http.protocol.HttpContext; 047import org.apache.http.protocol.HttpCoreContext; 048 049/** 050 * Adaptor class that provides convenience type safe setters and getters 051 * for common {@link HttpContext} attributes used in the course 052 * of HTTP request execution. 053 * 054 * @since 4.3 055 */ 056public class HttpClientContext extends HttpCoreContext { 057 058 /** 059 * Attribute name of a {@link org.apache.http.conn.routing.RouteInfo} 060 * object that represents the actual connection route. 061 */ 062 public static final String HTTP_ROUTE = "http.route"; 063 064 /** 065 * Attribute name of a {@link List} object that represents a collection of all 066 * redirect locations received in the process of request execution. 067 */ 068 public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations"; 069 070 /** 071 * Attribute name of a {@link org.apache.http.config.Lookup} object that represents 072 * the actual {@link CookieSpecProvider} registry. 073 */ 074 public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry"; 075 076 /** 077 * Attribute name of a {@link org.apache.http.cookie.CookieSpec} 078 * object that represents the actual cookie specification. 079 */ 080 public static final String COOKIE_SPEC = "http.cookie-spec"; 081 082 /** 083 * Attribute name of a {@link org.apache.http.cookie.CookieOrigin} 084 * object that represents the actual details of the origin server. 085 */ 086 public static final String COOKIE_ORIGIN = "http.cookie-origin"; 087 088 /** 089 * Attribute name of a {@link org.apache.http.client.CookieStore} 090 * object that represents the actual cookie store. 091 */ 092 public static final String COOKIE_STORE = "http.cookie-store"; 093 094 /** 095 * Attribute name of a {@link org.apache.http.client.CredentialsProvider} 096 * object that represents the actual credentials provider. 097 */ 098 public static final String CREDS_PROVIDER = "http.auth.credentials-provider"; 099 100 /** 101 * Attribute name of a {@link org.apache.http.client.AuthCache} object 102 * that represents the auth scheme cache. 103 */ 104 public static final String AUTH_CACHE = "http.auth.auth-cache"; 105 106 /** 107 * Attribute name of a {@link org.apache.http.auth.AuthState} 108 * object that represents the actual target authentication state. 109 */ 110 public static final String TARGET_AUTH_STATE = "http.auth.target-scope"; 111 112 /** 113 * Attribute name of a {@link org.apache.http.auth.AuthState} 114 * object that represents the actual proxy authentication state. 115 */ 116 public static final String PROXY_AUTH_STATE = "http.auth.proxy-scope"; 117 118 /** 119 * Attribute name of a {@link java.lang.Object} object that represents 120 * the actual user identity such as user {@link java.security.Principal}. 121 */ 122 public static final String USER_TOKEN = "http.user-token"; 123 124 /** 125 * Attribute name of a {@link org.apache.http.config.Lookup} object that represents 126 * the actual {@link AuthSchemeProvider} registry. 127 */ 128 public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry"; 129 130 /** 131 * Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that 132 * represents the actual request configuration. 133 */ 134 public static final String REQUEST_CONFIG = "http.request-config"; 135 136 public static HttpClientContext adapt(final HttpContext context) { 137 if (context instanceof HttpClientContext) { 138 return (HttpClientContext) context; 139 } else { 140 return new HttpClientContext(context); 141 } 142 } 143 144 public static HttpClientContext create() { 145 return new HttpClientContext(new BasicHttpContext()); 146 } 147 148 public HttpClientContext(final HttpContext context) { 149 super(context); 150 } 151 152 public HttpClientContext() { 153 super(); 154 } 155 156 public RouteInfo getHttpRoute() { 157 return getAttribute(HTTP_ROUTE, HttpRoute.class); 158 } 159 160 @SuppressWarnings("unchecked") 161 public List<URI> getRedirectLocations() { 162 return getAttribute(REDIRECT_LOCATIONS, List.class); 163 } 164 165 public CookieStore getCookieStore() { 166 return getAttribute(COOKIE_STORE, CookieStore.class); 167 } 168 169 public void setCookieStore(final CookieStore cookieStore) { 170 setAttribute(COOKIE_STORE, cookieStore); 171 } 172 173 public CookieSpec getCookieSpec() { 174 return getAttribute(COOKIE_SPEC, CookieSpec.class); 175 } 176 177 public CookieOrigin getCookieOrigin() { 178 return getAttribute(COOKIE_ORIGIN, CookieOrigin.class); 179 } 180 181 @SuppressWarnings("unchecked") 182 private <T> Lookup<T> getLookup(final String name, final Class<T> clazz) { 183 return getAttribute(name, Lookup.class); 184 } 185 186 public Lookup<CookieSpecProvider> getCookieSpecRegistry() { 187 return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class); 188 } 189 190 public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) { 191 setAttribute(COOKIESPEC_REGISTRY, lookup); 192 } 193 194 public Lookup<AuthSchemeProvider> getAuthSchemeRegistry() { 195 return getLookup(AUTHSCHEME_REGISTRY, AuthSchemeProvider.class); 196 } 197 198 public void setAuthSchemeRegistry(final Lookup<AuthSchemeProvider> lookup) { 199 setAttribute(AUTHSCHEME_REGISTRY, lookup); 200 } 201 202 public CredentialsProvider getCredentialsProvider() { 203 return getAttribute(CREDS_PROVIDER, CredentialsProvider.class); 204 } 205 206 public void setCredentialsProvider(final CredentialsProvider credentialsProvider) { 207 setAttribute(CREDS_PROVIDER, credentialsProvider); 208 } 209 210 public AuthCache getAuthCache() { 211 return getAttribute(AUTH_CACHE, AuthCache.class); 212 } 213 214 public void setAuthCache(final AuthCache authCache) { 215 setAttribute(AUTH_CACHE, authCache); 216 } 217 218 public AuthState getTargetAuthState() { 219 return getAttribute(TARGET_AUTH_STATE, AuthState.class); 220 } 221 222 public AuthState getProxyAuthState() { 223 return getAttribute(PROXY_AUTH_STATE, AuthState.class); 224 } 225 226 public <T> T getUserToken(final Class<T> clazz) { 227 return getAttribute(USER_TOKEN, clazz); 228 } 229 230 public Object getUserToken() { 231 return getAttribute(USER_TOKEN); 232 } 233 234 public void setUserToken(final Object obj) { 235 setAttribute(USER_TOKEN, obj); 236 } 237 238 public RequestConfig getRequestConfig() { 239 final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class); 240 return config != null ? config : RequestConfig.DEFAULT; 241 } 242 243 public void setRequestConfig(final RequestConfig config) { 244 setAttribute(REQUEST_CONFIG, config); 245 } 246 247}