Search in sources :

Example 36 with ParseException

use of org.apache.http.ParseException in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testCompression.

@Test
public void testCompression() throws Exception {
    SolrQuery q = new SolrQuery("*:*");
    final String clientUrl = jetty.getBaseUrl().toString() + "/debug/foo";
    try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
        // verify request header gets set
        DebugServlet.clear();
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNull(DebugServlet.headers.toString(), DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, true)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNotNull(DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, false)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
    }
    assertNull(DebugServlet.headers.get("Accept-Encoding"));
    // verify server compresses output
    HttpGet get = new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml");
    get.setHeader("Accept-Encoding", "gzip");
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION, true);
    RequestConfig config = RequestConfig.custom().setDecompressionEnabled(false).build();
    get.setConfig(config);
    CloseableHttpClient httpclient = HttpClientUtil.createClient(params);
    HttpEntity entity = null;
    try {
        HttpResponse response = httpclient.execute(get, HttpClientUtil.createNewHttpClientRequestContext());
        entity = response.getEntity();
        Header ceheader = entity.getContentEncoding();
        assertNotNull(Arrays.asList(response.getAllHeaders()).toString(), ceheader);
        assertEquals("gzip", ceheader.getValue());
    } finally {
        if (entity != null) {
            entity.getContent().close();
        }
        HttpClientUtil.close(httpclient);
    }
    // verify compressed response can be handled
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) {
        q = new SolrQuery("foo");
        QueryResponse response = client.query(q);
        assertEquals(0, response.getStatus());
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 37 with ParseException

use of org.apache.http.ParseException in project algoliasearch-client-java by algolia.

the class APIClient method _requestByHost.

private JSONObject _requestByHost(HttpRequestBase req, String host, String url, String json, List<AlgoliaInnerException> errors, boolean searchTimeout) throws AlgoliaException {
    req.reset();
    // set URL
    try {
        req.setURI(new URI("https://" + host + url));
    } catch (URISyntaxException e) {
        // never reached
        throw new IllegalStateException(e);
    }
    // set auth headers
    req.setHeader("Accept-Encoding", "gzip");
    req.setHeader("X-Algolia-Application-Id", this.applicationID);
    if (forwardAdminAPIKey == null) {
        req.setHeader("X-Algolia-API-Key", this.apiKey);
    } else {
        req.setHeader("X-Algolia-API-Key", this.forwardAdminAPIKey);
        req.setHeader("X-Forwarded-For", this.forwardEndUserIP);
        req.setHeader("X-Forwarded-API-Key", this.forwardRateLimitAPIKey);
    }
    for (Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    // set user agent
    req.setHeader("User-Agent", userAgent);
    // set JSON entity
    if (json != null) {
        if (!(req instanceof HttpEntityEnclosingRequestBase)) {
            throw new IllegalArgumentException("Method " + req.getMethod() + " cannot enclose entity");
        }
        req.setHeader("Content-type", "application/json");
        try {
            StringEntity se = new StringEntity(json, "UTF-8");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            ((HttpEntityEnclosingRequestBase) req).setEntity(se);
        } catch (Exception e) {
            // $COVERAGE-IGNORE$
            throw new AlgoliaException("Invalid JSON Object: " + json);
        }
    }
    RequestConfig config = RequestConfig.custom().setSocketTimeout(searchTimeout ? httpSearchTimeoutMS : httpSocketTimeoutMS).setConnectTimeout(httpConnectTimeoutMS).setConnectionRequestTimeout(httpConnectTimeoutMS).build();
    req.setConfig(config);
    HttpResponse response;
    try {
        response = httpClient.execute(req);
    } catch (IOException e) {
        // on error continue on the next host
        if (verbose) {
            System.out.println(String.format("%s: %s=%s", host, e.getClass().getName(), e.getMessage()));
        }
        errors.add(new AlgoliaInnerException(host, e));
        return null;
    }
    try {
        int code = response.getStatusLine().getStatusCode();
        if (code / 100 == 4) {
            String message = "";
            try {
                message = EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (code == 400) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Bad request");
            } else if (code == 403) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Invalid Application-ID or API-Key");
            } else if (code == 404) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Resource does not exist");
            } else {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Error");
            }
        }
        if (code / 100 != 2) {
            try {
                if (verbose) {
                    System.out.println(String.format("%s: %s", host, EntityUtils.toString(response.getEntity())));
                }
                errors.add(new AlgoliaInnerException(host, EntityUtils.toString(response.getEntity())));
            } catch (IOException e) {
                if (verbose) {
                    System.out.println(String.format("%s: %s", host, String.valueOf(code)));
                }
                errors.add(new AlgoliaInnerException(host, e));
            }
            // KO, continue
            return null;
        }
        try {
            InputStream istream = response.getEntity().getContent();
            String encoding = response.getEntity().getContentEncoding() != null ? response.getEntity().getContentEncoding().getValue() : null;
            if (encoding != null && encoding.contains("gzip")) {
                istream = new GZIPInputStream(istream);
            }
            InputStreamReader is = new InputStreamReader(istream, "UTF-8");
            StringBuilder jsonRaw = new StringBuilder();
            char[] buffer = new char[4096];
            int read;
            while ((read = is.read(buffer)) > 0) {
                jsonRaw.append(buffer, 0, read);
            }
            is.close();
            return new JSONObject(jsonRaw.toString());
        } catch (IOException e) {
            if (verbose) {
                System.out.println(String.format("%s: %s=%s", host, e.getClass().getName(), e.getMessage()));
            }
            errors.add(new AlgoliaInnerException(host, e));
            return null;
        } catch (JSONException e) {
            throw new AlgoliaException("JSON decode error:" + e.getMessage());
        }
    } finally {
        req.releaseConnection();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) GZIPInputStream(java.util.zip.GZIPInputStream) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ParseException(org.apache.http.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) GZIPInputStream(java.util.zip.GZIPInputStream) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) ParseException(org.apache.http.ParseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 38 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class LaxContentLengthStrategy method determineLength.

public long determineLength(final HttpMessage message) throws HttpException {
    if (message == null) {
        throw new IllegalArgumentException("HTTP message may not be null");
    }
    HttpParams params = message.getParams();
    boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
    Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
    Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
    // RFC2616, 4.4 item number 3
    if (transferEncodingHeader != null) {
        HeaderElement[] encodings = null;
        try {
            encodings = transferEncodingHeader.getElements();
        } catch (ParseException px) {
            throw new ProtocolException("Invalid Transfer-Encoding header value: " + transferEncodingHeader, px);
        }
        if (strict) {
            // Currently only chunk and identity are supported
            for (int i = 0; i < encodings.length; i++) {
                String encoding = encodings[i].getName();
                if (encoding != null && encoding.length() > 0 && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING) && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
                    throw new ProtocolException("Unsupported transfer encoding: " + encoding);
                }
            }
        }
        // The chunked encoding must be the last one applied RFC2616, 14.41
        int len = encodings.length;
        if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
            return IDENTITY;
        } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(encodings[len - 1].getName()))) {
            return CHUNKED;
        } else {
            if (strict) {
                throw new ProtocolException("Chunk-encoding must be the last one applied");
            }
            return IDENTITY;
        }
    } else if (contentLengthHeader != null) {
        long contentlen = -1;
        Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
        if (strict && headers.length > 1) {
            throw new ProtocolException("Multiple content length headers");
        }
        for (int i = headers.length - 1; i >= 0; i--) {
            Header header = headers[i];
            try {
                contentlen = Long.parseLong(header.getValue());
                break;
            } catch (NumberFormatException e) {
                if (strict) {
                    throw new ProtocolException("Invalid content length: " + header.getValue());
                }
            }
        // See if we can have better luck with another header, if present
        }
        if (contentlen >= 0) {
            return contentlen;
        } else {
            return IDENTITY;
        }
    } else {
        return IDENTITY;
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) ParseException(org.apache.http.ParseException)

Aggregations

ParseException (org.apache.http.ParseException)38 Header (org.apache.http.Header)14 ProtocolVersion (org.apache.http.ProtocolVersion)12 IOException (java.io.IOException)10 ProtocolException (org.apache.http.ProtocolException)9 HttpException (org.apache.http.HttpException)6 HttpParams (org.apache.http.params.HttpParams)6 Test (org.junit.Test)5 HttpEntity (org.apache.http.HttpEntity)4 Socket (java.net.Socket)3 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 SSLSocket (javax.net.ssl.SSLSocket)3 HeaderElement (org.apache.http.HeaderElement)3 HeaderIterator (org.apache.http.HeaderIterator)3 HttpConnection (org.apache.http.HttpConnection)3 HttpMessage (org.apache.http.HttpMessage)3 HttpResponse (org.apache.http.HttpResponse)3