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

The following examples show how to use org.apache.cxf.jaxrs.utils.InjectionUtils#processGenericTypeIfNeeded() . 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: MicroProfileClientProxyImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
protected Type getGenericReturnType(Class<?> serviceCls, Method method, Class<?> returnType) {
    final Type genericReturnType = super.getGenericReturnType(serviceCls, method, returnType);
    
    if (genericReturnType instanceof ParameterizedType) {
        final ParameterizedType pt = (ParameterizedType)genericReturnType;
        if (CompletionStage.class.isAssignableFrom(InjectionUtils.getRawType(pt))) {
            final Type[] actualTypeArguments = pt.getActualTypeArguments();
            if (actualTypeArguments.length > 0 && actualTypeArguments[0] instanceof ParameterizedType) {
                return InjectionUtils.processGenericTypeIfNeeded(serviceCls, returnType, 
                    (ParameterizedType)actualTypeArguments[0]);
            } else {
                return returnType;
            }
        }
    }
    
    return genericReturnType;
}
 
Example 2
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected Type getGenericReturnType(Class<?> serviceCls, Method method, Class<?> returnType) {
    return InjectionUtils.processGenericTypeIfNeeded(serviceCls, returnType, method.getGenericReturnType());
}
 
Example 3
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected void doWriteBody(Message outMessage,
                           Object body,
                           Type bodyType,
                           Annotation[] customAnns,
                           OutputStream os) throws Fault {


    OperationResourceInfo ori = outMessage.getContent(OperationResourceInfo.class);
    if (ori == null) {
        return;
    }

    Method method = ori.getMethodToInvoke();
    int bodyIndex = (Integer)outMessage.get(PROXY_METHOD_PARAM_BODY_INDEX);

    Annotation[] anns = customAnns != null ? customAnns
        : getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex);
    try {
        if (bodyIndex != -1) {
            Class<?> paramClass = method.getParameterTypes()[bodyIndex];
            Class<?> bodyClass =
                paramClass.isAssignableFrom(body.getClass()) ? paramClass : body.getClass();
            Type genericType = method.getGenericParameterTypes()[bodyIndex];
            if (bodyType != null) {
                genericType = bodyType;
            }
            genericType = InjectionUtils.processGenericTypeIfNeeded(
                ori.getClassResourceInfo().getServiceClass(), bodyClass, genericType);
            bodyClass = InjectionUtils.updateParamClassToTypeIfNeeded(bodyClass, genericType);
            writeBody(body, outMessage, bodyClass, genericType, anns, os);
        } else {
            Type paramType = body.getClass();
            if (bodyType != null) {
                paramType = bodyType;
            }
            writeBody(body, outMessage, body.getClass(), paramType,
                      anns, os);
        }
    } catch (Exception ex) {
        throw new Fault(ex);
    }

}
 
Example 4
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);
        }
    }

}