Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Support for custom cookiespec registries and cookie stores to Apache … #245

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
package org.glassfish.jersey.apache.connector;

import java.util.Map;

import org.glassfish.jersey.internal.util.PropertiesClass;
import org.glassfish.jersey.internal.util.PropertiesHelper;

Expand Down Expand Up @@ -145,6 +144,17 @@ public final class ApacheClientProperties {
*/
public static final String REQUEST_CONFIG = "jersey.config.apache.client.requestConfig";

/**
* An implementation of {@link org.apache.http.config.Lookup} that should be used as a
* {@link org.apache.http.config.Registry} of {@link org.apache.http.cookie.CookieSpecProvider}s.
*/
public static final String COOKIE_SPEC_REGISTRY = "jersey.config.apache.client.cookieSpecRegistry";

/**
* An implementation of {@link org.apache.http.client.CookieStore} that should be used as the cookie store
*/
public static final String COOKIE_STORE = "jersey.config.apache.client.cookieStore";

/**
* Get the value of the specified property.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Lookup;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
Expand All @@ -105,6 +106,7 @@
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ContentLengthStrategy;
Expand Down Expand Up @@ -134,6 +136,8 @@
* <li>{@link ApacheClientProperties#REQUEST_CONFIG}</li>
* <li>{@link ApacheClientProperties#CREDENTIALS_PROVIDER}</li>
* <li>{@link ApacheClientProperties#DISABLE_COOKIES}</li>
* <li>{@link ApacheClientProperties#COOKIE_SPEC_REGISTRY}</li>
* <li>{@link ApacheClientProperties#COOKIE_STORE}</li>
* <li>{@link ClientProperties#PROXY_URI}</li>
* <li>{@link ClientProperties#PROXY_USERNAME}</li>
* <li>{@link ClientProperties#PROXY_PASSWORD}</li>
Expand Down Expand Up @@ -198,6 +202,7 @@ class ApacheConnector implements Connector {
private final CookieStore cookieStore;
private final boolean preemptiveBasicAuth;
private final RequestConfig requestConfig;
private final Lookup<CookieSpecProvider> cookieSpecRegistry;

/**
* Create the new Apache HTTP Client connector.
Expand Down Expand Up @@ -233,6 +238,24 @@ class ApacheConnector implements Connector {
}
}

Object cookieSpecRegistryObj = config.getProperties().get(ApacheClientProperties.COOKIE_SPEC_REGISTRY);
if (cookieSpecRegistryObj == null) {
cookieSpecRegistry = null;
} else {
if (cookieSpecRegistryObj instanceof Lookup) {
cookieSpecRegistry = (Lookup<CookieSpecProvider>) cookieSpecRegistryObj;
} else {
LOGGER.log(
Level.WARNING,
LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(
HttpClientContext.COOKIESPEC_REGISTRY,
cookieSpecRegistryObj.getClass().getName(),
Registry.class.getName())
);
cookieSpecRegistry = null;
}
}

final SSLContext sslContext = client.getSslContext();
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();

Expand Down Expand Up @@ -290,8 +313,25 @@ class ApacheConnector implements Connector {
requestConfig = requestConfigBuilder.build();
}


if (requestConfig.getCookieSpec() == null || !requestConfig.getCookieSpec().equals(CookieSpecs.IGNORE_COOKIES)) {
this.cookieStore = new BasicCookieStore();
Object cookieStoreObj = config.getProperties().get(ApacheClientProperties.COOKIE_STORE);
if (cookieStoreObj == null) {
this.cookieStore = new BasicCookieStore();
} else {
if (cookieStoreObj instanceof CookieStore) {
cookieStore = (CookieStore) cookieStoreObj;
} else {
LOGGER.log(
Level.WARNING,
LocalizationMessages.IGNORING_VALUE_OF_PROPERTY(
HttpClientContext.COOKIE_STORE,
cookieStoreObj.getClass().getName(),
Registry.class.getName())
);
this.cookieStore = new BasicCookieStore();
}
}
clientBuilder.setDefaultCookieStore(cookieStore);
} else {
this.cookieStore = null;
Expand Down Expand Up @@ -432,6 +472,11 @@ public ClientResponse apply(final ClientRequest clientRequest) throws Processing
authCache.put(getHost(request), basicScheme);
context.setAuthCache(authCache);
}

if (cookieSpecRegistry != null) {
context.setCookieSpecRegistry(cookieSpecRegistry);
}

response = client.execute(getHost(request), request, context);
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());

Expand Down