Search in sources :

Example 51 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSUtilsTest method testFormParametersBeanWithMap.

@Test
public void testFormParametersBeanWithMap() throws Exception {
    Class<?>[] argType = { Customer.CustomerBean.class };
    Method m = Customer.class.getMethod("testFormBean", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.REQUEST_URI, "/bar");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    String body = "g.b=1&g.b=2";
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb);
    assertNotNull(cb.getG());
    List<String> values = cb.getG().get("b");
    assertEquals(2, values.size());
    assertEquals("1", values.get(0));
    assertEquals("2", values.get(1));
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 52 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSUtilsTest method testSelectResourceMethod.

@Test
public void testSelectResourceMethod() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class);
    OperationResourceInfo ori1 = new OperationResourceInfo(Customer.class.getMethod("getItAsXML", new Class[] {}), cri);
    ori1.setHttpMethod("GET");
    ori1.setURITemplate(new URITemplate("/"));
    OperationResourceInfo ori2 = new OperationResourceInfo(Customer.class.getMethod("getItPlain", new Class[] {}), cri);
    ori2.setHttpMethod("GET");
    ori2.setURITemplate(new URITemplate("/"));
    MethodDispatcher md = new MethodDispatcher();
    md.bind(ori1, Customer.class.getMethod("getItAsXML", new Class[] {}));
    md.bind(ori2, Customer.class.getMethod("getItPlain", new Class[] {}));
    cri.setMethodDispatcher(md);
    OperationResourceInfo ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", getTypes("text/plain"));
    assertSame(ori, ori2);
    ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", getTypes("text/xml"));
    assertSame(ori, ori1);
    ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", sortMediaTypes(getTypes("*/*;q=0.1,text/plain,text/xml;q=0.8")));
    assertSame(ori, ori2);
    ori = JAXRSUtils.findTargetMethod(getMap(cri), createMessage2(), "GET", new MetadataMap<String, String>(), "*/*", sortMediaTypes(getTypes("*;q=0.1,text/plain,text/xml;q=0.9,x/y")));
    assertSame(ori, ori2);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 53 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class InjectionUtils method handleBean.

