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

Commit 4dbe88a

Browse files
committed
Merge pull request #88 from eoff/master
Introduced getCookieStore method in ApacheConnectorProvider.
2 parents 0acf9f9 + 94f0152 commit 4dbe88a

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnectorProvider.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
*/
4040
package org.glassfish.jersey.apache.connector;
4141

42-
import javax.ws.rs.client.Client;
43-
import javax.ws.rs.core.Configurable;
44-
import javax.ws.rs.core.Configuration;
45-
42+
import org.apache.http.client.CookieStore;
43+
import org.apache.http.client.HttpClient;
4644
import org.glassfish.jersey.client.Initializable;
4745
import org.glassfish.jersey.client.spi.Connector;
4846
import org.glassfish.jersey.client.spi.ConnectorProvider;
4947

50-
import org.apache.http.client.HttpClient;
48+
import javax.ws.rs.client.Client;
49+
import javax.ws.rs.core.Configurable;
50+
import javax.ws.rs.core.Configuration;
5151

5252
/**
5353
* Connector provider for Jersey {@link Connector connectors} that utilize
@@ -105,6 +105,7 @@
105105
* @author jorgeluisw at mac.com
106106
* @author Marek Potociar (marek.potociar at oracle.com)
107107
* @author Paul Sandoz (paul.sandoz at oracle.com)
108+
* @author Maksim Mukosey (mmukosey at gmail.com)
108109
* @since 2.5
109110
*/
110111
public class ApacheConnectorProvider implements ConnectorProvider {
@@ -129,6 +130,40 @@ public Connector getConnector(Client client, Configuration runtimeConfig) {
129130
* @since 2.8
130131
*/
131132
public static HttpClient getHttpClient(Configurable<?> component) {
133+
Connector connector = getConnector(component);
134+
135+
if (connector instanceof ApacheConnector) {
136+
return ((ApacheConnector) connector).getHttpClient();
137+
}
138+
139+
throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED());
140+
}
141+
142+
/**
143+
* Retrieve the underlying Apache {@link CookieStore} instance from
144+
* {@link org.glassfish.jersey.client.JerseyClient} or {@link org.glassfish.jersey.client.JerseyWebTarget}
145+
* configured to use {@code ApacheConnectorProvider}.
146+
*
147+
* @param component {@code JerseyClient} or {@code JerseyWebTarget} instance that is configured to use
148+
* {@code ApacheConnectorProvider}.
149+
* @return underlying Apache {@code CookieStore} instance.
150+
*
151+
* @throws java.lang.IllegalArgumentException in case the {@code component} is neither {@code JerseyClient}
152+
* nor {@code JerseyWebTarget} instance or in case the component
153+
* is not configured to use a {@code ApacheConnectorProvider}.
154+
* @since 2.10
155+
*/
156+
public static CookieStore getCookieStore(Configurable<?> component) {
157+
Connector connector = getConnector(component);
158+
159+
if (connector instanceof ApacheConnector) {
160+
return ((ApacheConnector) connector).getCookieStore();
161+
}
162+
163+
throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED());
164+
}
165+
166+
private static Connector getConnector(Configurable<?> component) {
132167
if (!(component instanceof Initializable)) {
133168
throw new IllegalArgumentException(
134169
LocalizationMessages.INVALID_CONFIGURABLE_COMPONENT_TYPE(component.getClass().getName()));
@@ -140,11 +175,6 @@ public static HttpClient getHttpClient(Configurable<?> component) {
140175
initializable.preInitialize();
141176
connector = initializable.getConfiguration().getConnector();
142177
}
143-
144-
if (connector instanceof ApacheConnector) {
145-
return ((ApacheConnector) connector).getHttpClient();
146-
}
147-
148-
throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED());
178+
return connector;
149179
}
150180
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.apache.connector;
41+
42+
import org.apache.http.client.CookieStore;
43+
import org.glassfish.jersey.client.ClientConfig;
44+
import org.junit.Test;
45+
46+
import javax.ws.rs.client.Client;
47+
import javax.ws.rs.client.ClientBuilder;
48+
import javax.ws.rs.client.WebTarget;
49+
50+
import static org.junit.Assert.assertNotNull;
51+
import static org.junit.Assert.assertSame;
52+
53+
/**
54+
* Test of access to the underlying CookieStore instance used by the connector.
55+
*
56+
* @author Maksim Mukosey (mmukosey at gmail.com)
57+
*/
58+
public class UnderlyingCookieStoreAccessTest {
59+
60+
@Test
61+
public void testCookieStoreInstanceAccess() {
62+
final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new ApacheConnectorProvider()));
63+
final CookieStore csOnClient = ApacheConnectorProvider.getCookieStore(client);
64+
// important: the web target instance in this test must be only created AFTER the client has been pre-initialized
65+
// (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
66+
// connector provider's static getCookieStore method above.
67+
final WebTarget target = client.target("http://localhost/");
68+
final CookieStore csOnTarget = ApacheConnectorProvider.getCookieStore(target);
69+
70+
assertNotNull("CookieStore instance set on JerseyClient should not be null.", csOnClient);
71+
assertNotNull("CookieStore instance set on JerseyWebTarget should not be null.", csOnTarget);
72+
assertSame("CookieStore instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" +
73+
"(provided the target instance has not been further configured).",
74+
csOnClient, csOnTarget
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)