Search in sources :

Example 26 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.

the class ProxyController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
    HttpGet method = new HttpGet(url);
    method.setConfig(requestConfig);
    InputStream in = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        try (CloseableHttpResponse resp = client.execute(method)) {
            int statusCode = resp.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                response.sendError(statusCode);
            } else {
                in = resp.getEntity().getContent();
                IOUtils.copy(in, response.getOutputStream());
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return null;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.

the class VersionService method readLatestVersion.

/**
     * Resolves the latest available Libresonic version by screen-scraping a web page.
     *
     * @throws IOException If an I/O error occurs.
     */
private void readLatestVersion() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
    HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion());
    method.setConfig(requestConfig);
    String content;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        content = client.execute(method, responseHandler);
    }
    Pattern finalPattern = Pattern.compile("LIBRESONIC_FULL_VERSION_BEGIN(.*)LIBRESONIC_FULL_VERSION_END");
    Pattern betaPattern = Pattern.compile("LIBRESONIC_BETA_VERSION_BEGIN(.*)LIBRESONIC_BETA_VERSION_END");
    try (BufferedReader reader = new BufferedReader(new StringReader(content))) {
        String line = reader.readLine();
        while (line != null) {
            Matcher finalMatcher = finalPattern.matcher(line);
            if (finalMatcher.find()) {
                latestFinalVersion = new Version(finalMatcher.group(1));
                LOG.info("Resolved latest Libresonic final version to: " + latestFinalVersion);
            }
            Matcher betaMatcher = betaPattern.matcher(line);
            if (betaMatcher.find()) {
                latestBetaVersion = new Version(betaMatcher.group(1));
                LOG.info("Resolved latest Libresonic beta version to: " + latestBetaVersion);
            }
            line = reader.readLine();
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Version(org.libresonic.player.domain.Version) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 28 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project knime-core by knime.

the class ProfileManager method downloadProfiles.

private Path downloadProfiles(final URI profileLocation) {
    Path stateDir = getStateLocation();
    Path profileDir = stateDir.resolve("profiles");
    try {
        Files.createDirectories(stateDir);
        URIBuilder builder = new URIBuilder(profileLocation);
        builder.addParameter("profiles", String.join(",", m_provider.getRequestedProfiles()));
        URI profileUri = builder.build();
        m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).info("Downloading profiles from " + profileUri));
        // proxies
        HttpHost proxy = ProxySelector.getDefault().select(profileUri).stream().filter(p -> p.address() != null).findFirst().map(p -> new HttpHost(((InetSocketAddress) p.address()).getAddress())).orElse(null);
        // timeout; we cannot access KNIMEConstants here because that would acccess preferences
        int timeout = 2000;
        String to = System.getProperty("knime.url.timeout", Integer.toString(timeout));
        try {
            timeout = Integer.parseInt(to);
        } catch (NumberFormatException ex) {
            m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).warn("Illegal value for system property knime.url.timeout :" + to, ex));
        }
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setProxy(proxy).setConnectionRequestTimeout(timeout).build();
        try (CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setRedirectStrategy(new DefaultRedirectStrategy()).build()) {
            HttpGet get = new HttpGet(profileUri);
            if (Files.isDirectory(profileDir)) {
                Instant lastModified = Files.getLastModifiedTime(profileDir).toInstant();
                get.setHeader("If-Modified-Since", DateTimeFormatter.RFC_1123_DATE_TIME.format(lastModified.atZone(ZoneId.of("GMT"))));
            }
            try (CloseableHttpResponse response = client.execute(get)) {
                int code = response.getStatusLine().getStatusCode();
                if ((code >= 200) && (code < 300)) {
                    Header ct = response.getFirstHeader("Content-Type");
                    if ((ct == null) || (ct.getValue() == null) || !ct.getValue().startsWith("application/zip")) {
                        // no zip file - it just processes an empty zip
                        throw new IOException("Server did not return a ZIP file containing the selected profiles");
                    }
                    Path tempDir = PathUtils.createTempDir("profile-download", stateDir);
                    try (InputStream is = response.getEntity().getContent()) {
                        PathUtils.unzip(new ZipInputStream(is), tempDir);
                    }
                    // replace profiles only if new data has been downloaded successfully
                    PathUtils.deleteDirectoryIfExists(profileDir);
                    Files.move(tempDir, profileDir, StandardCopyOption.ATOMIC_MOVE);
                } else if (code != 304) {
                    // 304 = Not Modified
                    HttpEntity body = response.getEntity();
                    String msg;
                    if (body.getContentType().getValue().startsWith("text/")) {
                        byte[] buf = new byte[Math.min(4096, Math.max(4096, (int) body.getContentLength()))];
                        int read = body.getContent().read(buf);
                        msg = new String(buf, 0, read, "US-ASCII").trim();
                    } else if (!response.getStatusLine().getReasonPhrase().isEmpty()) {
                        msg = response.getStatusLine().getReasonPhrase();
                    } else {
                        msg = "Server returned status " + response.getStatusLine().getStatusCode();
                    }
                    throw new IOException(msg);
                }
            }
        }
    } catch (IOException | URISyntaxException ex) {
        String msg = "Could not download profiles from " + profileLocation + ": " + ex.getMessage() + ". " + (Files.isDirectory(profileDir) ? "Will use existing but potentially outdated profiles." : "No profiles will be applied.");
        m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).error(msg, ex));
    }
    return profileDir;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) ZipInputStream(java.util.zip.ZipInputStream) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) RequestConfig(org.apache.http.client.config.RequestConfig) Supplier(java.util.function.Supplier) Header(org.apache.http.Header) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) ProxySelector(java.net.ProxySelector) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) NodeLogger(org.knime.core.node.NodeLogger) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) PathUtils(org.knime.core.util.PathUtils) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) URI(java.net.URI) Bundle(org.osgi.framework.Bundle) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Properties(java.util.Properties) Files(java.nio.file.Files) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) Reader(java.io.Reader) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) HttpGet(org.apache.http.client.methods.HttpGet) DateTimeFormatter(java.time.format.DateTimeFormatter) DefaultPreferences(org.eclipse.core.internal.preferences.DefaultPreferences) Optional(java.util.Optional) Platform(org.eclipse.core.runtime.Platform) Collections(java.util.Collections) HttpHost(org.apache.http.HttpHost) FrameworkUtil(org.osgi.framework.FrameworkUtil) HttpClients(org.apache.http.impl.client.HttpClients) InputStream(java.io.InputStream) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Instant(java.time.Instant) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) URIBuilder(org.apache.http.client.utils.URIBuilder) ZipInputStream(java.util.zip.ZipInputStream) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy)

