org.eclipse.jetty.client.api.Request Java Examples

The following examples show how to use org.eclipse.jetty.client.api.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestContentListener.java    From logbook-kai with MIT License 6 votes vote down vote up
@Override
public void onContent(Request request, ByteBuffer buffer) {
    int length = buffer.remaining();

    if (((length > 0) && (length <= Filter.MAX_POST_FIELD_SIZE))) {
        byte[] bytes = new byte[length];
        buffer.get(bytes);

        CaptureHolder holder = (CaptureHolder) this.httpRequest.getAttribute(Filter.CONTENT_HOLDER);
        if (holder == null) {
            holder = new CaptureHolder();
            this.httpRequest.setAttribute(Filter.CONTENT_HOLDER, holder);
        }
        holder.putRequest(bytes);
    }
}
 
Example #2
Source File: JettyResponseListener.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Abort the request/response. The request is associated with the http
 * request/response is aborted. If we already have the response, then it's
 * {@link InputStream} is closed.
 * 
 * @param cause
 *            The cause (required).
 */
public void abort(final Throwable cause) {

	final InputStream is = m_cachedStream;
	if (is != null) {
		m_cachedStream = null;
		try {
			is.close();
		} catch (IOException ex) {
			log.warn(ex);
		}
	}

	final Request r = m_request;
	if (r != null) {
		m_request = null;
		r.abort(cause);
	}

}
 
Example #3
Source File: OAuthConnector.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
private void setAuthentication(@Nullable String clientId, @Nullable String clientSecret, Request request,
        Fields fields, boolean supportsBasicAuth) {
    logger.debug("Setting authentication for clientId {}. Using basic auth {}", clientId, supportsBasicAuth);
    if (supportsBasicAuth && clientSecret != null) {
        String authString = clientId + ":" + clientSecret;
        request.header(HttpHeader.AUTHORIZATION,
                "Basic " + Base64.getEncoder().encodeToString(authString.getBytes(StandardCharsets.UTF_8)));
    } else {
        if (clientId != null) {
            fields.add(CLIENT_ID, clientId);
        }
        if (clientSecret != null) {
            fields.add(CLIENT_SECRET, clientSecret);
        }
    }
}
 
Example #4
Source File: BloomFilter.java    From presto-bloomfilter with Apache License 2.0 6 votes vote down vote up
public static BloomFilter fromUrl(String url) throws Exception
{
    log.info("Loading bloom filter from " + url);

    Request request = BloomFilterScalarFunctions.HTTP_CLIENT.newRequest(url);
    request.method("GET");
    InputStreamResponseListener listener = new InputStreamResponseListener();
    request.send(listener);

    // Wait for the response headers to arrive
    Response response = listener.get(10, TimeUnit.SECONDS);

    // Look at the response
    if (response.getStatus() == 200) {
        // Use try-with-resources to close input stream.
        try (InputStream responseContent = listener.getInputStream()) {
            byte[] bytes = ByteStreams.toByteArray(responseContent);
            return newInstance(bytes);
        }
    }
    log.warn("Non-200 response status " + response.getStatus());
    return null;
}
 
Example #5
Source File: JettyClientMetrics.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Override
public void onQueued(Request request) {
    Timer.Sample sample = Timer.start(registry);

    request.onComplete(result -> {
        long requestLength = result.getRequest().getContent().getLength();
        Iterable<Tag> httpRequestTags = tagsProvider.httpRequestTags(result);
        if (requestLength >= 0) {
            DistributionSummary.builder(contentSizeMetricName)
                    .description("Content sizes for Jetty HTTP client requests")
                    .tags(httpRequestTags)
                    .register(registry)
                    .record(requestLength);
        }

        sample.stop(Timer.builder(timingMetricName)
                .description("Jetty HTTP client request timing")
                .tags(httpRequestTags)
                .register(registry));
    });
}
 
