Search in sources :

Example 41 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());
        final Object o;
        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 42 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<>();
    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 43 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) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter) IOException(java.io.IOException)

Example 44 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) 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 45 with Parameter

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

the class JAXRSParameterNameProvider method getParameterNames.

@Override
public List<String> getParameterNames(final Method method) {
    final List<Parameter> parameters = ResourceUtils.getParameters(method);
    final List<String> parameterNames = new ArrayList<>();
    for (int i = 0; i < parameters.size(); ++i) {
        final StringBuilder sb = new StringBuilder();
        sb.append("arg").append(i);
        sb.append('(');
        Parameter parameter = parameters.get(i);
        if (parameter.getName() != null) {
            sb.append(parameter.getType().toString());
            sb.append("(\"").append(parameter.getName()).append("\")");
            sb.append(' ');
        }
        sb.append(method.getParameterTypes()[i].getSimpleName());
        sb.append(')');
        parameterNames.add(sb.toString());
    }
    return parameterNames;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter)

Aggregations

Parameter (org.apache.cxf.jaxrs.model.Parameter)54 Method (java.lang.reflect.Method)16 UserOperation (org.apache.cxf.jaxrs.model.UserOperation)15 Endpoint (org.apache.cxf.endpoint.Endpoint)13 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)13 ArrayList (java.util.ArrayList)11 LinkedHashMap (java.util.LinkedHashMap)10 UserResource (org.apache.cxf.jaxrs.model.UserResource)10 HashMap (java.util.HashMap)8 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)8 UserApplication (org.apache.cxf.jaxrs.model.UserApplication)8 MediaType (javax.ws.rs.core.MediaType)7 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)7 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)7 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)7 Type (java.lang.reflect.Type)6 Annotation (java.lang.annotation.Annotation)5 Map (java.util.Map)5 Context (javax.ws.rs.core.Context)5 Field (java.lang.reflect.Field)4