Search in sources :

Example 1 with Parameter

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

the class ResourceUtils method createConstructorArguments.

public static Object[] createConstructorArguments(Constructor<?> c, Message m, boolean perRequest, Map<Class<?>, Object> contextValues) {
    if (m == null) {
        m = new MessageImpl();
    }
    Class<?>[] params = c.getParameterTypes();
    Annotation[][] anns = c.getParameterAnnotations();
    Type[] genericTypes = c.getGenericParameterTypes();
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> templateValues = (MultivaluedMap<String, String>) m.get(URITemplate.TEMPLATE_PARAMETERS);
    Object[] values = new Object[params.length];
    for (int i = 0; i < params.length; i++) {
        if (AnnotationUtils.getAnnotation(anns[i], Context.class) != null) {
            Object contextValue = contextValues != null ? contextValues.get(params[i]) : null;
            if (contextValue == null) {
                if (perRequest || InjectionUtils.VALUE_CONTEXTS.contains(params[i].getName())) {
                    values[i] = JAXRSUtils.createContextValue(m, genericTypes[i], params[i]);
                } else {
                    values[i] = InjectionUtils.createThreadLocalProxy(params[i]);
                }
            } else {
                values[i] = contextValue;
            }
        } else {
            // this branch won't execute for singletons given that the found constructor
            // is guaranteed to have only Context parameters, if any, for singletons
            Parameter p = ResourceUtils.getParameter(i, anns[i], params[i]);
            values[i] = JAXRSUtils.createHttpParameterValue(p, params[i], genericTypes[i], anns[i], m, templateValues, null);
        }
    }
    return values;
}
Also used : Context(javax.ws.rs.core.Context) JAXBContext(javax.xml.bind.JAXBContext) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Parameter(org.apache.cxf.jaxrs.model.Parameter) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 2 with Parameter

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

the class ClientProxyImpl method handleForm.

private MultivaluedMap<String, String> handleForm(Method m, Object[] params, MultivaluedMap<ParameterType, Parameter> map, List<Parameter> beanParams) {
    MultivaluedMap<String, String> form = new MetadataMap<String, String>();
    List<Parameter> fm = getParameters(map, ParameterType.FORM);
    for (Parameter p : fm) {
        addFormValue(form, p.getName(), params[p.getIndex()], getParamAnnotations(m, p));
    }
    for (Parameter p : beanParams) {
        Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], FormParam.class);
        for (Map.Entry<String, BeanPair> entry : values.entrySet()) {
            addFormValue(form, entry.getKey(), entry.getValue().getValue(), entry.getValue().getAnns());
        }
    }
    return form;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Parameter(org.apache.cxf.jaxrs.model.Parameter) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 3 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project tomee 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 4 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project tomee 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 5 with Parameter

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

the class ResourceUtils method createConstructorArguments.

public static Object[] createConstructorArguments(Constructor<?> c, Message m, boolean perRequest, Map<Class<?>, Object> contextValues, Class<?>[] params, Annotation[][] anns, Type[] genericTypes) {
    if (m == null) {
        m = new MessageImpl();
    }
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> templateValues = (MultivaluedMap<String, String>) m.get(URITemplate.TEMPLATE_PARAMETERS);
    Object[] values = new Object[params.length];
    for (int i = 0; i < params.length; i++) {
        if (AnnotationUtils.getAnnotation(anns[i], Context.class) != null) {
            Object contextValue = contextValues != null ? contextValues.get(params[i]) : null;
            if (contextValue == null) {
                if (perRequest || InjectionUtils.VALUE_CONTEXTS.contains(params[i].getName())) {
                    values[i] = JAXRSUtils.createContextValue(m, genericTypes[i], params[i]);
                } else {
                    values[i] = InjectionUtils.createThreadLocalProxy(params[i]);
                }
            } else {
                values[i] = contextValue;
            }
        } else {
            // this branch won't execute for singletons given that the found constructor
            // is guaranteed to have only Context parameters, if any, for singletons
            Parameter p = ResourceUtils.getParameter(i, anns[i], params[i]);
            values[i] = JAXRSUtils.createHttpParameterValue(p, params[i], genericTypes[i], anns[i], m, templateValues, null);
        }
    }
    return values;
}
Also used : Context(javax.ws.rs.core.Context) Parameter(org.apache.cxf.jaxrs.model.Parameter) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageImpl(org.apache.cxf.message.MessageImpl)

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