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);
}
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);
}
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();
}
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;
}
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;
}
Aggregations