Java Code Examples for org.eclipse.jetty.client.api.Request#send()
The following examples show how to use
org.eclipse.jetty.client.api.Request#send() .
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: JettyXhrTransport.java From spring-analysis-note with MIT License | 6 votes |
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, @Nullable 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<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status)); }
Example 2
Source File: BloomFilterPersistScalarFunction.java From presto-bloomfilter with Apache License 2.0 | 6 votes |
@SqlType(StandardTypes.BOOLEAN) @Nullable @SqlNullable public static Boolean bloomFilterPersist(@SqlNullable @SqlType(BloomFilterType.TYPE) Slice bloomFilterSlice, @SqlType(StandardTypes.VARCHAR) Slice urlSlice) throws Exception { // Nothing todo if (urlSlice == null) { return true; } BloomFilter bf = getOrLoadBloomFilter(bloomFilterSlice); // Persist // we do not try catch here to make sure that errors are communicated clearly to the client // and typical retry logic continues to work String url = new String(urlSlice.getBytes()); if (!HTTP_CLIENT.isStarted()) { log.warn("Http client was not started, trying to start"); HTTP_CLIENT.start(); } Request post = HTTP_CLIENT.POST(url); post.content(new StringContentProvider(new String(bf.toBase64()))); post.method("PUT"); post.send(); log.info("Persisted " + bf.toString() + " " + url); return true; }
Example 3
Source File: BloomFilter.java From presto-bloomfilter with Apache License 2.0 | 6 votes |
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 4
Source File: ProxyServlet.java From logbook-kai with MIT License | 6 votes |
@Override public void onComplete(Result result) { if (this.retryEnabled) { // 再度リトライはしない this.retryEnabled = false; if (ProxyServlet.this._isDebugEnabled) { ProxyServlet.this._log.debug("{} retrying proxy request", getRequestId(this.request)); } Request proxyRequest = ProxyServlet.this.createProxyRequest(this.request, this.response, this.targetUri, this.createRetryContentProvider()); proxyRequest.send(this); } else { if (ProxyServlet.this._isDebugEnabled) { ProxyServlet.this._log.debug("{} proxying complete", getRequestId(this.request)); } } }
Example 5
Source File: ZeppelinhubRestApiHandler.java From zeppelin with Apache License 2.0 | 5 votes |
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 6
Source File: AudioServletTest.java From smarthome with Eclipse Public License 2.0 | 5 votes |
@Test public void requestToMultitimeStreamCannotBeDoneAfterTheTimeoutOfTheStreamHasExipred() throws Exception { AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE, AudioFormat.CODEC_MP3); int streamTimeout = 1; String url = serveStream(audioStream, streamTimeout); Request request = getHttpRequest(url); ContentResponse response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200)); assertThat("The response content was not as expected", response.getContent(), is(testByteArray)); assertThat("The response media type was not as expected", response.getMediaType(), is(MEDIA_TYPE_AUDIO_MPEG)); assertThat("The audio stream was not added to the multitime streams", audioServlet.getMultiTimeStreams().containsValue(audioStream), is(true)); waitForAssert(() -> { try { request.send(); } catch (Exception e) { throw new IllegalStateException(e); } assertThat("The audio stream was not removed from multitime streams", audioServlet.getMultiTimeStreams().containsValue(audioStream), is(false)); }); response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.NOT_FOUND_404)); }
Example 7
Source File: Http2SolrClient.java From lucene-solr with Apache License 2.0 | 5 votes |
public OutStream initOutStream(String baseUrl, UpdateRequest updateRequest, String collection) throws IOException { String contentType = requestWriter.getUpdateContentType(); final ModifiableSolrParams origParams = new ModifiableSolrParams(updateRequest.getParams()); // The parser 'wt=' and 'version=' params are used instead of the // original params ModifiableSolrParams requestParams = new ModifiableSolrParams(origParams); requestParams.set(CommonParams.WT, parser.getWriterType()); requestParams.set(CommonParams.VERSION, parser.getVersion()); String basePath = baseUrl; if (collection != null) basePath += "/" + collection; if (!basePath.endsWith("/")) basePath += "/"; OutputStreamContentProvider provider = new OutputStreamContentProvider(); Request postRequest = httpClient .newRequest(basePath + "update" + requestParams.toQueryString()) .method(HttpMethod.POST) .header(HttpHeader.CONTENT_TYPE, contentType) .content(provider); decorateRequest(postRequest, updateRequest); InputStreamResponseListener responseListener = new InputStreamResponseListener(); postRequest.send(responseListener); boolean isXml = ClientUtils.TEXT_XML.equals(requestWriter.getUpdateContentType()); OutStream outStream = new OutStream(collection, origParams, provider, responseListener, isXml); if (isXml) { outStream.write("<stream>".getBytes(FALLBACK_CHARSET)); } return outStream; }
Example 8
Source File: JettyXhrTransport.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void executeReceiveRequest(URI url, HttpHeaders headers, SockJsResponseListener listener) { if (logger.isTraceEnabled()) { logger.trace("Starting XHR receive request, url=" + url); } Request httpRequest = this.httpClient.newRequest(url).method(HttpMethod.POST); addHttpHeaders(httpRequest, headers); httpRequest.send(listener); }
Example 9
Source File: HttpCommandProxy.java From ja-micro with Apache License 2.0 | 5 votes |
protected String sendRequest(String path, String data, HttpMethod method) throws Exception { String url = getServiceUrl(path); Request request = httpClient.newRequest(url).method(method). header(HttpHeader.CONTENT_TYPE, "application/json"); if (data != null) { request.content(new StringContentProvider(data)); } ContentResponse response = request.send(); return response.getContentAsString(); }
Example 10
Source File: JettyConnectionMetricsTest.java From micrometer with Apache License 2.0 | 5 votes |
@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 11
Source File: ZeppelinhubRestApiHandler.java From zeppelin with Apache License 2.0 | 5 votes |
/** * Fetch zeppelin instances for a given user. * @param ticket * @return * @throws IOException */ public List<Instance> getInstances(String ticket) throws IOException { InputStreamResponseListener listener = new InputStreamResponseListener(); Response response; String url = zepelinhubUrl + "instances"; String data; Request request = client.newRequest(url).header(USER_SESSION_HEADER, ticket); request.send(listener); try { response = listener.get(30, TimeUnit.SECONDS); } catch (InterruptedException | TimeoutException | ExecutionException e) { LOG.error("Cannot perform request to ZeppelinHub", e); throw new IOException("Cannot perform GET request to ZeppelinHub", e); } int code = response.getStatus(); if (code == 200) { try (InputStream responseContent = listener.getInputStream()) { data = IOUtils.toString(responseContent, "UTF-8"); } } else { LOG.error("ZeppelinHub GET {} returned with status {} ", url, code); throw new IOException("Cannot perform GET request to ZeppelinHub"); } Type listType = new TypeToken<ArrayList<Instance>>() {}.getType(); return new Gson().fromJson(data, listType); }
Example 12
Source File: JettyClientMetricsTest.java From micrometer with Apache License 2.0 | 5 votes |
@Test void serverError() throws Exception { Request post = httpClient.POST("http://localhost:" + connector.getLocalPort() + "/error"); post.content(new StringContentProvider("123456")); post.send(); httpClient.stop(); assertTrue(singleRequestLatch.await(10, SECONDS)); assertThat(registry.get("jetty.client.requests") .tag("outcome", "SERVER_ERROR") .tag("status", "500") .tag("uri", "/error") .timer().count()).isEqualTo(1); }
Example 13
Source File: JettyClientMetricsTest.java From micrometer with Apache License 2.0 | 5 votes |
@Test void successfulRequest() throws Exception { Request post = httpClient.POST("http://localhost:" + connector.getLocalPort() + "/ok"); post.content(new StringContentProvider("123456")); post.send(); httpClient.stop(); assertTrue(singleRequestLatch.await(10, SECONDS)); assertThat(registry.get("jetty.client.requests") .tag("outcome", "SUCCESS") .tag("status", "200") .tag("uri", "/ok") .timer().count()).isEqualTo(1); }
Example 14
Source File: CountersLBSPolicy.java From database with GNU General Public License v2.0 | 4 votes |
/** * Connect to an HTTP end point. * * @param opts * The connection options. * * @return The connection. */ static private JettyResponseListener doConnect(final HttpClient httpClient, final ConnectOptions opts) throws IOException { /* * Generate the fully formed and encoded URL. */ // The requestURL (w/o URL query parameters). final String requestURL = opts.serviceURL; final StringBuilder urlString = new StringBuilder(requestURL); ConnectOptions.addQueryParams(urlString, opts.requestParams); if (log.isDebugEnabled()) { log.debug("*** Request ***"); log.debug(requestURL); log.debug(opts.method); log.debug(urlString.toString()); } Request request = null; try { // request = RemoteRepository.newRequest(httpClient, urlString.toString(), // opts.method); request = httpClient.newRequest(urlString.toString()).method( HttpMethod.GET); if (opts.requestHeaders != null) { for (Map.Entry<String, String> e : opts.requestHeaders .entrySet()) { request.header(e.getKey(), e.getValue()); if (log.isDebugEnabled()) log.debug(e.getKey() + ": " + e.getValue()); } } if (opts.entity != null) { final EntityContentProvider cp = new EntityContentProvider(opts.entity); request.content(cp, cp.getContentType()); } final JettyResponseListener listener = new JettyResponseListener( request, TimeUnit.SECONDS.toMillis(300)); request.send(listener); return listener; } catch (Throwable t) { /* * If something goes wrong, then close the http connection. * Otherwise, the connection will be closed by the caller. */ try { if (request != null) request.abort(t); } catch (Throwable t2) { log.warn(t2); // ignored. } throw new RuntimeException(requestURL + " : " + t, t); } }
Example 15
Source File: BlockingProxyServlet.java From smarthome with Eclipse Public License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { URI uri = service.uriFromRequest(request); if (uri == null) { service.sendError(request, response); } else { Request httpRequest = httpClient.newRequest(uri); service.maybeAppendAuthHeader(uri, httpRequest); InputStreamResponseListener listener = new InputStreamResponseListener(); // do the client request try { httpRequest.send(listener); // wait for the response headers to arrive or the timeout to expire Response httpResponse = listener.get(TIMEOUT, TimeUnit.MILLISECONDS); // get response headers HttpFields headers = httpResponse.getHeaders(); Iterator<HttpField> iterator = headers.iterator(); // copy all headers while (iterator.hasNext()) { HttpField header = iterator.next(); response.setHeader(header.getName(), header.getValue()); } } catch (Exception e) { if (e instanceof TimeoutException) { logger.warn("Proxy servlet failed to stream content due to a timeout"); response.sendError(HttpServletResponse.SC_GATEWAY_TIMEOUT); } else { logger.warn("Proxy servlet failed to stream content: {}", e.getMessage()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } return; } // now copy/stream the body content try (InputStream responseContent = listener.getInputStream()) { IOUtils.copy(responseContent, response.getOutputStream()); } } }
Example 16
Source File: LoginHelper.java From EMP-Connector with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static BayeuxParameters login(URL loginEndpoint, String username, String password, BayeuxParameters parameters) throws Exception { HttpClient client = new HttpClient(parameters.sslContextFactory()); try { client.getProxyConfiguration().getProxies().addAll(parameters.proxies()); client.start(); URL endpoint = new URL(loginEndpoint, getSoapUri()); Request post = client.POST(endpoint.toURI()); post.content(new ByteBufferContentProvider("text/xml", ByteBuffer.wrap(soapXmlForLogin(username, password)))); post.header("SOAPAction", "''"); post.header("PrettyPrint", "Yes"); ContentResponse response = post.send(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/external-general-entities", false); spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); spf.setNamespaceAware(true); SAXParser saxParser = spf.newSAXParser(); LoginResponseParser parser = new LoginResponseParser(); saxParser.parse(new ByteArrayInputStream(response.getContent()), parser); String sessionId = parser.sessionId; if (sessionId == null || parser.serverUrl == null) { throw new ConnectException( String.format("Unable to login: %s", parser.faultstring)); } URL soapEndpoint = new URL(parser.serverUrl); String cometdEndpoint = Float.parseFloat(parameters.version()) < 37 ? COMETD_REPLAY_OLD : COMETD_REPLAY; URL replayEndpoint = new URL(soapEndpoint.getProtocol(), soapEndpoint.getHost(), soapEndpoint.getPort(), new StringBuilder().append(cometdEndpoint).append(parameters.version()).toString()); return new DelegatingBayeuxParameters(parameters) { @Override public String bearerToken() { return sessionId; } @Override public URL endpoint() { return replayEndpoint; } }; } finally { client.stop(); client.destroy(); } }
Example 17
Source File: JerseyUnixSocketConnector.java From tessera with Apache License 2.0 | 4 votes |
private ClientResponse doApply(ClientRequest request) throws Exception { HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod()); final URI originalUri = request.getUri(); final URI uri; Path basePath = Paths.get(unixfile); if (originalUri.getScheme().startsWith("unix")) { String path = originalUri.getRawPath() .replaceFirst(basePath.toString(), ""); LOGGER.trace("Extracted path {} from {}",path, originalUri.getRawPath()); uri = UriBuilder.fromUri(originalUri) .replacePath(path) .scheme("http") .port(99) .host("localhost") .build(); LOGGER.trace("Created psuedo uri {} for originalUri {}", uri, originalUri); } else { uri = originalUri; } Request clientRequest = httpClient.newRequest(uri) .method(httpMethod); MultivaluedMap<String, Object> headers = request.getHeaders(); headers.keySet().stream().forEach(name -> { headers.get(name).forEach(value -> { clientRequest.header(name, Objects.toString(value)); }); }); if (request.hasEntity()) { final long length = request.getLengthLong(); try (ByteArrayOutputStream bout = new ByteArrayOutputStream()){ request.setStreamProvider((int contentLength) -> bout); request.writeEntity(); ContentProvider content = new BytesContentProvider(bout.toByteArray()); clientRequest.content(content); } } final ContentResponse contentResponse = clientRequest.send(); int statusCode = contentResponse.getStatus(); String reason = contentResponse.getReason(); LOGGER.trace("uri {}, method: {},statusCode:{},reason: {} ", uri, httpMethod, statusCode, reason); final Response.StatusType status = Statuses.from(statusCode, reason); ClientResponse response = new ClientResponse(status, request); contentResponse.getHeaders().stream() .forEach(header -> { response.headers(header.getName(), (Object[]) header.getValues()); }); response.setEntityStream(new ByteArrayInputStream(contentResponse.getContent())); return response; }
Example 18
Source File: CommandProxyServlet.java From Scribengin with GNU Affero General Public License v3.0 | 4 votes |
@Override protected void onResponseFailure(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, Throwable failure){ //System.err.println("Response Failure!"); this.setForwardingUrl(); HttpClient c = null; try { c = this.createHttpClient(); } catch (ServletException e1) { e1.printStackTrace(); } final Request proxyRequest = c.newRequest(this.forwardingUrl) .method(request.getMethod()) .version(HttpVersion.fromString(request.getProtocol())); boolean hasContent = request.getContentLength() > 0 || request.getContentType() != null; for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();){ String headerName = headerNames.nextElement(); if (HttpHeader.TRANSFER_ENCODING.is(headerName)) hasContent = true; for (Enumeration<String> headerValues = request.getHeaders(headerName); headerValues.hasMoreElements();){ String headerValue = headerValues.nextElement(); if (headerValue != null) proxyRequest.header(headerName, headerValue); } } // Add proxy headers addViaHeader(proxyRequest); addXForwardedHeaders(proxyRequest, request); final AsyncContext asyncContext = request.getAsyncContext(); // We do not timeout the continuation, but the proxy request asyncContext.setTimeout(0); proxyRequest.timeout(getTimeout(), TimeUnit.MILLISECONDS); if (hasContent) try { proxyRequest.content(proxyRequestContent(proxyRequest, request)); } catch (IOException e) { e.printStackTrace(); } customizeProxyRequest(proxyRequest, request); proxyRequest.send(new ProxyResponseListener(request, response)); }
Example 19
Source File: AudioServletTest.java From openhab-core with Eclipse Public License 2.0 | 4 votes |
@Test public void onlyOneRequestToOneTimeStreamsCanBeMade() throws Exception { AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE, AudioFormat.CODEC_MP3); String url = serveStream(audioStream); Request request = getHttpRequest(url); ContentResponse response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200)); assertThat("The response content was not as expected", response.getContent(), is(testByteArray)); assertThat("The response media type was not as expected", response.getMediaType(), is(MEDIA_TYPE_AUDIO_MPEG)); response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.NOT_FOUND_404)); }
Example 20
Source File: AudioServletTest.java From smarthome with Eclipse Public License 2.0 | 4 votes |
@Test public void onlyOneRequestToOneTimeStreamsCanBeMade() throws Exception { AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE, AudioFormat.CODEC_MP3); String url = serveStream(audioStream); Request request = getHttpRequest(url); ContentResponse response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.OK_200)); assertThat("The response content was not as expected", response.getContent(), is(testByteArray)); assertThat("The response media type was not as expected", response.getMediaType(), is(MEDIA_TYPE_AUDIO_MPEG)); response = request.send(); assertThat("The response status was not as expected", response.getStatus(), is(HttpStatus.NOT_FOUND_404)); }