Search in sources :

Example 11 with ServerProviderFactory

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

the class InjectionUtils method getParamConverter.

public static <T> Optional<ParamConverter<T>> getParamConverter(Class<T> pClass, Type genericType, Annotation[] anns, Message message) {
    if (message != null) {
        ServerProviderFactory pf = ServerProviderFactory.getInstance(message);
        ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns, message);
        return Optional.ofNullable(pm);
    }
    return Optional.empty();
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory)

Example 12 with ServerProviderFactory

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

the class AbstractSearchConditionParser method paramConverterAvailable.

private boolean paramConverterAvailable(final Class<?> pClass) {
    Message m = JAXRSUtils.getCurrentMessage();
    ServerProviderFactory pf = m == null ? null : ServerProviderFactory.getInstance(m);
    return pf != null && pf.createParameterHandler(pClass, pClass, EMPTY_ANNOTTAIONS, m) != null;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message)

Example 13 with ServerProviderFactory

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

the class ServerProviderFactoryTest method testCustomWadlHandler.

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

Example 14 with ServerProviderFactory

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

Example 15 with ServerProviderFactory

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

the class OpenApiFeature method initialize.

@Override
public void initialize(Server server, Bus bus) {
    final JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    final ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    final Set<String> packages = new HashSet<>();
    if (resourcePackages != null) {
        packages.addAll(resourcePackages);
    }
    Properties swaggerProps = null;
    GenericOpenApiContextBuilder<?> openApiConfiguration;
    final Application application = getApplicationOrDefault(server, factory, sfb, bus);
    if (StringUtils.isEmpty(getConfigLocation())) {
        swaggerProps = getSwaggerProperties(propertiesLocation, bus);
        if (isScan()) {
            packages.addAll(scanResourcePackages(sfb));
        }
        final OpenAPI oas = new OpenAPI().info(getInfo(swaggerProps));
        registerComponents(securityDefinitions).ifPresent(oas::setComponents);
        final SwaggerConfiguration config = new SwaggerConfiguration().openAPI(oas).prettyPrint(getOrFallback(isPrettyPrint(), swaggerProps, PRETTY_PRINT_PROPERTY)).readAllResources(isReadAllResources()).ignoredRoutes(getIgnoredRoutes()).filterClass(getOrFallback(getFilterClass(), swaggerProps, FILTER_CLASS_PROPERTY)).resourceClasses(getResourceClasses()).resourcePackages(getOrFallback(packages, swaggerProps, RESOURCE_PACKAGE_PROPERTY));
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).openApiConfiguration(config);
    } else {
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).configLocation(getConfigLocation());
    }
    try {
        final OpenApiContext context = openApiConfiguration.buildContext(true);
        final Properties userProperties = getUserProperties(context.getOpenApiConfiguration().getUserDefinedOptions());
        registerOpenApiResources(sfb, packages, context.getOpenApiConfiguration());
        registerSwaggerUiResources(sfb, combine(swaggerProps, userProperties), factory, bus);
        if (customizer != null) {
            customizer.setApplicationInfo(factory.getApplicationProvider());
        }
    } catch (OpenApiConfigurationException ex) {
        throw new RuntimeException("Unable to initialize OpenAPI context", ex);
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) Properties(java.util.Properties) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Application(javax.ws.rs.core.Application) OpenAPI(io.swagger.v3.oas.models.OpenAPI) HashSet(java.util.HashSet) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

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