use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class WebClient method path.
/**
* Updates the current URI path with path segment which may contain template variables
* @param path new relative path segment
* @param values template variable values
* @return updated WebClient
*/
public WebClient path(String path, Object... values) {
URI u = new UriBuilderImpl().uri(URI.create("http://tempuri")).path(path).buildFromEncoded(values);
getState().setTemplates(getTemplateParametersMap(new URITemplate(path), Arrays.asList(values)));
return path(u.getRawPath());
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class JAXRSUtilsTest method testSelectResourceMethod.
@Test
public void testSelectResourceMethod() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class);
OperationResourceInfo ori1 = new OperationResourceInfo(Customer.class.getMethod("getItAsXML", new Class[] {}), cri);
ori1.setHttpMethod("GET");
ori1.setURITemplate(new URITemplate("/"));
OperationResourceInfo ori2 = new OperationResourceInfo(Customer.class.getMethod("getItPlain", new Class[] {}), cri);
ori2.setHttpMethod("GET");
ori2.setURITemplate(new URITemplate("/"));
MethodDispatcher md = new MethodDispatcher();
md.bind(ori1, Customer.class.getMethod("getItAsXML", new Class[] {}));
md.bind(ori2, Customer.class.getMethod("getItPlain", new Class[] {}));
cri.setMethodDispatcher(md);
OperationResourceInfo ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", getTypes("text/plain"));
assertSame(ori, ori2);
ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", getTypes("text/xml"));
assertSame(ori, ori1);
ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", sortMediaTypes(getTypes("*/*;q=0.1,text/plain,text/xml;q=0.8")));
assertSame(ori, ori2);
ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", sortMediaTypes(getTypes("*;q=0.1,text/plain,text/xml;q=0.9,x/y")));
assertSame(ori, ori2);
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class JAXRSUtils method findTargetMethod.
// CHECKSTYLE:OFF
public static OperationResourceInfo findTargetMethod(Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources, Message message, String httpMethod, MultivaluedMap<String, String> matchedValues, String requestContentType, List<MediaType> acceptContentTypes, boolean throwException, boolean recordMatchedUri) {
// CHECKSTYLE:ON
final boolean getMethod = HttpMethod.GET.equals(httpMethod);
MediaType requestType;
try {
requestType = toMediaType(requestContentType);
} catch (IllegalArgumentException ex) {
throw ExceptionUtils.toNotSupportedException(ex, null);
}
SortedMap<OperationResourceInfo, MultivaluedMap<String, String>> candidateList = new TreeMap<OperationResourceInfo, MultivaluedMap<String, String>>(new OperationResourceInfoComparator(message, httpMethod, getMethod, requestType, acceptContentTypes));
int pathMatched = 0;
int methodMatched = 0;
int consumeMatched = 0;
List<OperationResourceInfo> finalPathSubresources = null;
for (Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> rEntry : matchedResources.entrySet()) {
ClassResourceInfo resource = rEntry.getKey();
MultivaluedMap<String, String> values = rEntry.getValue();
String path = getCurrentPath(values);
LOG.fine(() -> new org.apache.cxf.common.i18n.Message("START_OPER_MATCH", BUNDLE, resource.getServiceClass().getName()).toString());
for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) {
boolean added = false;
URITemplate uriTemplate = ori.getURITemplate();
MultivaluedMap<String, String> map = new MetadataMap<String, String>(values);
if (uriTemplate != null && uriTemplate.match(path, map)) {
String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP);
boolean finalPath = StringUtils.isEmpty(finalGroup) || PATH_SEGMENT_SEP.equals(finalGroup);
if (ori.isSubResourceLocator()) {
candidateList.put(ori, map);
if (finalPath) {
if (finalPathSubresources == null) {
finalPathSubresources = new LinkedList<OperationResourceInfo>();
}
finalPathSubresources.add(ori);
}
added = true;
} else if (finalPath) {
pathMatched++;
if (matchHttpMethod(ori.getHttpMethod(), httpMethod)) {
methodMatched++;
// CHECKSTYLE:OFF
if (getMethod || matchConsumeTypes(requestType, ori)) {
consumeMatched++;
for (MediaType acceptType : acceptContentTypes) {
if (matchProduceTypes(acceptType, ori)) {
candidateList.put(ori, map);
added = true;
break;
}
}
}
// CHECKSTYLE:ON
}
}
}
LOG.fine(matchMessageLogSupplier(ori, path, httpMethod, requestType, acceptContentTypes, added));
}
}
if (finalPathSubresources != null && pathMatched > 0 && !MessageUtils.getContextualBoolean(message, KEEP_SUBRESOURCE_CANDIDATES, false)) {
for (OperationResourceInfo key : finalPathSubresources) {
candidateList.remove(key);
}
}
if (!candidateList.isEmpty()) {
Map.Entry<OperationResourceInfo, MultivaluedMap<String, String>> firstEntry = candidateList.entrySet().iterator().next();
matchedValues.clear();
matchedValues.putAll(firstEntry.getValue());
OperationResourceInfo ori = firstEntry.getKey();
if (headMethodPossible(ori.getHttpMethod(), httpMethod)) {
LOG.info(new org.apache.cxf.common.i18n.Message("GET_INSTEAD_OF_HEAD", BUNDLE, ori.getClassResourceInfo().getServiceClass().getName(), ori.getMethodToInvoke().getName()).toString());
}
LOG.fine(() -> new org.apache.cxf.common.i18n.Message("OPER_SELECTED", BUNDLE, ori.getMethodToInvoke().getName(), ori.getClassResourceInfo().getServiceClass().getName()).toString());
if (!ori.isSubResourceLocator()) {
MediaType responseMediaType = intersectSortMediaTypes(acceptContentTypes, ori.getProduceTypes(), false).get(0);
message.getExchange().put(Message.CONTENT_TYPE, mediaTypeToString(responseMediaType, MEDIA_TYPE_Q_PARAM, MEDIA_TYPE_QS_PARAM));
}
if (recordMatchedUri) {
pushOntoStack(ori, matchedValues, message);
}
return ori;
}
if (!throwException) {
return null;
}
int status;
// priority : path, method, consumes, produces;
if (pathMatched == 0) {
status = 404;
} else if (methodMatched == 0) {
status = 405;
} else if (consumeMatched == 0) {
status = 415;
} else {
// Not a single Produces match
status = 406;
}
Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> firstCri = matchedResources.entrySet().iterator().next();
String name = firstCri.getKey().isRoot() ? "NO_OP_EXC" : "NO_SUBRESOURCE_METHOD_FOUND";
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message(name, BUNDLE, message.get(Message.REQUEST_URI), getCurrentPath(firstCri.getValue()), httpMethod, mediaTypeToString(requestType), convertTypesToString(acceptContentTypes));
if (!"OPTIONS".equalsIgnoreCase(httpMethod)) {
Level logLevel = getExceptionLogLevel(message, ClientErrorException.class);
LOG.log(logLevel == null ? Level.FINE : logLevel, () -> errorMsg.toString());
}
Response response = createResponse(getRootResources(message), message, errorMsg.toString(), status, methodMatched == 0);
throw ExceptionUtils.toHttpException(null, response);
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class ResourceUtils method createServiceClassResourceInfo.
public static ClassResourceInfo createServiceClassResourceInfo(Map<String, UserResource> resources, UserResource model, Class<?> sClass, boolean isRoot, boolean enableStatic, Bus bus) {
if (model == null) {
throw new RuntimeException("Resource class " + sClass.getName() + " has no model info");
}
ClassResourceInfo cri = new ClassResourceInfo(sClass, sClass, isRoot, enableStatic, true, model.getConsumes(), model.getProduces(), bus);
URITemplate t = URITemplate.createTemplate(model.getPath());
cri.setURITemplate(t);
MethodDispatcher md = new MethodDispatcher();
Map<String, UserOperation> ops = model.getOperationsAsMap();
Method defaultMethod = null;
Map<String, Method> methodNames = new HashMap<>();
for (Method m : cri.getServiceClass().getMethods()) {
if (m.getAnnotation(DefaultMethod.class) != null) {
// if needed we can also support multiple default methods
defaultMethod = m;
}
methodNames.put(m.getName(), m);
}
for (Map.Entry<String, UserOperation> entry : ops.entrySet()) {
UserOperation op = entry.getValue();
Method actualMethod = methodNames.get(op.getName());
if (actualMethod == null) {
actualMethod = defaultMethod;
}
if (actualMethod == null) {
continue;
}
OperationResourceInfo ori = new OperationResourceInfo(actualMethod, cri, URITemplate.createTemplate(op.getPath()), op.getVerb(), op.getConsumes(), op.getProduces(), op.getParameters(), op.isOneway());
String rClassName = actualMethod.getReturnType().getName();
if (op.getVerb() == null) {
if (resources.containsKey(rClassName)) {
ClassResourceInfo subCri = rClassName.equals(model.getName()) ? cri : createServiceClassResourceInfo(resources, resources.get(rClassName), actualMethod.getReturnType(), false, enableStatic, bus);
if (subCri != null) {
cri.addSubClassResourceInfo(subCri);
md.bind(ori, actualMethod);
}
}
} else {
md.bind(ori, actualMethod);
}
}
cri.setMethodDispatcher(md);
return checkMethodDispatcher(cri) ? cri : null;
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class JAXRSServiceFactoryBeanTest method testSubResources.
@Test
public void testSubResources() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setEnableStaticResolution(true);
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(1, resources.size());
// Verify root ClassResourceInfo: BookStore
ClassResourceInfo rootCri = resources.get(0);
assertNotNull(rootCri.getURITemplate());
URITemplate template = rootCri.getURITemplate();
MultivaluedMap<String, String> values = new MetadataMap<String, String>();
assertTrue(template.match("/bookstore/books/123", values));
assertTrue(rootCri.hasSubResources());
MethodDispatcher md = rootCri.getMethodDispatcher();
assertEquals(7, md.getOperationResourceInfos().size());
for (OperationResourceInfo ori : md.getOperationResourceInfos()) {
if ("getDescription".equals(ori.getMethodToInvoke().getName())) {
assertEquals("GET", ori.getHttpMethod());
assertEquals("/path", ori.getURITemplate().getValue());
assertEquals("text/bar", ori.getProduceTypes().get(0).toString());
assertEquals("text/foo", ori.getConsumeTypes().get(0).toString());
assertFalse(ori.isSubResourceLocator());
} else if ("getAuthor".equals(ori.getMethodToInvoke().getName())) {
assertEquals("GET", ori.getHttpMethod());
assertEquals("/path2", ori.getURITemplate().getValue());
assertEquals("text/bar2", ori.getProduceTypes().get(0).toString());
assertEquals("text/foo2", ori.getConsumeTypes().get(0).toString());
assertFalse(ori.isSubResourceLocator());
} else if ("getBook".equals(ori.getMethodToInvoke().getName())) {
assertNull(ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertTrue(ori.isSubResourceLocator());
} else if ("getNewBook".equals(ori.getMethodToInvoke().getName())) {
assertNull(ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertTrue(ori.isSubResourceLocator());
} else if ("addBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("POST", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else if ("updateBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("PUT", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else if ("deleteBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("DELETE", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else {
fail("unexpected OperationResourceInfo" + ori.getMethodToInvoke().getName());
}
}
// Verify sub-resource ClassResourceInfo: Book
assertEquals(1, rootCri.getSubResources().size());
ClassResourceInfo subCri = rootCri.getSubResources().iterator().next();
assertNull(subCri.getURITemplate());
assertEquals(org.apache.cxf.jaxrs.resources.Book.class, subCri.getResourceClass());
MethodDispatcher subMd = subCri.getMethodDispatcher();
assertEquals(2, subMd.getOperationResourceInfos().size());
// getChapter method
OperationResourceInfo subOri = subMd.getOperationResourceInfos().iterator().next();
assertEquals("GET", subOri.getHttpMethod());
assertNotNull(subOri.getURITemplate());
// getState method
OperationResourceInfo subOri2 = subMd.getOperationResourceInfos().iterator().next();
assertEquals("GET", subOri2.getHttpMethod());
assertNotNull(subOri2.getURITemplate());
}
Aggregations