use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class ResourceUtils method createOperationInfo.
// CHECKSTYLE:ON
private static OperationResourceInfo createOperationInfo(Method m, Method annotatedMethod, ClassResourceInfo cri, Path path, String httpMethod) {
OperationResourceInfo ori = new OperationResourceInfo(m, annotatedMethod, cri);
URITemplate t = URITemplate.createTemplate(path);
ori.setURITemplate(t);
ori.setHttpMethod(httpMethod);
return ori;
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class JAXRSUtilsTest method testConversion.
@Test
public void testConversion() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testConversion", new Class[] { PathSegmentImpl.class, SimpleFactory.class }), cri);
ori.setHttpMethod("GET");
ori.setURITemplate(new URITemplate("{id1}/{id2}"));
MultivaluedMap<String, String> values = new MetadataMap<String, String>();
values.putSingle("id1", "1");
values.putSingle("id2", "2");
Message m = createMessage();
List<Object> params = JAXRSUtils.processParameters(ori, values, m);
PathSegment ps = (PathSegment) params.get(0);
assertEquals("1", ps.getPath());
SimpleFactory sf = (SimpleFactory) params.get(1);
assertEquals(2, sf.getId());
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class JAXRSServiceFactoryBeanTest method testNoSubResources.
@Test
public void testNoSubResources() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setEnableStaticResolution(true);
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(1, resources.size());
// Verify root ClassResourceInfo: BookStoreNoSubResource
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));
assertFalse(rootCri.hasSubResources());
MethodDispatcher md = rootCri.getMethodDispatcher();
assertEquals(7, md.getOperationResourceInfos().size());
Set<OperationResourceInfo> ops = md.getOperationResourceInfos();
assertTrue("No operation found", verifyOp(ops, "getBook", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBookStoreInfo", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBooks", "GET", false));
assertTrue("No operation found", verifyOp(ops, "getBookJSON", "GET", false));
assertTrue("No operation found", verifyOp(ops, "addBook", "POST", false));
assertTrue("No operation found", verifyOp(ops, "updateBook", "PUT", false));
assertTrue("No operation found", verifyOp(ops, "deleteBook", "DELETE", false));
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class WadlGenerator method handleOperation.
// CHECKSTYLE:OFF
protected boolean handleOperation(StringBuilder sb, Set<Class<?>> jaxbTypes, ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, OperationResourceInfo ori, Map<Parameter, Object> classParams, OperationResourceInfo nextOp, boolean resourceTagOpened, boolean isJson, int index) {
Annotation[] anns = getMethod(ori).getAnnotations();
// CHECKSTYLE:ON
boolean samePathOperationFollows = singleResourceMultipleMethods && compareOperations(ori, nextOp);
String path = ori.getURITemplate().getValue();
if (!resourceTagOpened && openResource(path)) {
resourceTagOpened = true;
URITemplate template = ori.getClassResourceInfo().getURITemplate();
if (template != null) {
String parentPath = template.getValue();
if (parentPath.endsWith("/") && path.startsWith("/") && path.length() > 1) {
path = path.substring(1);
}
}
startResourceTag(sb, ori, getPath(path));
handleDocs(anns, sb, DocTarget.RESOURCE, false, isJson);
handlePathAndMatrixClassParams(ori, sb, classParams, isJson);
handlePathAndMatrixParams(sb, ori, isJson);
} else if (index == 0) {
handlePathAndMatrixClassParams(ori, sb, classParams, isJson);
handlePathAndMatrixParams(sb, ori, isJson);
}
startMethodTag(sb, ori);
if (!handleDocs(anns, sb, DocTarget.METHOD, true, isJson)) {
handleOperJavaDocs(ori, sb);
}
int numOfParams = getMethod(ori).getParameterTypes().length;
if ((numOfParams > 1 || numOfParams == 1 && !ori.isAsync()) || !classParams.isEmpty()) {
startMethodRequestTag(sb, ori);
handleDocs(anns, sb, DocTarget.REQUEST, false, isJson);
boolean isForm = isFormRequest(ori);
doHandleClassParams(ori, sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER);
doHandleJaxrsBeanParamClassParams(ori, sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER);
for (Parameter p : ori.getParameters()) {
if (isForm && p.getType() == ParameterType.REQUEST_BODY) {
continue;
}
handleParameter(sb, jaxbTypes, qnameResolver, clsMap, ori, p, isJson);
}
if (isForm) {
handleFormRepresentation(sb, jaxbTypes, qnameResolver, clsMap, ori, getFormClass(ori), isJson);
}
endMethodRequestTag(sb, ori);
}
startMethodResponseTag(sb, ori);
Class<?> returnType = getMethod(ori).getReturnType();
boolean isVoid = void.class == returnType && !ori.isAsync();
ResponseStatus responseStatus = getMethod(ori).getAnnotation(ResponseStatus.class);
if (responseStatus != null) {
setResponseStatus(sb, responseStatus.value());
} else if (isVoid) {
boolean oneway = getMethod(ori).getAnnotation(Oneway.class) != null;
setResponseStatus(sb, oneway ? Response.Status.ACCEPTED : Response.Status.NO_CONTENT);
}
sb.append(">");
handleDocs(anns, sb, DocTarget.RESPONSE, false, isJson);
if (!isVoid) {
handleRepresentation(sb, jaxbTypes, qnameResolver, clsMap, ori, returnType, isJson, false);
}
endMethodResponseTag(sb, ori);
endMethodTag(sb, ori);
if (resourceTagOpened && !samePathOperationFollows) {
endResourceTag(sb, ori);
resourceTagOpened = false;
}
return resourceTagOpened;
}
use of org.apache.cxf.jaxrs.model.URITemplate in project cxf by apache.
the class Validator method checkMethodsForInvalidURITemplates.
private static void checkMethodsForInvalidURITemplates(Class<?> userType, Method[] methods) throws RestClientDefinitionException {
Path classPathAnno = userType.getAnnotation(Path.class);
final Set<String> classLevelVariables = new HashSet<>();
URITemplate classTemplate = null;
if (classPathAnno != null) {
classTemplate = new URITemplate(classPathAnno.value());
classLevelVariables.addAll(classTemplate.getVariables());
}
URITemplate template;
for (Method method : methods) {
Path methodPathAnno = method.getAnnotation(Path.class);
if (methodPathAnno != null) {
template = classPathAnno == null ? new URITemplate(methodPathAnno.value()) : new URITemplate(classPathAnno.value() + "/" + methodPathAnno.value());
} else {
template = classTemplate;
}
if (template == null) {
continue;
}
Set<String> allVariables = new HashSet<>(template.getVariables());
if (!allVariables.isEmpty()) {
Map<String, String> paramMap = new HashMap<>();
for (Parameter p : method.getParameters()) {
PathParam pathParam = p.getAnnotation(PathParam.class);
if (pathParam != null) {
paramMap.put(pathParam.value(), "x");
}
}
try {
template.substitute(paramMap, Collections.<String>emptySet(), false);
} catch (IllegalArgumentException ex) {
throwException("VALIDATION_UNRESOLVED_PATH_PARAMS", userType, method);
}
} else {
List<String> foundParams = new ArrayList<>();
for (Parameter p : method.getParameters()) {
PathParam pathParam = p.getAnnotation(PathParam.class);
if (pathParam != null) {
foundParams.add(pathParam.value());
}
}
if (!foundParams.isEmpty()) {
throwException("VALIDATION_EXTRA_PATH_PARAMS", userType, method);
}
}
}
}
Aggregations