Search in sources :

Example 1 with DefaultMethod

use of org.apache.cxf.jaxrs.ext.DefaultMethod 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;
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) Method(java.lang.reflect.Method) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 DefaultMethod (org.apache.cxf.jaxrs.ext.DefaultMethod)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)1 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)1 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)1 UserOperation (org.apache.cxf.jaxrs.model.UserOperation)1