Search in sources :

Example 6 with URITemplate

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;
}
Also used : URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 7 with URITemplate

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());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) SimpleFactory(org.apache.cxf.jaxrs.SimpleFactory) PathSegment(javax.ws.rs.core.PathSegment) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) PathSegmentImpl(org.apache.cxf.jaxrs.impl.PathSegmentImpl) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 8 with URITemplate

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));
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 9 with URITemplate

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;
}
Also used : ResponseStatus(org.apache.cxf.jaxrs.ext.ResponseStatus) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) Parameter(org.apache.cxf.jaxrs.model.Parameter) Annotation(java.lang.annotation.Annotation) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 10 with URITemplate

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);
            }
        }
    }
}
Also used : Path(javax.ws.rs.Path) HashMap(java.util.HashMap) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) ArrayList(java.util.ArrayList) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) Parameter(java.lang.reflect.Parameter) PathParam(javax.ws.rs.PathParam) HashSet(java.util.HashSet)

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