Skip to content

Commit f4eb889

Browse files
committed
removing apache httpclient dependency, introducing SchemaClient interface with a default implementation using java.net.URL
1 parent 1880148 commit f4eb889

File tree

9 files changed

+123
-50
lines changed

9 files changed

+123
-50
lines changed

core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,5 @@
9595
<version>4.12</version>
9696
<scope>test</scope>
9797
</dependency>
98-
<dependency>
99-
<groupId>org.apache.httpcomponents</groupId>
100-
<artifactId>httpclient</artifactId>
101-
<version>4.5.1</version>
102-
</dependency>
10398
</dependencies>
10499
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2011 Everit Kft. (http://www.everit.org)
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 org.everit.expression.json.schema.loader;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.UncheckedIOException;
21+
import java.net.URL;
22+
23+
/**
24+
* A {@link SchemaClient} implementation which uses {@link URL} for reading the remote content.
25+
*/
26+
public class DefaultSchemaClient implements SchemaClient {
27+
28+
@Override
29+
public InputStream get(final String url) {
30+
try {
31+
return (InputStream) new URL(url).getContent();
32+
} catch (IOException e) {
33+
throw new UncheckedIOException(e);
34+
}
35+
}
36+
37+
}

core/src/main/java/org/everit/expression/json/schema/loader/JSONPointer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import java.util.Objects;
2525
import java.util.function.Supplier;
2626

27-
import org.apache.http.HttpResponse;
28-
import org.apache.http.client.HttpClient;
29-
import org.apache.http.client.methods.HttpGet;
3027
import org.everit.expression.json.schema.SchemaException;
3128
import org.json.JSONArray;
3229
import org.json.JSONException;
@@ -72,18 +69,16 @@ public JSONObject getQueryResult() {
7269

7370
}
7471

75-
private static JSONObject executeWith(final HttpClient client, final String url) {
72+
private static JSONObject executeWith(final SchemaClient client, final String url) {
7673
String resp = null;
7774
BufferedReader buffReader = null;
7875
InputStreamReader reader = null;
7976
try {
80-
HttpResponse response = client.execute(new HttpGet(url));
81-
InputStream content = response.getEntity().getContent();
82-
int capacity = (int) response.getEntity().getContentLength();
83-
reader = new InputStreamReader(content, Charset.defaultCharset());
77+
InputStream responseStream = client.get(url);
78+
reader = new InputStreamReader(responseStream, Charset.defaultCharset());
8479
buffReader = new BufferedReader(reader);
8580
String line;
86-
StringBuilder strBuilder = new StringBuilder(capacity);
81+
StringBuilder strBuilder = new StringBuilder();
8782
while ((line = buffReader.readLine()) != null) {
8883
strBuilder.append(line);
8984
}
@@ -114,7 +109,7 @@ public static final JSONPointer forDocument(final JSONObject document, final Str
114109
/**
115110
* Static factory method.
116111
*/
117-
public static final JSONPointer forURL(final HttpClient httpClient, final String url) {
112+
public static final JSONPointer forURL(final SchemaClient httpClient, final String url) {
118113
int poundIdx = url.indexOf('#');
119114
String fragment;
120115
String toBeQueried;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2011 Everit Kft. (http://www.everit.org)
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 org.everit.expression.json.schema.loader;
17+
18+
import java.io.InputStream;
19+
import java.util.function.Function;
20+
21+
/**
22+
* This interface is used by {@link SchemaLoader} and {@link JSONPointer} to fetch the contents
23+
* denoted by remote JSON pointer.
24+
*
25+
* <p>
26+
* Implementations are expected to support the HTTP/1.1 protocol, the support of other protocols is
27+
* optional.
28+
* </p>
29+
*
30+
* @see DefaultSchemaClient
31+
*/
32+
@FunctionalInterface
33+
public interface SchemaClient extends Function<String, InputStream> {
34+
35+
@Override
36+
default InputStream apply(final String url) {
37+
return get(url);
38+
}
39+
40+
/**
41+
* Returns a stream to be used for reading the remote content (response body) of the URL. In the
42+
* case of a HTTP URL, implementations are expected send HTTP GET requests and the response is
43+
* expected to be represented in UTF-8 character set.
44+
*
45+
* @param url
46+
* the URL of the remote resource
47+
* @return the input stream of the response
48+
* @throws java.io.UncheckedIOException
49+
* if an IO error occurs.
50+
*/
51+
InputStream get(String url);
52+
53+
}

core/src/main/java/org/everit/expression/json/schema/loader/SchemaLoader.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.util.stream.Collectors;
3131
import java.util.stream.IntStream;
3232

33-
import org.apache.http.client.HttpClient;
34-
import org.apache.http.impl.client.HttpClients;
3533
import org.everit.expression.json.schema.ArraySchema;
3634
import org.everit.expression.json.schema.BooleanSchema;
3735
import org.everit.expression.json.schema.CombinedSchema;
@@ -41,11 +39,11 @@
4139
import org.everit.expression.json.schema.NullSchema;
4240
import org.everit.expression.json.schema.NumberSchema;
4341
import org.everit.expression.json.schema.ObjectSchema;
42+
import org.everit.expression.json.schema.ObjectSchema.Builder;
4443
import org.everit.expression.json.schema.ReferenceSchema;
4544
import org.everit.expression.json.schema.Schema;
4645
import org.everit.expression.json.schema.SchemaException;
4746
import org.everit.expression.json.schema.StringSchema;
48-
import org.everit.expression.json.schema.ObjectSchema.Builder;
4947
import org.everit.expression.json.schema.loader.JSONPointer.QueryResult;
5048
import org.json.JSONArray;
5149
import org.json.JSONObject;
@@ -83,15 +81,15 @@ public class SchemaLoader {
8381
}
8482

8583
/**
86-
* Loads a JSON schema to a schema validator using a {@link HttpClients#createDefault() default
87-
* HTTP client}.
84+
* Loads a JSON schema to a schema validator using a {@link DefaultSchemaClient default HTTP
85+
* client}.
8886
*
8987
* @param schemaJson
9088
* the JSON representation of the schema.
9189
* @return the schema validator object
9290
*/
9391
public static Schema load(final JSONObject schemaJson) {
94-
return load(schemaJson, HttpClients.createDefault());
92+
return load(schemaJson, new DefaultSchemaClient());
9593
}
9694

9795
/**
@@ -102,7 +100,7 @@ public static Schema load(final JSONObject schemaJson) {
102100
* @param httpClient
103101
* the HTTP client to be used for resolving remote JSON references.
104102
*/
105-
public static Schema load(final JSONObject schemaJson, final HttpClient httpClient) {
103+
public static Schema load(final JSONObject schemaJson, final SchemaClient httpClient) {
106104
String schemaId = schemaJson.optString("id");
107105
return new SchemaLoader(schemaId, schemaJson, schemaJson, new HashMap<>(), httpClient)
108106
.load().build();
@@ -289,7 +287,7 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
289287

290288
private final JSONObject rootSchemaJson;
291289

292-
private final HttpClient httpClient;
290+
private final SchemaClient httpClient;
293291

294292
private final Map<String, ReferenceSchema.Builder> pointerSchemas;
295293

@@ -298,7 +296,7 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
298296
*/
299297
SchemaLoader(final String id, final JSONObject schemaJson,
300298
final JSONObject rootSchemaJson, final Map<String, ReferenceSchema.Builder> pointerSchemas,
301-
final HttpClient httpClient) {
299+
final SchemaClient httpClient) {
302300
this.schemaJson = Objects.requireNonNull(schemaJson, "schemaJson cannot be null");
303301
this.rootSchemaJson = Objects.requireNonNull(rootSchemaJson, "rootSchemaJson cannot be null");
304302
this.id = id;

core/src/test/java/org/everit/expression/json/schema/loader/SchemaLoaderTest.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22-
import org.apache.http.client.HttpClient;
23-
import org.apache.http.impl.client.HttpClients;
2422
import org.everit.expression.json.schema.ArraySchema;
2523
import org.everit.expression.json.schema.BooleanSchema;
2624
import org.everit.expression.json.schema.CombinedSchema;
@@ -34,7 +32,6 @@
3432
import org.everit.expression.json.schema.Schema;
3533
import org.everit.expression.json.schema.SchemaException;
3634
import org.everit.expression.json.schema.StringSchema;
37-
import org.everit.expression.json.schema.loader.SchemaLoader;
3835
import org.json.JSONArray;
3936
import org.json.JSONObject;
4037
import org.json.JSONTokener;
@@ -46,27 +43,27 @@ public class SchemaLoaderTest {
4643

4744
private static JSONObject ALL_SCHEMAS;
4845

49-
private final HttpClient httpClient = HttpClients.createDefault();
46+
private final SchemaClient httpClient = new DefaultSchemaClient();
5047

5148
@Test
5249
public void typeBasedMultiplexerTest() {
5350
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject(),
5451
new HashMap<>(),
5552
httpClient);
5653
loader.typeMultiplexer(new JSONObject())
57-
.ifObject().then(jsonObj -> {
58-
})
59-
.ifIs(JSONArray.class).then(jsonArr -> {
60-
})
61-
.orElse(obj -> {
62-
});
54+
.ifObject().then(jsonObj -> {
55+
})
56+
.ifIs(JSONArray.class).then(jsonArr -> {
57+
})
58+
.orElse(obj -> {
59+
});
6360

6461
loader.typeMultiplexer(new JSONObject())
65-
.ifObject().then(jsonObj -> {
66-
})
67-
.ifIs(JSONArray.class).then(jsonArr -> {
68-
})
69-
.requireAny();
62+
.ifObject().then(jsonObj -> {
63+
})
64+
.ifIs(JSONArray.class).then(jsonArr -> {
65+
})
66+
.requireAny();
7067
}
7168

7269
@Test(expected = SchemaException.class)
@@ -75,11 +72,11 @@ public void typeBasedMultiplexerFailure() {
7572
new HashMap<>(),
7673
httpClient);
7774
loader.typeMultiplexer("foo")
78-
.ifObject().then(o -> {
79-
})
80-
.ifIs(JSONArray.class).then(o -> {
81-
})
82-
.requireAny();
75+
.ifObject().then(o -> {
76+
})
77+
.ifIs(JSONArray.class).then(o -> {
78+
})
79+
.requireAny();
8380
}
8481

8582
@BeforeClass

tests/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646
<dependencies>
4747
<dependency>
48-
<groupId>org.everit</groupId>
49-
<artifactId>org.everit.jsonvalidator</artifactId>
48+
<groupId>org.everit.expression</groupId>
49+
<artifactId>org.everit.expression.json.schema</artifactId>
5050
<version>${project.version}</version>
5151
</dependency>
5252
<dependency>

tests/src/test/java/org/everit/expression/json/schema/IntegrationTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323

2424
import org.eclipse.jetty.server.Server;
2525
import org.eclipse.jetty.servlet.ServletHandler;
26-
import org.everit.jsonvalidator.Schema;
27-
import org.everit.jsonvalidator.SchemaException;
28-
import org.everit.jsonvalidator.ValidationException;
29-
import org.everit.jsonvalidator.loader.SchemaLoader;
26+
import org.everit.expression.json.schema.loader.SchemaLoader;
3027
import org.json.JSONArray;
3128
import org.json.JSONException;
3229
import org.json.JSONObject;

tests/src/test/java/org/everit/expression/json/schema/TestServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
3737
resp.getWriter().println("resource " + req.getPathInfo() + " not found");
3838
return;
3939
}
40+
resp.setContentType("application/json");
4041
int b;
4142
ServletOutputStream out = resp.getOutputStream();
4243
while ((b = in.read()) != -1) { // cannot believe i wrote this crap

0 commit comments

Comments
 (0)