Example 29 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project wikidata-query-rdf by wikimedia.

the class HttpClientUtils method ignoreCookies.

/**
 * Configure request to ignore cookies.
 */
public static void ignoreCookies(HttpRequestBase request) {
    RequestConfig noCookiesConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    request.setConfig(noCookiesConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig)

Example 30 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project pancm_project by xuwujing.

the class HttpRequest method doJSONPost.

/**
 * Do json post string.
 *
 * @param url  the url
 * @param json the json
 * @return the string
 */
public static String doJSONPost(String url, String json) {
    CloseableHttpClient httpclient = null;
    try {
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        RequestConfig requestConfig = null;
        // 设置http的状态参数
        requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_TIME_OUT).setConnectTimeout(HTTP_TIME_OUT).setConnectionRequestTimeout(HTTP_TIME_OUT).build();
        // 添加HTTP POST参数
        StringEntity input = new StringEntity(json, "UTF-8");
        input.setContentType("application/json");
        HttpUriRequest reqMethod = RequestBuilder.post().setUri(url).setEntity(input).setConfig(requestConfig).build();
        // 创建响应处理器处理服务器响应内容
        ResponseHandler<String> responseHandler = new ResponseHandler() {

            // 自定义响应处理
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    ContentType contentType = ContentType.get(entity);
                    Charset charset = (null == contentType ? Charset.forName(DEFAULT_CHARSET) : (null == contentType.getCharset() ? Charset.forName(DEFAULT_CHARSET) : contentType.getCharset()));
                    return new String(EntityUtils.toByteArray(entity), charset);
                } else {
                    return null;
                }
            }
        };
        // // 执行请求并获取结果
        String responseBody = httpclient.execute(reqMethod, responseHandler);
        return responseBody;
    } catch (Exception ex) {
        // throw new HttpPostException("doxmlpost_error", ex.getMessage(), ex);
        ex.printStackTrace();
        throw new RuntimeException(ex.getMessage());
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) HttpResponse(org.apache.http.HttpResponse) Charset(java.nio.charset.Charset) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)146 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)97 HttpGet (org.apache.http.client.methods.HttpGet)94 IOException (java.io.IOException)78 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)65 HttpResponse (org.apache.http.HttpResponse)60 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)55 URI (java.net.URI)46 StringEntity (org.apache.http.entity.StringEntity)43 Map (java.util.Map)41 Test (org.junit.Test)41 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 HttpHost (org.apache.http.HttpHost)32 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)32 HttpClient (org.apache.http.client.HttpClient)31 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)27 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)24 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)24