Search in sources :

Example 26 with PathSegment

use of javax.ws.rs.core.PathSegment in project cxf by apache.

the class UriBuilderImpl method doPath.

private UriBuilder doPath(String path, boolean checkSegments) {
    if (path == null) {
        throw new IllegalArgumentException("path is null");
    }
    if (isAbsoluteUriPath(path)) {
        try {
            URI uri = URI.create(path);
            this.originalPathEmpty = StringUtils.isEmpty(uri.getPath());
            uri(uri);
        } catch (IllegalArgumentException ex) {
            if (!URITemplate.createExactTemplate(path).getVariables().isEmpty()) {
                return uriAsTemplate(path);
            }
            String pathEncoded = HttpUtils.pathEncode(path);
            // Bad hack to bypass the TCK usage of bogus URI with empty paths containing matrix parameters,
            // which even URI class chokes upon; cheaper to do the following than try to challenge,
            // given that URI RFC mentions the possibility of empty paths, though no word on the possibility of
            // such empty paths having matrix parameters...
            int schemeIndex = pathEncoded.indexOf("//");
            if (schemeIndex != -1) {
                int pathComponentStart = pathEncoded.indexOf("/", schemeIndex + 2);
                if (pathComponentStart == -1) {
                    this.originalPathEmpty = true;
                    pathComponentStart = pathEncoded.indexOf(";");
                    if (pathComponentStart != -1) {
                        pathEncoded = pathEncoded.substring(0, pathComponentStart) + "/" + pathEncoded.substring(pathComponentStart);
                    }
                }
            }
            setUriParts(URI.create(pathEncoded));
        }
        return this;
    }
    if (paths.isEmpty()) {
        leadingSlash = path.startsWith("/");
    }
    List<PathSegment> segments;
    if (checkSegments) {
        segments = JAXRSUtils.getPathSegments(path, false, false);
    } else {
        segments = new ArrayList<>();
        path = path.replaceAll("/", "%2F");
        segments.add(new PathSegmentImpl(path, false));
    }
    if (!paths.isEmpty() && !matrix.isEmpty()) {
        PathSegment ps = paths.remove(paths.size() - 1);
        paths.add(replacePathSegment(ps));
    }
    paths.addAll(segments);
    matrix.clear();
    if (!paths.isEmpty()) {
        matrix = paths.get(paths.size() - 1).getMatrixParameters();
    }
    return this;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) URI(java.net.URI)

Example 27 with PathSegment

use of javax.ws.rs.core.PathSegment in project cxf by apache.

the class UriBuilderImpl method buildPath.

private String buildPath() {
    StringBuilder sb = new StringBuilder();
    Iterator<PathSegment> iter = paths.iterator();
    while (iter.hasNext()) {
        PathSegment ps = iter.next();
        String p = ps.getPath();
        if (p.length() != 0 || !iter.hasNext()) {
            p = URITemplate.createExactTemplate(p).encodeLiteralCharacters(false);
            if (sb.length() == 0 && leadingSlash) {
                sb.append('/');
            } else if (!p.startsWith("/") && sb.length() > 0) {
                sb.append('/');
            }
            sb.append(p);
            if (iter.hasNext()) {
                buildMatrix(sb, ps.getMatrixParameters());
            }
        }
    }
    buildMatrix(sb, matrix);
    return sb.toString();
}
Also used : PathSegment(javax.ws.rs.core.PathSegment)

Example 28 with PathSegment

use of javax.ws.rs.core.PathSegment in project cxf by apache.

the class URITemplate method match.

