Search in sources :

Example 11 with Request

use of org.apache.hc.client5.http.fluent.Request in project ksql by confluentinc.

the class WebClient method send.

/**
 * Sends a POST request to a web server
 * This method requires a pre-configured http client instance
 *
 * @param customerId customer Id on behalf of which the request is sent
 * @param bytes request payload
 * @param httpPost A POST request structure
 * @param proxy a http (passive) proxy
 * @param httpClient http client instance configured by caller
 * @return an HTTP Status code
 * @see #send(String, byte[], HttpPost, ResponseHandler)
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:FinalParameters" })
protected static int send(final String customerId, final byte[] bytes, final HttpPost httpPost, final HttpHost proxy, CloseableHttpClient httpClient, final ResponseHandler responseHandler) {
    int statusCode = DEFAULT_STATUS_CODE;
    if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) {
        // add the body to the request
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.LEGACY);
        builder.addTextBody("cid", customerId);
        builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename");
        httpPost.setEntity(builder.build());
        httpPost.addHeader("api-version", "phone-home-v1");
        // set the HTTP config
        RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setConnectionRequestTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setResponseTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).build();
        CloseableHttpResponse response = null;
        try {
            if (proxy != null) {
                log.debug("setting proxy to {}", proxy);
                config = RequestConfig.copy(config).setProxy(proxy).build();
                httpPost.setConfig(config);
                final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
                if (httpClient == null) {
                    httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).setDefaultRequestConfig(config).build();
                }
            } else {
                if (httpClient == null) {
                    httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
                }
            }
            response = httpClient.execute(httpPost);
            if (responseHandler != null) {
                responseHandler.handle(response);
            }
            // send request
            log.debug("POST request returned {}", new StatusLine(response).toString());
            statusCode = response.getCode();
        } catch (IOException e) {
            log.error("Could not submit metrics to Confluent: {}", e.getMessage());
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.warn("could not close http client", e);
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    log.warn("could not close http response", e);
                }
            }
        }
    } else {
        statusCode = HttpStatus.SC_BAD_REQUEST;
    }
    return statusCode;
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) DefaultProxyRoutePlanner(org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner) IOException(java.io.IOException)

Example 12 with Request

use of org.apache.hc.client5.http.fluent.Request in project OpenRefine by OpenRefine.

the class ImportingUtilities method retrieveContentFromPostRequest.

public static void retrieveContentFromPostRequest(HttpServletRequest request, Properties parameters, File rawDataDir, ObjectNode retrievalRecord, final Progress progress) throws IOException, FileUploadException {
    ArrayNode fileRecords = ParsingUtilities.mapper.createArrayNode();
    JSONUtilities.safePut(retrievalRecord, "files", fileRecords);
    int clipboardCount = 0;
    int uploadCount = 0;
    int downloadCount = 0;
    int archiveCount = 0;
    // This tracks the total progress, which involves uploading data from the client
    // as well as downloading data from URLs.
    final SavingUpdate update = new SavingUpdate() {

        @Override
        public void savedMore() {
            progress.setProgress(null, calculateProgressPercent(totalExpectedSize, totalRetrievedSize));
        }

        @Override
        public boolean isCanceled() {
            return progress.isCanceled();
        }
    };
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
    upload.setProgressListener(new ProgressListener() {

        boolean setContentLength = false;

        long lastBytesRead = 0;

        @Override
        public void update(long bytesRead, long contentLength, int itemCount) {
            if (!setContentLength) {
                // Only try to set the content length if we really know it.
                if (contentLength >= 0) {
                    update.totalExpectedSize += contentLength;
                    setContentLength = true;
                }
            }
            if (setContentLength) {
                update.totalRetrievedSize += (bytesRead - lastBytesRead);
                lastBytesRead = bytesRead;
                update.savedMore();
            }
        }
    });
    List<FileItem> tempFiles = (List<FileItem>) upload.parseRequest(request);
    progress.setProgress("Uploading data ...", -1);
    parts: for (FileItem fileItem : tempFiles) {
        if (progress.isCanceled()) {
            break;
        }
        InputStream stream = fileItem.getInputStream();
        String name = fileItem.getFieldName().toLowerCase();
        if (fileItem.isFormField()) {
            if (name.equals("clipboard")) {
                String encoding = request.getCharacterEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                File file = allocateFile(rawDataDir, "clipboard.txt");
                ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
                JSONUtilities.safePut(fileRecord, "origin", "clipboard");
                JSONUtilities.safePut(fileRecord, "declaredEncoding", encoding);
                JSONUtilities.safePut(fileRecord, "declaredMimeType", (String) null);
                JSONUtilities.safePut(fileRecord, "format", "text");
                JSONUtilities.safePut(fileRecord, "fileName", "(clipboard)");
                JSONUtilities.safePut(fileRecord, "location", getRelativePath(file, rawDataDir));
                progress.setProgress("Uploading pasted clipboard text", calculateProgressPercent(update.totalExpectedSize, update.totalRetrievedSize));
                JSONUtilities.safePut(fileRecord, "size", saveStreamToFile(stream, file, null));
                JSONUtilities.append(fileRecords, fileRecord);
                clipboardCount++;
            } else if (name.equals("download")) {
                String urlString = Streams.asString(stream);
                URL url = new URL(urlString);
                ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
                JSONUtilities.safePut(fileRecord, "origin", "download");
                JSONUtilities.safePut(fileRecord, "url", urlString);
                for (UrlRewriter rewriter : ImportingManager.urlRewriters) {
                    Result result = rewriter.rewrite(urlString);
                    if (result != null) {
                        urlString = result.rewrittenUrl;
                        url = new URL(urlString);
                        JSONUtilities.safePut(fileRecord, "url", urlString);
                        JSONUtilities.safePut(fileRecord, "format", result.format);
                        if (!result.download) {
                            downloadCount++;
                            JSONUtilities.append(fileRecords, fileRecord);
                            continue parts;
                        }
                    }
                }
                if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
                    final URL lastUrl = url;
                    final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() {

                        @Override
                        public String handleResponse(final ClassicHttpResponse response) throws IOException {
                            final int status = response.getCode();
                            if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
                                final HttpEntity entity = response.getEntity();
                                if (entity == null) {
                                    throw new IOException("No content found in " + lastUrl.toExternalForm());
                                }
                                try {
                                    InputStream stream2 = entity.getContent();
                                    String mimeType = null;
                                    String charset = null;
                                    ContentType contentType = ContentType.parse(entity.getContentType());
                                    if (contentType != null) {
                                        mimeType = contentType.getMimeType();
                                        Charset cs = contentType.getCharset();
                                        if (cs != null) {
                                            charset = cs.toString();
                                        }
                                    }
                                    JSONUtilities.safePut(fileRecord, "declaredMimeType", mimeType);
                                    JSONUtilities.safePut(fileRecord, "declaredEncoding", charset);
                                    if (saveStream(stream2, lastUrl, rawDataDir, progress, update, fileRecord, fileRecords, entity.getContentLength())) {
                                        // signal to increment archive count
                                        return "saved";
                                    }
                                } catch (final IOException ex) {
                                    throw new ClientProtocolException(ex);
                                }
                                return null;
                            } else {
                                // String errorBody = EntityUtils.toString(response.getEntity());
                                throw new ClientProtocolException(String.format("HTTP error %d : %s for URL %s", status, response.getReasonPhrase(), lastUrl.toExternalForm()));
                            }
                        }
                    };
                    HttpClient httpClient = new HttpClient();
                    if (httpClient.getResponse(urlString, null, responseHandler) != null) {
                        archiveCount++;
                    }
                    ;
                    downloadCount++;
                } else {
                    // Fallback handling for non HTTP connections (only FTP?)
                    URLConnection urlConnection = url.openConnection();
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.connect();
                    InputStream stream2 = urlConnection.getInputStream();
                    JSONUtilities.safePut(fileRecord, "declaredEncoding", urlConnection.getContentEncoding());
                    JSONUtilities.safePut(fileRecord, "declaredMimeType", urlConnection.getContentType());
                    try {
                        if (saveStream(stream2, url, rawDataDir, progress, update, fileRecord, fileRecords, urlConnection.getContentLength())) {
                            archiveCount++;
                        }
                        downloadCount++;
                    } finally {
                        stream2.close();
                    }
                }
            } else {
                String value = Streams.asString(stream);
                parameters.put(name, value);
            // TODO: We really want to store this on the request so it's available for everyone
            // request.getParameterMap().put(name, value);
            }
        } else {
            // is file content
            String fileName = fileItem.getName();
            if (fileName.length() > 0) {
                long fileSize = fileItem.getSize();
                File file = allocateFile(rawDataDir, fileName);
                ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
                JSONUtilities.safePut(fileRecord, "origin", "upload");
                JSONUtilities.safePut(fileRecord, "declaredEncoding", request.getCharacterEncoding());
                JSONUtilities.safePut(fileRecord, "declaredMimeType", fileItem.getContentType());
                JSONUtilities.safePut(fileRecord, "fileName", fileName);
                JSONUtilities.safePut(fileRecord, "location", getRelativePath(file, rawDataDir));
                progress.setProgress("Saving file " + fileName + " locally (" + formatBytes(fileSize) + " bytes)", calculateProgressPercent(update.totalExpectedSize, update.totalRetrievedSize));
                JSONUtilities.safePut(fileRecord, "size", saveStreamToFile(stream, file, null));
                // TODO: This needs to be refactored to be able to test import from archives
                if (postProcessRetrievedFile(rawDataDir, file, fileRecord, fileRecords, progress)) {
                    archiveCount++;
                }
                uploadCount++;
            }
        }
        stream.close();
    }
    // Delete all temp files.
    for (FileItem fileItem : tempFiles) {
        fileItem.delete();
    }
    JSONUtilities.safePut(retrievalRecord, "uploadCount", uploadCount);
    JSONUtilities.safePut(retrievalRecord, "downloadCount", downloadCount);
    JSONUtilities.safePut(retrievalRecord, "clipboardCount", clipboardCount);
    JSONUtilities.safePut(retrievalRecord, "archiveCount", archiveCount);
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) ContentType(org.apache.hc.core5.http.ContentType) URL(java.net.URL) Result(com.google.refine.importing.UrlRewriter.Result) ClientProtocolException(org.apache.hc.client5.http.ClientProtocolException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) List(java.util.List) ArrayList(java.util.ArrayList) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) URLConnection(java.net.URLConnection) FileItem(org.apache.commons.fileupload.FileItem) HttpClientResponseHandler(org.apache.hc.core5.http.io.HttpClientResponseHandler) ProgressListener(org.apache.commons.fileupload.ProgressListener) HttpClient(com.google.refine.util.HttpClient) File(java.io.File)

Example 13 with Request

use of org.apache.hc.client5.http.fluent.Request in project OpenRefine by OpenRefine.

the class ImportingUtilities method loadDataAndPrepareJob.

public static void loadDataAndPrepareJob(HttpServletRequest request, HttpServletResponse response, Properties parameters, final ImportingJob job, ObjectNode config) throws IOException, ServletException {
    ObjectNode retrievalRecord = ParsingUtilities.mapper.createObjectNode();
    JSONUtilities.safePut(config, "retrievalRecord", retrievalRecord);
    JSONUtilities.safePut(config, "state", "loading-raw-data");
    final ObjectNode progress = ParsingUtilities.mapper.createObjectNode();
    JSONUtilities.safePut(config, "progress", progress);
    try {
        ImportingUtilities.retrieveContentFromPostRequest(request, parameters, job.getRawDataDir(), retrievalRecord, new Progress() {

            @Override
            public void setProgress(String message, int percent) {
                if (message != null) {
                    JSONUtilities.safePut(progress, "message", message);
                }
                JSONUtilities.safePut(progress, "percent", percent);
            }

            @Override
            public boolean isCanceled() {
                return job.canceled;
            }
        });
    } catch (Exception e) {
        JSONUtilities.safePut(config, "state", "error");
        JSONUtilities.safePut(config, "error", "Error uploading data");
        JSONUtilities.safePut(config, "errorDetails", e.getLocalizedMessage());
        throw new IOException(e.getMessage());
    }
    ArrayNode fileSelectionIndexes = ParsingUtilities.mapper.createArrayNode();
    JSONUtilities.safePut(config, "fileSelection", fileSelectionIndexes);
    EncodingGuesser.guess(job);
    String bestFormat = ImportingUtilities.autoSelectFiles(job, retrievalRecord, fileSelectionIndexes);
    bestFormat = ImportingUtilities.guessBetterFormat(job, bestFormat);
    ArrayNode rankedFormats = ParsingUtilities.mapper.createArrayNode();
    ImportingUtilities.rankFormats(job, bestFormat, rankedFormats);
    JSONUtilities.safePut(config, "rankedFormats", rankedFormats);
    JSONUtilities.safePut(config, "state", "ready");
    JSONUtilities.safePut(config, "hasData", true);
    config.remove("progress");
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ServletException(javax.servlet.ServletException) ClientProtocolException(org.apache.hc.client5.http.ClientProtocolException) ZipException(java.util.zip.ZipException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 14 with Request

use of org.apache.hc.client5.http.fluent.Request in project OpenRefine by OpenRefine.

the class HttpClient method postNameValue.

public String postNameValue(String serviceUrl, String name, String value) throws IOException {
    HttpPost request = new HttpPost(serviceUrl);
    List<NameValuePair> body = Collections.singletonList(new BasicNameValuePair(name, value));
    request.setEntity(new UrlEncodedFormEntity(body, StandardCharsets.UTF_8));
    try (CloseableHttpResponse response = httpClient.execute(request)) {
        String reasonPhrase = response.getReasonPhrase();
        int statusCode = response.getCode();
        if (statusCode >= 400) {
            // We should never see 3xx since they get handled automatically
            throw new IOException(String.format("HTTP error %d : %s for URL %s", statusCode, reasonPhrase, request.getRequestUri()));
        }
        return ParsingUtilities.inputStreamToString(response.getEntity().getContent());
    }
}
Also used : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Example 15 with Request

use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.

the class InstrumentedHttpAsyncClientsTest method registersExpectedExceptionMetrics.

@Test
public void registersExpectedExceptionMetrics() throws Exception {
    client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();
    client.start();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
    final String requestMetricName = "request";
    final String exceptionMetricName = "exception";
    httpServer.createContext("/", HttpExchange::close);
    httpServer.start();
    when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
    when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
    try {
        final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void completed(SimpleHttpResponse result) {
                fail();
            }

            @Override
            public void failed(Exception ex) {
                countDownLatch.countDown();
            }

            @Override
            public void cancelled() {
                fail();
            }
        });
        countDownLatch.await(5, TimeUnit.SECONDS);
        responseFuture.get(5, TimeUnit.SECONDS);
        fail();
    } catch (ExecutionException e) {
        assertThat(e).hasCauseInstanceOf(ConnectionClosedException.class);
        await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(metricRegistry.getMeters()).containsKey("exception"));
    }
}
Also used : SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpExchange(com.sun.net.httpserver.HttpExchange) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) ExecutionException(java.util.concurrent.ExecutionException) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 HttpRequest (org.apache.hc.core5.http.HttpRequest)7 Test (org.junit.Test)7 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)5 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)5 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)4 ExecutionException (java.util.concurrent.ExecutionException)3 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HttpExchange (com.sun.net.httpserver.HttpExchange)2 ClientProtocolException (org.apache.hc.client5.http.ClientProtocolException)2 Configurable (org.apache.hc.client5.http.config.Configurable)2 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)2 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)2 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)2 Timer (com.codahale.metrics.Timer)1 Gson (com.google.gson.Gson)1