Search in sources :

Example 1 with JAXRSServiceImpl

use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project tomee by apache.

the class CxfRsHttpListener method isCXFResource.

public boolean isCXFResource(final HttpServletRequest request) {
    try {
        Application application = findApplication();
        if (!applicationProvidesResources(application)) {
            JAXRSServiceImpl service = (JAXRSServiceImpl) server.getEndpoint().getService();
            if (service == null) {
                return false;
            }
            String pathToMatch = HttpUtils.getPathToMatch(request.getServletPath(), pattern, true);
            final List<ClassResourceInfo> resources = service.getClassResourceInfos();
            for (final ClassResourceInfo info : resources) {
                if (info.getResourceClass() == null || info.getURITemplate() == null) {
                    // possible?
                    continue;
                }
                final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
                if (info.getURITemplate().match(pathToMatch, parameters)) {
                    return true;
                }
            }
        } else {
            return true;
        }
    } catch (final Exception e) {
        LOGGER.info("No JAX-RS service");
    }
    return false;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) InternalApplication(org.apache.openejb.server.rest.InternalApplication) Application(javax.ws.rs.core.Application) ResponseConstraintViolationException(org.apache.cxf.validation.ResponseConstraintViolationException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) EndpointException(org.apache.cxf.endpoint.EndpointException) ServletException(javax.servlet.ServletException) BusException(org.apache.cxf.BusException) InjectionException(javax.enterprise.inject.InjectionException)

Example 2 with JAXRSServiceImpl

use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses3.

@Test
public void testSelectBetweenMultipleResourceClasses3() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate4.class, org.apache.cxf.jaxrs.resources.TestResourceTemplate3.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate3.class, firstResourceClass(resources, "/"));
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate4.class, firstResourceClass(resources, "/test"));
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Test(org.junit.Test)

Example 3 with JAXRSServiceImpl

use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.

the class WadlGenerator method getResourcesList.

public List<ClassResourceInfo> getResourcesList(Message m, UriInfo ui) {
    final String slash = "/";
    String path = ui.getPath();
    if (!path.startsWith(slash)) {
        path = slash + path;
    }
    List<ClassResourceInfo> all = ((JAXRSServiceImpl) m.getExchange().getService()).getClassResourceInfos();
    boolean absolutePathSlashOn = checkAbsolutePathSlash && ui.getAbsolutePath().getPath().endsWith(slash);
    if (slash.equals(path) && !absolutePathSlashOn) {
        return all;
    }
    List<ClassResourceInfo> cris = new LinkedList<>();
    for (ClassResourceInfo cri : all) {
        MultivaluedMap<String, String> map = new MetadataMap<>();
        if (cri.getURITemplate().match(path, map) && slash.equals(map.getFirst(URITemplate.FINAL_MATCH_GROUP))) {
            cris.add(cri);
        }
    }
    return cris;
}
Also used : JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) LinkedList(java.util.LinkedList)

Example 4 with JAXRSServiceImpl

use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project cxf by apache.

the class JAXRSUtilsTest method testFindTargetResourceClassWithSubResource.

@Test
public void testFindTargetResourceClassWithSubResource() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/sub/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/123/true/chapter/1", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getNewBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("addBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 5 with JAXRSServiceImpl

use of org.apache.cxf.jaxrs.JAXRSServiceImpl in project tomee by apache.

the class CxfRsHttpListener method logEndpoints.

