Search in sources :

Example 1 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project ddf by codice.

the class BodyWriter method doWriteBody.

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("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);
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 2 with OperationResourceInfo

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

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        OperationResourceInfo operationResourceInfo = contextMessage.getExchange().get(OperationResourceInfo.class);
        if (operationResourceInfo != null) {
            o = new ResourceContextImpl(contextMessage, operationResourceInfo);
        }
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 3 with OperationResourceInfo

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

the class ResourceUtils method createServiceClassResourceInfo.

public static ClassResourceInfo createServiceClassResourceInfo(Map<String, UserResource> resources, UserResource model, Class<?> sClass, boolean isRoot, boolean enableStatic, Bus bus) {
    if (model == null) {
        throw new RuntimeException("Resource class " + sClass.getName() + " has no model info");
    }
    ClassResourceInfo cri = new ClassResourceInfo(sClass, sClass, isRoot, enableStatic, true, model.getConsumes(), model.getProduces(), bus);
    URITemplate t = URITemplate.createTemplate(model.getPath());
    cri.setURITemplate(t);
    MethodDispatcher md = new MethodDispatcher();
    Map<String, UserOperation> ops = model.getOperationsAsMap();
    Method defaultMethod = null;
    Map<String, Method> methodNames = new HashMap<>();
    for (Method m : cri.getServiceClass().getMethods()) {
        if (m.getAnnotation(DefaultMethod.class) != null) {
            // if needed we can also support multiple default methods
            defaultMethod = m;
        }
        methodNames.put(m.getName(), m);
    }
    for (Map.Entry<String, UserOperation> entry : ops.entrySet()) {
        UserOperation op = entry.getValue();
        Method actualMethod = methodNames.get(op.getName());
        if (actualMethod == null) {
            actualMethod = defaultMethod;
        }
        if (actualMethod == null) {
            continue;
        }
        OperationResourceInfo ori = new OperationResourceInfo(actualMethod, cri, URITemplate.createTemplate(op.getPath()), op.getVerb(), op.getConsumes(), op.getProduces(), op.getParameters(), op.isOneway());
        String rClassName = actualMethod.getReturnType().getName();
        if (op.getVerb() == null) {
            if (resources.containsKey(rClassName)) {
                ClassResourceInfo subCri = rClassName.equals(model.getName()) ? cri : createServiceClassResourceInfo(resources, resources.get(rClassName), actualMethod.getReturnType(), false, enableStatic, bus);
                if (subCri != null) {
                    cri.addSubClassResourceInfo(subCri);
                    md.bind(ori, actualMethod);
                }
            }
        } else {
            md.bind(ori, actualMethod);
        }
    }
    cri.setMethodDispatcher(md);
    return checkMethodDispatcher(cri) ? cri : null;
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) Method(java.lang.reflect.Method) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 4 with OperationResourceInfo

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

the class JAXRSOutInterceptor method processResponse.

// Response shouldn't be closed here
@SuppressWarnings("resource")
private void processResponse(ServerProviderFactory providerFactory, Message message) {
    if (isResponseAlreadyHandled(message)) {
        return;
    }
    MessageContentsList objs = MessageContentsList.getContentsList(message);
    if (objs == null || objs.isEmpty()) {
        return;
    }
    Object responseObj = objs.get(0);
    final Response response;
    if (responseObj instanceof Response) {
        response = (Response) responseObj;
        if (response.getStatus() == 500 && message.getExchange().get(JAXRSUtils.EXCEPTION_FROM_MAPPER) != null) {
            message.put(Message.RESPONSE_CODE, 500);
            return;
        }
    } else {
        int status = getStatus(message, responseObj != null ? 200 : 204);
        response = JAXRSUtils.toResponseBuilder(status).entity(responseObj).build();
    }
    Exchange exchange = message.getExchange();
    OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
    serializeMessage(providerFactory, message, response, ori, true);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Exchange(org.apache.cxf.message.Exchange) MessageContentsList(org.apache.cxf.message.MessageContentsList) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 5 with OperationResourceInfo

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

the class JAXRSInvoker method getResourceProvider.

private ResourceProvider getResourceProvider(Exchange exchange) {
    Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
    if (provider == null) {
        OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
        ClassResourceInfo cri = ori.getClassResourceInfo();
        return cri.getResourceProvider();
    }
    return (ResourceProvider) provider;
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)129 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)104 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)52 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 List (java.util.List)12 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)11 Response (javax.ws.rs.core.Response)11 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)11 Exchange (org.apache.cxf.message.Exchange)11 MediaType (javax.ws.rs.core.MediaType)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)7