public static Object handleBean(Class<?> paramType, Annotation[] paramAnns, MultivaluedMap<String, String> values, ParameterType pType, Message message, boolean decoded) {
    Object bean = null;
    try {
        if (paramType.isInterface()) {
            paramType = org.apache.cxf.jaxrs.utils.JAXBUtils.getValueTypeFromAdapter(paramType, paramType, paramAnns);
        }
        bean = paramType.newInstance();
    } catch (IllegalAccessException ex) {
        reportServerError("CLASS_ACCESS_FAILURE", paramType.getName());
    } catch (Exception ex) {
        reportServerError("CLASS_INSTANTIATION_FAILURE", paramType.getName());
    }
    Map<String, MultivaluedMap<String, String>> parsedValues = new HashMap<String, MultivaluedMap<String, String>>();
    for (Map.Entry<String, List<String>> entry : values.entrySet()) {
        String memberKey = entry.getKey();
        String beanKey = null;
        int idx = memberKey.indexOf('.');
        if (idx == -1) {
            beanKey = "." + memberKey;
        } else {
            beanKey = memberKey.substring(0, idx);
            memberKey = memberKey.substring(idx + 1);
        }
        MultivaluedMap<String, String> value = parsedValues.get(beanKey);
        if (value == null) {
            value = new MetadataMap<String, String>();
            parsedValues.put(beanKey, value);
        }
        value.put(memberKey, entry.getValue());
    }
    if (!parsedValues.isEmpty()) {
        for (Map.Entry<String, MultivaluedMap<String, String>> entry : parsedValues.entrySet()) {
            String memberKey = entry.getKey();
            boolean isbean = !memberKey.startsWith(".");
            if (!isbean) {
                memberKey = memberKey.substring(1);
            }
            Object setter = null;
            Object getter = null;
            for (Method m : paramType.getMethods()) {
                if (m.getName().equalsIgnoreCase("set" + memberKey) && m.getParameterTypes().length == 1) {
                    setter = m;
                } else if (m.getName().equalsIgnoreCase("get" + memberKey) || isBooleanType(m.getReturnType()) && m.getName().equalsIgnoreCase("is" + memberKey)) {
                    getter = m;
                }
                if (setter != null && getter != null) {
                    break;
                }
            }
            if (setter == null) {
                for (Field f : paramType.getFields()) {
                    if (f.getName().equalsIgnoreCase(memberKey)) {
                        setter = f;
                        getter = f;
                        break;
                    }
                }
            }
            if (setter != null && getter != null) {
                Class<?> type = null;
                Type genericType = null;
                Object paramValue = null;
                if (setter instanceof Method) {
                    type = Method.class.cast(setter).getParameterTypes()[0];
                    genericType = Method.class.cast(setter).getGenericParameterTypes()[0];
                    paramValue = InjectionUtils.extractFromMethod(bean, (Method) getter);
                } else {
                    type = Field.class.cast(setter).getType();
                    genericType = Field.class.cast(setter).getGenericType();
                    paramValue = InjectionUtils.extractFieldValue((Field) getter, bean);
                }
                List<MultivaluedMap<String, String>> processedValuesList = processValues(type, genericType, entry.getValue(), isbean);
                for (MultivaluedMap<String, String> processedValues : processedValuesList) {
                    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
                        Object appendValue = InjectionUtils.injectIntoCollectionOrArray(type, genericType, paramAnns, processedValues, isbean, true, pType, message);
                        paramValue = InjectionUtils.mergeCollectionsOrArrays(paramValue, appendValue, genericType);
                    } else if (isSupportedMap(genericType)) {
                        Object appendValue = InjectionUtils.injectIntoMap(type, genericType, paramAnns, processedValues, true, pType, message);
                        paramValue = InjectionUtils.mergeMap(paramValue, appendValue, genericType);
                    } else if (isbean) {
                        paramValue = InjectionUtils.handleBean(type, paramAnns, processedValues, pType, message, decoded);
                    } else {
                        paramValue = InjectionUtils.handleParameter(processedValues.values().iterator().next().get(0), decoded, type, type, paramAnns, pType, message);
                    }
                    if (paramValue != null) {
                        if (setter instanceof Method) {
                            InjectionUtils.injectThroughMethod(bean, (Method) setter, paramValue);
                        } else {
                            InjectionUtils.injectFieldValue((Field) setter, bean, paramValue);
                        }
                    }
                }
            }
        }
    }
    return bean;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) WebApplicationException(javax.ws.rs.WebApplicationException) Field(java.lang.reflect.Field) 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) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) 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 54 with MetadataMap

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

Example 55 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class InjectionUtils method processValues.

private static List<MultivaluedMap<String, String>> processValues(Class<?> type, Type genericType, MultivaluedMap<String, String> values, boolean isbean) {
    List<MultivaluedMap<String, String>> valuesList = new ArrayList<MultivaluedMap<String, String>>();
    if (isbean && InjectionUtils.isSupportedCollectionOrArray(type)) {
        Class<?> realType = InjectionUtils.getActualType(genericType);
        for (Map.Entry<String, List<String>> entry : values.entrySet()) {
            String memberKey = entry.getKey();
            Class<?> memberType = null;
            for (Method m : realType.getMethods()) {
                if (m.getName().equalsIgnoreCase("set" + memberKey) && m.getParameterTypes().length == 1) {
                    memberType = m.getParameterTypes()[0];
                    break;
                }
            }
            if (memberType == null) {
                for (Field f : realType.getFields()) {
                    if (f.getName().equalsIgnoreCase(memberKey)) {
                        memberType = f.getType();
                        break;
                    }
                }
            }
            // {c=[71, 81, 91, 72, 82, 92], a=[C1, C2], b=[790, 791]}
            if (memberType != null && InjectionUtils.isSupportedCollectionOrArray(memberType)) {
                continue;
            }
            // Split multivaluedmap value list contents into separate multivaluedmap instances
            // whose list contents are only 1 level deep, for example:
            // {a=[C1, C2], b=[790, 791]}
            // becomes these 2 separate multivaluedmap instances:
            // {a=[C1], b=[790]} and {a=[C2], b=[791]}
            int idx = 0;
            for (String value : entry.getValue()) {
                MultivaluedMap<String, String> splitValues = (idx < valuesList.size()) ? valuesList.get(idx) : null;
                if (splitValues == null) {
                    splitValues = new MetadataMap<String, String>();
                    valuesList.add(splitValues);
                }
                splitValues.add(memberKey, value);
                idx++;
            }
        }
    } else {
        valuesList.add(values);
    }
    return valuesList;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) List(java.util.List) ArrayList(java.util.ArrayList) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) 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

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7