Search in sources :

Example 31 with Parameter

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

the class InjectionUtils method getParametersFromBeanClass.

public static Map<Parameter, Class<?>> getParametersFromBeanClass(Class<?> beanClass, ParameterType type, boolean checkIgnorable) {
    Map<Parameter, Class<?>> params = new LinkedHashMap<Parameter, Class<?>>();
    for (Method m : beanClass.getMethods()) {
        String methodName = m.getName();
        boolean startsFromGet = methodName.startsWith("get");
        if ((startsFromGet || isBooleanType(m.getReturnType()) && methodName.startsWith("is")) && m.getParameterTypes().length == 0) {
            int minLen = startsFromGet ? 3 : 2;
            if (methodName.length() <= minLen) {
                continue;
            }
            String propertyName = StringUtils.uncapitalize(methodName.substring(minLen));
            if (m.getReturnType() == Class.class || checkIgnorable && canPropertyBeIgnored(m, propertyName)) {
                continue;
            }
            params.put(new Parameter(type, propertyName), m.getReturnType());
        }
    }
    return params;
}
Also used : Parameter(org.apache.cxf.jaxrs.model.Parameter) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with Parameter

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

the class JAXRSUtils method processParameters.

// Message contains following information: PATH, HTTP_REQUEST_METHOD, CONTENT_TYPE, InputStream.
public static List<Object> processParameters(OperationResourceInfo ori, MultivaluedMap<String, String> values, Message message) throws IOException, WebApplicationException {
    Class<?>[] parameterTypes = ori.getInParameterTypes();
    List<Parameter> paramsInfo = ori.getParameters();
    boolean preferModelParams = paramsInfo.size() > parameterTypes.length && !PropertyUtils.isTrue(message.getContextualProperty("org.apache.cxf.preferMethodParameters"));
    int parameterTypesLengh = preferModelParams ? paramsInfo.size() : parameterTypes.length;
    Type[] genericParameterTypes = ori.getInGenericParameterTypes();
    Annotation[][] anns = ori.getInParameterAnnotations();
    List<Object> params = new ArrayList<>(parameterTypesLengh);
    for (int i = 0; i < parameterTypesLengh; i++) {
        Class<?> param = null;
        Type genericParam = null;
        Annotation[] paramAnns = null;
        if (!preferModelParams) {
            param = parameterTypes[i];
            genericParam = InjectionUtils.processGenericTypeIfNeeded(ori.getClassResourceInfo().getServiceClass(), param, genericParameterTypes[i]);
            param = InjectionUtils.updateParamClassToTypeIfNeeded(param, genericParam);
            paramAnns = anns == null ? EMPTY_ANNOTATIONS : anns[i];
        } else {
            param = paramsInfo.get(i).getJavaType();
            genericParam = param;
            paramAnns = EMPTY_ANNOTATIONS;
        }
        Object paramValue = processParameter(param, genericParam, paramAnns, paramsInfo.get(i), values, message, ori);
        params.add(paramValue);
    }
    return params;
}
Also used : ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) MediaType(javax.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Type(java.lang.reflect.Type) Parameter(org.apache.cxf.jaxrs.model.Parameter)

Example 33 with Parameter

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

the class JAXRSUtils method injectParameters.

@SuppressWarnings("unchecked")
public static void injectParameters(OperationResourceInfo ori, BeanResourceInfo bri, Object requestObject, Message message) {
    if (bri.isSingleton() && (!bri.getParameterMethods().isEmpty() || !bri.getParameterFields().isEmpty())) {
        LOG.fine("Injecting request parameters into singleton resource is not thread-safe");
    }
    // Param methods
    MultivaluedMap<String, String> values = (MultivaluedMap<String, String>) message.get(URITemplate.TEMPLATE_PARAMETERS);
    for (Method m : bri.getParameterMethods()) {
        Parameter p = ResourceUtils.getParameter(0, m.getAnnotations(), m.getParameterTypes()[0]);
        Object o;
        if (p.getType() == ParameterType.BEAN) {
            o = createBeanParamValue(message, m.getParameterTypes()[0], ori);
        } else {
            o = createHttpParameterValue(p, m.getParameterTypes()[0], m.getGenericParameterTypes()[0], m.getParameterAnnotations()[0], message, values, ori);
        }
        InjectionUtils.injectThroughMethod(requestObject, m, o, message);
    }
    // Param fields
    for (Field f : bri.getParameterFields()) {
        Parameter p = ResourceUtils.getParameter(0, f.getAnnotations(), f.getType());
        Object o = null;
        if (p.getType() == ParameterType.BEAN) {
            o = createBeanParamValue(message, f.getType(), ori);
        } else {
            o = createHttpParameterValue(p, f.getType(), f.getGenericType(), f.getAnnotations(), message, values, ori);
        }
        InjectionUtils.injectFieldValue(f, requestObject, o);
    }
}
Also used : Field(java.lang.reflect.Field) Parameter(org.apache.cxf.jaxrs.model.Parameter) HttpMethod(javax.ws.rs.HttpMethod) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) Method(java.lang.reflect.Method) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 34 with Parameter

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

