Search in sources :

Example 6 with RequestLine

use of org.apache.http.RequestLine in project indy by Commonjava.

the class ProxyResponseWriter method handleEvent.

@Override
public void handleEvent(final ConduitStreamSinkChannel channel) {
    HttpConduitWrapper http = new HttpConduitWrapper(channel, httpRequest, contentController, cacheProvider);
    if (httpRequest == null) {
        if (error != null) {
            logger.debug("handling error from request reader: " + error.getMessage(), error);
            handleError(error, http);
        } else {
            logger.debug("invalid state (no error or request) from request reader. Sending 400.");
            try {
                http.writeStatus(ApplicationStatus.BAD_REQUEST);
            } catch (final IOException e) {
                logger.error("Failed to write BAD REQUEST for missing HTTP first-line to response channel.", e);
            }
        }
        return;
    }
    // TODO: Can we handle this?
    final String oldThreadName = Thread.currentThread().getName();
    Thread.currentThread().setName("PROXY-" + httpRequest.getRequestLine().toString());
    channel.getCloseSetter().set((sinkChannel) -> {
        logger.debug("sink channel closing.");
        Thread.currentThread().setName(oldThreadName);
    });
    logger.info("\n\n\n>>>>>>> Handle write\n\n\n\n\n");
    if (error == null) {
        try {
            final UserPass proxyUserPass = UserPass.parse(ApplicationHeader.proxy_authorization, httpRequest, null);
            logger.debug("Proxy UserPass: {}\nConfig secured? {}\nConfig tracking type: {}", proxyUserPass, config.isSecured(), config.getTrackingType());
            if (proxyUserPass == null && (config.isSecured() || TrackingType.ALWAYS == config.getTrackingType())) {
                http.writeStatus(ApplicationStatus.PROXY_AUTHENTICATION_REQUIRED);
                http.writeHeader(ApplicationHeader.proxy_authenticate, String.format(PROXY_AUTHENTICATE_FORMAT, config.getProxyRealm()));
            } else {
                RequestLine requestLine = httpRequest.getRequestLine();
                String method = requestLine.getMethod().toUpperCase();
                switch(method) {
                    case GET_METHOD:
                    case HEAD_METHOD:
                        {
                            if (proxyUserPass != null) {
                                logger.debug("Passing BASIC authentication credentials to Keycloak bearer-token translation authenticator");
                                if (!proxyAuthenticator.authenticate(proxyUserPass, http)) {
                                    break;
                                }
                            }
                            final URL url = new URL(requestLine.getUri());
                            final RemoteRepository repo = getRepository(url);
                            transfer(http, repo, url.getPath(), GET_METHOD.equals(method), proxyUserPass);
                            break;
                        }
                    case OPTIONS_METHOD:
                        {
                            http.writeStatus(ApplicationStatus.OK);
                            http.writeHeader(ApplicationHeader.allow, ALLOW_HEADER_VALUE);
                            break;
                        }
                    default:
                        {
                            http.writeStatus(ApplicationStatus.METHOD_NOT_ALLOWED);
                        }
                }
            }
            logger.info("Response complete.");
        } catch (final Throwable e) {
            error = e;
        }
    }
    if (error != null) {
        handleError(error, http);
    }
    try {
        http.close();
    } catch (final IOException e) {
        logger.error("Failed to flush/shutdown response.", e);
    }
}
Also used : RequestLine(org.apache.http.RequestLine) HttpConduitWrapper(org.commonjava.indy.httprox.util.HttpConduitWrapper) UserPass(org.commonjava.indy.subsys.http.util.UserPass) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) IOException(java.io.IOException) URL(java.net.URL)

Example 7 with RequestLine

use of org.apache.http.RequestLine in project cdap-ingest by caskdata.

the class StreamConfigHttpRequestHandler method handle.

@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
    RequestLine requestLine = httpRequest.getRequestLine();
    String method = requestLine.getMethod();
    int statusCode;
    if (!HttpMethod.PUT.equals(method)) {
        statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
    } else {
        String uri = requestLine.getUri();
        String streamName = TestUtils.getStreamNameFromUri(uri);
        if (TestUtils.SUCCESS_STREAM_NAME.equals(streamName)) {
            statusCode = HttpStatus.SC_BAD_REQUEST;
            BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) httpRequest;
            HttpEntity requestEntity = request.getEntity();
            if (requestEntity != null) {
                JsonObject jsonContent = RestClient.toJsonObject(requestEntity);
                if (jsonContent != null) {
                    long ttl = jsonContent.get(TTL_ATTRIBUTE_NAME).getAsLong();
                    if (ttl == RestTest.STREAM_TTL) {
                        statusCode = HttpStatus.SC_OK;
                    }
                }
            }
        } else if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
            statusCode = TestUtils.authorize(httpRequest);
        } else {
            statusCode = TestUtils.getStatusCodeByStreamName(streamName);
        }
    }
    response.setStatusCode(statusCode);
}
Also used : RequestLine(org.apache.http.RequestLine) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest)

