Search in sources :

Example 1 with URITemplate

use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.

the class UriBuilderImpl method substituteMapped.

// CHECKSTYLE:OFF
private String substituteMapped(String path, Map<String, ? extends Object> varValueMap, Map<String, Object> alreadyResolvedTs, Map<String, Object> alreadyResolvedTsPathEnc, Map<String, Object> alreadyResolvedTsEnc, boolean isQuery, boolean fromEncoded, boolean encodePathSlash) {
    // CHECKSTYLE:ON
    URITemplate templ = URITemplate.createExactTemplate(path);
    Set<String> uniqueVars = new HashSet<>(templ.getVariables());
    if (varValueMap.size() + alreadyResolvedTs.size() + alreadyResolvedTsEnc.size() + alreadyResolvedTsPathEnc.size() < uniqueVars.size()) {
        throw new IllegalArgumentException("Unresolved variables; only " + varValueMap.size() + " value(s) given for " + uniqueVars.size() + " unique variable(s)");
    }
    Set<String> pathEncodeVars = alreadyResolvedTsPathEnc.isEmpty() && !encodePathSlash ? Collections.<String>emptySet() : new HashSet<>();
    Map<String, Object> theMap = new LinkedHashMap<String, Object>();
    for (String var : uniqueVars) {
        boolean isPathEncVar = !isQuery && alreadyResolvedTsPathEnc.containsKey(var);
        boolean isVarEncoded = isPathEncVar || alreadyResolvedTs.containsKey(var) ? false : true;
        Map<String, Object> resolved = isVarEncoded ? alreadyResolvedTsEnc : isPathEncVar ? alreadyResolvedTsPathEnc : alreadyResolvedTs;
        Object oval = resolved.isEmpty() ? null : resolved.remove(var);
        if (oval == null) {
            oval = varValueMap.get(var);
        }
        if (oval == null) {
            throw new IllegalArgumentException("No object for " + var);
        }
        if (fromEncoded) {
            oval = HttpUtils.encodePartiallyEncoded(oval.toString(), isQuery);
        } else {
            oval = isQuery ? HttpUtils.queryEncode(oval.toString()) : HttpUtils.pathEncode(oval.toString());
        }
        theMap.put(var, oval);
        if (!isQuery && (isPathEncVar || encodePathSlash)) {
            pathEncodeVars.add(var);
        }
    }
    return templ.substitute(theMap, pathEncodeVars, false);
}
Also used : URITemplate(org.apache.cxf.jaxrs.model.URITemplate) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with URITemplate

use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.

the class UriBuilderImpl method doBuildUriParts.

private UriParts doBuildUriParts(boolean fromEncoded, boolean encodePathSlash, boolean allowUnresolved, Object... values) {
    Map<String, Object> alreadyResolvedTs = getResolvedTemplates(resolvedTemplates);
    Map<String, Object> alreadyResolvedTsPathEnc = getResolvedTemplates(resolvedTemplatesPathEnc);
    Map<String, Object> alreadyResolvedEncTs = getResolvedTemplates(resolvedEncodedTemplates);
    final int resolvedTsSize = alreadyResolvedTs.size() + alreadyResolvedEncTs.size() + alreadyResolvedTsPathEnc.size();
    String thePath = buildPath();
    URITemplate pathTempl = URITemplate.createExactTemplate(thePath);
    thePath = substituteVarargs(pathTempl, alreadyResolvedTs, alreadyResolvedTsPathEnc, alreadyResolvedEncTs, values, 0, false, fromEncoded, allowUnresolved, encodePathSlash);
    int pathTemplateVarsSize = pathTempl.getVariables().size();
    String theQuery = buildQuery();
    int queryTemplateVarsSize = 0;
    if (theQuery != null) {
        URITemplate queryTempl = URITemplate.createExactTemplate(theQuery);
        queryTemplateVarsSize = queryTempl.getVariables().size();
        if (queryTemplateVarsSize > 0) {
            int lengthDiff = values.length + resolvedTsSize - alreadyResolvedTs.size() - alreadyResolvedTsPathEnc.size() - alreadyResolvedEncTs.size() - pathTemplateVarsSize;
            theQuery = substituteVarargs(queryTempl, alreadyResolvedTs, alreadyResolvedTsPathEnc, alreadyResolvedEncTs, values, values.length - lengthDiff, true, fromEncoded, allowUnresolved, false);
        }
    }
    String theFragment = fragment;
    if (theFragment != null) {
        URITemplate fragmentTempl = URITemplate.createExactTemplate(theFragment);
        if (fragmentTempl.getVariables().size() > 0) {
            int lengthDiff = values.length + resolvedTsSize - alreadyResolvedTs.size() - alreadyResolvedTsPathEnc.size() - alreadyResolvedEncTs.size() - pathTemplateVarsSize - queryTemplateVarsSize;
            theFragment = substituteVarargs(fragmentTempl, alreadyResolvedTs, alreadyResolvedTsPathEnc, alreadyResolvedEncTs, values, values.length - lengthDiff, true, fromEncoded, allowUnresolved, false);
        }
    }
    return new UriParts(thePath, theQuery, theFragment);
}
Also used : URITemplate(org.apache.cxf.jaxrs.model.URITemplate)