private void logEndpoints(final Application application, final String prefix, final Map<String, EJBRestServiceInfo> restEjbs, final JAXRSServerFactoryBean factory, final String base) {
    final List<Logs.LogResourceEndpointInfo> resourcesToLog = new ArrayList<>();
    int classSize = 0;
    int addressSize = 0;
    final JAXRSServiceImpl service = (JAXRSServiceImpl) factory.getServiceFactory().getService();
    final List<ClassResourceInfo> resources = service.getClassResourceInfos();
    for (final ClassResourceInfo info : resources) {
        if (info.getResourceClass() == null) {
            // possible?
            continue;
        }
        final String address = Logs.singleSlash(base, info.getURITemplate().getValue());
        final String clazz = info.getResourceClass().getName();
        final String type;
        if (restEjbs.containsKey(clazz)) {
            type = "EJB";
        } else {
            type = "Pojo";
        }
        classSize = Math.max(classSize, clazz.length());
        addressSize = Math.max(addressSize, address.length());
        int methodSize = 7;
        int methodStrSize = 0;
        final List<Logs.LogOperationEndpointInfo> toLog = new ArrayList<>();
        final MethodDispatcher md = info.getMethodDispatcher();
        for (final OperationResourceInfo ori : md.getOperationResourceInfos()) {
            final String httpMethod = ori.getHttpMethod();
            final String currentAddress = Logs.singleSlash(address, ori.getURITemplate().getValue());
            final String methodToStr = Logs.toSimpleString(ori.getMethodToInvoke());
            toLog.add(new Logs.LogOperationEndpointInfo(httpMethod, currentAddress, methodToStr));
            if (httpMethod != null) {
                methodSize = Math.max(methodSize, httpMethod.length());
            }
            addressSize = Math.max(addressSize, currentAddress.length());
            methodStrSize = Math.max(methodStrSize, methodToStr.length());
        }
        Collections.sort(toLog);
        resourcesToLog.add(new Logs.LogResourceEndpointInfo(type, address, clazz, toLog, methodSize, methodStrSize));
    }
    // effective logging
    LOGGER.info("REST Application: " + Logs.forceLength(prefix, addressSize, true) + " -> " + (InternalApplication.class.isInstance(application) && InternalApplication.class.cast(application).getOriginal() != null ? InternalApplication.class.cast(application).getOriginal() : application));
    Collections.sort(resourcesToLog);
    for (final Logs.LogResourceEndpointInfo resource : resourcesToLog) {
        // Init and register MBeans
        final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management").set("j2eeType", "JAX-RS").set("J2EEServer", "openejb").set("J2EEApplication", base).set("EndpointType", resource.type).set("name", resource.classname);
        final ObjectName jmxObjectName = jmxName.build();
        LocalMBeanServer.registerDynamicWrapperSilently(new RestServiceMBean(resource), jmxObjectName);
        jmxNames.add(jmxObjectName);
        LOGGER.info("     Service URI: " + Logs.forceLength(resource.address, addressSize, true) + " -> " + Logs.forceLength(resource.type, 4, false) + " " + Logs.forceLength(resource.classname, classSize, true));
        for (final Logs.LogOperationEndpointInfo log : resource.operations) {
            LOGGER.info("          " + Logs.forceLength(log.http, resource.methodSize, false) + " " + Logs.forceLength(log.address, addressSize, true) + " ->      " + Logs.forceLength(log.method, resource.methodStrSize, true));
        }
        resource.operations.clear();
    }
    resourcesToLog.clear();
}
Also used : InternalApplication(org.apache.openejb.server.rest.InternalApplication) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ManagedEndpoint(org.apache.cxf.endpoint.ManagedEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) ObjectName(javax.management.ObjectName) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher)

Aggregations

JAXRSServiceImpl (org.apache.cxf.jaxrs.JAXRSServiceImpl)12 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)9 JAXRSServiceFactoryBean (org.apache.cxf.jaxrs.JAXRSServiceFactoryBean)6 Test (org.junit.Test)6 Endpoint (org.apache.cxf.endpoint.Endpoint)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)4 EndpointImpl (org.apache.cxf.endpoint.EndpointImpl)2 Exchange (org.apache.cxf.message.Exchange)2 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)2 Message (org.apache.cxf.message.Message)2 MessageImpl (org.apache.cxf.message.MessageImpl)2 BindingInfo (org.apache.cxf.service.model.BindingInfo)2 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)2 ServletDestination (org.apache.cxf.transport.servlet.ServletDestination)2 InternalApplication (org.apache.openejb.server.rest.InternalApplication)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1