Search in sources :

Example 11 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class WadlGenerator method getClassParameters.

private Map<Parameter, Object> getClassParameters(ClassResourceInfo cri) {
    Map<Parameter, Object> classParams = new LinkedHashMap<>();
    List<Method> paramMethods = cri.getParameterMethods();
    for (Method m : paramMethods) {
        classParams.put(ResourceUtils.getParameter(0, m.getAnnotations(), m.getParameterTypes()[0]), m);
    }
    List<Field> fieldParams = cri.getParameterFields();
    for (Field f : fieldParams) {
        classParams.put(ResourceUtils.getParameter(0, f.getAnnotations(), f.getType()), f);
    }
    return classParams;
}
Also used : Field(java.lang.reflect.Field) Parameter(org.apache.cxf.jaxrs.model.Parameter) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class WadlGenerator method handleResource.

protected void handleResource(StringBuilder sb, Set<Class<?>> jaxbTypes, ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, ClassResourceInfo cri, Set<ClassResourceInfo> visitedResources, boolean isJson) {
    visitedResources.add(cri);
    Map<Parameter, Object> classParams = getClassParameters(cri);
    List<OperationResourceInfo> sortedOps = sortOperationsByPath(cri.getMethodDispatcher().getOperationResourceInfos());
    boolean resourceTagOpened = false;
    for (int i = 0; i < sortedOps.size(); i++) {
        OperationResourceInfo ori = sortedOps.get(i);
        if (i > 0 && ignoreOverloadedMethods && ori.getMethodToInvoke().getName().equals(sortedOps.get(i - 1).getMethodToInvoke().getName())) {
            continue;
        }
        if (ori.getHttpMethod() == null) {
            Class<?> cls = getMethod(ori).getReturnType();
            ClassResourceInfo subcri = cri.findResource(cls, cls);
            if (subcri != null && !visitedResources.contains(subcri)) {
                startResourceTag(sb, subcri, ori.getURITemplate().getValue());
                handleDocs(subcri.getServiceClass().getAnnotations(), sb, DocTarget.RESOURCE, true, isJson);
                handlePathAndMatrixParams(sb, ori, isJson);
                handleResource(sb, jaxbTypes, qnameResolver, clsMap, subcri, visitedResources, isJson);
                sb.append("</resource>");
            } else {
                handleDynamicSubresource(sb, jaxbTypes, qnameResolver, clsMap, ori, subcri, isJson);
            }
            continue;
        }
        OperationResourceInfo nextOp = i + 1 < sortedOps.size() ? sortedOps.get(i + 1) : null;
        resourceTagOpened = handleOperation(sb, jaxbTypes, qnameResolver, clsMap, ori, classParams, nextOp, resourceTagOpened, isJson, i);
    }
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 13 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class WadlGenerator method handleRepresentation.

// TODO: Collapse multiple parameters into a holder
// CHECKSTYLE:OFF
protected void handleRepresentation(StringBuilder sb, Set<Class<?>> jaxbTypes, ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, OperationResourceInfo ori, Class<?> type, boolean isJson, boolean inbound) {
    // CHECKSTYLE:ON
    List<MediaType> types = inbound ? ori.getConsumeTypes() : ori.getProduceTypes();
    if (MultivaluedMap.class.isAssignableFrom(type)) {
        types = Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    } else if (isWildcard(types)) {
        types = Collections.singletonList(defaultRepMediaType);
    }
    Method opMethod = getMethod(ori);
    boolean isPrimitive = InjectionUtils.isPrimitive(type) && !ori.isAsync();
    for (MediaType mt : types) {
        sb.append("<representation");
        sb.append(" mediaType=\"").append(JAXRSUtils.mediaTypeToString(mt)).append("\"");
        if (isJson && !mt.getSubtype().contains("json")) {
            sb.append("/>");
            continue;
        }
        boolean allowDefault = true;
        String docCategory;
        Annotation[] anns;
        int inParamIndex = -1;
        Type genericType;
        if (inbound) {
            inParamIndex = getRequestBodyParam(ori).getIndex();
            anns = opMethod.getParameterAnnotations()[inParamIndex];
            if (!isDocAvailable(anns)) {
                anns = opMethod.getAnnotations();
            }
            docCategory = DocTarget.PARAM;
            genericType = opMethod.getGenericParameterTypes()[inParamIndex];
        } else {
            anns = opMethod.getAnnotations();
            docCategory = DocTarget.RETURN;
            allowDefault = false;
            genericType = opMethod.getGenericReturnType();
        }
        if (isPrimitive) {
            sb.append(">");
            Parameter p = inbound ? getRequestBodyParam(ori) : new Parameter(ParameterType.REQUEST_BODY, 0, "result");
            doWriteParam(ori, sb, p, type, type, p.getName() == null ? "request" : p.getName(), anns, isJson);
            sb.append("</representation>");
        } else {
            boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
            Class<?> theActualType;
            if (isCollection) {
                theActualType = InjectionUtils.getActualType(genericType);
            } else {
                theActualType = ResourceUtils.getActualJaxbType(type, opMethod, inbound);
            }
            if (theActualType == Object.class && !(genericType instanceof Class) || genericType instanceof TypeVariable) {
                Type theType = InjectionUtils.processGenericTypeIfNeeded(ori.getClassResourceInfo().getServiceClass(), Object.class, genericType);
                theActualType = InjectionUtils.getActualType(theType);
            }
            if (isJson) {
                sb.append(" element=\"").append(theActualType.getSimpleName()).append("\"");
            } else if (qnameResolver != null && (linkAnyMediaTypeToXmlSchema || mt.getSubtype().contains("xml")) && jaxbTypes.contains(theActualType)) {
                generateQName(sb, qnameResolver, clsMap, theActualType, isCollection, getBodyAnnotations(ori, inbound));
            }
            addDocsAndCloseElement(ori, inParamIndex, sb, anns, "representation", docCategory, allowDefault, isJson);
        }
    }
}
Also used : Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Endpoint(org.apache.cxf.endpoint.Endpoint) MediaType(javax.ws.rs.core.MediaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) MediaType(javax.ws.rs.core.MediaType) Parameter(org.apache.cxf.jaxrs.model.Parameter)

Example 14 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter 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 15 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class WadlGenerator method doWriteJaxrsBeanParam.

private void doWriteJaxrsBeanParam(StringBuilder sb, OperationResourceInfo ori, Class<?> beanType, boolean isJson, ParameterType... parameterTypes) {
    for (Method m : beanType.getMethods()) {
        if (m.getName().startsWith("set")) {
            String propertyName = StringUtils.uncapitalize(m.getName().substring(3));
            Field f = InjectionUtils.getDeclaredField(beanType, propertyName);
            for (ParameterType parameterType : parameterTypes) {
                Class<? extends Annotation> annClass = getAnnotationFromParamType(parameterType);
                Annotation annotation = m.getAnnotation(annClass);
                if (annotation != null) {
                    Parameter pm = new Parameter(parameterType, propertyName);
                    pm.setEncoded(m.getAnnotation(Encoded.class) != null);
                    DefaultValue dv = m.getAnnotation(DefaultValue.class);
                    if (dv != null) {
                        pm.setDefaultValue(dv.value());
                    }
                    doWriteParam(ori, sb, pm, m.getParameterTypes()[0], m.getGenericParameterTypes()[0], propertyName, new Annotation[] {}, isJson);
                } else if (f != null) {
                    annotation = f.getAnnotation(annClass);
                    if (annotation != null) {
                        Parameter pm = new Parameter(parameterType, propertyName);
                        pm.setEncoded(f.getAnnotation(Encoded.class) != null);
                        DefaultValue dv = f.getAnnotation(DefaultValue.class);
                        if (dv != null) {
                            pm.setDefaultValue(dv.value());
                        }
                        doWriteParam(ori, sb, pm, f.getType(), f.getGenericType(), propertyName, new Annotation[] {}, isJson);
                    }
                }
            }
            if (m.getAnnotation(BeanParam.class) != null) {
                doWriteJaxrsBeanParam(sb, ori, m.getParameterTypes()[0], isJson, parameterTypes);
            } else if (f != null && f.getAnnotation(BeanParam.class) != null) {
                doWriteJaxrsBeanParam(sb, ori, f.getType(), isJson, parameterTypes);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) DefaultValue(javax.ws.rs.DefaultValue) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Encoded(javax.ws.rs.Encoded) Parameter(org.apache.cxf.jaxrs.model.Parameter) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) BeanParam(javax.ws.rs.BeanParam)

Aggregations

Parameter (org.apache.cxf.jaxrs.model.Parameter)38 UserOperation (org.apache.cxf.jaxrs.model.UserOperation)13 Method (java.lang.reflect.Method)12 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)10 Endpoint (org.apache.cxf.endpoint.Endpoint)9 UserResource (org.apache.cxf.jaxrs.model.UserResource)9 LinkedHashMap (java.util.LinkedHashMap)8 HashMap (java.util.HashMap)7 UserApplication (org.apache.cxf.jaxrs.model.UserApplication)7 MediaType (javax.ws.rs.core.MediaType)6 Annotation (java.lang.annotation.Annotation)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)5 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)5 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4