Search in sources :

Example 51 with HttpHead

use of org.apache.http.client.methods.HttpHead in project camel by apache.

the class CMProducer method doStart.

@Override
protected void doStart() throws Exception {
    // log at debug level for singletons, for prototype scoped log at trace
    // level to not spam logs
    log.debug("Starting CMProducer");
    final CMConfiguration configuration = getConfiguration();
    if (configuration.isTestConnectionOnStartup()) {
        try {
            log.debug("Checking connection - {}", getEndpoint().getCMUrl());
            HttpClientBuilder.create().build().execute(new HttpHead(getEndpoint().getCMUrl()));
            log.debug("Connection to {}: OK", getEndpoint().getCMUrl());
        } catch (final Exception e) {
            throw new HostUnavailableException(String.format("Connection to %s: NOT AVAILABLE", getEndpoint().getCMUrl()), e);
        }
    }
    // keep starting
    super.doStart();
    log.debug("CMProducer started");
}
Also used : HostUnavailableException(org.apache.camel.component.cm.exceptions.HostUnavailableException) HttpHead(org.apache.http.client.methods.HttpHead) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException) HostUnavailableException(org.apache.camel.component.cm.exceptions.HostUnavailableException)

Example 52 with HttpHead

use of org.apache.http.client.methods.HttpHead in project spark by perwendel.

the class SparkTestUtil method getHttpRequest.

private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
    try {
        String protocol = secureConnection ? "https" : "http";
        String uri = protocol + "://localhost:" + port + path;
        if (requestMethod.equals("GET")) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpGet);
            return httpGet;
        }
        if (requestMethod.equals("POST")) {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPost);
            httpPost.setEntity(new StringEntity(body));
            return httpPost;
        }
        if (requestMethod.equals("PATCH")) {
            HttpPatch httpPatch = new HttpPatch(uri);
            httpPatch.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPatch);
            httpPatch.setEntity(new StringEntity(body));
            return httpPatch;
        }
        if (requestMethod.equals("DELETE")) {
            HttpDelete httpDelete = new HttpDelete(uri);
            addHeaders(reqHeaders, httpDelete);
            httpDelete.setHeader("Accept", acceptType);
            return httpDelete;
        }
        if (requestMethod.equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri);
            httpPut.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPut);
            httpPut.setEntity(new StringEntity(body));
            return httpPut;
        }
        if (requestMethod.equals("HEAD")) {
            HttpHead httpHead = new HttpHead(uri);
            addHeaders(reqHeaders, httpHead);
            return httpHead;
        }
        if (requestMethod.equals("TRACE")) {
            HttpTrace httpTrace = new HttpTrace(uri);
            addHeaders(reqHeaders, httpTrace);
            return httpTrace;
        }
        if (requestMethod.equals("OPTIONS")) {
            HttpOptions httpOptions = new HttpOptions(uri);
            addHeaders(reqHeaders, httpOptions);
            return httpOptions;
        }
        if (requestMethod.equals("LOCK")) {
            HttpLock httpLock = new HttpLock(uri);
            addHeaders(reqHeaders, httpLock);
            return httpLock;
        }
        throw new IllegalArgumentException("Unknown method " + requestMethod);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) StringEntity(org.apache.http.entity.StringEntity) HttpTrace(org.apache.http.client.methods.HttpTrace)

Example 53 with HttpHead

use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.

the class CacheHeaderTestBase method getSelectMethod.