Example 3 with URITemplate

use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.

the class UriInfoImpl method getMatchedURIs.

public List<String> getMatchedURIs(boolean decode) {
    if (stack != null) {
        List<String> objects = new ArrayList<>();
        List<String> uris = new LinkedList<String>();
        StringBuilder sumPath = new StringBuilder("");
        for (MethodInvocationInfo invocation : stack) {
            List<String> templateObjects = invocation.getTemplateValues();
            OperationResourceInfo ori = invocation.getMethodInfo();
            URITemplate[] paths = { ori.getClassResourceInfo().getURITemplate(), ori.getURITemplate() };
            if (paths[0] != null) {
                int count = paths[0].getVariables().size();
                List<String> rootObjects = new ArrayList<>(count);
                for (int i = 0; i < count && i < templateObjects.size(); i++) {
                    rootObjects.add(templateObjects.get(i));
                }
                uris.add(0, createMatchedPath(paths[0].getValue(), rootObjects, decode));
            }
            if (paths[1] != null && paths[1].getValue().length() > 1) {
                for (URITemplate t : paths) {
                    if (t != null) {
                        sumPath.append("/").append(t.getValue());
                    }
                }
                objects.addAll(templateObjects);
                uris.add(0, createMatchedPath(sumPath.toString(), objects, decode));
            }
        }
        return uris;
    }
    LOG.fine("No resource stack information, returning empty list");
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodInvocationInfo(org.apache.cxf.jaxrs.model.MethodInvocationInfo) LinkedList(java.util.LinkedList)

Example 4 with URITemplate

use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.

the class OAuthUtils method checkRequestURI.

public static boolean checkRequestURI(String servletPath, String uri) {
    boolean wildcard = uri.endsWith("*");
    String theURI = wildcard ? uri.substring(0, uri.length() - 1) : uri;
    try {
        URITemplate template = new URITemplate(theURI);
        MultivaluedMap<String, String> map = new MetadataMap<String, String>();
        if (template.match(servletPath, map)) {
            String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP);
            if (wildcard || StringUtils.isEmpty(finalGroup) || "/".equals(finalGroup)) {
                return true;
            }
        }
    } catch (Exception ex) {
    // ignore
    }
    return false;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OAuthProblemException(net.oauth.OAuthProblemException) IOException(java.io.IOException)

Example 5 with URITemplate

use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.

the class ResourceUtils method createClassResourceInfo.

public static ClassResourceInfo createClassResourceInfo(final Class<?> rClass, final Class<?> sClass, ClassResourceInfo parent, boolean root, boolean enableStatic, Bus bus) {
    ClassResourceInfo cri = new ClassResourceInfo(rClass, sClass, root, enableStatic, bus);
    cri.setParent(parent);
    if (root) {
        URITemplate t = URITemplate.createTemplate(cri.getPath());
        cri.setURITemplate(t);
    }
    evaluateResourceClass(cri, enableStatic);
    return checkMethodDispatcher(cri) ? cri : null;
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate)

Aggregations

URITemplate (org.apache.cxf.jaxrs.model.URITemplate)17 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)8 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)7 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)7 Test (org.junit.Test)5 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)4 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Customer (org.apache.cxf.jaxrs.Customer)2 Message (org.apache.cxf.message.Message)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1