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);
}
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));
}
}
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));
}
}
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();
}
}
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));
}
Aggregations