Skip to content

Commit f764334

Browse files
committed
fix \#65 - replace Jersey with OkHttp -- this is an early minimal implementation, more testing and tweaking is most likely required
1 parent b1332f7 commit f764334

File tree

15 files changed

+1663
-1450
lines changed

15 files changed

+1663
-1450
lines changed

pom.xml

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
<url>file://${project.build.directory}/mvn-repo</url>
4747
</repository>
4848
</distributionManagement>
49+
<repositories>
50+
<repository>
51+
<id>jcenter</id>
52+
<url>http://jcenter.bintray.com </url>
53+
</repository>
54+
</repositories>
4955
<build>
5056
<plugins>
5157
<plugin>
@@ -217,44 +223,6 @@
217223
</plugins>
218224
</build>
219225
<dependencies>
220-
<!-- direct build / run dependencies -->
221-
<!--
222-
<dependency>
223-
<groupId>com.sun.jersey</groupId>
224-
<artifactId>jersey-client</artifactId>
225-
<version>1.17</version>
226-
</dependency>
227-
<dependency>
228-
<groupId>com.sun.jersey.contribs</groupId>
229-
<artifactId>jersey-apache-client4</artifactId>
230-
<version>1.17</version>
231-
</dependency>
232-
<dependency>
233-
<groupId>com.sun.jersey.contribs</groupId>
234-
<artifactId>jersey-multipart</artifactId>
235-
<version>1.17</version>
236-
</dependency>
237-
<dependency>
238-
<groupId>org.glassfish.jersey.media</groupId>
239-
<artifactId>jersey-media-multipart</artifactId>
240-
<version>2.12</version>
241-
</dependency>
242-
-->
243-
<dependency>
244-
<groupId>com.sun.jersey</groupId>
245-
<artifactId>jersey-client</artifactId>
246-
<version>1.17</version>
247-
</dependency>
248-
<dependency>
249-
<groupId>com.sun.jersey.contribs</groupId>
250-
<artifactId>jersey-apache-client4</artifactId>
251-
<version>1.17</version>
252-
</dependency>
253-
<dependency>
254-
<groupId>com.sun.jersey.contribs</groupId>
255-
<artifactId>jersey-multipart</artifactId>
256-
<version>1.17</version>
257-
</dependency>
258226
<dependency>
259227
<groupId>ch.qos.logback</groupId>
260228
<artifactId>logback-classic</artifactId>
@@ -270,16 +238,6 @@
270238
<artifactId>commons-codec</artifactId>
271239
<version>1.7</version>
272240
</dependency>
273-
<dependency>
274-
<groupId>com.sun.jersey</groupId>
275-
<artifactId>jersey-core</artifactId>
276-
<version>1.17</version>
277-
</dependency>
278-
<dependency>
279-
<groupId>org.jvnet.mimepull</groupId>
280-
<artifactId>mimepull</artifactId>
281-
<version>1.9.4</version>
282-
</dependency>
283241
<dependency>
284242
<groupId>org.slf4j</groupId>
285243
<artifactId>slf4j-api</artifactId>
@@ -300,6 +258,31 @@
300258
<artifactId>jackson-databind</artifactId>
301259
<version>2.8.3</version>
302260
</dependency>
261+
<dependency>
262+
<groupId>com.squareup.okhttp3</groupId>
263+
<artifactId>okhttp</artifactId>
264+
<version>3.6.0</version>
265+
</dependency>
266+
<dependency>
267+
<groupId>com.squareup.okhttp3</groupId>
268+
<artifactId>logging-interceptor</artifactId>
269+
<version>3.6.0</version>
270+
</dependency>
271+
<dependency>
272+
<groupId>com.burgstaller</groupId>
273+
<artifactId>okhttp-digest</artifactId>
274+
<version>1.12</version>
275+
</dependency>
276+
<dependency>
277+
<groupId>com.sun.mail</groupId>
278+
<artifactId>javax.mail</artifactId>
279+
<version>1.5.6</version>
280+
</dependency>
281+
<dependency>
282+
<groupId>javax.ws.rs</groupId>
283+
<artifactId>javax.ws.rs-api</artifactId>
284+
<version>2.0.1</version>
285+
</dependency>
303286
<!-- test dependencies -->
304287
<dependency>
305288
<groupId>junit</groupId>
@@ -313,6 +296,12 @@
313296
<version>1.3</version>
314297
<scope>test</scope>
315298
</dependency>
299+
<dependency>
300+
<groupId>org.apache.httpcomponents</groupId>
301+
<artifactId>httpclient</artifactId>
302+
<version>4.5.3</version>
303+
<scope>provided</scope>
304+
</dependency>
316305
<!-- example dependencies -->
317306
<dependency>
318307
<groupId>org.jdom</groupId>

