in juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockPathResolver.java [61:165]
private void init(String target, String contextPath, String servletPath, String pathToResolve, Map<String,Object> pathVars) {
target = trimTrailingSlashes(emptyIfNull(target));
if (target.isEmpty())
target = "http://localhost";
contextPath = fixSegment(contextPath, pathVars);
servletPath = fixSegment(servletPath, pathVars);
pathToResolve = emptyIfNull(pathToResolve);
if (! (pathToResolve.startsWith("http://") || pathToResolve.startsWith("https://"))) {
pathToResolve = fixSegment(pathToResolve, Collections.emptyMap());
this.uri = target + contextPath + servletPath + pathToResolve;
this.target = target;
this.contextPath = contextPath;
this.servletPath = servletPath;
this.remainder = pathToResolve;
return;
}
// Path starts with http[s]: so we have to parse it to resolve variables.
this.uri = pathToResolve;
// S03 - Found "http://", looking for any character other than '/' (end of target).
// S04 - Found any character, looking for 3rd '/' (end of target).
// S05 - Found 3rd '/', resolving contextPath.
// S06 - Resolved contextPath, resolving servletPath.
// S07 - Resolved servletPath.
StateMachineState state = S03;
int cpSegments = StringUtils.countChars(contextPath, '/');
int spSegments = StringUtils.countChars(servletPath, '/');
this.contextPath = "";
this.servletPath = "";
this.remainder = "";
int mark = 0;
for (int i = uri.indexOf("://")+3; i < uri.length(); i++) {
char c = uri.charAt(i);
if (state == S03) {
if (c != '/')
state = S04;
else
break;
} else if (state == S04) {
if (c == '/') {
this.target = uri.substring(0, i);
state = S05;
if (contextPath.isEmpty()) {
state = S06;
if (servletPath.isEmpty()) {
state = S07;
}
}
mark = i;
}
} else if (state == S05) {
if (c == '/') {
cpSegments--;
if (cpSegments == 0) {
this.contextPath = uri.substring(mark, i);
mark = i;
state = S06;
if (servletPath.isEmpty()) {
state = S07;
}
}
}
} else if (state == S06) {
if (c == '/') {
spSegments--;
if (spSegments == 0) {
this.servletPath = uri.substring(mark, i);
mark = i;
state = S07;
}
}
}
}
if (state == S04) {
this.target = uri;
} else if (state == S05) {
this.contextPath = uri.substring(mark);
} else if (state == S06) {
this.servletPath = uri.substring(mark);
} else if (state == S07) {
this.remainder = uri.substring(mark);
} else {
throw new BasicRuntimeException("Invalid URI pattern encountered: {0}", uri);
}
if (! contextPath.isEmpty()) {
UrlPathMatcher p = UrlPathMatcher.of(contextPath);
if (p.match(UrlPath.of(this.contextPath)) == null)
throw new BasicRuntimeException("Context path [{0}] not found in URI: {1}", contextPath, uri);
}
if (! servletPath.isEmpty()) {
UrlPathMatcher p = UrlPathMatcher.of(servletPath);
if (p.match(UrlPath.of(this.servletPath)) == null)
throw new BasicRuntimeException("Servlet path [{0}] not found in URI: {1}", servletPath, uri);
}
}