use of org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext 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.interception.jaxrs.PreMatchContainerRequestContext 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.interception.jaxrs.PreMatchContainerRequestContext 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.interception.jaxrs.PreMatchContainerRequestContext in project resteasy by resteasy.
the class ContainerRequestContextTest method testQueryParamatersClear.
/**
* @tpTestDetails Test that ContainerRequestContext setRequestUri clear previous query parameters
* @tpSince RESTEasy 3.0.17
*/
@Test
public void testQueryParamatersClear() throws URISyntaxException {
ContainerRequestContext containerRequestContext = new PreMatchContainerRequestContext(request, null, null);
logger.info("request uri: " + containerRequestContext.getUriInfo().getRequestUri());
assertEquals("Wrong count of parameters in getUriInfo response", 2, containerRequestContext.getUriInfo().getQueryParameters().size());
MultivaluedMap<String, String> expected = new MultivaluedHashMap<>();
expected.put("foo", Collections.singletonList("foo"));
expected.put("bar", Collections.singletonList("bar"));
MultivaluedMap<String, String> queryParameters = containerRequestContext.getUriInfo().getQueryParameters();
assertEquals("Wrong parameter in getUriInfo response", expected, queryParameters);
containerRequestContext.setRequestUri(new URI("http://foo.bar"));
logger.info("request uri: " + containerRequestContext.getUriInfo().getRequestUri());
assertTrue("Wrong count of parameters in getUriInfo response", containerRequestContext.getUriInfo().getQueryParameters().isEmpty());
containerRequestContext.setRequestUri(new URI("http://foo.bar?foo=foo"));
logger.info("request uri: " + containerRequestContext.getUriInfo().getRequestUri());
expected = new MultivaluedHashMap<>();
expected.put("foo", Collections.singletonList("foo"));
assertEquals("Wrong parameter in getUriInfo response", expected, containerRequestContext.getUriInfo().getQueryParameters());
}
use of org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext in project resteasy by resteasy.
the class SynchronousDispatcher method preprocess.
/*
* TODO: refactor this method
* This only used by org.jboss.restesy.springmvc.ResteasyHandlerMapping
* And most of the code is same with the other preprocess method.
* We should consider to refactor this method to reuse part of the code with
* another one.
*/
public Response preprocess(HttpRequest request) {
RESTEasyTracingLogger.initTracingSupport(providerFactory, request);
Response aborted = null;
RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
try {
final long totalTimestamp = tracingLogger.timestamp("PRE_MATCH_SUMMARY");
for (HttpRequestPreprocessor preprocessor : this.requestPreprocessors) {
final long timestamp = tracingLogger.timestamp("PRE_MATCH");
preprocessor.preProcess(request);
tracingLogger.logDuration("PRE_MATCH", timestamp, preprocessor.getClass().toString());
}
tracingLogger.logDuration("PRE_MATCH_SUMMARY", totalTimestamp, this.requestPreprocessors.size());
ContainerRequestFilter[] requestFilters = providerFactory.getContainerRequestFilterRegistry().preMatch();
// FIXME: support async
PreMatchContainerRequestContext requestContext = new PreMatchContainerRequestContext(request, requestFilters, null);
aborted = requestContext.filter();
} catch (Exception e) {
// logger.error("Failed in preprocess, mapping exception", e);
aborted = new ExceptionHandler(providerFactory, unwrappedExceptions).handleException(request, e);
}
return aborted;
}
Aggregations