List of usage examples for org.apache.http.nio.entity NStringEntity NStringEntity
public NStringEntity(final String s, String charset) throws UnsupportedEncodingException
From source file:org.apache.metron.elasticsearch.integration.ElasticsearchSearchIntegrationTest.java
protected static void loadTestData() throws ParseException, IOException { // add bro template JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class); addTestFieldMappings(broTemplate, "bro_doc"); String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true); HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON); Response response = lowLevelClient.performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity);/*from w ww . java 2s . c om*/ assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); // add snort template JSONObject snortTemplate = JSONUtils.INSTANCE.load(new File(snortTemplatePath), JSONObject.class); addTestFieldMappings(snortTemplate, "snort_doc"); String snortTemplateJson = JSONUtils.INSTANCE.toJSON(snortTemplate, true); HttpEntity snortEntity = new NStringEntity(snortTemplateJson, ContentType.APPLICATION_JSON); response = lowLevelClient.performRequest("PUT", "/_template/snort_template", Collections.emptyMap(), snortEntity); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); // create bro index response = lowLevelClient.performRequest("PUT", BRO_INDEX); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); // create snort index response = lowLevelClient.performRequest("PUT", SNORT_INDEX); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); JSONArray broRecords = (JSONArray) new JSONParser().parse(broData); BulkRequest bulkRequest = new BulkRequest(); for (Object o : broRecords) { JSONObject json = (JSONObject) o; IndexRequest indexRequest = new IndexRequest(BRO_INDEX, "bro_doc", (String) json.get("guid")); indexRequest.source(json); indexRequest.timestamp(json.get("timestamp").toString()); bulkRequest.add(indexRequest); } bulkRequest.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest); assertFalse(bulkResponse.hasFailures()); assertThat(bulkResponse.status().getStatus(), equalTo(200)); JSONArray snortRecords = (JSONArray) new JSONParser().parse(snortData); bulkRequest = new BulkRequest(); for (Object o : snortRecords) { JSONObject json = (JSONObject) o; IndexRequest indexRequest = new IndexRequest(SNORT_INDEX, "snort_doc", (String) json.get("guid")); indexRequest.source(json); indexRequest.timestamp(json.get("timestamp").toString()); bulkRequest.add(indexRequest); } bulkRequest.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); bulkResponse = highLevelClient.bulk(bulkRequest); assertFalse(bulkResponse.hasFailures()); assertThat(bulkResponse.status().getStatus(), equalTo(200)); }
From source file:org.kitodo.data.index.elasticsearch.RestClientImplementation.java
/** * Delete all documents of certain type from the index. * * @return response from server/*from w w w . j a v a 2s. c o m*/ */ public String deleteType() throws IOException { String query = "{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}"; HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON); Response indexResponse = restClient.performRequest("POST", "/" + this.getIndex() + "/" + this.getType() + "/_delete_by_query", Collections.<String, String>emptyMap(), entity); return indexResponse.toString(); }
From source file:com.aptana.webserver.internal.core.builtin.LocalWebServerHttpRequestHandler.java
private static HttpEntity createTextEntity(String text) throws UnsupportedEncodingException { NStringEntity entity = new NStringEntity( MessageFormat.format("<html><body><h1>{0}</h1></body></html>", text), //$NON-NLS-1$ IOUtil.UTF_8);/*w ww. j av a2 s. c o m*/ entity.setContentType(HTML_TEXT_TYPE + HTTP.CHARSET_PARAM + IOUtil.UTF_8); return entity; }
From source file:es.eucm.rage.realtime.states.elasticsearch.EsMapState.java
/** * MapState implementation//from w w w . j a v a 2s . co m **/ @Override public List<T> multiGet(List<List<Object>> keys) { List<T> ret = new ArrayList(keys.size()); String mgetJson = ""; try { Map<String, List> body = new HashMap<>(); List<Map> query = new ArrayList<>(); body.put("docs", query); for (List<Object> key : keys) { String index = key.get(0).toString(); String type = ESUtils.getOpaqueValuesType(); String id = toSingleKey(key); Map<String, String> doc = new HashMap<>(); query.add(doc); doc.put("_index", index); doc.put("_type", type); doc.put("_id", id); } mgetJson = gson.toJson(body, Map.class); HttpEntity entity = new NStringEntity(mgetJson, ContentType.APPLICATION_JSON); Response response = _client.performRequest("GET", "_mget", Collections.emptyMap(), entity); int status = response.getStatusLine().getStatusCode(); if (status > HttpStatus.SC_ACCEPTED) { LOG.info("There was an MGET error, mget JSON " + mgetJson); LOG.error("MGET error, status is " + status); return ret; } String responseString = EntityUtils.toString(response.getEntity()); Map<String, Object> responseDocs = gson.fromJson(responseString, Map.class); List<Object> docs = (List) responseDocs.get("docs"); for (Object doc : docs) { Map<String, Object> docMap = (Map) doc; T resDoc = (T) docMap.get("_source"); ret.add(resDoc); } if (_mreads != null) { _mreads.incrBy(ret.size()); } return ret; } catch (Exception e) { LOG.info("There was an MGET error, mget JSON " + mgetJson); LOG.error("Exception while mget", e); } return ret; }
From source file:marytts.server.http.MaryHttpServerUtils.java
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status);/* ww w. jav a 2 s .c o m*/ String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity( "<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
From source file:org.kitodo.data.elasticsearch.KitodoRestClient.java
/** * Create new index with mapping.//from w w w. j a v a2 s. c om * * @param query * contains mapping * @return true or false - can be used for displaying information to user if * success */ public boolean createIndex(String query) throws IOException, CustomResponseException { if (query == null) { query = "{\"settings\" : {\"index\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0}}}"; } HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON); Response indexResponse = client.performRequest(HttpMethod.PUT, "/" + index, Collections.emptyMap(), entity); int statusCode = processStatusCode(indexResponse.getStatusLine()); return statusCode == 200 || statusCode == 201; }
From source file:marytts.server.http.MaryHttpServerUtils.java
public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status);/*from w ww . ja va2s .com*/ StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
From source file:org.kitodo.data.elasticsearch.index.IndexRestClient.java
/** * Enable sorting by text field./* www . j a va 2s . c o m*/ * * @param type * as String * @param field * as String */ public void enableSortingByTextField(String type, String field) throws IOException, CustomResponseException { String query = "{\n \"properties\": {\n\"" + field + "\": {\n" + " \"type\": \"text\",\n" + " \"fielddata\": true,\n" + " \"fields\": {\n" + " \"raw\": {\n" + " \"type\": \"text\",\n" + " \"index\": false}\n" + " }\n" + " }}}"; HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON); Response indexResponse = client.performRequest(HttpMethod.PUT, "/" + this.getIndex() + "/_mapping/" + type + "?update_all_types", Collections.emptyMap(), entity); processStatusCode(indexResponse.getStatusLine()); }
From source file:org.jenkinsci.plugins.relution_publisher.net.RequestFactory.java
/** * Creates a {@link BaseRequest} that can be used to persist the specified {@link Application}. * <p/>//from w w w .j a va2s . c om * This call must not be used to persist an application that has already been persisted. Doing * so results in undefined behavior. An application is unpersisted if its * {@link Application#getUuid() identifier} is <code>null</code>. * @param store The {@link Store} this request should be executed against. * @param app The {@link Application} to persist. * @return A request that can be used to persist the specified application. */ public static EntityRequest createPersistApplicationRequest(final Store store, final JsonObject app) { final EntityRequest request = new EntityRequest(Method.POST, getUrl(store, URL_APPS)); final NStringEntity entity = new NStringEntity(app.toString(), CHARSET); request.setEntity(entity); request.setHeader(HEADER_CONTENT_TYPE, APPLICATION_JSON); addAuthentication(request, store); return request; }
From source file:com.thinkbiganalytics.search.ElasticSearchRestService.java
@Override public void index(@Nonnull String indexName, @Nonnull String typeName, @Nonnull String id, @Nonnull Map<String, Object> fields) { buildRestClient();/*from w ww. ja va2 s .c o m*/ try { JSONObject jsonContent = new JSONObject(fields); HttpEntity httpEntity = new NStringEntity(jsonContent.toString(), ContentType.APPLICATION_JSON); restClient.performRequest(PUT_METHOD, getIndexWriteEndPoint(indexName, typeName, id), Collections.emptyMap(), httpEntity); log.debug("Wrote to index with name {}", indexName); } catch (ResponseException responseException) { log.warn("Index write encountered issues in Elasticsearch for index={" + indexName + "}, type={" + typeName + "}, id={" + id + "}", responseException); } catch (ClientProtocolException clientProtocolException) { log.debug("Http protocol error for write for index {" + indexName + "}", clientProtocolException); } catch (IOException ioException) { log.error("IO Error in rest client", ioException); } finally { closeRestClient(); } }