Java Code Examples for org.apache.cxf.jaxrs.utils.InjectionUtils#isSupportedCollectionOrArray()

The following examples show how to use org.apache.cxf.jaxrs.utils.InjectionUtils#isSupportedCollectionOrArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XSLTJaxbProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {
    if (!super.isReadable(type, genericType, anns, mt)) {
        return false;
    }

    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        return supportJaxbOnly;
    }

    // if the user has set the list of in classes and a given class
    // is in that list then it can only be handled by the template
    if (inClassCanBeHandled(type.getName()) || inClassesToHandle == null && !supportJaxbOnly) {
        return inTemplatesAvailable(type, anns, mt);
    }
    return supportJaxbOnly;
}
 
Example 2
Source File: XSLTJaxbProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {
    // JAXB support is required
    if (!super.isWriteable(type, genericType, anns, mt)) {
        return false;
    }
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        return supportJaxbOnly;
    }

    // if the user has set the list of out classes and a given class
    // is in that list then it can only be handled by the template
    if (outClassCanBeHandled(type.getName()) || outClassesToHandle == null && !supportJaxbOnly) {
        return outTemplatesAvailable(type, anns, mt);
    }
    return supportJaxbOnly;
}
 
Example 3
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void addFormValue(MultivaluedMap<String, String> form, String name, Object pValue, Annotation[] anns) {
    if (pValue != null) {
        if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
            Collection<?> c = pValue.getClass().isArray()
                ? Arrays.asList((Object[]) pValue) : (Collection<?>) pValue;
            for (Iterator<?> it = c.iterator(); it.hasNext();) {
                FormUtils.addPropertyToForm(form, name, convertParamValue(it.next(), anns));
            }
        } else {
            FormUtils.addPropertyToForm(form, name, name.isEmpty()
                                        ? pValue : convertParamValue(pValue, anns));
        }

    }

}
 
Example 4
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {

        if (InjectionUtils.isSupportedCollectionOrArray(type)) {
            type = InjectionUtils.getActualType(genericType);
            if (type == null) {
                return false;
            }
        }
        return marshalAsJaxbElement && (!xmlTypeAsJaxbElementOnly || isXmlType(type))
            || isSupported(type, genericType, anns);
    }
 
Example 5
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean isReadable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        type = InjectionUtils.getActualType(genericType);
        if (type == null) {
            return false;
        }
    }
    return canBeReadAsJaxbElement(type) || isSupported(type, genericType, anns);
}
 
Example 6
Source File: JAXBElementProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected XMLStreamReader getStreamReader(InputStream is, Class<?> type, MediaType mt) {
    MessageContext mc = getContext();
    XMLStreamReader reader = mc != null ? mc.getContent(XMLStreamReader.class) : null;
    if (reader == null && mc != null) {
        XMLInputFactory factory = (XMLInputFactory)mc.get(XMLInputFactory.class.getName());
        if (factory != null) {
            try {
                reader = factory.createXMLStreamReader(is);
            } catch (XMLStreamException e) {
                throw ExceptionUtils.toInternalServerErrorException(
                    new RuntimeException("Can not create XMLStreamReader", e), null);
            }
        }
    }

    if (reader == null && is == null) {
        reader = getStreamHandlerFromCurrentMessage(XMLStreamReader.class);
    }

    reader = createTransformReaderIfNeeded(reader, is);
    reader = createDepthReaderIfNeeded(reader, is);
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
        return new JAXBCollectionWrapperReader(TransformUtils.createNewReaderIfNeeded(reader, is));
    }
    return reader;

}
 
Example 7
Source File: WadlGenerator.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doWriteBeanParam(OperationResourceInfo ori,
                              StringBuilder sb,
                              Class<?> type,
                              Parameter pm,
                              String parentName,
                              List<Class<?>> parentBeanClasses,
                              boolean isJson) {
    Map<Parameter, Class<?>> pms = InjectionUtils.getParametersFromBeanClass(type, pm.getType(), true);
    for (Map.Entry<Parameter, Class<?>> entry : pms.entrySet()) {
        String name = entry.getKey().getName();
        if (parentName != null) {
            name = parentName + "." + name;
        }
        Class<?> paramCls = entry.getValue();
        boolean isPrimitive = InjectionUtils.isPrimitive(paramCls) || paramCls.isEnum();
        if (isPrimitive
            || Date.class.isAssignableFrom(paramCls)
            || XMLGregorianCalendar.class.isAssignableFrom(paramCls)
            || InjectionUtils.isSupportedCollectionOrArray(paramCls)) {
            doWriteParam(ori, sb, entry.getKey(), paramCls, paramCls, name, new Annotation[] {}, isJson);
        } else if (!parentBeanClasses.contains(paramCls)) {
            parentBeanClasses.add(paramCls);
            doWriteBeanParam(ori, sb, paramCls, entry.getKey(), name, parentBeanClasses, isJson);
            parentBeanClasses.remove(paramCls);
        }
    }
}
 
