Search in sources :

Example 1 with AcceptParameterHttpPreprocessor

use of org.jboss.resteasy.core.AcceptParameterHttpPreprocessor in project resteasy by resteasy.

the class AcceptParameterHttpPreprocessorTest method complex.

/**
 * @tpTestDetails Request with complex query parameter
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void complex() throws Exception {
    String acceptParamName = "bar";
    AcceptParameterHttpPreprocessor processor = new AcceptParameterHttpPreprocessor(acceptParamName);
    List<MediaType> expected = Arrays.asList(MediaType.valueOf("application/xhtml+xml"), MediaType.valueOf("text/html"), MediaType.valueOf("application/xml;q=0.9"), MediaType.valueOf("*/*;q=0.8"));
    String param1 = URLEncoder.encode("application/xml;q=0.9,application/xhtml+xml,*/*;q=0.8", StandardCharsets.UTF_8.name());
    String param2 = URLEncoder.encode("text/html", StandardCharsets.UTF_8.name());
    HttpRequest request = MockHttpRequest.get("foo?" + acceptParamName + "=" + param1 + "&" + acceptParamName + "=" + param2);
    PreMatchContainerRequestContext context = new PreMatchContainerRequestContext(request, null, null);
    processor.filter(context);
    List<MediaType> actual = request.getHttpHeaders().getAcceptableMediaTypes();
    Assert.assertEquals("Incorrect acceptable media type extracted", expected, actual);
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) HttpRequest(org.jboss.resteasy.spi.HttpRequest) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.Test)

Example 2 with AcceptParameterHttpPreprocessor

use of org.jboss.resteasy.core.AcceptParameterHttpPreprocessor in project resteasy by resteasy.

the class AcceptParameterHttpPreprocessorTest method aLittleMoreComplicated.

