use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.
the class PathParamInjector method inject.
@Override
public Object inject(HttpRequest request, HttpResponse response, boolean unwrapAsync) {
if (// we are a PathSegment
extractor == null) {
ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
List<PathSegment[]> list = null;
if (encode) {
list = uriInfo.getEncodedPathParameterPathSegments().get(paramName);
} else {
list = uriInfo.getPathParameterPathSegments().get(paramName);
}
if (list == null) {
throw new InternalServerErrorException(Messages.MESSAGES.unknownPathParam(paramName, uriInfo.getPath()));
}
List<PathSegment> segmentList = flattenToList(list);
if (pathSegmentArray) {
PathSegment[] segments = new PathSegment[segmentList.size()];
segments = segmentList.toArray(segments);
return segments;
} else if (pathSegmentList) {
return segmentList;
} else {
return segmentList.get(segmentList.size() - 1);
}
} else {
List<String> list = request.getUri().getPathParameters(!encode).get(paramName);
if (list == null) {
if (extractor.isCollectionOrArray()) {
return extractor.extractValues(null);
} else {
return extractor.extractValue(null);
}
}
if (extractor.isCollectionOrArray()) {
return extractor.extractValues(list);
} else {
return extractor.extractValue(list.get(list.size() - 1));
}
}
}
use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.
the class MethodExpression method populatePathParams.
public void populatePathParams(HttpRequest request, Matcher matcher, String path) {
ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
for (Group group : groups) {
String value = matcher.group(group.group);
uriInfo.addEncodedPathParameter(group.name, value);
int index = matcher.start(group.group);
int start = 0;
if (path.charAt(0) == '/')
start++;
int segmentIndex = 0;
if (start < path.length()) {
int count = 0;
for (int i = start; i < index && i < path.length(); i++) {
if (path.charAt(i) == '/')
count++;
}
segmentIndex = count;
}
int numSegments = 1;
for (int i = 0; i < value.length(); i++) {
if (value.charAt(i) == '/')
numSegments++;
}
if (segmentIndex + numSegments > request.getUri().getPathSegments().size()) {
throw new BadRequestException(Messages.MESSAGES.numberOfMatchedSegments());
}
PathSegment[] encodedSegments = new PathSegment[numSegments];
PathSegment[] decodedSegments = new PathSegment[numSegments];
for (int i = 0; i < numSegments; i++) {
decodedSegments[i] = request.getUri().getPathSegments().get(segmentIndex + i);
encodedSegments[i] = request.getUri().getPathSegments(false).get(segmentIndex + i);
}
uriInfo.getEncodedPathParameterPathSegments().add(group.name, encodedSegments);
uriInfo.getPathParameterPathSegments().add(group.name, decodedSegments);
}
}
use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.
the class SegmentNode method match.
public MatchCache match(HttpRequest request, int start) {
String path = ((ResteasyUriInfo) request.getUri()).getMatchingPath();
RESTEasyTracingLogger logger = RESTEasyTracingLogger.getInstance(request);
logger.log("MATCH_PATH_FIND", ((ResteasyUriInfo) request.getUri()).getMatchingPath());
if (start < path.length() && path.charAt(start) == '/')
start++;
List<MethodExpression> potentials = new ArrayList<MethodExpression>();
potentials(path, start, potentials);
Collections.sort(potentials);
boolean expressionMatched = false;
List<Match> matches = new ArrayList<Match>();
for (MethodExpression expression : potentials) {
// We ignore locators if the first match was a resource method as per the spec Section 3, Step 2(h)
if (expressionMatched && expression.isLocator()) {
logger.log("MATCH_PATH_SKIPPED", expression.getRegex());
continue;
}
Pattern pattern = expression.getPattern();
Matcher matcher = pattern.matcher(path);
matcher.region(start, path.length());
if (matcher.matches()) {
expressionMatched = true;
ResourceInvoker invoker = expression.getInvoker();
if (invoker instanceof ResourceLocatorInvoker) {
MatchCache ctx = new MatchCache();
ctx.invoker = invoker;
ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
int length = matcher.start(expression.getNumGroups() + 1);
if (length == -1) {
uriInfo.pushMatchedPath(path);
uriInfo.pushMatchedURI(path);
} else {
// must find the end of the matched pattern
// and get the substring from 1st char thru end
// of matched chars
Pattern p = expression.getPattern();
Matcher m = p.matcher(path);
m.region(start, path.length());
String substring = path;
while (m.find()) {
String endText = m.group(m.groupCount());
if (endText != null && !endText.isEmpty()) {
int indx = path.indexOf(endText, length);
if (indx > -1) {
substring = path.substring(0, indx);
}
}
}
uriInfo.pushMatchedPath(substring);
uriInfo.pushMatchedURI(substring);
}
expression.populatePathParams(request, matcher, path);
logger.log("MATCH_LOCATOR", invoker.getMethod());
return ctx;
} else {
matches.add(new Match(expression, matcher));
}
} else {
logger.log("MATCH_PATH_NOT_MATCHED", expression.getRegex());
}
}
if (matches.size() == 0) {
throw new NotFoundException(Messages.MESSAGES.couldNotFindResourceForFullPath(request.getUri().getRequestUri()));
}
MatchCache match = match(matches, request.getHttpMethod(), request);
match.match.expression.populatePathParams(request, match.match.matcher, path);
logger.log("MATCH_PATH_SELECTED", match.match.expression.getRegex());
return match;
}
use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.
the class ClassNode method match.
public RootNode match(HttpRequest request, int start) {
String path = ((ResteasyUriInfo) request.getUri()).getMatchingPath();
if (start < path.length() && path.charAt(start) == '/')
start++;
List<ClassExpression> potentials = new ArrayList<ClassExpression>();
potentials(path, start, potentials);
Collections.sort(potentials);
for (ClassExpression expression : potentials) {
Pattern pattern = expression.getPattern();
Matcher matcher = pattern.matcher(path);
matcher.region(start, path.length());
if (matcher.matches()) {
ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
int length = matcher.start(expression.getNumGroups() + 1);
if (length == -1) {
uriInfo.pushMatchedURI(path);
} else {
String substring = path.substring(0, length);
uriInfo.pushMatchedURI(substring);
}
RESTEasyTracingLogger logger = RESTEasyTracingLogger.getInstance(request);
if (logger.isLogEnabled("MATCH_RUNTIME_RESOURCE")) {
logger.log("MATCH_RUNTIME_RESOURCE", expression, expression.getRegex(), expression.getRoot().root, expression.getPathExpression());
}
return expression.getRoot();
}
}
throw new NotFoundException(Messages.MESSAGES.couldNotFindResourceForFullPath(request.getUri().getRequestUri()));
}
use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.
the class MockHttpRequest method create.
public static MockHttpRequest create(String httpMethod, String absolute, String query, String contextPath) {
MockHttpRequest request = new MockHttpRequest();
request.httpHeaders = new ResteasyHttpHeaders(new CaseInsensitiveMap<String>());
if (query != null && query.length() > 0) {
absolute = absolute + "?" + query;
}
request.uri = new ResteasyUriInfo(absolute, contextPath);
request.httpMethod = httpMethod;
return request;
}
Aggregations