Example 8
Source File: AbstractSearchConditionVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private ClassValue doGetPrimitiveFieldClass(PrimitiveStatement ps,
                                            String name, Class<?> valueCls, Type type, Object value,
                                            Set<String> set) {
    boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(valueCls);
    Class<?> actualCls = isCollection ? InjectionUtils.getActualType(type) : valueCls;
    CollectionCheckInfo collInfo = null;
    int index = name.indexOf('.');
    if (index != -1) {
        String[] names = name.split("\\.");
        name = name.substring(index + 1);
        if (value != null && !InjectionUtils.isPrimitive(actualCls)) {
            try {
                String nextPart = StringUtils.capitalize(names[1]);

                Method m = actualCls.getMethod("get" + nextPart, new Class[]{});
                if (isCollection) {
                    value = ((Collection)value).iterator().next();
                    set.add(names[0]);
                }
                value = m.invoke(value, new Object[]{});
                valueCls = value.getClass();
                type = m.getGenericReturnType();
            } catch (Throwable ex) {
                throw new RuntimeException(ex);
            }
            return doGetPrimitiveFieldClass(ps, name, valueCls, type, value, set);
        }
    } else if (isCollection) {
        set.add(name);
        Collection coll = (Collection)value;
        value = coll.isEmpty() ? null : coll.iterator().next();
        valueCls = actualCls;
        if (ps instanceof CollectionCheckStatement) {
            collInfo = ((CollectionCheckStatement)ps).getCollectionCheckInfo();
        }
    }

    Class<?> cls = null;
    if (primitiveFieldTypeMap != null) {
        cls = primitiveFieldTypeMap.get(name);
    }
    if (cls == null) {
        cls = valueCls;
    }
    return new ClassValue(cls, value, collInfo, set);
}
 
Example 9
Source File: WadlGenerator.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void handleRepresentation(StringBuilder sb, Set<Class<?>> jaxbTypes,
                                    ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap,
                                    OperationResourceInfo ori, Class<?> type, boolean isJson,
                                    boolean inbound) {
    // CHECKSTYLE:ON
    List<MediaType> types = inbound ? ori.getConsumeTypes() : ori.getProduceTypes();
    if (MultivaluedMap.class.isAssignableFrom(type)) {
        types = Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    } else if (isWildcard(types)) {
        types = Collections.singletonList(defaultRepMediaType);
    }

    Method opMethod = getMethod(ori);
    boolean isPrimitive = InjectionUtils.isPrimitive(type) && !ori.isAsync();
    for (MediaType mt : types) {

        sb.append("<representation");
        sb.append(" mediaType=\"").append(JAXRSUtils.mediaTypeToString(mt)).append('"');
        if (isJson && !mt.getSubtype().contains("json")) {
            sb.append("/>");
            continue;
        }

        boolean allowDefault = true;
        String docCategory;
        Annotation[] anns;
        int inParamIndex = -1;
        Type genericType;
        if (inbound) {
            inParamIndex = getRequestBodyParam(ori).getIndex();
            anns = opMethod.getParameterAnnotations()[inParamIndex];
            if (!isDocAvailable(anns)) {
                anns = opMethod.getAnnotations();
            }
            docCategory = DocTarget.PARAM;
            genericType = opMethod.getGenericParameterTypes()[inParamIndex];
        } else {
            anns = opMethod.getAnnotations();
            docCategory = DocTarget.RETURN;
            allowDefault = false;
            genericType = opMethod.getGenericReturnType();
        }
        if (isPrimitive) {
            sb.append('>');
            Parameter p = inbound ? getRequestBodyParam(ori) : new Parameter(ParameterType.REQUEST_BODY,
                                                                             0, "result");
            doWriteParam(ori, sb, p, type, type, p.getName() == null ? "request" : p.getName(), anns, isJson);
            sb.append("</representation>");
        } else {
            boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
            Class<?> theActualType;
            if (isCollection) {
                theActualType = InjectionUtils.getActualType(genericType);
            } else {
                theActualType = ResourceUtils.getActualJaxbType(type, opMethod, inbound);
            }
            if (theActualType == Object.class && !(genericType instanceof Class)
                || genericType instanceof TypeVariable) {
                Type theType = InjectionUtils.processGenericTypeIfNeeded(
                    ori.getClassResourceInfo().getServiceClass(), Object.class, genericType);
                theActualType = InjectionUtils.getActualType(theType);
            }
            if (isJson) {
                sb.append(" element=\"").append(theActualType.getSimpleName()).append('"');
            } else if (qnameResolver != null
                       && (linkAnyMediaTypeToXmlSchema || mt.getSubtype().contains("xml"))
                       && jaxbTypes.contains(theActualType)) {
                generateQName(sb, qnameResolver, clsMap, theActualType, isCollection,
                              getBodyAnnotations(ori, inbound));
            }
            addDocsAndCloseElement(ori, inParamIndex, sb, anns, "representation",
                                   docCategory, allowDefault, isJson);
        }
    }

}