Search in sources :

Example 11 with ParameterType

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

the class SwaggerParseUtils method getUserApplicationFromJson.

public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
    JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
    Map<String, Object> map = reader.fromJson(json);
    UserApplication app = new UserApplication();
    String relativePath = (String) map.get("basePath");
    app.setBasePath(StringUtils.isEmpty(relativePath) ? "/" : relativePath);
    Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<>();
    Set<String> tags = new HashSet<>();
    List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
    if (tagsProp != null) {
        for (Map<String, Object> tagProp : tagsProp) {
            tags.add((String) tagProp.get("name"));
        }
    } else {
        tags.add("");
    }
    for (String tag : tags) {
        userOpsMap.put(tag, new LinkedList<UserOperation>());
    }
    Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
    for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
        String operPath = pathEntry.getKey();
        Map<String, Object> operations = pathEntry.getValue();
        for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
            UserOperation userOp = new UserOperation();
            userOp.setVerb(operEntry.getKey().toUpperCase());
            Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
            // operations but Swagger may still include it.
            if (oper != null) {
                userOp.setPath(operPath);
                userOp.setName((String) oper.get("operationId"));
                List<String> opProduces = CastUtils.cast((List<?>) oper.get("produces"));
                userOp.setProduces(listToString(opProduces));
                List<String> opConsumes = CastUtils.cast((List<?>) oper.get("consumes"));
                userOp.setConsumes(listToString(opConsumes));
                List<Parameter> userOpParams = new LinkedList<>();
                List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
                for (Map<String, Object> param : params) {
                    String name = (String) param.get("name");
                    // "query", "header", "path", "formData" or "body"
                    String paramType = (String) param.get("in");
                    ParameterType pType = "body".equals(paramType) ? ParameterType.REQUEST_BODY : "formData".equals(paramType) ? ParameterType.FORM : ParameterType.valueOf(paramType.toUpperCase());
                    Parameter userParam = new Parameter(pType, name);
                    setJavaType(userParam, (String) param.get("type"));
                    userOpParams.add(userParam);
                }
                if (!userOpParams.isEmpty()) {
                    userOp.setParameters(userOpParams);
                }
                List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
                if (opTags == null) {
                    opTags = Collections.singletonList("");
                }
                for (String opTag : opTags) {
                    userOpsMap.get(opTag).add(userOp);
                }
            }
        }
    }
    List<UserResource> resources = new LinkedList<>();
    for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
        UserResource ur = new UserResource();
        ur.setPath("/");
        ur.setOperations(entry.getValue());
        ur.setName(entry.getKey());
        resources.add(ur);
    }
    app.setResources(resources);
    return app;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) UserResource(org.apache.cxf.jaxrs.model.UserResource) LinkedList(java.util.LinkedList) UserApplication(org.apache.cxf.jaxrs.model.UserApplication) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) Parameter(org.apache.cxf.jaxrs.model.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with ParameterType

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

the class WadlGenerator method doWriteParam.

// CHECKSTYLE:OFF
protected void doWriteParam(OperationResourceInfo ori, StringBuilder sb, Parameter pm, Class<?> type, Type genericType, String paramName, Annotation[] anns, boolean isJson) {
    // CHECKSTYLE:ON
    ParameterType pType = pm.getType();
    boolean isForm = isFormParameter(pm, type, anns);
    if (paramName == null && isForm) {
        Multipart m = AnnotationUtils.getAnnotation(anns, Multipart.class);
        if (m != null) {
            paramName = m.value();
        }
    }
    sb.append("<param name=\"").append(paramName).append("\" ");
    String style = ParameterType.PATH == pType ? "template" : isForm ? "query" : ParameterType.REQUEST_BODY == pType ? "plain" : pType.toString().toLowerCase();
    sb.append("style=\"").append(style).append('"');
    if (pm.getDefaultValue() != null) {
        sb.append(" default=\"").append(xmlEncodeIfNeeded(pm.getDefaultValue())).append('"');
    }
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        sb.append(" repeating=\"true\"");
    }
    String value = XmlSchemaPrimitiveUtils.getSchemaRepresentation(type);
    if (value == null) {
        if (type.isEnum()) {
            value = "xs:string";
        } else if (type == InputStream.class) {
            value = "xs:anyType";
        }
    }
    if (value != null) {
        if (isJson) {
            value = value.substring(3);
        }
        sb.append(" type=\"").append(value).append('"');
    }
    if (type.isEnum()) {
        sb.append('>');
        handleDocs(anns, sb, DocTarget.PARAM, true, isJson);
        setEnumOptions(sb, type);
        sb.append("</param>");
    } else {
        addDocsAndCloseElement(ori, pm.getIndex(), sb, anns, "param", DocTarget.PARAM, true, isJson);
    }
}
Also used : ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 13 with ParameterType

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

the class WadlGenerator method doHandleClassParams.

protected void doHandleClassParams(OperationResourceInfo ori, StringBuilder sb, Map<Parameter, Object> params, boolean isJson, ParameterType... pType) {
    Set<ParameterType> pTypes = new LinkedHashSet<>(Arrays.asList(pType));
    for (Map.Entry<Parameter, Object> entry : params.entrySet()) {
        Parameter pm = entry.getKey();
        Object obj = entry.getValue();
        if (pTypes.contains(pm.getType())) {
            Class<?> cls = obj instanceof Method ? ((Method) obj).getParameterTypes()[0] : ((Field) obj).getType();
            Type type = obj instanceof Method ? ((Method) obj).getGenericParameterTypes()[0] : ((Field) obj).getGenericType();
            Annotation[] ann = obj instanceof Method ? ((Method) obj).getParameterAnnotations()[0] : ((Field) obj).getAnnotations();
            doWriteParam(ori, sb, pm, cls, type, pm.getName(), ann, isJson);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Type(java.lang.reflect.Type) Parameter(org.apache.cxf.jaxrs.model.Parameter) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) EnumMap(java.util.EnumMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Annotation(java.lang.annotation.Annotation)

Example 14 with ParameterType

use of org.apache.cxf.jaxrs.model.ParameterType in project cxf 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 15 with ParameterType

use of org.apache.cxf.jaxrs.model.ParameterType 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<>();
    for (Map.Entry<String, List<String>> entry : values.entrySet()) {
        String memberKey = entry.getKey();
        final String beanKey;
        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<>();
            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) {
                final Class<?> type;
                final Type genericType;
                Object paramValue;
                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 = injectIntoMap(genericType, paramAnns, processedValues, true, pType, message);
                        paramValue = mergeMap(paramValue, appendValue);
                    } 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)

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