Example #6
Source File: OcJettyHttpClientExtractorTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtraction() {
  HttpFields fields = new HttpFields();
  fields.add(new HttpField("User-Agent", "Test 1.0"));

  Request request = mock(Request.class);
  Response response = mock(Response.class);
  OcJettyHttpClientExtractor extractor = new OcJettyHttpClientExtractor();
  when(request.getHost()).thenReturn("localhost");
  when(request.getMethod()).thenReturn("GET");
  when(request.getHeaders()).thenReturn(fields);
  when(request.getPath()).thenReturn("/test");
  when(request.getURI()).thenReturn(uri);
  when(response.getStatus()).thenReturn(0);

  assertThat(extractor.getHost(request)).contains("localhost");
  assertThat(extractor.getMethod(request)).contains("GET");
  assertThat(extractor.getPath(request)).contains("/test");
  assertThat(extractor.getUrl(request)).contains(URI_STR);
  assertThat(extractor.getRoute(request)).contains("");
  assertThat(extractor.getUserAgent(request)).contains("Test 1.0");
  assertThat(extractor.getStatusCode(response)).isEqualTo(0);
}
 
Example #7
Source File: WebClientLoggingIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
    SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
    org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) {
        @Override
        public Request newRequest(URI uri) {
            Request request = super.newRequest(uri);
            return enhance(request);
        }
    };

    WebClient
      .builder()
      .clientConnector(new JettyClientHttpConnector(httpClient))
      .build()
      .post()
      .uri(sampleUrl)
      .body(BodyInserters.fromObject(post))
      .retrieve()
      .bodyToMono(String.class)
      .block();

    verify(jettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
}
 
Example #8
Source File: JettyXhrTransport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, String body) {
	Request httpRequest = this.httpClient.newRequest(url).method(method);
	addHttpHeaders(httpRequest, headers);
	if (body != null) {
		httpRequest.content(new StringContentProvider(body));
	}
	ContentResponse response;
	try {
		response = httpRequest.send();
	}
	catch (Exception ex) {
		throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
	}
	HttpStatus status = HttpStatus.valueOf(response.getStatus());
	HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
	return (response.getContent() != null ?
		new ResponseEntity<String>(response.getContentAsString(), responseHeaders, status) :
		new ResponseEntity<String>(responseHeaders, status));
}
 
Example #9
Source File: OAuthConnector.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void setAuthentication(@Nullable String clientId, @Nullable String clientSecret, Request request,
        Fields fields, boolean supportsBasicAuth) {
    logger.debug("Setting authentication for clientId {}. Using basic auth {}", clientId, supportsBasicAuth);
    if (supportsBasicAuth && clientSecret != null) {
        String authString = clientId + ":" + clientSecret;
        request.header(HttpHeader.AUTHORIZATION,
                "Basic " + Base64.getEncoder().encodeToString(authString.getBytes(StandardCharsets.UTF_8)));
    } else {
        if (clientId != null) {
            fields.add(CLIENT_ID, clientId);
        }
        if (clientSecret != null) {
            fields.add(CLIENT_SECRET, clientSecret);
        }
    }
}
 
Example #10
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateX509CertificateInvalidStatus() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    Request request = Mockito.mock(Request.class);
    Mockito.when(httpClient.POST("https://localhost:443/certsign/v2/x509")).thenReturn(request);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(request.send()).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(400);

    assertNull(certSigner.generateX509Certificate("csr", null, 0));
    certSigner.close();
}
 
Example #11
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateX509CertificateResponseNull() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    Request request = Mockito.mock(Request.class);
    Mockito.when(httpClient.POST("https://localhost:443/certsign/v2/x509")).thenReturn(request);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(request.send()).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(201);
    Mockito.when(response.getContentAsString()).thenReturn(null);

    assertNull(certSigner.generateX509Certificate("csr", null, 0));
    certSigner.close();
}
 