protected HttpRequestBase getSelectMethod(String method, String... params) throws URISyntaxException {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    HttpRequestBase m = null;
    ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
    if (params.length == 0) {
        qparams.add(new BasicNameValuePair("q", "solr"));
        qparams.add(new BasicNameValuePair("qt", "standard"));
    }
    for (int i = 0; i < params.length / 2; i++) {
        qparams.add(new BasicNameValuePair(params[i * 2], params[i * 2 + 1]));
    }
    URI uri = URI.create(client.getBaseURL() + "/select?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
    if ("GET".equals(method)) {
        m = new HttpGet(uri);
    } else if ("HEAD".equals(method)) {
        m = new HttpHead(uri);
    } else if ("POST".equals(method)) {
        m = new HttpPost(uri);
    }
    return m;
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead)

Example 54 with HttpHead

use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.

the class HttpSolrCall method remoteQuery.

private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
        String urlstr = coreUrl + queryParams.toQueryString();
        boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
        if ("GET".equals(req.getMethod())) {
            method = new HttpGet(urlstr);
        } else if ("HEAD".equals(req.getMethod())) {
            method = new HttpHead(urlstr);
        } else if (isPostOrPutRequest) {
            HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
            // Prevent close of container streams
            InputStream in = new CloseShieldInputStream(req.getInputStream());
            HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
            entityRequest.setEntity(entity);
            method = entityRequest;
        } else if ("DELETE".equals(req.getMethod())) {
            method = new HttpDelete(urlstr);
        } else {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
        }
        for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
            String headerName = e.nextElement();
            if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) {
                method.addHeader(headerName, req.getHeader(headerName));
            }
        }
        // These headers not supported for HttpEntityEnclosingRequests
        if (method instanceof HttpEntityEnclosingRequest) {
            method.removeHeaders(TRANSFER_ENCODING_HEADER);
            method.removeHeaders(CONTENT_LENGTH_HEADER);
        }
        final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext());
        int httpStatus = response.getStatusLine().getStatusCode();
        httpEntity = response.getEntity();
        resp.setStatus(httpStatus);
        for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) {
            Header header = responseHeaders.nextHeader();
            // encoding issues with Tomcat
            if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
                resp.addHeader(header.getName(), header.getValue());
            }
        }
        if (httpEntity != null) {
            if (httpEntity.getContentEncoding() != null)
                resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
            if (httpEntity.getContentType() != null)
                resp.setContentType(httpEntity.getContentType().getValue());
            InputStream is = httpEntity.getContent();
            OutputStream os = resp.getOutputStream();
            IOUtils.copyLarge(is, os);
        }
    } catch (IOException e) {
        sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e));
    } finally {
        Utils.consumeFully(httpEntity);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HeaderIterator(org.apache.http.HeaderIterator) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) SolrException(org.apache.solr.common.SolrException)

Example 55 with HttpHead

use of org.apache.http.client.methods.HttpHead in project ddf by codice.

the class HttpSolrClientFactory method createSolrCore.

private static void createSolrCore(String url, String coreName, String configFileName, HttpClient httpClient) throws IOException, SolrServerException {
    HttpSolrClient client;
    if (httpClient != null) {
        client = new HttpSolrClient(url, httpClient);
    } else {
        client = new HttpSolrClient(url);
    }
    HttpResponse ping = client.getHttpClient().execute(new HttpHead(url));
    if (ping != null && ping.getStatusLine().getStatusCode() == 200) {
        ConfigurationFileProxy configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance());
        configProxy.writeSolrConfiguration(coreName);
        if (!solrCoreExists(client, coreName)) {
            LOGGER.debug("Creating Solr core {}", coreName);
            String configFile = StringUtils.defaultIfBlank(configFileName, DEFAULT_SOLRCONFIG_XML);
            String solrDir;
            if (System.getProperty("solr.data.dir") != null) {
                solrDir = System.getProperty("solr.data.dir");
            } else {
                solrDir = Paths.get(System.getProperty("karaf.home"), "data", "solr").toString();
            }
            String instanceDir = Paths.get(solrDir, coreName).toString();
            String dataDir = Paths.get(instanceDir, "data").toString();
            CoreAdminRequest.createCore(coreName, instanceDir, client, configFile, DEFAULT_SCHEMA_XML, dataDir, dataDir);
        } else {
            LOGGER.debug("Solr core ({}) already exists - reloading it", coreName);
            CoreAdminRequest.reloadCore(coreName, client);
        }
    } else {
        LOGGER.debug("Unable to ping Solr core {}", coreName);
        throw new SolrServerException("Unable to ping Solr core");
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrServerException(org.apache.solr.client.solrj.SolrServerException) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6