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);
}
}
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);
}
}
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);
}
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));
}
}
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);
}
}
Aggregations