Search in sources :

Example 1 with Response

use of org.apache.hc.client5.http.fluent.Response in project feign by OpenFeign.

the class AsyncApacheHttp5ClientTest method throwsFeignExceptionIncludingBody.

@Test
public void throwsFeignExceptionIncludingBody() throws Throwable {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = AsyncFeign.asyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target(TestInterfaceAsync.class, "http://localhost:" + server.getPort());
    final CompletableFuture<?> cf = api.body("Request body");
    server.takeRequest();
    try {
        unwrap(cf);
    } catch (final FeignException e) {
        assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        assertThat(e.contentUTF8()).isEqualTo("Request body");
        return;
    }
    fail();
}
Also used : java.util(java.util) TypeToken(com.google.gson.reflect.TypeToken) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) feign.codec(feign.codec) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Buffer(okio.Buffer) java.util.concurrent(java.util.concurrent) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HardCodedTarget(feign.Target.HardCodedTarget) Rule(org.junit.Rule) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) feign(feign) Assert.assertEquals(org.junit.Assert.assertEquals) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Response

use of org.apache.hc.client5.http.fluent.Response in project feign by OpenFeign.

the class ApacheHttp5Client method execute.

@Override
public Response execute(Request request, Request.Options options) throws IOException {
    ClassicHttpRequest httpUriRequest;
    try {
        httpUriRequest = toClassicHttpRequest(request, options);
    } catch (final URISyntaxException e) {
        throw new IOException("URL '" + request.url() + "' couldn't be parsed into a URI", e);
    }
    final HttpHost target = HttpHost.create(URI.create(request.url()));
    final HttpClientContext context = configureTimeouts(options);
    final ClassicHttpResponse httpResponse = (ClassicHttpResponse) client.execute(target, httpUriRequest, context);
    return toFeignResponse(httpResponse, request);
}
Also used : HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException)

Example 3 with Response

use of org.apache.hc.client5.http.fluent.Response in project scoold by Erudika.

the class HttpUtils method isValidCaptcha.

/**
 * @param token CAPTCHA
 * @return boolean
 */
public static boolean isValidCaptcha(String token) {
    if (StringUtils.isBlank(Config.getConfigParam("signup_captcha_secret_key", ""))) {
        return true;
    }
    if (StringUtils.isBlank(token)) {
        return false;
    }
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("secret", Config.getConfigParam("signup_captcha_secret_key", "")));
    params.add(new BasicNameValuePair("response", token));
    HttpPost post = new HttpPost("https://www.google.com/recaptcha/api/siteverify");
    post.setEntity(new UrlEncodedFormEntity(params));
    try (CloseableHttpResponse resp = HttpUtils.getHttpClient().execute(post)) {
        if (resp.getCode() == HttpStatus.SC_OK && resp.getEntity() != null) {
            Map<String, Object> data = ParaObjectUtils.getJsonReader(Map.class).readValue(resp.getEntity().getContent());
            if (data != null && data.containsKey("success")) {
                return (boolean) data.getOrDefault("success", false);
            }
        }
    } catch (Exception ex) {
        LoggerFactory.getLogger(HttpUtils.class).debug("Failed to verify CAPTCHA: {}", ex.getMessage());
    }
    return false;
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.NameValuePair) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) Map(java.util.Map) IOException(java.io.IOException)

Example 4 with Response

use of org.apache.hc.client5.http.fluent.Response in project cxf by apache.

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) {
    if (client != null) {
        return;
    }
    final IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(TimeValue.ofMilliseconds(selectInterval)).setSoLinger(TimeValue.ofMilliseconds(soLinger)).setSoTimeout(Timeout.ofMilliseconds(soTimeout)).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    final Registry<TlsStrategy> tlsStrategy = RegistryBuilder.<TlsStrategy>create().register("https", DefaultClientTlsStrategy.getSystemDefault()).build();
    connectionManager = new PoolingAsyncClientConnectionManager(tlsStrategy, PoolConcurrencyPolicy.STRICT, PoolReusePolicy.LIFO, TimeValue.ofMilliseconds(connectionTTL), DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    final RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    final HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.setIOReactorConfig(config).build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultClientTlsStrategy(org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy) HttpRequest(org.apache.hc.core5.http.HttpRequest) Cookie(org.apache.hc.client5.http.cookie.Cookie) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpAsyncClientBuilder(org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) BasicCookieStore(org.apache.hc.client5.http.cookie.BasicCookieStore) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager) RedirectStrategy(org.apache.hc.client5.http.protocol.RedirectStrategy)

Example 5 with Response

use of org.apache.hc.client5.http.fluent.Response 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)

Aggregations

IOException (java.io.IOException)7 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)5 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 Gson (com.google.gson.Gson)3 TypeToken (com.google.gson.reflect.TypeToken)3 feign (feign)3 ResponseMappingDecoder (feign.Feign.ResponseMappingDecoder)3 HttpMethod (feign.Request.HttpMethod)3 HardCodedTarget (feign.Target.HardCodedTarget)3 MockWebServerAssertions.assertThat (feign.assertj.MockWebServerAssertions.assertThat)3 feign.codec (feign.codec)3 BeanQueryMapEncoder (feign.querymap.BeanQueryMapEncoder)3 FieldQueryMapEncoder (feign.querymap.FieldQueryMapEncoder)3 Type (java.lang.reflect.Type)3 URI (java.net.URI)3 java.util (java.util)3 java.util.concurrent (java.util.concurrent)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3