Search in sources :

Example 1 with Response

use of org.simpleframework.http.Response in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method startServer.

@BeforeClass
public static void startServer() throws IOException {
    socketConnection = new SocketConnection(new Container() {

        public void handle(Request req, Response resp) {
            try {
                if (req.getPath().getPath().contains("/redirect/")) {
                    resp.setCode(303);
                    resp.add("Location", "/");
                } else {
                    if (req.getPath().getPath().contains("/timeout/")) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                    if (req.getPath().getPath().contains("/gzip/")) {
                        if (!"gzip".equals(req.getValue("Accept-Encoding"))) {
                            throw new IllegalStateException("Should accept gzip");
                        }
                        resp.set("Content-Encoding", "gzip");
                        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
                        gzipOutputStream.write("GZIP response".getBytes());
                        gzipOutputStream.close();
                    } else {
                        resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
                    }
                }
            } catch (IOException e) {
                throw new IllegalStateException(e);
            } finally {
                try {
                    resp.close();
                } catch (IOException ignored) {
                }
            }
        }
    });
    SocketAddress address = socketConnection.connect(new InetSocketAddress("localhost", 0));
    baseUrl = String.format("http://%s:%d", ((InetSocketAddress) address).getAddress().getHostAddress(), ((InetSocketAddress) address).getPort());
}
Also used : Response(org.simpleframework.http.Response) Container(org.simpleframework.http.core.Container) SocketConnection(org.simpleframework.transport.connect.SocketConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) InetSocketAddress(java.net.InetSocketAddress) Request(org.simpleframework.http.Request) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) BeforeClass(org.junit.BeforeClass)

Example 2 with Response

use of org.simpleframework.http.Response in project jersey by jersey.

the class SimpleContainer method handle.

@Override
public void handle(final Request request, final Response response) {
    final ResponseWriter responseWriter = new ResponseWriter(response, scheduler);
    final URI baseUri = getBaseUri(request);
    final URI requestUri = getRequestUri(request, baseUri);
    try {
        final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, request.getMethod(), getSecurityContext(request), new MapPropertiesDelegate());
        requestContext.setEntityStream(request.getInputStream());
        for (final String headerName : request.getNames()) {
            requestContext.headers(headerName, request.getValue(headerName));
        }
        requestContext.setWriter(responseWriter);
        requestContext.setRequestScopedInitializer(injectionManager -> {
            injectionManager.<Ref<Request>>getInstance(RequestTYPE).set(request);
            injectionManager.<Ref<Response>>getInstance(ResponseTYPE).set(response);
        });
        appHandler.handle(requestContext);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (!responseWriter.isSuspended()) {
            close(response);
        }
    }
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(org.simpleframework.http.Response) MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) Request(org.simpleframework.http.Request) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ContainerException(org.glassfish.jersey.server.ContainerException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 Request (org.simpleframework.http.Request)2 Response (org.simpleframework.http.Response)2 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)1 ContainerException (org.glassfish.jersey.server.ContainerException)1 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)1 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)1 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)1 BeforeClass (org.junit.BeforeClass)1 Container (org.simpleframework.http.core.Container)1 SocketConnection (org.simpleframework.transport.connect.SocketConnection)1