Search in sources :

Example 1 with ParameterType

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

the class InjectionUtils method injectIntoMap.

private static Object injectIntoMap(Type genericType, Annotation[] paramAnns, MultivaluedMap<String, String> processedValues, boolean decoded, ParameterType pathParam, Message message) {
    ParameterizedType paramType = (ParameterizedType) genericType;
    Class<?> keyType = (Class<?>) paramType.getActualTypeArguments()[0];
    Type secondType = InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    if (secondType instanceof ParameterizedType) {
        MultivaluedMap<Object, Object> theValues = new MetadataMap<>();
        ParameterizedType valueParamType = (ParameterizedType) secondType;
        Class<?> valueType = (Class<?>) InjectionUtils.getType(valueParamType.getActualTypeArguments(), 0);
        for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
            List<String> valuesList = processedValuesEntry.getValue();
            for (String value : valuesList) {
                Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
                theValues.add(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
            }
        }
        return theValues;
    }
    Map<Object, Object> theValues = new HashMap<>();
    Class<?> valueType = (Class<?>) InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
        List<String> valuesList = processedValuesEntry.getValue();
        for (String value : valuesList) {
            Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
            theValues.put(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
        }
    }
    return theValues;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) 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 2 with ParameterType

use of org.apache.cxf.jaxrs.model.ParameterType 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)

Example 3 with ParameterType

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

the class ClientProxyImpl method invoke.

/**
 * Updates the current state if Client method is invoked, otherwise
 * does the remote invocation or returns a new proxy if subresource
 * method is invoked. Can throw an expected exception if ResponseExceptionMapper
 * is registered
 */
@Override
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
    checkClosed();
    Class<?> declaringClass = m.getDeclaringClass();
    if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass || Object.class == declaringClass || Closeable.class == declaringClass || AutoCloseable.class == declaringClass) {
        return m.invoke(this, params);
    }
    resetResponse();
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
    if (ori == null) {
        if (m.isDefault()) {
            return invokeDefaultMethod(declaringClass, o, m, params);
        }
        reportInvalidResourceMethod(m, "INVALID_RESOURCE_METHOD");
    }
    MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(m, params, ori);
    List<Parameter> beanParamsList = getParameters(types, ParameterType.BEAN);
    int bodyIndex = getBodyIndex(types, ori);
    List<Object> pathParams = getPathParamValues(m, params, types, beanParamsList, ori, bodyIndex);
    UriBuilder builder = getCurrentBuilder().clone();
    if (isRoot) {
        addNonEmptyPath(builder, ori.getClassResourceInfo().getURITemplate().getValue());
    }
    addNonEmptyPath(builder, ori.getURITemplate().getValue());
    handleMatrixes(m, params, types, beanParamsList, builder);
    handleQueries(m, params, types, beanParamsList, builder);
    URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();
    MultivaluedMap<String, String> headers = getHeaders();
    MultivaluedMap<String, String> paramHeaders = new MetadataMap<>();
    handleHeaders(m, params, paramHeaders, beanParamsList, types);
    handleCookies(m, params, paramHeaders, beanParamsList, types);
    if (ori.isSubResourceLocator()) {
        ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
        if (subCri == null) {
            reportInvalidResourceMethod(m, "INVALID_SUBRESOURCE");
        }
        MultivaluedMap<String, String> subHeaders = paramHeaders;
        if (inheritHeaders) {
            subHeaders.putAll(headers);
        }
        ClientState newState = getState().newState(uri, subHeaders, getTemplateParametersMap(ori.getURITemplate(), pathParams));
        ClientProxyImpl proxyImpl = new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
        proxyImpl.setConfiguration(getConfiguration());
        return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
    }
    headers.putAll(paramHeaders);
    getState().setTemplates(getTemplateParametersMap(ori.getURITemplate(), pathParams));
    Object body = null;
    if (bodyIndex != -1) {
        body = params[bodyIndex];
        if (body == null) {
            bodyIndex = -1;
        }
    } else if (types.containsKey(ParameterType.FORM)) {
        body = handleForm(m, params, types, beanParamsList);
    } else if (types.containsKey(ParameterType.REQUEST_BODY)) {
        body = handleMultipart(types, ori, params);
    } else if (hasFormParams(params, beanParamsList)) {
        body = handleForm(m, params, types, beanParamsList);
    }
    setRequestHeaders(headers, ori, types.containsKey(ParameterType.FORM), body == null ? null : body.getClass(), m.getReturnType());
    try {
        return doChainedInvocation(uri, headers, ori, params, body, bodyIndex, null, null);
    } finally {
        resetResponseStateImmediatelyIfNeeded();
    }
}
Also used : ParameterType(org.apache.cxf.jaxrs.model.ParameterType) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URI(java.net.URI) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 4 with ParameterType

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