Example #12
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateX509CertificateResponseEmpty() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    Request request = Mockito.mock(Request.class);
    Mockito.when(httpClient.POST("https://localhost:443/certsign/v2/x509")).thenReturn(request);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(request.send()).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(201);
    Mockito.when(response.getContentAsString()).thenReturn("");

    assertNull(certSigner.generateX509Certificate("csr", null, 0));
    certSigner.close();
}
 
Example #13
Source File: ProxyServletService.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the URI contains user info in the form <code>user[:pass]@</code>, attempt to preempt the server
 * returning a 401 by providing Basic Authentication support in the initial request to the server.
 *
 * @param uri the URI which may contain user info
 * @param request the outgoing request to which an authorization header may be added
 */
void maybeAppendAuthHeader(URI uri, Request request) {
    if (uri != null && uri.getUserInfo() != null) {
        String[] userInfo = uri.getUserInfo().split(":");

        if (userInfo.length >= 1) {
            String user = userInfo[0];
            String password = userInfo.length >= 2 ? userInfo[1] : null;
            String authString = password != null ? user + ":" + password : user + ":";

            String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1);
            request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
        }
    }
}
 
Example #14
Source File: ProxyServletServiceTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMaybeAppendAuthHeaderWithFullCredentials() throws URISyntaxException {
    Request request = mock(Request.class);
    URI uri = new URI("http://testuser:[email protected]:8080/content");
    service.maybeAppendAuthHeader(uri, request);
    verify(request).header(HttpHeader.AUTHORIZATION,
            "Basic " + B64Code.encode("testuser:testpassword", StringUtil.__ISO_8859_1));
}
 
Example #15
Source File: ZeppelinhubRestApiHandler.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private String sendToZeppelinHubWithoutResponseBody(Request request) throws IOException {
  request.send(new Response.CompleteListener() {
    @Override
    public void onComplete(Result result) {
      Request req = result.getRequest();
      LOG.info("ZeppelinHub {} {} returned with status {}: {}", req.getMethod(),
          req.getURI(), result.getResponse().getStatus(), result.getResponse().getReason());
    }
  });
  return StringUtils.EMPTY;
}
 
Example #16
Source File: BasicAuthPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean interceptInternodeRequest(Request request) {
  if (forwardCredentials) {
    Object userToken = request.getAttributes().get(Http2SolrClient.REQ_PRINCIPAL_KEY);
    if (userToken instanceof BasicAuthUserPrincipal) {
      BasicAuthUserPrincipal principal = (BasicAuthUserPrincipal) userToken;
      String userPassBase64 = Base64.encodeBase64String((principal.getName() + ":" + principal.getPassword()).getBytes(StandardCharsets.UTF_8));
      request.header(HttpHeaders.AUTHORIZATION, "Basic " + userPassBase64);
      return true;
    }
  }
  return false;
}
 
Example #17
Source File: KerberosPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(Http2SolrClient client) {
  final HttpListenerFactory.RequestResponseListener listener = new HttpListenerFactory.RequestResponseListener() {
    @Override
    public void onQueued(Request request) {
      interceptInternodeRequest(request);
    }
  };
  client.addListenerFactory(() -> listener);

  kerberosBuilder.setup(client);
}
 
Example #18
Source File: JettyConnectionMetricsTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void contributesClientConnectorMetrics() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.setFollowRedirects(false);
    httpClient.addBean(new JettyConnectionMetrics(registry));

    CountDownLatch latch = new CountDownLatch(1);
    httpClient.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
        @Override
        public void lifeCycleStopped(LifeCycle event) {
            latch.countDown();
        }
    });

    httpClient.start();

    Request post = httpClient.POST("http://localhost:" + connector.getLocalPort());
    post.content(new StringContentProvider("123456"));
    post.send();
    httpClient.stop();

    assertTrue(latch.await(10, SECONDS));
    assertThat(registry.get("jetty.connections.max").gauge().value()).isEqualTo(1.0);
    assertThat(registry.get("jetty.connections.request").tag("type", "client").timer().count())
            .isEqualTo(1);
    assertThat(registry.get("jetty.connections.bytes.out").summary().totalAmount()).isGreaterThan(1);
}
 
