Skip to content

Commit 1f9fecc

Browse files
committed
fixes #5 sync with KaiZen parser
1 parent ff62818 commit 1f9fecc

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

src/main/java/com/networknt/oas/validator/ValidatorBase.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
import com.fasterxml.jackson.databind.JsonNode;
1414
import com.fasterxml.jackson.databind.node.*;
1515
import com.networknt.jsonoverlay.*;
16-
import com.networknt.oas.validator.oasparser.fake.scheme.Handler;
1716

1817
import javax.mail.internet.AddressException;
1918
import javax.mail.internet.InternetAddress;
19+
import java.io.IOException;
2020
import java.net.MalformedURLException;
2121
import java.net.URL;
22+
import java.net.URLConnection;
23+
import java.net.URLStreamHandler;
2224
import java.util.*;
2325
import java.util.function.Consumer;
2426
import java.util.function.Function;
@@ -104,32 +106,38 @@ public Overlay<String> validateUrlField(String name, boolean required, boolean a
104106
return validateStringField(name, required, pattern, field -> checkUrl(field, allowRelative, allowVars));
105107
}
106108

107-
public static final String SPECIAL_SCHEME = Handler.class.getPackage().getName()
108-
.substring(ValidatorBase.class.getPackage().getName().length() + 1);
109-
private static boolean specialSchemeInited = false;
109+
private static String FAKE_SCHEME = "oasparser.fake.scheme";
110+
private static URLStreamHandler fakeHandler = new URLStreamHandler() {
111+
@Override
112+
protected URLConnection openConnection(URL u) throws IOException {
113+
return null;
114+
}
115+
};
110116

111117
private void checkUrl(Overlay<String> overlay, boolean allowRelative, boolean allowVars) {
112-
initSpecialScheme();
113118
// TODO Q: Any help from spec in being able to validate URLs with vars? E.g is
114119
// our treatment here valid? We assume vars can only appear where simple text
115120
// can appear, so handling vars means relacing {.*} with "1" and testing for URL
116121
// validity. We use a digit instead of a letter because it covers vars in the
117122
// port, and elsewhere digits are always allowed where letters are.
118123
String origUrl = overlay.get();
119124
String url = origUrl;
125+
boolean fake = false;
120126
if (allowVars) {
121127
url = url.replaceAll("\\{[^}]+\\}", "1");
122128
if (url.startsWith("1:")) {
123129
// "1" is not a valid scheme name, so we need to replace it with special scheme,
124130
// for which we provide a do-nothing protocol handler implementation
125-
url = SPECIAL_SCHEME + url.substring(1);
131+
url = FAKE_SCHEME + url.substring(1);
132+
fake = true;
126133
}
127134
}
128135
try {
129-
new URL(url);
136+
new URL(null, url, fake ? fakeHandler : null);
130137
} catch (MalformedURLException e) {
131138
try {
132-
new URL(new URL(SPECIAL_SCHEME + ":/"), url);
139+
URL context = new URL(null, FAKE_SCHEME + ":/", fakeHandler);
140+
new URL(context, url);
133141
if (!allowRelative) {
134142
results.addError(msg(BaseValidationMessages.NoRelUrl, origUrl, e.toString()), overlay);
135143
}
@@ -139,25 +147,6 @@ private void checkUrl(Overlay<String> overlay, boolean allowRelative, boolean al
139147
}
140148
}
141149

142-
private void initSpecialScheme() {
143-
if (!specialSchemeInited) {
144-
String prop = "java.protocol.handler.pkgs";
145-
String former = System.getProperty(prop);
146-
try {
147-
System.setProperty(prop, ValidatorBase.class.getPackage().getName());
148-
new URL(SPECIAL_SCHEME + ":");
149-
} catch (MalformedURLException e) {
150-
} finally {
151-
if (former != null) {
152-
System.setProperty(prop, former);
153-
} else {
154-
System.getProperties().remove(prop);
155-
}
156-
}
157-
specialSchemeInited = true;
158-
}
159-
}
160-
161150
public Overlay<String> validateEmailField(String name, boolean required) {
162151
return validateEmailField(name, required, (Pattern) null);
163152
}
@@ -206,7 +195,7 @@ public Overlay<Number> validateNumericField(String name, boolean required, Funct
206195
public final <F> Overlay<F> validateField(String name, boolean required, Class<F> fieldClass,
207196
Validator<F> validator, Consumer<Overlay<F>>... otherChecks) {
208197
@SuppressWarnings("unchecked")
209-
PropertiesOverlay<V> propValue = (PropertiesOverlay<V>) value.get();
198+
PropertiesOverlay<V> propValue = (PropertiesOverlay<V>) value.get();
210199
Overlay<F> field = Overlay.of(propValue, name, fieldClass);
211200
checkJsonType(field, getAllowedJsonTypes(field), results);
212201
checkMissing(field, required);
@@ -220,7 +209,7 @@ public final <F> Overlay<F> validateField(String name, boolean required, Class<F
220209
}
221210

222211
public <X> Overlay<List<X>> validateListField(String name, boolean nonEmpty, boolean unique, Class<X> itemClass,
223-
Validator<X> itemValidator) {
212+
Validator<X> itemValidator) {
224213
@SuppressWarnings("unchecked")
225214
Overlay<List<X>> list = (Overlay<List<X>>) (Object) Overlay.of((PropertiesOverlay<V>) value.get(), name,
226215
List.class);

0 commit comments

Comments
 (0)