Search in sources :

Example 1 with PreMatchContainerRequestContext

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);
}
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 PreMatchContainerRequestContext

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));
    }
}
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 PreMatchContainerRequestContext

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));
    }
}
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 PreMatchContainerRequestContext

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());
}
Also used : MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) URI(java.net.URI) Test(org.junit.Test)

Example 5 with PreMatchContainerRequestContext

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;
}
Also used : Response(jakarta.ws.rs.core.Response) HttpResponse(org.jboss.resteasy.spi.HttpResponse) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) HttpRequestPreprocessor(org.jboss.resteasy.spi.HttpRequestPreprocessor) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) NotFoundException(jakarta.ws.rs.NotFoundException) UnhandledException(org.jboss.resteasy.spi.UnhandledException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) InternalServerErrorException(org.jboss.resteasy.spi.InternalServerErrorException)

Aggregations

PreMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext)7 MediaType (jakarta.ws.rs.core.MediaType)4 AcceptParameterHttpPreprocessor (org.jboss.resteasy.core.AcceptParameterHttpPreprocessor)4 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)4 Test (org.junit.Test)4 HttpRequest (org.jboss.resteasy.spi.HttpRequest)3 NotFoundException (jakarta.ws.rs.NotFoundException)2 ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)2 Response (jakarta.ws.rs.core.Response)2 IOException (java.io.IOException)2 CompletionException (java.util.concurrent.CompletionException)2 BuiltResponse (org.jboss.resteasy.specimpl.BuiltResponse)2 HttpRequestPreprocessor (org.jboss.resteasy.spi.HttpRequestPreprocessor)2 HttpResponse (org.jboss.resteasy.spi.HttpResponse)2 InternalServerErrorException (org.jboss.resteasy.spi.InternalServerErrorException)2 UnhandledException (org.jboss.resteasy.spi.UnhandledException)2 RESTEasyTracingLogger (org.jboss.resteasy.tracing.RESTEasyTracingLogger)2 ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)1 ResourceContext (jakarta.ws.rs.container.ResourceContext)1 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)1