Example #19
Source File: ProxyServletService.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the URI contains user info in the form <code>user[:pass]@</code>, attempt to preempt the server
 * returning a 401 by providing Basic Authentication support in the initial request to the server.
 *
 * @param uri the URI which may contain user info
 * @param request the outgoing request to which an authorization header may be added
 */
void maybeAppendAuthHeader(URI uri, Request request) {
    if (uri != null && uri.getUserInfo() != null) {
        String[] userInfo = uri.getUserInfo().split(":");

        if (userInfo.length >= 1) {
            String user = userInfo[0];
            String password = userInfo.length >= 2 ? userInfo[1] : null;
            String authString = password != null ? user + ":" + password : user + ":";

            String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1);
            request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
        }
    }
}
 
Example #20
Source File: ProxyServletServiceTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMaybeAppendAuthHeaderWithoutPassword() throws URISyntaxException {
    Request request = mock(Request.class);
    URI uri = new URI("http://[email protected]:8080/content");
    service.maybeAppendAuthHeader(uri, request);
    verify(request).header(HttpHeader.AUTHORIZATION,
            "Basic " + B64Code.encode("testuser:", StringUtil.__ISO_8859_1));
}
 
Example #21
Source File: HttpParamDelegationTokenPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(Http2SolrClient client) {
  final HttpListenerFactory.RequestResponseListener listener = new HttpListenerFactory.RequestResponseListener() {
    @Override
    public void onQueued(Request request) {
      getPrincipal().ifPresent(usr -> request.header(INTERNAL_REQUEST_HEADER, usr));
    }
  };
  client.addListenerFactory(() -> listener);
}
 
Example #22
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateX509Certificate() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    Request request = Mockito.mock(Request.class);
    Mockito.when(httpClient.POST("https://localhost:443/certsign/v2/x509")).thenReturn(request);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(request.send()).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(201);
    Mockito.when(response.getContentAsString()).thenReturn("{\"pem\": \"pem-value\"}");

    String pem = certSigner.generateX509Certificate("csr", null, 0);
    assertEquals(pem, "pem-value");

    pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 0);
    assertEquals(pem, "pem-value");

    pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 30);
    assertEquals(pem, "pem-value");

    certSigner.requestTimeout = 120;
    pem = certSigner.generateX509Certificate("csr", InstanceProvider.ZTS_CERT_USAGE_CLIENT, 30);
    assertEquals(pem, "pem-value");

    certSigner.close();
}
 
Example #23
Source File: OcJettyHttpClientExtractor.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public String getUrl(Request request) {
  try {
    return request.getURI().toURL().toString();
  } catch (MalformedURLException e) {
    return "";
  }
}
 
Example #24
Source File: ForceStreamConsumer.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private BayeuxClient makeClient() throws Exception {
  httpClient = new HttpClient(ForceUtils.makeSslContextFactory(conf));
  httpClient.setConnectTimeout(CONNECTION_TIMEOUT);
  httpClient.setIdleTimeout(READ_TIMEOUT);
  if (conf.useProxy) {
    ForceUtils.setProxy(httpClient, conf);
  }
  httpClient.start();

  final String sessionid = connection.getConfig().getSessionId();
  String soapEndpoint = connection.getConfig().getServiceEndpoint();
  String endpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("/services/Soap/"));
  LOG.info("Server URL: {} ", endpoint);
  LOG.info("Session ID: {}", sessionid);

  Map<String, Object> options = new HashMap<>();
  options.put(ClientTransport.MAX_NETWORK_DELAY_OPTION, READ_TIMEOUT);
  options.put(LongPollingTransport.MAX_BUFFER_SIZE_OPTION, conf.streamingBufferSize);
  LongPollingTransport transport = new LongPollingTransport(options, httpClient) {

    @Override
    protected void customize(Request request) {
      super.customize(request);
      request.header(HttpHeader.AUTHORIZATION, "OAuth " + sessionid);
    }
  };

  String streamingEndpoint = salesforceStreamingEndpoint(endpoint);

  LOG.info("Streaming Endpoint: {}", streamingEndpoint);

  return new BayeuxClient(streamingEndpoint, transport);
}
 