src/main/java/com/marklogic/client/DatabaseClientFactory.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
package com.marklogic.client;
1717

1818
import java.io.Serializable;
19+
import java.lang.reflect.TypeVariable;
1920
import java.util.Set;
2021

2122
import javax.net.ssl.SSLContext;
2223
import javax.net.ssl.SSLException;
2324

25+
import okhttp3.OkHttpClient;
2426
import org.slf4j.Logger;
2527
import org.slf4j.LoggerFactory;
2628

29+
import com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator;
2730
import com.marklogic.client.extra.httpclient.HttpClientConfigurator;
2831
import com.marklogic.client.impl.DatabaseClientImpl;
2932
import com.marklogic.client.impl.HandleFactoryRegistryImpl;
30-
import com.marklogic.client.impl.JerseyServices;
33+
import com.marklogic.client.impl.OkHttpServices;
3134
import com.marklogic.client.io.marker.ContentHandle;
3235
import com.marklogic.client.io.marker.ContentHandleFactory;
3336

@@ -185,7 +188,7 @@ public interface HandleFactoryRegistry {
185188
/**
186189
* A ClientConfigurator provides custom configuration for the communication library
187190
* used to sending client requests and receiving server responses.
188-
* @see com.marklogic.client.extra.httpclient.HttpClientConfigurator
191+
* @see com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator
189192
* @param <T> the configurable class for the communication library
190193
*/
191194
public interface ClientConfigurator<T> {
@@ -319,13 +322,22 @@ static public DatabaseClient newClient(String host, int port, String database, S
319322
}
320323
static private DatabaseClientImpl newClientImpl(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
321324
logger.debug("Creating new database client for server at "+host+":"+port);
322-
JerseyServices services = new JerseyServices();
325+
OkHttpServices services = new OkHttpServices();
323326
services.connect(host, port, database, user, password, type, context, verifier);
324327

325-
if (clientConfigurator != null) {
326-
((HttpClientConfigurator) clientConfigurator).configure(
327-
services.getClientImplementation()
328-
);
328+
if ( clientConfigurator != null ) {
329+
if ( clientConfigurator instanceof OkHttpClientConfigurator ) {
330+
OkHttpClient client = services.getClientImplementation();
331+
OkHttpClient.Builder clientBuilder = client.newBuilder();
332+
((OkHttpClientConfigurator) clientConfigurator).configure(
333+
clientBuilder
334+
);
335+
((OkHttpServices) services).setClientImplementation(clientBuilder.build());
336+
} else if ( clientConfigurator instanceof HttpClientConfigurator ) {
337+
// do nothing as we not longer use HttpClient so there's nothing this can configure
338+
} else {
339+
throw new IllegalArgumentException("A ClientConfigurator must implement OkHttpClientConfigurator");
340+
}
329341
}
330342

331343
return new DatabaseClientImpl(services, host, port, database, user, password, type, context, verifier);
@@ -360,13 +372,13 @@ static public void registerDefaultHandles() {
360372
/**
361373
* Adds a listener that provides custom configuration when a communication library
362374
* is created.
363-
* @see com.marklogic.client.extra.httpclient.HttpClientConfigurator
375+
* @see com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator
364376
* @param configurator the listener for configuring the communication library
365377
*/
366378
static public void addConfigurator(ClientConfigurator<?> configurator) {
367-
if (!HttpClientConfigurator.class.isInstance(configurator)) {
379+
if (!HttpClientConfigurator.class.isInstance(configurator) && !OkHttpClientConfigurator.class.isInstance(configurator)) {
368380
throw new IllegalArgumentException(
369-
"Configurator must implement HttpClientConfigurator"
381+
"Configurator must implement OkHttpClientConfigurator"
370382
);
371383
}
372384

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2016 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.extra.okhttpclient;
17+
18+
import okhttp3.OkHttpClient;
19+
20+
import com.marklogic.client.DatabaseClientFactory.ClientConfigurator;
21+
22+
/**
23+
* Provides configuration for the HttpClient communications library.
24+
* The configurator executes during creation of a database client
25+
* after the built-in configuration completes. Use this class only
26+
* if testing demonstrates that your environment requires a different
27+
* HTTP configuration than the default HTTP configuration.
28+
*
29+
* Note: If the API moves to a different HTTP communications library or
30+
* a different protocol, the configurator will no longer be called.
31+
*
32+
* @see com.marklogic.client.DatabaseClientFactory#addConfigurator(ClientConfigurator)
33+
*/
34+
public interface OkHttpClientConfigurator extends ClientConfigurator<OkHttpClient.Builder> {
35+
}

src/main/java/com/marklogic/client/impl/DigestChallengeFilter.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/main/java/com/marklogic/client/impl/FailedRequest.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020

21-
import javax.ws.rs.core.MediaType;
2221
import javax.xml.parsers.DocumentBuilder;
2322
import javax.xml.parsers.DocumentBuilderFactory;
2423
import javax.xml.parsers.ParserConfigurationException;
2524

25+
import okhttp3.MediaType;
2626
import org.w3c.dom.Document;
2727
import org.w3c.dom.NodeList;
2828
import org.xml.sax.SAXException;
@@ -44,6 +44,7 @@
4444
*
4545
*/
4646
public class FailedRequest {
47+
private static final String ERROR_NS = "http://marklogic.com/xdmp/error";
4748

4849
private String messageCode;
4950

@@ -67,7 +68,7 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream is) {
6768
builder = factory.newDocumentBuilder();
6869
Document doc = builder.parse(is);
6970
String statusCode = null;
70-
NodeList statusCodes = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "status-code");
71+
NodeList statusCodes = doc.getElementsByTagNameNS(ERROR_NS, "status-code");
7172
if ( statusCodes != null && statusCodes.getLength() > 0 ) {
7273
statusCode = statusCodes.item(0).getTextContent();
7374
}
@@ -76,24 +77,24 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream is) {
7677
} else {
7778
failure.setStatusCode(httpStatus);
7879
}
79-
NodeList statuses = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "status");
80+
NodeList statuses = doc.getElementsByTagNameNS(ERROR_NS, "status");
8081
if ( statuses != null && statuses.getLength() > 0 ) {
8182
failure.setStatusString( statuses.item(0).getTextContent() );
8283
}
83-
NodeList messageCodes = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "message-code");
84+
NodeList messageCodes = doc.getElementsByTagNameNS(ERROR_NS, "message-code");
8485
if ( messageCodes != null && messageCodes.getLength() > 0 ) {
8586
failure.setMessageCode( messageCodes.item(0).getTextContent() );
8687
}
8788
// the following is for eval errors
8889
String formatString = null;
89-
NodeList formatStrings = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "format-string");
90+
NodeList formatStrings = doc.getElementsByTagNameNS(ERROR_NS, "format-string");
9091
if ( formatStrings != null && formatStrings.getLength() > 0 ) {
9192
formatString = formatStrings.item(0).getTextContent();
9293
}
9394
if ( formatString != null ) {
9495
failure.setMessageString(formatString);
9596
} else {
96-
NodeList messageStrings = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "message");
97+
NodeList messageStrings = doc.getElementsByTagNameNS(ERROR_NS, "message");
9798
if ( messageStrings != null && messageStrings.getLength() > 0 ) {
9899
failure.setMessageString( messageStrings.item(0).getTextContent() );
99100
}
@@ -112,30 +113,30 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream is) {
112113
}
113114

114115
}
115-
/*
116-
* send an InputStream to this handler in order to create an error block.
117-
*/
118-
public static FailedRequest getFailedRequest(int httpStatus, MediaType contentType, InputStream content) {
119-
FailedRequest failure;
120-
116+
117+
/*
118+
* send an InputStream to this handler in order to create an error block.
119+
*/
120+
public static FailedRequest getFailedRequest(int httpStatus, String contentType, InputStream content) {
121+
FailedRequest failure = null;
122+
121123
// by default XML is supported
122-
if (MediaType.APPLICATION_XML_TYPE.isCompatible(contentType)) {
123-
124-
FailedRequestParser xmlParser = new FailedRequestXMLParser();
125-
126-
failure = xmlParser.parseFailedRequest(httpStatus, content);
127-
128-
}
129-
else if (MediaType.APPLICATION_JSON_TYPE.isCompatible(contentType)) {
130-
failure = jsonFailedRequest(httpStatus, content);
131-
}
132-
else if (contentType == null && httpStatus == 404) {
124+
if ( contentType != null ) {
125+
MediaType mediaType = MediaType.parse(contentType);
126+
if ( "xml".equals(mediaType.subtype()) ) {
127+
FailedRequestParser xmlParser = new FailedRequestXMLParser();
128+
129+
failure = xmlParser.parseFailedRequest(httpStatus, content);
130+
} else if ( "json".equals(mediaType.subtype()) ) {
131+
failure = jsonFailedRequest(httpStatus, content);
132+
}
133+
} else if (httpStatus == 404) {
133134
failure = new FailedRequest();
134135
failure.setStatusCode(httpStatus);
135136
failure.setMessageString("");
136137
failure.setStatusString("Not Found");
137138
}
138-
else {
139+
if ( failure == null ) {
139140
failure = new FailedRequest();
140141
failure.setStatusCode(httpStatus);
141142
failure.setMessageCode("UNKNOWN");

0 commit comments

Comments
 (0)