Search in sources :

Example 81 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo 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)

Example 82 with OperationResourceInfo

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

the class ResourceUtils method getAllTypesForResource.

private static void getAllTypesForResource(ClassResourceInfo resource, ResourceTypes types, boolean jaxbOnly, MessageBodyWriter<?> jaxbWriter) {
    Class<?> jaxbElement = null;
    try {
        jaxbElement = ClassLoaderUtils.loadClass("javax.xml.bind.JAXBElement", ResourceUtils.class);
    } catch (final ClassNotFoundException e) {
    // no-op
    }
    for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) {
        Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke() : ori.getAnnotatedMethod();
        Class<?> realReturnType = method.getReturnType();
        Class<?> cls = realReturnType;
        if (cls == Response.class || ori.isAsync()) {
            cls = getActualJaxbType(cls, method, false);
        }
        Type type = method.getGenericReturnType();
        if (jaxbOnly) {
            checkJaxbType(resource.getServiceClass(), cls, realReturnType == Response.class || ori.isAsync() ? cls : type, types, method.getAnnotations(), jaxbWriter, jaxbElement);
        } else {
            types.getAllTypes().put(cls, type);
        }
        for (Parameter pm : ori.getParameters()) {
            if (pm.getType() == ParameterType.REQUEST_BODY) {
                Class<?> inType = method.getParameterTypes()[pm.getIndex()];
                if (inType != AsyncResponse.class) {
                    Type paramType = method.getGenericParameterTypes()[pm.getIndex()];
                    if (jaxbOnly) {
                        checkJaxbType(resource.getServiceClass(), inType, paramType, types, method.getParameterAnnotations()[pm.getIndex()], jaxbWriter, jaxbElement);
                    } else {
                        types.getAllTypes().put(inType, paramType);
                    }
                }
            }
        }
    }
    for (ClassResourceInfo sub : resource.getSubResources()) {
        if (!isRecursiveSubResource(resource, sub)) {
            getAllTypesForResource(sub, types, jaxbOnly, jaxbWriter);
        }
    }
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod)

Example 83 with OperationResourceInfo

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

the class ResourceUtils method createOperationInfo.

// CHECKSTYLE:ON
private static OperationResourceInfo createOperationInfo(Method m, Method annotatedMethod, ClassResourceInfo cri, Path path, String httpMethod) {
    OperationResourceInfo ori = new OperationResourceInfo(m, annotatedMethod, cri);
    URITemplate t = URITemplate.createTemplate(path);
    ori.setURITemplate(t);
    ori.setHttpMethod(httpMethod);
    return ori;
}
Also used : URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 84 with OperationResourceInfo

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

the class ClientProxyImpl method retryInvoke.

@Override
protected Object retryInvoke(URI newRequestURI, MultivaluedMap<String, String> headers, Object body, Exchange exchange, Map<String, Object> invContext) throws Throwable {
    Map<String, Object> reqContext = CastUtils.cast((Map<?, ?>) invContext.get(REQUEST_CONTEXT));
    int bodyIndex = body != null ? (Integer) reqContext.get(PROXY_METHOD_PARAM_BODY_INDEX) : -1;
    OperationResourceInfo ori = (OperationResourceInfo) reqContext.get(OperationResourceInfo.class.getName());
    return doChainedInvocation(newRequestURI, headers, ori, null, body, bodyIndex, exchange, invContext);
}
Also used : OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 85 with OperationResourceInfo

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

the class OpenApiCustomizer method customize.

public void customize(final OpenAPI oas) {
    if (replaceTags || javadocProvider != null) {
        Map<String, ClassResourceInfo> operations = new HashMap<>();
        Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
        cris.forEach(cri -> {
            cri.getMethodDispatcher().getOperationResourceInfos().forEach(ori -> {
                String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
                operations.put(normalizedPath, cri);
                methods.put(Pair.of(ori.getHttpMethod(), normalizedPath), ori);
            });
        });
        List<Tag> tags = new ArrayList<>();
        oas.getPaths().forEach((pathKey, pathItem) -> {
            Optional<Tag> tag;
            if (replaceTags && operations.containsKey(pathKey)) {
                ClassResourceInfo cri = operations.get(pathKey);
                tag = Optional.of(new Tag());
                tag.get().setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
                if (javadocProvider != null) {
                    tag.get().setDescription(javadocProvider.getClassDoc(cri));
                }
                if (!tags.contains(tag.get())) {
                    tags.add(tag.get());
                }
            } else {
                tag = Optional.empty();
            }
            pathItem.readOperationsMap().forEach((method, operation) -> {
                if (replaceTags && tag.isPresent()) {
                    operation.setTags(Collections.singletonList(tag.get().getName()));
                }
                Pair<String, String> key = Pair.of(method.name(), pathKey);
                if (methods.containsKey(key) && javadocProvider != null) {
                    OperationResourceInfo ori = methods.get(key);
                    if (StringUtils.isBlank(operation.getSummary())) {
                        operation.setSummary(javadocProvider.getMethodDoc(ori));
                    }
                    if (operation.getParameters() == null) {
                        List<Parameter> parameters = new ArrayList<>();
                        addParameters(parameters);
                        operation.setParameters(parameters);
                    }
                    for (int i = 0; i < operation.getParameters().size(); i++) {
                        if (StringUtils.isBlank(operation.getParameters().get(i).getDescription())) {
                            operation.getParameters().get(i).setDescription(extractJavadoc(operation, ori, i));
                        }
                    }
                    addParameters(operation.getParameters());
                    customizeResponses(operation, ori);
                }
            });
        });
        if (replaceTags && oas.getTags() != null) {
            oas.setTags(tags);
        }
    }
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Tag(io.swagger.v3.oas.models.tags.Tag) Pair(org.apache.commons.lang3.tuple.Pair)

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