Search in sources :

Example 6 with ResteasyUriInfo

use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project policies-ui-backend by RedHatInsights.

the class PagingUtilsTest method extractPagerInvalidOffset.

@Test
void extractPagerInvalidOffset() throws URISyntaxException {
    UriInfo info = new ResteasyUriInfo(new URI("https://foo?offset=bar&limit=100"));
    assertThrows(IllegalArgumentException.class, () -> {
        PagingUtils.extractPager(info);
    });
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) URI(java.net.URI) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.jupiter.api.Test)

Example 7 with ResteasyUriInfo

use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.

the class HttpExchangeUtil method extractUriInfo.

public static ResteasyUriInfo extractUriInfo(HttpExchange exchange) {
    String host = exchange.getLocalAddress().getHostName();
    if (exchange.getLocalAddress().getPort() != 80 && exchange.getLocalAddress().getPort() != 443) {
        host += ":" + exchange.getLocalAddress().getPort();
    }
    String uri = exchange.getRequestURI().toString();
    String protocol = exchange.getHttpContext().getServer() instanceof HttpsServer ? "https" : "http";
    URI absoluteURI = URI.create(protocol + "://" + host + uri);
    String contextPath = exchange.getHttpContext().getPath();
    String path = PathHelper.getEncodedPathInfo(absoluteURI.getRawPath(), contextPath);
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    URI baseURI = absoluteURI;
    if (!path.trim().equals("")) {
        String tmpContextPath = contextPath;
        if (!tmpContextPath.endsWith("/"))
            tmpContextPath += "/";
        baseURI = UriBuilder.fromUri(absoluteURI).replacePath(tmpContextPath).replaceQuery(null).build();
    } else {
        baseURI = UriBuilder.fromUri(absoluteURI).replaceQuery(null).build();
    }
    URI relativeURI = UriBuilder.fromUri(path).replaceQuery(absoluteURI.getRawQuery()).build();
    // System.out.println("path: " + path);
    // System.out.println("query string: " + request.getQueryString());
    ResteasyUriInfo uriInfo = new ResteasyUriInfo(baseURI, relativeURI);
    return uriInfo;
}
Also used : ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) HttpsServer(com.sun.net.httpserver.HttpsServer) URI(java.net.URI)

Example 8 with ResteasyUriInfo

use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.

the class VertxRequestHandler method handle.

@Override
public void handle(HttpServerRequest request) {
    request.bodyHandler(buff -> {
        Context ctx = vertx.getOrCreateContext();
        ResteasyUriInfo uriInfo = VertxUtil.extractUriInfo(request, servletMappingPrefix);
        HttpServerResponse response = request.response();
        VertxHttpResponse vertxResponse = new VertxHttpResponse(response, dispatcher.getProviderFactory(), request.method());
        VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, request, uriInfo, dispatcher.getDispatcher(), vertxResponse, false);
        if (buff.length() > 0) {
            ByteBufInputStream in = new ByteBufInputStream(buff.getByteBuf());
            vertxRequest.setInputStream(in);
        }
        try {
            dispatcher.service(ctx, request, response, vertxRequest, vertxResponse, true);
        } catch (Failure e1) {
            vertxResponse.setStatus(e1.getErrorCode());
        } catch (Exception ex) {
            vertxResponse.setStatus(500);
            LogMessages.LOGGER.error(Messages.MESSAGES.unexpected(), ex);
        }
        if (!vertxRequest.getAsyncContext().isSuspended()) {
            try {
                vertxResponse.finish();
            } catch (IOException e) {
                LogMessages.LOGGER.error(Messages.MESSAGES.unexpected(), e);
            }
        }
    });
}
Also used : Context(io.vertx.core.Context) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) HttpServerResponse(io.vertx.core.http.HttpServerResponse) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) Failure(org.jboss.resteasy.spi.Failure) IOException(java.io.IOException)

Example 9 with ResteasyUriInfo

use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.

the class ServletUtilTest method extractUriInfo_encoded.

@Test
public void extractUriInfo_encoded() {
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getContextPath()).thenReturn("");
    when(request.getQueryString()).thenReturn(null);
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource!"));
    ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
    assertEquals("", rui.getContextPath());
    assertEquals("/app/resource!", rui.getPath());
    request = mock(HttpServletRequest.class);
    when(request.getContextPath()).thenReturn("");
    when(request.getQueryString()).thenReturn(null);
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource%21"));
    rui = ServletUtil.extractUriInfo(request, null);
    assertEquals("", rui.getContextPath());
    assertEquals("/app/resource!", rui.getPath());
    request = mock(HttpServletRequest.class);
    when(request.getContextPath()).thenReturn("");
    when(request.getQueryString()).thenReturn(null);
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app!/resource"));
    rui = ServletUtil.extractUriInfo(request, null);
    assertEquals("", rui.getContextPath());
    assertEquals("/app!/resource", rui.getPath());
    request = mock(HttpServletRequest.class);
    when(request.getContextPath()).thenReturn("");
    when(request.getQueryString()).thenReturn(null);
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app%21/resource"));
    rui = ServletUtil.extractUriInfo(request, null);
    assertEquals("", rui.getContextPath());
    assertEquals("/app!/resource", rui.getPath());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) Test(org.junit.Test)

Example 10 with ResteasyUriInfo

use of org.jboss.resteasy.specimpl.ResteasyUriInfo in project resteasy by resteasy.

the class ServletUtilTest method extractUriInfo_simple.

@Test
public void extractUriInfo_simple() {
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getContextPath()).thenReturn("");
    when(request.getQueryString()).thenReturn(null);
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8000/app/resource"));
    ResteasyUriInfo rui = ServletUtil.extractUriInfo(request, null);
    assertEquals("", rui.getContextPath());
    assertEquals("/app/resource", rui.getPath());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ResteasyUriInfo(org.jboss.resteasy.specimpl.ResteasyUriInfo) Test(org.junit.Test)

Aggregations

ResteasyUriInfo (org.jboss.resteasy.specimpl.ResteasyUriInfo)42 URI (java.net.URI)19 UriInfo (javax.ws.rs.core.UriInfo)14 Test (org.junit.jupiter.api.Test)14 Pager (com.redhat.cloud.policies.app.model.pager.Pager)8 Test (org.junit.Test)6 ResteasyHttpHeaders (org.jboss.resteasy.specimpl.ResteasyHttpHeaders)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 NotFoundException (jakarta.ws.rs.NotFoundException)4 IOException (java.io.IOException)3 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)2 Context (io.vertx.core.Context)2 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 PathSegment (jakarta.ws.rs.core.PathSegment)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 HttpRequest (org.jboss.resteasy.spi.HttpRequest)2 HttpResponse (org.jboss.resteasy.spi.HttpResponse)2