Search in sources :

Example 11 with HttpRequest

use of org.apache.http.HttpRequest in project uavstack by uavorg.

the class ApacheHttpClientAdapter method afterPreCap.

@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
    if (args.length < 2) {
        return;
    }
    /**
     * after precap the client's span is created, set the span meta into http request header
     */
    String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
    Span span = this.spanFactory.getSpanFromContext(url);
    String spanMeta = this.spanFactory.getSpanMeta(span);
    HttpRequest request = (HttpRequest) args[1];
    request.removeHeaders(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
    request.addHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
    handleSlowOperSupporter(request, span, context);
}
Also used : HttpRequest(org.apache.http.HttpRequest) Span(com.creditease.uav.apm.invokechain.span.Span)

Example 12 with HttpRequest

use of org.apache.http.HttpRequest in project SEPA by arces-wot.

the class SPARQL11Handler method handle.

@Override
public void handle(HttpRequest request, HttpAsyncExchange httpExchange, HttpContext context) throws HttpException, IOException {
    Instant start = Instant.now();
    // CORS
    if (!corsHandling(httpExchange)) {
        jmx.corsFailed();
        return;
    }
    // Parsing SPARQL 1.1 request and attach a token
    Request sepaRequest = parse(httpExchange);
    // Parsing failed
    if (sepaRequest == null) {
        logger.error("Parsing failed: " + httpExchange.getRequest());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_BAD_REQUEST, "Parsing failed: " + httpExchange.getRequest());
        jmx.parsingFailed();
        return;
    }
    // Validate
    if (!validate(httpExchange.getRequest())) {
        logger.error("Validation failed SPARQL: " + sepaRequest.getSPARQL());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_BAD_REQUEST, "Validation failed SPARQL: " + sepaRequest.getSPARQL());
        jmx.validatingFailed();
        return;
    }
    // Authorize
    if (!authorize(httpExchange.getRequest())) {
        logger.error("Authorization failed SPARQL: " + sepaRequest.getSPARQL());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_UNAUTHORIZED, "Authorization failed SPARQL: " + sepaRequest.getSPARQL());
        jmx.authorizingFailed();
        return;
    }
    // Schedule request
    scheduler.schedule(sepaRequest, new SPARQL11ResponseHandler(httpExchange, jmx, start));
}
Also used : Instant(java.time.Instant) Request(it.unibo.arces.wot.sepa.commons.request.Request) HttpRequest(org.apache.http.HttpRequest)

Example 13 with HttpRequest

use of org.apache.http.HttpRequest in project pact-jvm by DiUS.

the class HttpTarget method getProviderInfo.

protected ProviderInfo getProviderInfo() {
    Provider provider = testClass.getAnnotation(Provider.class);
    final ProviderInfo providerInfo = new ProviderInfo(provider.value());
    providerInfo.setPort(port);
    providerInfo.setHost(host);
    providerInfo.setProtocol(protocol);
    providerInfo.setPath(path);
    providerInfo.setInsecure(insecure);
    if (testClass != null) {
        final List<FrameworkMethod> methods = testClass.getAnnotatedMethods(TargetRequestFilter.class);
        if (!methods.isEmpty()) {
            providerInfo.setRequestFilter((Consumer<HttpRequest>) httpRequest -> methods.forEach(method -> {
                try {
                    method.invokeExplosively(testTarget, httpRequest);
                } catch (Throwable t) {
                    throw new AssertionError("Request filter method " + method.getName() + " failed with an exception", t);
                }
            }));
        }
    }
    return providerInfo;
}
Also used : HttpRequest(org.apache.http.HttpRequest) SystemPropertyResolver(au.com.dius.pact.provider.junit.sysprops.SystemPropertyResolver) FrameworkMethod(org.junit.runners.model.FrameworkMethod) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) ValueResolver(au.com.dius.pact.provider.junit.sysprops.ValueResolver) URL(java.net.URL) HashMap(java.util.HashMap) Interaction(au.com.dius.pact.model.Interaction) Provider(au.com.dius.pact.provider.junit.Provider) HttpRequest(org.apache.http.HttpRequest) Consumer(java.util.function.Consumer) List(java.util.List) ProviderVerifier(au.com.dius.pact.provider.ProviderVerifier) TestClass(org.junit.runners.model.TestClass) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) Map(java.util.Map) TargetRequestFilter(au.com.dius.pact.provider.junit.TargetRequestFilter) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Provider(au.com.dius.pact.provider.junit.Provider)

Example 14 with HttpRequest

use of org.apache.http.HttpRequest in project pact-jvm by DiUS.

the class MockMvcTarget method getProviderInfo.

@Override
protected ProviderInfo getProviderInfo() {
    Provider provider = testClass.getAnnotation(Provider.class);
    final ProviderInfo providerInfo = new ProviderInfo(provider.value());
    if (testClass != null) {
        final List<FrameworkMethod> methods = testClass.getAnnotatedMethods(TargetRequestFilter.class);
        if (!methods.isEmpty()) {
            providerInfo.setRequestFilter((Consumer<HttpRequest>) httpRequest -> methods.forEach(method -> {
                try {
                    method.invokeExplosively(testTarget, httpRequest);
                } catch (Throwable t) {
                    throw new AssertionError("Request filter method " + method.getName() + " failed with an exception", t);
                }
            }));
        }
    }
    return providerInfo;
}
Also used : HttpRequest(org.apache.http.HttpRequest) java.util(java.util) FrameworkMethod(org.junit.runners.model.FrameworkMethod) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) URL(java.net.URL) MvcProviderVerifier(au.com.dius.pact.provider.spring.MvcProviderVerifier) MockMvcBuilders.standaloneSetup(org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup) PactVerification(au.com.dius.pact.provider.PactVerification) BaseTarget(au.com.dius.pact.provider.junit.target.BaseTarget) Interaction(au.com.dius.pact.model.Interaction) Provider(au.com.dius.pact.provider.junit.Provider) HttpRequest(org.apache.http.HttpRequest) MockMvc(org.springframework.test.web.servlet.MockMvc) Consumer(java.util.function.Consumer) URLClassLoader(java.net.URLClassLoader) ProviderVerifier(au.com.dius.pact.provider.ProviderVerifier) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) RequestResponseInteraction(au.com.dius.pact.model.RequestResponseInteraction) TargetRequestFilter(au.com.dius.pact.provider.junit.TargetRequestFilter) Target(au.com.dius.pact.provider.junit.target.Target) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Provider(au.com.dius.pact.provider.junit.Provider)

Example 15 with HttpRequest

use of org.apache.http.HttpRequest in project opennms by OpenNMS.

the class HttpClientWrapper method enablePreemptiveAuth.

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**
     * Add an HttpRequestInterceptor that will perform preemptive authentication
     * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
     */
    final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context) throws IOException {
            if (context instanceof HttpClientContext) {
                final HttpClientContext clientContext = (HttpClientContext) context;
                final AuthState authState = clientContext.getTargetAuthState();
                final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
                final HttpHost targetHost = clientContext.getTargetHost();
                // If not authentication scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    final Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.update(new BasicScheme(), creds);
                    }
                }
            } else {
                throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
            }
        }
    };
    builder.addInterceptorFirst(preemptiveAuth);
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

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