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

Commit 372b0e5

Browse files
agherardipavelbucek
authored andcommitted
Added Apache client property to allow application to specify a retry handler.
Change-Id: I38d1a1348f1415e03f8d8b39a86fd48d1b630ff2
1 parent 6090d54 commit 372b0e5

File tree

4 files changed

+155
-3
lines changed

4 files changed

+155
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -145,6 +145,18 @@ public final class ApacheClientProperties {
145145
*/
146146
public static final String REQUEST_CONFIG = "jersey.config.apache.client.requestConfig";
147147

148+
/**
149+
* HttpRequestRetryHandler which will be used to create {@link org.apache.http.client.HttpClient}.
150+
* <p/>
151+
* The value MUST be an instance of {@link org.apache.http.client.HttpRequestRetryHandler}.
152+
* <p/>
153+
* If the property is absent a default retry handler will be used
154+
* ({@link org.apache.http.impl.client.DefaultHttpRequestRetryHandler}).
155+
* <p/>
156+
* The name of the configuration property is <tt>{@value}</tt>.
157+
*/
158+
public static final String RETRY_HANDLER = "jersey.config.apache.client.retryHandler";
159+
148160
/**
149161
* Get the value of the specified property.
150162
*

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2016 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -90,6 +90,7 @@
9090
import org.apache.http.client.CookieStore;
9191
import org.apache.http.client.CredentialsProvider;
9292
import org.apache.http.client.HttpClient;
93+
import org.apache.http.client.HttpRequestRetryHandler;
9394
import org.apache.http.client.config.CookieSpecs;
9495
import org.apache.http.client.config.RequestConfig;
9596
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -139,6 +140,7 @@
139140
* <li>{@link ClientProperties#PROXY_PASSWORD}</li>
140141
* <li>{@link ClientProperties#REQUEST_ENTITY_PROCESSING} - default value is {@link RequestEntityProcessing#CHUNKED}</li>
141142
* <li>{@link ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}</li>
143+
* <li>{@link ApacheClientProperties#RETRY_HANDLER}</li>
142144
* </ul>
143145
* <p>
144146
* This connector uses {@link RequestEntityProcessing#CHUNKED chunked encoding} as a default setting. This can
@@ -248,6 +250,11 @@ class ApacheConnector implements Connector {
248250
clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider);
249251
}
250252

253+
final Object retryHandler = config.getProperties().get(ApacheClientProperties.RETRY_HANDLER);
254+
if (retryHandler != null && (retryHandler instanceof HttpRequestRetryHandler)) {
255+
clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler);
256+
}
257+
251258
final Object proxyUri;
252259
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
253260
if (proxyUri != null) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -66,6 +66,7 @@
6666
* <li>{@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING}
6767
* - default value is {@link org.glassfish.jersey.client.RequestEntityProcessing#CHUNKED}</li>
6868
* <li>{@link ApacheClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}</li>
69+
* <li>{@link ApacheClientProperties#RETRY_HANDLER}</li>
6970
* </ul>
7071
* </p>
7172
* <p>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 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+
41+
package org.glassfish.jersey.apache.connector;
42+
43+
import java.io.IOException;
44+
45+
import javax.ws.rs.GET;
46+
import javax.ws.rs.POST;
47+
import javax.ws.rs.Path;
48+
import javax.ws.rs.client.Client;
49+
import javax.ws.rs.client.ClientBuilder;
50+
import javax.ws.rs.client.Entity;
51+
import javax.ws.rs.client.WebTarget;
52+
import javax.ws.rs.core.Application;
53+
import javax.ws.rs.core.Context;
54+
import javax.ws.rs.core.HttpHeaders;
55+
56+
import org.glassfish.jersey.client.ClientConfig;
57+
import org.glassfish.jersey.client.ClientProperties;
58+
import org.glassfish.jersey.client.RequestEntityProcessing;
59+
import org.glassfish.jersey.server.ResourceConfig;
60+
import org.glassfish.jersey.test.JerseyTest;
61+
62+
import org.apache.http.client.HttpRequestRetryHandler;
63+
import org.junit.Test;
64+
import static org.junit.Assert.assertEquals;
65+
66+
public class RetryHandlerTest extends JerseyTest {
67+
private static final int READ_TIMEOUT_MS = 100;
68+
69+
@Override
70+
protected Application configure() {
71+
return new ResourceConfig(RetryHandlerResource.class);
72+
}
73+
74+
@Path("/")
75+
public static class RetryHandlerResource {
76+
private static volatile int postRequestNumber = 0;
77+
private static volatile int getRequestNumber = 0;
78+
79+
// Cause a timeout on the first GET and POST request
80+
@GET
81+
public String get(@Context HttpHeaders h) {
82+
if (getRequestNumber++ == 0) {
83+
try {
84+
Thread.sleep(READ_TIMEOUT_MS * 10);
85+
} catch (InterruptedException ex) {
86+
// ignore
87+
}
88+
}
89+
return "GET";
90+
}
91+
92+
@POST
93+
public String post(@Context HttpHeaders h, String e) {
94+
if (postRequestNumber++ == 0) {
95+
try {
96+
Thread.sleep(READ_TIMEOUT_MS * 10);
97+
} catch (InterruptedException ex) {
98+
// ignore
99+
}
100+
}
101+
return "POST";
102+
}
103+
}
104+
105+
@Test
106+
public void testRetryGet() throws IOException {
107+
ClientConfig cc = new ClientConfig();
108+
cc.connectorProvider(new ApacheConnectorProvider());
109+
cc.property(ApacheClientProperties.RETRY_HANDLER,
110+
(HttpRequestRetryHandler) (exception, executionCount, context) -> true);
111+
cc.property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MS);
112+
Client client = ClientBuilder.newClient(cc);
113+
114+
WebTarget r = client.target(getBaseUri());
115+
assertEquals("GET", r.request().get(String.class));
116+
}
117+
118+
@Test
119+
public void testRetryPost() throws IOException {
120+
ClientConfig cc = new ClientConfig();
121+
cc.connectorProvider(new ApacheConnectorProvider());
122+
cc.property(ApacheClientProperties.RETRY_HANDLER,
123+
(HttpRequestRetryHandler) (exception, executionCount, context) -> true);
124+
cc.property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MS);
125+
Client client = ClientBuilder.newClient(cc);
126+
127+
WebTarget r = client.target(getBaseUri());
128+
assertEquals("POST", r.request()
129+
.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED)
130+
.post(Entity.text("POST"), String.class));
131+
}
132+
}

0 commit comments

Comments
 (0)