Search in sources :

Example 16 with HttpRequest

use of org.apache.http.HttpRequest in project oap by oaplatform.

the class BlockingHandlerAdapter method handle.

@Override
public void handle(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws IOException {
    log.trace("Handling [{}]", httpRequest);
    final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HTTP_CONNECTION);
    final InetAddress remoteAddress = connection.getRemoteAddress();
    final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
    final Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
    RequestCors cors = corsPolicy.getCors(request);
    final Response response = new Response(httpResponse, cors);
    if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
        response.respond(HTTP_FORBIDDEN);
    } else if (cors.autoOptions && request.httpMethod == Request.HttpMethod.OPTIONS) {
        response.respond(NO_CONTENT);
    } else {
        handler.handle(request, response);
    }
}
Also used : HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) RequestCors(oap.http.cors.RequestCors) HttpRequest(org.apache.http.HttpRequest) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Example 17 with HttpRequest

use of org.apache.http.HttpRequest in project oap by oaplatform.

the class NioHandlerAdapter method handle.

@Override
public void handle(final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange, final HttpContext httpContext) throws HttpException, IOException {
    LOGGER.trace("handling [{}]", httpRequest);
    final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HttpCoreContext.HTTP_CONNECTION);
    final InetAddress remoteAddress = connection.getRemoteAddress();
    final HttpResponse response = httpAsyncExchange.getResponse();
    final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
    if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
        response.setStatusCode(HTTP_FORBIDDEN);
    } else {
        Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
        handler.handle(request, new Response(response, corsPolicy.getCors(request)));
    }
    httpAsyncExchange.submitResponse();
}
Also used : Context(oap.http.Context) HttpCoreContext(org.apache.http.protocol.HttpCoreContext) HttpContext(org.apache.http.protocol.HttpContext) Response(oap.http.Response) HttpResponse(org.apache.http.HttpResponse) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(org.apache.http.HttpResponse) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Example 18 with HttpRequest

use of org.apache.http.HttpRequest in project jaeger-client-java by jaegertracing.

the class TracingRequestInterceptorTest method testProcessNullScope.

@Test
public void testProcessNullScope() throws Exception {
    ScopeManager mockScopeManager = Mockito.mock(ScopeManager.class);
    when(mockScopeManager.active()).thenReturn(null);
    Tracer mockTracer = Mockito.mock(Tracer.class);
    when(mockTracer.scopeManager()).thenReturn(mockScopeManager);
    HttpRequestInterceptor interceptor = new TracingRequestInterceptor(mockTracer);
    PowerMockito.spy(interceptor);
    HttpRequest mockRequest = Mockito.mock(HttpRequest.class);
    HttpContext mockContext = Mockito.mock(HttpContext.class);
    interceptor.process(mockRequest, mockContext);
    PowerMockito.verifyPrivate(interceptor, times(0)).invoke("onSpanStarted", any(Span.class), mockRequest, mockContext);
}
Also used : HttpRequest(org.apache.http.HttpRequest) ScopeManager(io.opentracing.ScopeManager) Tracer(io.opentracing.Tracer) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) Span(io.opentracing.Span) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with HttpRequest

use of org.apache.http.HttpRequest in project oap by oaplatform.

the class SecurityInterceptorTest method testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent.

@Test
public void testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent() throws UnknownHostException {
    final Reflection.Method methodWithAnnotation = REFLECTION.method(method -> method.name().equals("methodWithAnnotation")).get();
    final Context context = new Context("/", InetAddress.getLocalHost(), Protocol.HTTP.name());
    final String tokenId = UUID.randomUUID().toString();
    final HttpRequest httpRequest = new HttpGet();
    httpRequest.setHeader("Authorization", tokenId);
    httpRequest.setHeader("Host", "localhost");
    final Request request = new Request(httpRequest, context);
    final User user = new DefaultUser(Role.ADMIN, "testOrg", "test@example.com");
    final Token token = new Token();
    token.user = new DefaultUser(user);
    token.id = tokenId;
    token.created = DateTime.now();
    when(mockTokenService.getToken(tokenId)).thenReturn(Optional.of(token));
    final Session session = new Session();
    final Optional<HttpResponse> httpResponse = securityInterceptor.intercept(request, session, methodWithAnnotation, p -> null);
    assertFalse(httpResponse.isPresent());
    assertNotNull(session.get("user"));
}
Also used : Context(oap.http.Context) Reflection(oap.reflect.Reflection) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Assert.assertNotNull(org.testng.Assert.assertNotNull) HttpRequest(org.apache.http.HttpRequest) UnknownHostException(java.net.UnknownHostException) Request(oap.http.Request) Reflect(oap.reflect.Reflect) InetAddress(java.net.InetAddress) HttpGet(org.apache.http.client.methods.HttpGet) Session(oap.http.Session) Optional(java.util.Optional) Protocol(oap.http.Protocol) Assert.assertFalse(org.testng.Assert.assertFalse) HttpResponse(oap.http.HttpResponse) Mockito.mock(org.mockito.Mockito.mock) Context(oap.http.Context) HttpRequest(org.apache.http.HttpRequest) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(oap.http.HttpResponse) Reflection(oap.reflect.Reflection) Session(oap.http.Session) Test(org.testng.annotations.Test)

Example 20 with HttpRequest

use of org.apache.http.HttpRequest in project wso2-synapse by wso2.

the class ProxyTunnelHandler method generateRequest.

public HttpRequest generateRequest(final HttpContext context) throws IOException, HttpException {
    HttpHost target = this.route.getTargetHost();
    HttpRequest connect = new BasicHttpRequest("CONNECT", target.toHostString(), HttpVersion.HTTP_1_1);
    connect.setHeader(HttpHeaders.HOST, target.toHostString());
    this.httpProcessor.process(connect, context);
    context.setAttribute(SynapseHTTPRequestFactory.ENDPOINT_URL, target.toString());
    return connect;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) HttpHost(org.apache.http.HttpHost) BasicHttpRequest(org.apache.http.message.BasicHttpRequest)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14