Example 8 with RequestLine

use of org.apache.http.RequestLine in project cdap-ingest by caskdata.

the class StreamHttpRequestHandler method handle.

@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
    RequestLine requestLine = httpRequest.getRequestLine();
    String method = requestLine.getMethod();
    int statusCode;
    String uri = requestLine.getUri();
    String streamName = TestUtils.getStreamNameFromUri(uri + "/");
    if (HttpMethod.PUT.equals(method)) {
        if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
            statusCode = TestUtils.authorize(httpRequest);
        } else {
            statusCode = TestUtils.getStatusCodeByStreamName(streamName);
        }
    } else if (HttpMethod.POST.equals(method)) {
        String fullStreamName = streamName;
        streamName = streamName.replace(TestUtils.WRITER_TEST_STREAM_NAME_POSTFIX, StringUtils.EMPTY);
        if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
            statusCode = TestUtils.authorize(httpRequest);
        } else if (TestUtils.WITH_CUSTOM_HEADER_STREAM_NAME.endsWith(streamName)) {
            Header testHeader = httpRequest.getFirstHeader(fullStreamName + "." + RestTest.TEST_HEADER_NAME);
            if (testHeader != null && RestTest.TEST_HEADER_VALUE.equals(testHeader.getValue())) {
                statusCode = HttpStatus.SC_OK;
            } else {
                statusCode = HttpStatus.SC_BAD_REQUEST;
            }
        } else {
            statusCode = TestUtils.getStatusCodeByStreamName(streamName);
        }
        if (HttpStatus.SC_OK == statusCode && !TestUtils.FILE_STREAM_NAME.equals(streamName)) {
            //check request content
            BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) httpRequest;
            HttpEntity requestEntity = request.getEntity();
            if (requestEntity != null) {
                String content = RestClient.toString(requestEntity);
                if (!RestTest.EXPECTED_WRITER_CONTENT.equals(content) && !TestUtils.ALLOW_ANY_EVENT_STREAM.equals(streamName)) {
                    statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
                }
            } else {
                statusCode = HttpStatus.SC_BAD_REQUEST;
            }
        }
    } else {
        statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
    }
    response.setStatusCode(statusCode);
}
Also used : RequestLine(org.apache.http.RequestLine) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest)

Example 9 with RequestLine

use of org.apache.http.RequestLine in project elasticsearch by elastic.

the class FailureTrackingResponseListenerTests method mockResponse.

private static Response mockResponse() {
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
    StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "OK");
    HttpResponse httpResponse = new BasicHttpResponse(statusLine);
    return new Response(requestLine, new HttpHost("localhost", 9200), httpResponse);
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicRequestLine(org.apache.http.message.BasicRequestLine) RequestLine(org.apache.http.RequestLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicRequestLine(org.apache.http.message.BasicRequestLine) HttpHost(org.apache.http.HttpHost) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 10 with RequestLine

use of org.apache.http.RequestLine in project okhttp by square.

the class OkApacheClient method transformRequest.

private static Request transformRequest(HttpRequest request) {
    Request.Builder builder = new Request.Builder();
    RequestLine requestLine = request.getRequestLine();
    String method = requestLine.getMethod();
    builder.url(requestLine.getUri());
    String contentType = null;
    for (Header header : request.getAllHeaders()) {
        String name = header.getName();
        if ("Content-Type".equalsIgnoreCase(name)) {
            contentType = header.getValue();
        } else {
            builder.header(name, header.getValue());
        }
    }
    RequestBody body = null;
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity != null) {
            // Wrap the entity in a custom Body which takes care of the content, length, and type.
            body = new HttpEntityBody(entity, contentType);
            Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                builder.header(encoding.getName(), encoding.getValue());
            }
        } else {
            body = Util.EMPTY_REQUEST;
        }
    }
    builder.method(method, body);
    return builder.build();
}
Also used : RequestLine(org.apache.http.RequestLine) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) Request(okhttp3.Request) HttpRequest(org.apache.http.HttpRequest) RequestBody(okhttp3.RequestBody)

Aggregations

RequestLine (org.apache.http.RequestLine)13 HttpEntity (org.apache.http.HttpEntity)5 HttpHost (org.apache.http.HttpHost)4 HttpResponse (org.apache.http.HttpResponse)4 ProtocolVersion (org.apache.http.ProtocolVersion)4 StatusLine (org.apache.http.StatusLine)4 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)4 BasicRequestLine (org.apache.http.message.BasicRequestLine)4 BasicStatusLine (org.apache.http.message.BasicStatusLine)4 ConnectionClosedException (org.apache.http.ConnectionClosedException)3 Header (org.apache.http.Header)3 StringEntity (org.apache.http.entity.StringEntity)3 ParserCursor (org.apache.http.message.ParserCursor)3 IOException (java.io.IOException)2 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URL (java.net.URL)1