/**
 * @tpTestDetails Query parameter with multiple types
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void aLittleMoreComplicated() throws Exception {
    String acceptParamName = "bar";
    AcceptParameterHttpPreprocessor processor = new AcceptParameterHttpPreprocessor(acceptParamName);
    List<MediaType> expected = Arrays.asList(MediaType.TEXT_XML_TYPE, MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_HTML_TYPE, MediaType.APPLICATION_XHTML_XML_TYPE);
    MockHttpRequest request = MockHttpRequest.get("foo?" + acceptParamName + "=" + expected.get(0) + "," + expected.get(1));
    PreMatchContainerRequestContext context = new PreMatchContainerRequestContext(request, null, null);
    request.accept(expected.get(2));
    request.accept(expected.get(3));
    processor.filter(context);
    List<MediaType> actual = request.getHttpHeaders().getAcceptableMediaTypes();
    for (MediaType expect : expected) {
        Assert.assertTrue(actual.contains(expect));
    }
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.Test)

Example 3 with AcceptParameterHttpPreprocessor

use of org.jboss.resteasy.core.AcceptParameterHttpPreprocessor in project resteasy by resteasy.

the class AcceptParameterHttpPreprocessorTest method withoutParam.

/**
 * @tpTestDetails Request without query parameter
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void withoutParam() throws Exception {
    String acceptParamName = "baz";
    AcceptParameterHttpPreprocessor processor = new AcceptParameterHttpPreprocessor(acceptParamName);
    List<MediaType> expected = Arrays.asList(MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_HTML_TYPE);
    MockHttpRequest request = MockHttpRequest.get("foo");
    PreMatchContainerRequestContext context = new PreMatchContainerRequestContext(request, null, null);
    request.accept(expected.get(0));
    request.accept(expected.get(1));
    processor.filter(context);
    List<MediaType> actual = request.getHttpHeaders().getAcceptableMediaTypes();
    for (MediaType expect : expected) {
        Assert.assertTrue(actual.contains(expect));
    }
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.Test)

Example 4 with AcceptParameterHttpPreprocessor

use of org.jboss.resteasy.core.AcceptParameterHttpPreprocessor in project sofa-rpc by sofastack.

the class SofaResteasyDeployment method start.

@Override
public void start() {
    // this allows each WAR to have their own set of providers
    if (providerFactory == null) {
        providerFactory = new ResteasyProviderFactory();
    }
    providerFactory.setRegisterBuiltins(registerBuiltin);
    if (deploymentSensitiveFactoryEnabled) {
        // and still be able to call ResteasyProviderFactory.getInstance()
        if (!(providerFactory instanceof ThreadLocalResteasyProviderFactory)) {
            if (ResteasyProviderFactory.peekInstance() == null || !(ResteasyProviderFactory.peekInstance() instanceof ThreadLocalResteasyProviderFactory)) {
                threadLocalProviderFactory = new ThreadLocalResteasyProviderFactory(providerFactory);
                ResteasyProviderFactory.setInstance(threadLocalProviderFactory);
            }
        }
    } else {
        ResteasyProviderFactory.setInstance(providerFactory);
    }
    if (asyncJobServiceEnabled) {
        AsynchronousDispatcher asyncDispatcher = new AsynchronousDispatcher(providerFactory);
        asyncDispatcher.setMaxCacheSize(asyncJobServiceMaxJobResults);
        asyncDispatcher.setMaxWaitMilliSeconds(asyncJobServiceMaxWait);
        asyncDispatcher.setThreadPoolSize(asyncJobServiceThreadPoolSize);
        asyncDispatcher.setBasePath(asyncJobServiceBasePath);
        asyncDispatcher.getUnwrappedExceptions().addAll(unwrappedExceptions);
        dispatcher = asyncDispatcher;
        asyncDispatcher.start();
    } else {
        // CHANGE: 只改了这里
        SynchronousDispatcher dis = new SofaSynchronousDispatcher(providerFactory);
        dis.getUnwrappedExceptions().addAll(unwrappedExceptions);
        dispatcher = dis;
    }
    registry = dispatcher.getRegistry();
    if (widerRequestMatching) {
        ((ResourceMethodRegistry) registry).setWiderMatching(widerRequestMatching);
    }
    dispatcher.getDefaultContextObjects().putAll(defaultContextObjects);
    dispatcher.getDefaultContextObjects().put(Configurable.class, providerFactory);
    dispatcher.getDefaultContextObjects().put(Providers.class, providerFactory);
    dispatcher.getDefaultContextObjects().put(Registry.class, registry);
    dispatcher.getDefaultContextObjects().put(Dispatcher.class, dispatcher);
    dispatcher.getDefaultContextObjects().put(InternalDispatcher.class, InternalDispatcher.getInstance());
    // push context data so we can inject it
    Map contextDataMap = ResteasyProviderFactory.getContextDataMap();
    contextDataMap.putAll(dispatcher.getDefaultContextObjects());
    try {
        if (injectorFactoryClass != null) {
            InjectorFactory injectorFactory;
            try {
                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(injectorFactoryClass);
                injectorFactory = (InjectorFactory) clazz.newInstance();
            } catch (ClassNotFoundException cnfe) {
                throw new RuntimeException("Unable to find InjectorFactory implementation.", cnfe);
            } catch (Exception e) {
                throw new RuntimeException("Unable to instantiate InjectorFactory implementation.", e);
            }
            providerFactory.setInjectorFactory(injectorFactory);
        }
        // see ResteasyContextParameters.RESTEASY_CONTEXT_OBJECTS
        if (constructedDefaultContextObjects != null && constructedDefaultContextObjects.size() > 0) {
            for (Map.Entry<String, String> entry : constructedDefaultContextObjects.entrySet()) {
                Class<?> key = null;
                try {
                    key = Thread.currentThread().getContextClassLoader().loadClass(entry.getKey());
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException("Unable to instantiate context object " + entry.getKey(), e);
                }
                Object obj = createFromInjectorFactory(entry.getValue(), providerFactory);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Creating context object <" + entry.getKey() + " : " + entry.getValue() + ">");
                }
                defaultContextObjects.put(key, obj);
                dispatcher.getDefaultContextObjects().put(key, obj);
                contextDataMap.put(key, obj);
            }
        }
        if (interceptorPrecedences != null) {
            for (String precedence : interceptorPrecedences) {
                providerFactory.appendInterceptorPrecedence(precedence.trim());
            }
        }
        if (interceptorBeforePrecedences != null) {
            for (Map.Entry<String, String> ext : interceptorBeforePrecedences.entrySet()) {
                providerFactory.insertInterceptorPrecedenceBefore(ext.getKey().trim(), ext.getValue().trim());
            }
        }
        if (interceptorAfterPrecedences != null) {
            for (Map.Entry<String, String> ext : interceptorAfterPrecedences.entrySet()) {
                providerFactory.insertInterceptorPrecedenceAfter(ext.getKey().trim(), ext.getValue().trim());
            }
        }
        if (securityEnabled) {
            providerFactory.register(RoleBasedSecurityFeature.class);
        }
        if (registerBuiltin) {
            providerFactory.setRegisterBuiltins(true);
            RegisterBuiltin.register(providerFactory);
            // having problems using form parameters from container for a couple of TCK tests.  I couldn't figure out
            // why, specifically:
            // com/sun/ts/tests/jaxrs/spec/provider/standardhaspriority/JAXRSClient.java#readWriteMapProviderTest_from_standalone                                               Failed. Test case throws exception: [JAXRSCommonClient] null failed!  Check output for cause of failure.
            // com/sun/ts/tests/jaxrs/spec/provider/standardwithjaxrsclient/JAXRSClient.java#mapElementProviderTest_from_standalone                                             Failed. Test case throws exception: returned MultivaluedMap is null
            providerFactory.registerProviderInstance(new ServerFormUrlEncodedProvider(useContainerFormParams), null, null, true);
        } else {
            providerFactory.setRegisterBuiltins(false);
        }
        if (applicationClass != null) {
            application = createApplication(applicationClass, dispatcher, providerFactory);
        }
        // register all providers
        registration();
        if (paramMapping != null) {
            providerFactory.getContainerRequestFilterRegistry().registerSingleton(new AcceptParameterHttpPreprocessor(paramMapping));
        }
        AcceptHeaderByFileSuffixFilter suffixNegotiationFilter = null;
        if (mediaTypeMappings != null) {
            Map<String, MediaType> extMap = new HashMap<String, MediaType>();
            for (Map.Entry<String, String> ext : mediaTypeMappings.entrySet()) {
                String value = ext.getValue();
                extMap.put(ext.getKey().trim(), MediaType.valueOf(value.trim()));
            }
            if (suffixNegotiationFilter == null) {
                suffixNegotiationFilter = new AcceptHeaderByFileSuffixFilter();
                providerFactory.getContainerRequestFilterRegistry().registerSingleton(suffixNegotiationFilter);
            }
            suffixNegotiationFilter.setMediaTypeMappings(extMap);
        }
        if (languageExtensions != null) {
            if (suffixNegotiationFilter == null) {
                suffixNegotiationFilter = new AcceptHeaderByFileSuffixFilter();
                providerFactory.getContainerRequestFilterRegistry().registerSingleton(suffixNegotiationFilter);
            }
            suffixNegotiationFilter.setLanguageMappings(languageExtensions);
        }
    } finally {
        ResteasyProviderFactory.removeContextDataLevel();
    }
}
Also used : AcceptHeaderByFileSuffixFilter(org.jboss.resteasy.core.AcceptHeaderByFileSuffixFilter) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) HashMap(java.util.HashMap) ResourceMethodRegistry(org.jboss.resteasy.core.ResourceMethodRegistry) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) InjectorFactory(org.jboss.resteasy.spi.InjectorFactory) AsynchronousDispatcher(org.jboss.resteasy.core.AsynchronousDispatcher) ThreadLocalResteasyProviderFactory(org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory) MediaType(javax.ws.rs.core.MediaType) ThreadLocalResteasyProviderFactory(org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HashMap(java.util.HashMap) Map(java.util.Map) ServerFormUrlEncodedProvider(org.jboss.resteasy.plugins.providers.ServerFormUrlEncodedProvider)

Example 5 with AcceptParameterHttpPreprocessor

use of org.jboss.resteasy.core.AcceptParameterHttpPreprocessor in project resteasy by resteasy.

the class AcceptParameterHttpPreprocessorTest method simple.

/**
 * @tpTestDetails Basic query parameter with xml type
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void simple() throws Exception {
    String acceptParamName = "accept";
    AcceptParameterHttpPreprocessor processor = new AcceptParameterHttpPreprocessor(acceptParamName);
    String type = MediaType.TEXT_XML;
    HttpRequest request = MockHttpRequest.get("foo?" + acceptParamName + "=" + type);
    MediaType mediaType = MediaType.valueOf(type);
    PreMatchContainerRequestContext context = new PreMatchContainerRequestContext(request, null, null);
    processor.filter(context);
    List<MediaType> list = request.getHttpHeaders().getAcceptableMediaTypes();
    Assert.assertEquals("Incorrect acceptable list size", 1, list.size());
    Assert.assertEquals("Incorrect media type extracted", mediaType, list.get(0));
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) HttpRequest(org.jboss.resteasy.spi.HttpRequest) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.Test)

Aggregations

AcceptParameterHttpPreprocessor (org.jboss.resteasy.core.AcceptParameterHttpPreprocessor)5 MediaType (jakarta.ws.rs.core.MediaType)4 PreMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext)4 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)4 Test (org.junit.Test)4 HttpRequest (org.jboss.resteasy.spi.HttpRequest)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MediaType (javax.ws.rs.core.MediaType)1 AcceptHeaderByFileSuffixFilter (org.jboss.resteasy.core.AcceptHeaderByFileSuffixFilter)1 AsynchronousDispatcher (org.jboss.resteasy.core.AsynchronousDispatcher)1 ResourceMethodRegistry (org.jboss.resteasy.core.ResourceMethodRegistry)1 SynchronousDispatcher (org.jboss.resteasy.core.SynchronousDispatcher)1 ThreadLocalResteasyProviderFactory (org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory)1 ServerFormUrlEncodedProvider (org.jboss.resteasy.plugins.providers.ServerFormUrlEncodedProvider)1 InjectorFactory (org.jboss.resteasy.spi.InjectorFactory)1 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)1