Skip to content

Commit ef579d1

Browse files
authored
Merge pull request #43 from escenic/master
Replaced 'new URL' with a call to a new URLFactory
2 parents 3dd481e + 4bb14fb commit ef579d1

File tree

5 files changed

+126
-5
lines changed

5 files changed

+126
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.networknt</groupId>
2121
<artifactId>json-schema-validator</artifactId>
22-
<version>0.1.10</version>
22+
<version>0.1.10-escenic</version>
2323
<description>A json schema validator that supports draft v4</description>
2424
<url>https://github.com/networknt/json-schema-validator</url>
2525
<name>JsonSchemaValidator</name>

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.networknt.schema.url.URLFactory;
2021
import org.apache.commons.lang3.StringUtils;
2122
import org.slf4j.Logger;
2223

@@ -77,8 +78,14 @@ protected JsonSchema obainSubSchemaNode(JsonNode schemaNode){
7778

7879
try {
7980
JsonSchemaFactory factory = new JsonSchemaFactory();
80-
URL url = new URL(node.textValue());
81-
return factory.getSchema(url);
81+
String text = node.textValue();
82+
if (text == null) {
83+
return null;
84+
}
85+
else {
86+
URL url = URLFactory.toURL(node.textValue());
87+
return factory.getSchema(url);
88+
}
8289
} catch (MalformedURLException e) {
8390
return null;
8491
}

src/main/java/com/networknt/schema/RefValidator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.networknt.schema.url.URLFactory;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

@@ -53,7 +54,7 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
5354

5455
JsonSchemaFactory factory = new JsonSchemaFactory(mapper);
5556
try {
56-
URL url = new URL(schemaUrl);
57+
URL url = URLFactory.toURL(schemaUrl);
5758
parentSchema = factory.getSchema(url);
5859
} catch (MalformedURLException e) {
5960
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
@@ -89,7 +90,7 @@ private String obtainAbsolutePath(JsonSchema parentSchema, String schemaUrl) {
8990
if(schemaRef.startsWith(REF_DOMAIN)){
9091
// from domain add ref
9192
try {
92-
URL url = new URL(baseSchemaUrl);
93+
URL url = URLFactory.toURL(baseSchemaUrl);
9394
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
9495
} catch (MalformedURLException e) {
9596
e.printStackTrace();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.networknt.schema.url;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.URL;
6+
import java.net.URLConnection;
7+
import java.net.URLStreamHandler;
8+
9+
/**
10+
* An {@link URLStreamHandler} capable of loading resources from the classpath.
11+
*
12+
* @author <a href="mailto:[email protected]">Kenneth Waldenstrom</a>
13+
*/
14+
class ClasspathURLStreamHandler extends URLStreamHandler {
15+
private final static String CLASSPATH_PREFIX = "classpath:";
16+
private final static String RESOURCE_PREFIX = "resource:";
17+
18+
boolean supports(final String pURL) {
19+
return pURL.startsWith(CLASSPATH_PREFIX) || pURL.startsWith(RESOURCE_PREFIX);
20+
}
21+
22+
@Override
23+
protected URLConnection openConnection(final URL pURL) throws IOException {
24+
return new ClassPathURLConnection(pURL);
25+
}
26+
27+
class ClassPathURLConnection extends URLConnection {
28+
29+
private Class<?> mHost = null;
30+
31+
protected ClassPathURLConnection(URL pURL) {
32+
super(pURL);
33+
}
34+
35+
@Override
36+
public final void connect() throws IOException {
37+
String className = url.getHost();
38+
try {
39+
if (className != null && className.length() > 0) {
40+
mHost = Class.forName(className);
41+
}
42+
connected = true;
43+
}
44+
catch (ClassNotFoundException e) {
45+
throw new IOException("Class not found: " + e.toString());
46+
}
47+
}
48+
49+
@Override
50+
public final InputStream getInputStream() throws IOException {
51+
if (!connected) {
52+
connect();
53+
}
54+
return getResourceAsStream(url);
55+
}
56+
57+
private InputStream getResourceAsStream(URL pURL) throws IOException {
58+
String path = pURL.getPath();
59+
60+
if (path.startsWith("/")) {
61+
path = path.substring(1);
62+
}
63+
64+
InputStream stream;
65+
if (mHost != null) {
66+
stream = mHost.getClassLoader().getResourceAsStream(path);
67+
}
68+
else {
69+
stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
70+
if (stream == null) {
71+
stream = getClass().getClassLoader().getResourceAsStream(path);
72+
}
73+
if (stream == null) {
74+
stream = ClassLoader.getSystemResourceAsStream(path);
75+
}
76+
}
77+
if (stream == null) {
78+
throw new IOException("Resource " + path + " not found in classpath.");
79+
}
80+
return stream;
81+
}
82+
}
83+
84+
85+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.networknt.schema.url;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
6+
/**
7+
* A factory for creating {@link URL}'s. This factory creates {@link URL}'s that in additional to the standard {@link URL}'s
8+
* capability of loading resources using http, https, file, etc. also makes it possible to load resources from
9+
* the applications classpath. To load a resource from classpath, the url must be prefixed either with <i>classpath:</i>
10+
* or <i>resource:</i>
11+
*
12+
* To ensure that we support classpath resources, this class should be used instead of <code>new URL(pURL)</code>
13+
*
14+
* @author <a href="mailto:[email protected]">Kenneth Waldenstrom</a>
15+
*/
16+
public class URLFactory {
17+
private static final ClasspathURLStreamHandler sClasspathURLStreamHandler = new ClasspathURLStreamHandler();
18+
19+
/**
20+
* Creates an {@link URL} based on the provided string
21+
* @param pURL the url
22+
* @return a {@link URL}
23+
* @throws MalformedURLException if the url is not a proper URL
24+
*/
25+
public static URL toURL(final String pURL) throws MalformedURLException {
26+
return new URL(null, pURL, sClasspathURLStreamHandler.supports(pURL) ? sClasspathURLStreamHandler : null);
27+
}
28+
}

0 commit comments

Comments
 (0)