Search in sources :

Example 16 with ResteasyUriInfo

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));
        }
    }
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) InternalServerErrorException(org.jboss.resteasy.spi.InternalServerErrorException) PathSegment(jakarta.ws.rs.core.PathSegment)

Example 17 with ResteasyUriInfo

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);
    }
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) BadRequestException(jakarta.ws.rs.BadRequestException) PathSegment(jakarta.ws.rs.core.PathSegment)

Example 18 with ResteasyUriInfo

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ResourceLocatorInvoker(org.jboss.resteasy.core.ResourceLocatorInvoker) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) ArrayList(java.util.ArrayList) NotFoundException(jakarta.ws.rs.NotFoundException) ResourceInvoker(org.jboss.resteasy.spi.ResourceInvoker) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger)

Example 19 with ResteasyUriInfo

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()));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) ArrayList(java.util.ArrayList) NotFoundException(jakarta.ws.rs.NotFoundException) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger)

Example 20 with ResteasyUriInfo

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;
}
Also used : CaseInsensitiveMap(org.jboss.resteasy.util.CaseInsensitiveMap) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) ResteasyHttpHeaders(org.jboss.resteasy.specimpl.ResteasyHttpHeaders)

Aggregations

ResteasyUriInfo (org.jboss.resteasy.specimpl.ResteasyUriInfo)42 URI (java.net.URI)19 UriInfo (javax.ws.rs.core.UriInfo)14 Test (org.junit.jupiter.api.Test)14 Pager (com.redhat.cloud.policies.app.model.pager.Pager)8 Test (org.junit.Test)6 ResteasyHttpHeaders (org.jboss.resteasy.specimpl.ResteasyHttpHeaders)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 NotFoundException (jakarta.ws.rs.NotFoundException)4 IOException (java.io.IOException)3 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)2 Context (io.vertx.core.Context)2 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 PathSegment (jakarta.ws.rs.core.PathSegment)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 HttpRequest (org.jboss.resteasy.spi.HttpRequest)2 HttpResponse (org.jboss.resteasy.spi.HttpResponse)2