the class ClientProxyImpl method getParametersInfo.

protected MultivaluedMap<ParameterType, Parameter> getParametersInfo(Method m, Object[] params, OperationResourceInfo ori) {
    MultivaluedMap<ParameterType, Parameter> map = new MetadataMap<>();
    List<Parameter> parameters = ori.getParameters();
    if (parameters.isEmpty()) {
        return map;
    }
    int requestBodyParam = 0;
    int multipartParam = 0;
    for (Parameter p : parameters) {
        if (isIgnorableParameter(m, p)) {
            continue;
        }
        if (p.getType() == ParameterType.REQUEST_BODY) {
            requestBodyParam++;
            if (getMultipart(ori, p.getIndex()) != null) {
                multipartParam++;
            }
        }
        map.add(p.getType(), p);
    }
    if (map.containsKey(ParameterType.REQUEST_BODY)) {
        if (requestBodyParam > 1 && requestBodyParam != multipartParam) {
            reportInvalidResourceMethod(ori.getMethodToInvoke(), "SINGLE_BODY_ONLY");
        }
        if (map.containsKey(ParameterType.FORM)) {
            reportInvalidResourceMethod(ori.getMethodToInvoke(), "ONLY_FORM_ALLOWED");
        }
    }
    return map;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Parameter(org.apache.cxf.jaxrs.model.Parameter) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 5 with ParameterType

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

the class InjectionUtils method injectIntoMap.

// CHECKSTYLE:OFF
private static Object injectIntoMap(Class<?> rawType, Type genericType, Annotation[] paramAnns, MultivaluedMap<String, String> processedValues, boolean decoded, ParameterType pathParam, Message message) {
    // CHECKSTYLE:ON
    ParameterizedType paramType = (ParameterizedType) genericType;
    Class<?> keyType = (Class<?>) paramType.getActualTypeArguments()[0];
    Type secondType = InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    if (secondType instanceof ParameterizedType) {
        MultivaluedMap<Object, Object> theValues = new MetadataMap<Object, Object>();
        ParameterizedType valueParamType = (ParameterizedType) secondType;
        Class<?> valueType = (Class<?>) InjectionUtils.getType(valueParamType.getActualTypeArguments(), 0);
        for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
            List<String> valuesList = processedValuesEntry.getValue();
            for (String value : valuesList) {
                Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
                theValues.add(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
            }
        }
        return theValues;
    }
    Map<Object, Object> theValues = new HashMap<>();
    Class<?> valueType = (Class<?>) InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
        List<String> valuesList = processedValuesEntry.getValue();
        for (String value : valuesList) {
            Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
            theValues.put(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
        }
    }
    return theValues;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) 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)

Aggregations

ParameterType (org.apache.cxf.jaxrs.model.ParameterType)16 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)10 Type (java.lang.reflect.Type)8 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 MediaType (javax.ws.rs.core.MediaType)8 Parameter (org.apache.cxf.jaxrs.model.Parameter)8 GenericArrayType (java.lang.reflect.GenericArrayType)7 ParameterizedType (java.lang.reflect.ParameterizedType)7 WildcardType (java.lang.reflect.WildcardType)7 List (java.util.List)7 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)6 ArrayList (java.util.ArrayList)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 Endpoint (org.apache.cxf.endpoint.Endpoint)4 Field (java.lang.reflect.Field)3 InputStream (java.io.InputStream)2