Search in sources :

Example 36 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project jena by apache.

the class HttpOp method execHttpPost.

/**
     * Executes a HTTP POST with request body from an input stream and response
     * handling.
     * <p>
     * The input stream is assumed to be UTF-8.
     * </p>
     * 
     * @param url
     *            URL
     * @param contentType
     *            Content Type to POST
     * @param input
     *            Input Stream to POST content from
     * @param length
     *            Length of content to POST
     * @param acceptType
     *            Accept Type
     * @param handler
     *            Response handler called to process the response
     * @param httpClient
     *            HTTP Client
     * @param httpContext
     *            HTTP Context
     *
     */
public static void execHttpPost(String url, String contentType, InputStream input, long length, String acceptType, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext) {
    InputStreamEntity e = new InputStreamEntity(input, length);
    e.setContentType(contentType);
    e.setContentEncoding("UTF-8");
    try {
        execHttpPost(url, e, acceptType, handler, httpClient, httpContext);
    } finally {
        closeEntity(e);
    }
}
Also used : InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 37 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity in project jena by apache.

the class HttpOp method execHttpPut.

/**
     * Executes a HTTP PUT operation
     * 
     * @param url
     *            URL
     * @param contentType
     *            Content Type for the PUT
     * @param input
     *            Input Stream to read PUT content from
     * @param length
     *            Amount of content to PUT
     * @param httpClient
     *            HTTP Client
     * @param httpContext
     *            HTTP Context
     */
public static void execHttpPut(String url, String contentType, InputStream input, long length, HttpClient httpClient, HttpContext httpContext) {
    InputStreamEntity e = new InputStreamEntity(input, length);
    e.setContentType(contentType);
    e.setContentEncoding("UTF-8");
    try {
        execHttpPut(url, e, httpClient, httpContext);
    } finally {
        closeEntity(e);
    }
}
Also used : InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 38 with InputStreamEntity

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

the class SolrSchemalessExampleTest method testArbitraryJsonIndexing.

@Test
public void testArbitraryJsonIndexing() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    // two docs, one with uniqueKey, another without it
    String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
    HttpClient httpClient = client.getHttpClient();
    HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
    Utils.consumeFully(response.getEntity());
    assertEquals(200, response.getStatusLine().getStatusCode());
    client.commit();
    assertNumFound("*:*", 2);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 39 with InputStreamEntity

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

the class SolrSchemalessExampleTest method testFieldMutating.

@Test
public void testFieldMutating() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    // two docs, one with uniqueKey, another without it
    String json = "{\"name one\": \"name\"} " + "{\"name  two\" : \"name\"}" + "{\"first-second\" : \"name\"}" + "{\"x+y\" : \"name\"}" + "{\"p%q\" : \"name\"}" + "{\"p.q\" : \"name\"}" + "{\"a&b\" : \"name\"}";
    HttpClient httpClient = client.getHttpClient();
    HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
    client.commit();
    List<String> expected = Arrays.asList("name_one", "name__two", "first-second", "a_b", "p_q", "p.q", "x_y");
    HashSet set = new HashSet();
    QueryResponse rsp = assertNumFound("*:*", expected.size());
    for (SolrDocument doc : rsp.getResults()) set.addAll(doc.getFieldNames());
    for (String s : expected) {
        assertTrue(s + " not created " + rsp, set.contains(s));
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 40 with InputStreamEntity

use of org.apache.http.entity.InputStreamEntity 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)

Aggregations

InputStreamEntity (org.apache.http.entity.InputStreamEntity)50 HttpPost (org.apache.http.client.methods.HttpPost)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 InputStream (java.io.InputStream)15 HttpResponse (org.apache.http.HttpResponse)12 Test (org.junit.Test)12 HttpEntity (org.apache.http.HttpEntity)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpPut (org.apache.http.client.methods.HttpPut)9 IOException (java.io.IOException)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 Map (java.util.Map)5 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)4 HttpClient (org.apache.http.client.HttpClient)4 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Header (org.apache.http.Header)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 HttpGet (org.apache.http.client.methods.HttpGet)3