Search in sources :

Example 66 with OperationResourceInfo

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

the class ValidationUtils method getResourceInstance.

public static Object getResourceInstance(Message message) {
    final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
    if (ori == null) {
        return null;
    }
    if (!ori.getClassResourceInfo().isRoot()) {
        return message.getExchange().get("org.apache.cxf.service.object.last");
    }
    final ResourceProvider resourceProvider = ori.getClassResourceInfo().getResourceProvider();
    if (!resourceProvider.isSingleton()) {
        String error = "Service object is not a singleton, use a custom invoker to validate";
        LOG.warning(error);
        return null;
    }
    return resourceProvider.getInstance(message);
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 67 with OperationResourceInfo

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

the class JAXRSServiceFactoryBeanTest method verifyOp.

private boolean verifyOp(Set<OperationResourceInfo> ops, String opName, String httpMethod, boolean isSubresource) {
    for (OperationResourceInfo ori : ops) {
        if (opName.equals(ori.getMethodToInvoke().getName())) {
            assertEquals(httpMethod, ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertEquals(isSubresource, ori.isSubResourceLocator());
            return true;
        }
    }
    return false;
}
Also used : OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 68 with OperationResourceInfo

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

the class JAXRSServiceFactoryBeanTest method testNoSubResources.

@Test
public void testNoSubResources() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setEnableStaticResolution(true);
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(1, resources.size());
    // Verify root ClassResourceInfo: BookStoreNoSubResource
    ClassResourceInfo rootCri = resources.get(0);
    assertNotNull(rootCri.getURITemplate());
    URITemplate template = rootCri.getURITemplate();
    MultivaluedMap<String, String> values = new MetadataMap<>();
    assertTrue(template.match("/bookstore/books/123", values));
    assertFalse(rootCri.hasSubResources());
    MethodDispatcher md = rootCri.getMethodDispatcher();
    assertEquals(7, md.getOperationResourceInfos().size());
    Set<OperationResourceInfo> ops = md.getOperationResourceInfos();
    assertTrue("No operation found", verifyOp(ops, "getBook", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBookStoreInfo", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBooks", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "getBookJSON", "GET", false));
    assertTrue("No operation found", verifyOp(ops, "addBook", "POST", false));
    assertTrue("No operation found", verifyOp(ops, "updateBook", "PUT", false));
    assertTrue("No operation found", verifyOp(ops, "deleteBook", "DELETE", false));
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 69 with OperationResourceInfo

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

the class CustomJAXRSInvoker method invoke.

@Override
public Object invoke(Exchange exchange, Object requestParams, Object resourceObject) {
    OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
    Method m = ori.getMethodToInvoke();
    Class<?> realClass = ClassHelper.getRealClass(exchange.getBus(), resourceObject);
    Principal p = new SecurityContextImpl(exchange.getInMessage()).getUserPrincipal();
    if (realClass == SecureBookStore.class && "getThatBook".equals(m.getName()) && "baddy".equals(p.getName())) {
        return new MessageContentsList(Response.status(Response.Status.FORBIDDEN).build());
    }
    return super.invoke(exchange, requestParams, resourceObject);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) MessageContentsList(org.apache.cxf.message.MessageContentsList) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Principal(java.security.Principal)

Example 70 with OperationResourceInfo

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

the class SseInterceptor method handleMessage.

public void handleMessage(Message message) {
    // Not an SSE invocation, skipping it in favor of normal processing
    if (message.get(SseEventSink.class) == null) {
        return;
    }
    if (!isRequestor(message) && message.get(SseInterceptor.class) == null) {
        message.put(SseInterceptor.class, this);
        final Exchange exchange = message.getExchange();
        OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
        if (ori != null) {
            Response.ResponseBuilder builder = Response.ok();
            HttpServletResponse servletResponse = null;
            final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
            final Object response = message.get(AbstractHTTPDestination.HTTP_RESPONSE);
            if (response instanceof HttpServletResponse) {
                servletResponse = (HttpServletResponse) response;
                builder = Response.status(servletResponse.getStatus());
                for (final String header : servletResponse.getHeaderNames()) {
                    final Collection<String> headers = servletResponse.getHeaders(header);
                    addHeader(builder, header, headers);
                }
            }
            // Run the filters
            try {
                final ResponseImpl responseImpl = (ResponseImpl) builder.build();
                final Message outMessage = getOutMessage(message);
                JAXRSUtils.runContainerResponseFilters(providerFactory, responseImpl, outMessage, ori, ori.getAnnotatedMethod());
                if (servletResponse != null) {
                    servletResponse.setStatus(responseImpl.getStatus());
                    final Map<String, List<String>> userHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
                    if (userHeaders != null) {
                        for (Map.Entry<String, List<String>> entry : userHeaders.entrySet()) {
                            setHeader(servletResponse, entry);
                        }
                    }
                    final MultivaluedMap<String, String> headers = responseImpl.getStringHeaders();
                    if (headers != null) {
                        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
                            setHeader(servletResponse, entry);
                        }
                    }
                }
            } catch (Throwable ex) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.log(Level.FINE, ex.getMessage(), ex);
                }
            }
        }
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) Exchange(org.apache.cxf.message.Exchange) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) SseEventSink(javax.ws.rs.sse.SseEventSink) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

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