Example #25
Source File: ReverseProxyServlet.java    From logbook with MIT License 5 votes vote down vote up
@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse,
        Request proxyRequest) {
    proxyRequest.onRequestContent(new RequestContentListener(clientRequest));

    super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
 
Example #26
Source File: AsyncProxyServlet.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Add Basic Authentication header to request if user and password are specified in URI.
 */
@Override
protected void copyRequestHeaders(HttpServletRequest clientRequest, Request proxyRequest) {
    super.copyRequestHeaders(clientRequest, proxyRequest);

    service.maybeAppendAuthHeader(service.uriFromRequest(clientRequest), proxyRequest);
}
 
Example #27
Source File: ReverseProxyServlet.java    From logbook-kai with MIT License 5 votes vote down vote up
/**
 * <p>
 * ライブラリのバグを修正します<br>
 * URLにマルチバイト文字が含まれている場合にURLが正しく組み立てられないバグを修正します
 * </p>
 */
private static void fixQueryString(Request proxyRequest, String queryString) {
    if (queryString != null && !queryString.isEmpty()) {
        if (proxyRequest instanceof HttpRequest) {
            try {
                FieldHolder.QUERY_FIELD.set(proxyRequest, queryString);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
 
Example #28
Source File: AdminProxyHandler.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRequest) {
    super.addProxyHeaders(clientRequest, proxyRequest);
    String user = (String) clientRequest.getAttribute(AuthenticationFilter.AuthenticatedRoleAttributeName);
    if (user != null) {
        proxyRequest.header("X-Original-Principal", user);
    }
}
 
Example #29
Source File: HttpCertSigner.java    From athenz with Apache License 2.0 5 votes vote down vote up
ContentResponse processX509CertRequest(final String csr, final List<Integer> extKeyUsage,
        int expiryTime, int retryCount) {
    
    ContentResponse response = null;
    try {
        Request request = httpClient.POST(x509CertUri);
        request.header(HttpHeader.ACCEPT, CONTENT_JSON);
        request.header(HttpHeader.CONTENT_TYPE, CONTENT_JSON);
        
        X509CertSignObject csrCert = new X509CertSignObject();
        csrCert.setPem(csr);
        csrCert.setX509ExtKeyUsage(extKeyUsage);
        if (expiryTime > 0 && expiryTime < maxCertExpiryTimeMins) {
            csrCert.setExpiryTime(expiryTime);
        }
        request.content(new StringContentProvider(JSON.string(csrCert)), CONTENT_JSON);
        
        // our max timeout is going to be 30 seconds. By default
        // we're picking a small value to quickly recognize when
        // our idle connections are disconnected by certsigner but
        // we won't allow any connections taking longer than 30 secs
        
        long timeout = retryCount * requestTimeout;
        if (timeout > 30) {
            timeout = 30;
        }
        request.timeout(timeout, TimeUnit.SECONDS);
        response = request.send();
    } catch (Exception ex) {
        LOGGER.error("Unable to process x509 certificate request", ex);
    }
    return response;
}
 
Example #30
Source File: AsyncProxyServlet.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Add Basic Authentication header to request if user and password are specified in URI.
 */
@Override
protected void copyRequestHeaders(HttpServletRequest clientRequest, Request proxyRequest) {
    super.copyRequestHeaders(clientRequest, proxyRequest);

    service.maybeAppendAuthHeader(service.uriFromRequest(clientRequest), proxyRequest);
}