public boolean match(String uri, MultivaluedMap<String, String> templateVariableToValue) {
    if (uri == null) {
        return (templateRegexPattern == null) ? true : false;
    }
    if (templateRegexPattern == null) {
        return false;
    }
    Matcher m = templateRegexPattern.matcher(uri);
    if (!m.matches() || template.equals(SLASH) && uri.startsWith(SLASH_QUOTE)) {
        if (uri.contains(";")) {
            // we might be trying to match one or few path segments
            // containing matrix
            // parameters against a clear path segment as in @Path("base").
            List<PathSegment> pList = JAXRSUtils.getPathSegments(template, false);
            List<PathSegment> uList = JAXRSUtils.getPathSegments(uri, false);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < uList.size(); i++) {
                String segment = null;
                if (pList.size() > i && pList.get(i).getPath().indexOf('{') == -1) {
                    segment = uList.get(i).getPath();
                } else {
                    segment = HttpUtils.fromPathSegment(uList.get(i));
                }
                if (segment.length() > 0) {
                    sb.append(SLASH);
                }
                sb.append(segment);
            }
            uri = sb.toString();
            if (uri.length() == 0) {
                uri = SLASH;
            }
            m = templateRegexPattern.matcher(uri);
            if (!m.matches()) {
                return false;
            }
        } else {
            return false;
        }
    }
    // Assign the matched template values to template variables
    int groupCount = m.groupCount();
    int i = 1;
    for (String name : variables) {
        while (i <= groupCount) {
            String value = m.group(i++);
            if ((value == null || value.length() == 0 && i < groupCount) && variables.size() + 1 < groupCount) {
                continue;
            }
            templateVariableToValue.add(name, value);
            break;
        }
    }
    // The right hand side value, might be used to further resolve
    // sub-resources.
    String finalGroup = i > groupCount ? SLASH : m.group(groupCount);
    if (finalGroup == null || finalGroup.startsWith(SLASH_QUOTE)) {
        finalGroup = SLASH;
    }
    templateVariableToValue.putSingle(FINAL_MATCH_GROUP, finalGroup);
    return true;
}
Also used : Matcher(java.util.regex.Matcher) PathSegment(javax.ws.rs.core.PathSegment)

Example 29 with PathSegment

use of javax.ws.rs.core.PathSegment in project scheduling by ow2-proactive.

the class SchedulerRestWorkflowFromCatalogExecutionTest method getOneVariablePathSegment.

private PathSegment getOneVariablePathSegment(String key, String value) {
    PathSegment pathSegment = mock(PathSegment.class);
    MultivaluedMap<String, String> matrix = new MultivaluedHashMap<>();
    if (key != null) {
        matrix.put(key, Arrays.asList(value));
    }
    when(pathSegment.getMatrixParameters()).thenReturn(matrix);
    return pathSegment;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PathSegment(javax.ws.rs.core.PathSegment)

Example 30 with PathSegment

use of javax.ws.rs.core.PathSegment in project scheduling by ow2-proactive.

the class WorkflowVariablesTransformerTest method testTwoVariablesEmptyMap.

@Test
public void testTwoVariablesEmptyMap() {
    Map<String, String> expectedVariables = Maps.newHashMap();
    expectedVariables.put("KEY1", "");
    expectedVariables.put("KEY2", "");
    PathSegment pathSegment = mock(PathSegment.class);
    MultivaluedMap<String, String> multivalueMap = new MultivaluedHashMap();
    multivalueMap.put("KEY1", Lists.newArrayList(""));
    multivalueMap.put("KEY2", Lists.newArrayList(""));
    when(pathSegment.getMatrixParameters()).thenReturn(multivalueMap);
    Map<String, String> variables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
    assertThat(variables, is(expectedVariables));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Aggregations

PathSegment (javax.ws.rs.core.PathSegment)33 Test (org.junit.Test)13 UriInfo (javax.ws.rs.core.UriInfo)10 Map (java.util.Map)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 URI (java.net.URI)4 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 Response (javax.ws.rs.core.Response)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 Access (io.druid.server.security.Access)3 AuthorizationInfo (io.druid.server.security.AuthorizationInfo)3 Resource (io.druid.server.security.Resource)3 TreeMap (java.util.TreeMap)3 Produces (javax.ws.rs.Produces)3 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3