the class ResourceUtils method getParameter.

// CHECKSTYLE:OFF
public static Parameter getParameter(int index, Annotation[] anns, Class<?> type) {
    Context ctx = AnnotationUtils.getAnnotation(anns, Context.class);
    if (ctx != null) {
        return new Parameter(ParameterType.CONTEXT, index, null);
    }
    boolean isEncoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
    BeanParam bp = AnnotationUtils.getAnnotation(anns, BeanParam.class);
    if (bp != null) {
        return new Parameter(ParameterType.BEAN, index, null, isEncoded, null);
    }
    String dValue = AnnotationUtils.getDefaultParameterValue(anns);
    PathParam a = AnnotationUtils.getAnnotation(anns, PathParam.class);
    if (a != null) {
        return new Parameter(ParameterType.PATH, index, a.value(), isEncoded, dValue);
    }
    QueryParam q = AnnotationUtils.getAnnotation(anns, QueryParam.class);
    if (q != null) {
        return new Parameter(ParameterType.QUERY, index, q.value(), isEncoded, dValue);
    }
    MatrixParam m = AnnotationUtils.getAnnotation(anns, MatrixParam.class);
    if (m != null) {
        return new Parameter(ParameterType.MATRIX, index, m.value(), isEncoded, dValue);
    }
    FormParam f = AnnotationUtils.getAnnotation(anns, FormParam.class);
    if (f != null) {
        return new Parameter(ParameterType.FORM, index, f.value(), isEncoded, dValue);
    }
    HeaderParam h = AnnotationUtils.getAnnotation(anns, HeaderParam.class);
    if (h != null) {
        return new Parameter(ParameterType.HEADER, index, h.value(), isEncoded, dValue);
    }
    CookieParam c = AnnotationUtils.getAnnotation(anns, CookieParam.class);
    if (c != null) {
        return new Parameter(ParameterType.COOKIE, index, c.value(), isEncoded, dValue);
    }
    return new Parameter(ParameterType.REQUEST_BODY, index, null);
}
Also used : Context(javax.ws.rs.core.Context) JAXBContext(javax.xml.bind.JAXBContext) CookieParam(javax.ws.rs.CookieParam) MatrixParam(javax.ws.rs.MatrixParam) HeaderParam(javax.ws.rs.HeaderParam) Encoded(javax.ws.rs.Encoded) QueryParam(javax.ws.rs.QueryParam) Parameter(org.apache.cxf.jaxrs.model.Parameter) PathParam(javax.ws.rs.PathParam) FormParam(javax.ws.rs.FormParam) BeanParam(javax.ws.rs.BeanParam)

Example 35 with Parameter

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

the class ResourceUtils method getOperationFromElement.

private static UserOperation getOperationFromElement(Element e) {
    UserOperation op = new UserOperation();
    op.setName(e.getAttribute("name"));
    op.setVerb(e.getAttribute("verb"));
    op.setPath(e.getAttribute("path"));
    op.setOneway(Boolean.parseBoolean(e.getAttribute("oneway")));
    op.setConsumes(e.getAttribute("consumes"));
    op.setProduces(e.getAttribute("produces"));
    List<Element> paramEls = DOMUtils.findAllElementsByTagNameNS(e, "http://cxf.apache.org/jaxrs", "param");
    List<Parameter> params = new ArrayList<>(paramEls.size());
    for (int i = 0; i < paramEls.size(); i++) {
        Element paramEl = paramEls.get(i);
        Parameter p = new Parameter(paramEl.getAttribute("type"), i, paramEl.getAttribute("name"));
        p.setEncoded(Boolean.valueOf(paramEl.getAttribute("encoded")));
        p.setDefaultValue(paramEl.getAttribute("defaultValue"));
        String pClass = paramEl.getAttribute("class");
        if (!StringUtils.isEmpty(pClass)) {
            try {
                p.setJavaType(ClassLoaderUtils.loadClass(pClass, ResourceUtils.class));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        params.add(p);
    }
    op.setParameters(params);
    return op;
}
Also used : UserOperation(org.apache.cxf.jaxrs.model.UserOperation) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter) JAXBException(javax.xml.bind.JAXBException)

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