Search in sources :

Example 21 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class JAXRSInInterceptor method processRequest.

private void processRequest(Message message, Exchange exchange) throws IOException {
    ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
    RequestPreprocessor rp = providerFactory.getRequestPreprocessor();
    if (rp != null) {
        rp.preprocess(message, new UriInfoImpl(message, null));
    }
    // Global pre-match request filters
    if (JAXRSUtils.runContainerRequestFilters(providerFactory, message, true, null)) {
        return;
    }
    // HTTP method
    String httpMethod = HttpUtils.getProtocolHeader(message, Message.HTTP_REQUEST_METHOD, HttpMethod.POST, true);
    // Path to match
    String rawPath = HttpUtils.getPathToMatch(message, true);
    Map<String, List<String>> protocolHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
    // Content-Type
    String requestContentType = null;
    List<String> ctHeaderValues = protocolHeaders.get(Message.CONTENT_TYPE);
    if (ctHeaderValues != null && !ctHeaderValues.isEmpty()) {
        requestContentType = ctHeaderValues.get(0);
        message.put(Message.CONTENT_TYPE, requestContentType);
    }
    if (requestContentType == null) {
        requestContentType = (String) message.get(Message.CONTENT_TYPE);
        if (requestContentType == null) {
            requestContentType = MediaType.WILDCARD;
        }
    }
    // Accept
    String acceptTypes = null;
    List<String> acceptHeaderValues = protocolHeaders.get(Message.ACCEPT_CONTENT_TYPE);
    if (acceptHeaderValues != null && !acceptHeaderValues.isEmpty()) {
        acceptTypes = acceptHeaderValues.get(0);
        message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
    }
    if (acceptTypes == null) {
        acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
        if (acceptTypes == null) {
            acceptTypes = "*/*";
            message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
        }
    }
    final List<MediaType> acceptContentTypes;
    try {
        acceptContentTypes = JAXRSUtils.sortMediaTypes(acceptTypes, JAXRSUtils.MEDIA_TYPE_Q_PARAM);
    } catch (IllegalArgumentException ex) {
        throw ExceptionUtils.toNotAcceptableException(null, null);
    }
    exchange.put(Message.ACCEPT_CONTENT_TYPE, acceptContentTypes);
    // 1. Matching target resource class
    List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(message);
    Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, rawPath, message);
    if (matchedResources == null) {
        org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("NO_ROOT_EXC", BUNDLE, message.get(Message.REQUEST_URI), rawPath);
        Level logLevel = JAXRSUtils.getExceptionLogLevel(message, NotFoundException.class);
        LOG.log(logLevel == null ? Level.FINE : logLevel, errorMsg.toString());
        Response resp = JAXRSUtils.createResponse(resources, message, errorMsg.toString(), Response.Status.NOT_FOUND.getStatusCode(), false);
        throw ExceptionUtils.toNotFoundException(null, resp);
    }
    MultivaluedMap<String, String> matchedValues = new MetadataMap<>();
    final OperationResourceInfo ori;
    try {
        ori = JAXRSUtils.findTargetMethod(matchedResources, message, httpMethod, matchedValues, requestContentType, acceptContentTypes, true, true);
        setExchangeProperties(message, exchange, ori, matchedValues, resources.size());
    } catch (WebApplicationException ex) {
        if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), httpMethod)) {
            Response response = JAXRSUtils.createResponse(resources, null, null, 200, true);
            exchange.put(Response.class, response);
            return;
        }
        throw ex;
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Request path is: " + rawPath);
        LOG.fine("Request HTTP method is: " + httpMethod);
        LOG.fine("Request contentType is: " + requestContentType);
        LOG.fine("Accept contentType is: " + acceptTypes);
        LOG.fine("Found operation: " + ori.getMethodToInvoke().getName());
    }
    // Global and name-bound post-match request filters
    if (!ori.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, message, false, ori.getNameBindings())) {
        return;
    }
    // Process parameters
    List<Object> params = JAXRSUtils.processParameters(ori, matchedValues, message);
    message.setContent(List.class, params);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MediaType(javax.ws.rs.core.MediaType) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Response(javax.ws.rs.core.Response) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Level(java.util.logging.Level) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfoImpl(org.apache.cxf.jaxrs.impl.UriInfoImpl)

Example 22 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class ServerProviderFactoryTest method testCustomTestAndWadlHandler.

@Test
public void testCustomTestAndWadlHandler() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    assertEquals(1, pf.getPreMatchContainerRequestFilters().size());
    assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
    List<Object> providers = new ArrayList<>();
    WadlGenerator wg = new WadlGenerator();
    providers.add(wg);
    TestHandler th = new TestHandler();
    providers.add(th);
    pf.setUserProviders(providers);
    assertEquals(2, pf.getPreMatchContainerRequestFilters().size());
    assertSame(wg, pf.getPreMatchContainerRequestFilters().get(0).getProvider());
    assertSame(th, pf.getPreMatchContainerRequestFilters().get(1).getProvider());
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 23 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class ServerProviderFactoryTest method testCustomTestHandler.

@Test
public void testCustomTestHandler() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    assertEquals(1, pf.getPreMatchContainerRequestFilters().size());
    assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
    TestHandler th = new TestHandler();
    pf.setUserProviders(Collections.singletonList(th));
    assertEquals(2, pf.getPreMatchContainerRequestFilters().size());
    assertTrue(pf.getPreMatchContainerRequestFilters().get(0).getProvider() instanceof WadlGenerator);
    assertSame(th, pf.getPreMatchContainerRequestFilters().get(1).getProvider());
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Test(org.junit.Test)

Aggregations

ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)23 ArrayList (java.util.ArrayList)5 Application (javax.ws.rs.core.Application)5 Message (org.apache.cxf.message.Message)5 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 Response (javax.ws.rs.core.Response)4 RequestPreprocessor (org.apache.cxf.jaxrs.impl.RequestPreprocessor)4 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)4 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)4 Level (java.util.logging.Level)3 MediaType (javax.ws.rs.core.MediaType)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)3 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)3 Endpoint (org.apache.cxf.endpoint.Endpoint)3 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)3 MessageContentsList (org.apache.cxf.message.MessageContentsList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2