Java Code Examples for org.apache.http.entity.ContentType#APPLICATION_JSON

The following examples show how to use org.apache.http.entity.ContentType#APPLICATION_JSON . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearchUtil.java    From RCT with Apache License 2.0 6 votes vote down vote up
/**
 * kyle
 * 
 * @param documents
 * @throws Exception
 *             {"took":84,"errors":false,"items":[]}
 */
private static void bulkIndexDocumentToES(String doc, List<String> documents, int retryCount) throws Exception {
	StringEntity entity = new StringEntity(doc, ContentType.APPLICATION_JSON);
	String result = httpPost(getUrl() + "/_bulk", entity);
	JSONObject json = JSONObject.parseObject(result);
	List<Integer> errorIndex = null;
	List<String> retryDoc = null;
	// 判断是否有失败的记录
	if (null != json && json.getBoolean("errors")) {
		if (retryCount == 0) {
			LOG.warn("insert to es failed, data:" + doc);
			return;
		}
		errorIndex = getErrorIndexByResult(json);
		retryDoc = getRetryDoc(errorIndex, documents);
		bulkIndexDocumentToES(doc, retryDoc, --retryCount);
	}
}
 
Example 2
Source File: ElasticSearchClientServiceImpl.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public DeleteOperationResponse deleteById(String index, String type, List<String> ids) {
    try {
        StringBuilder sb = new StringBuilder();
        for (int idx = 0; idx < ids.size(); idx++) {
            String header = buildBulkHeader("delete", index, type, ids.get(idx));
            sb.append(header).append("\n");
        }
        HttpEntity entity = new NStringEntity(sb.toString(), ContentType.APPLICATION_JSON);
        StopWatch watch = new StopWatch();
        watch.start();
        Response response = client.performRequest("POST", "/_bulk", Collections.emptyMap(), entity);
        watch.stop();

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(String.format("Response for bulk delete: %s",
                    IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)));
        }

        DeleteOperationResponse dor = new DeleteOperationResponse(watch.getDuration(TimeUnit.MILLISECONDS));

        return dor;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 3
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public void migrateBucket(String bucketName, String orgId, String teamId)
    throws GalaxyFDSClientException {
  ContentType contentType = ContentType.APPLICATION_JSON;
  StringEntity requestEntity = getJsonStringEntity("", contentType);
  HashMap<String, String> params = new HashMap<String, String>();
  params.put("migrate", "");
  params.put("orgId", orgId);
  params.put("teamId", teamId);
  URI uri = formatUri(fdsConfig.getBaseUri(), bucketName, (SubResource[]) null);
  HttpUriRequest httpRequest = fdsHttpClient.prepareRequestMethod(uri, HttpMethod.PUT,
    contentType, null, params, null, requestEntity);
  HttpResponse response = fdsHttpClient.executeHttpRequest(httpRequest, Action.MigrateBucket);
  fdsHttpClient.processResponse(response, null,
    "migrate bucket [" + bucketName + "] to orgId [" + orgId + "]");
}
 
Example 4
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a mapping type to an index if it does not exist.
 *
 * @param index           The name of the index.
 * @param mappingType     The name of the mapping type.
 * @param mappingFilename The name of the mapping file to use to add the mapping if it does not exist.
 * @throws IOException If an error occurred during requests to ES.
 */
private void addMappingToIndex(final String index, final String mappingType, final String mappingFilename)
    throws IOException {

    logger.info("Adding '{}' mapping to index '{}'...", mappingType, index);

    String resourcePath = "/" + index + "/_mapping/" + mappingType;

    if (doesResourceNotExist(resourcePath)) {
        HttpEntity entity = new NByteArrayEntity(loadTypeMappingSource(mappingFilename).getBytes(),
            ContentType.APPLICATION_JSON);
        elasticSearchAdminClient.performRequest(HttpMethod.PUT, resourcePath, Collections.emptyMap(), entity);
        logger.info("Added '{}' mapping", mappingType);
    } else {
        logger.info("Mapping '{}' already exists", mappingType);
    }
}
 
Example 5
Source File: EmbeddedElasticsearchPolicy.java    From Quicksql with MIT License 6 votes vote down vote up
/**
 * Creates index in elastic search given a mapping. Mapping can contain nested fields expressed
 * as dots({@code .}).
 *
 * <p>Example
 * <pre>
 *  {@code
 *     b.a: long
 *     b.b: keyword
 *  }
 * </pre>
 *
 * @param index index of the index
 * @param mapping field and field type mapping
 * @throws IOException if there is an error
 */
public void createIndex(String index, Map<String, String> mapping) throws IOException {
  Objects.requireNonNull(index, "index");
  Objects.requireNonNull(mapping, "mapping");

  ObjectNode mappings = mapper().createObjectNode();

  ObjectNode properties = mappings.with("mappings").with(index).with("properties");
  for (Map.Entry<String, String> entry: mapping.entrySet()) {
    applyMapping(properties, entry.getKey(), entry.getValue());
  }

  // create index and mapping
  final HttpEntity entity = new StringEntity(mapper().writeValueAsString(mappings),
      ContentType.APPLICATION_JSON);
  restClient().performRequest("PUT", "/" + index, Collections.emptyMap(), entity);
}
 
Example 6
Source File: EmbeddedElasticsearchPolicy.java    From calcite with Apache License 2.0 6 votes vote down vote up
void insertBulk(String index, List<ObjectNode> documents) throws IOException {
  Objects.requireNonNull(index, "index");
  Objects.requireNonNull(documents, "documents");

  if (documents.isEmpty()) {
    // nothing to process
    return;
  }

  List<String> bulk = new ArrayList<>(documents.size() * 2);
  for (ObjectNode doc: documents) {
    bulk.add(String.format(Locale.ROOT, "{\"index\": {\"_index\":\"%s\"}}", index));
    bulk.add(mapper().writeValueAsString(doc));
  }

  final StringEntity entity = new StringEntity(String.join("\n", bulk) + "\n",
      ContentType.APPLICATION_JSON);

  final Request r = new Request("POST", "/_bulk?refresh");
  r.setEntity(entity);
  restClient().performRequest(r);
}
 
Example 7
Source File: QueuesRestApiTest.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
@Parameters({"admin-username", "admin-password"})
@Test
public void testPositiveCreateQueue(String username, String password) throws IOException {
    String queueName = "testPositiveCreateQueue";
    QueueCreateRequest request = new QueueCreateRequest()
            .name(queueName).durable(false).autoDelete(false);

    HttpPost httpPost = new HttpPost(apiBasePath + "/queues");
    ClientHelper.setAuthHeader(httpPost, username, password);
    String value = objectMapper.writeValueAsString(request);
    StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_JSON);
    httpPost.setEntity(stringEntity);

    CloseableHttpResponse response = client.execute(httpPost);

    Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED);
    Assert.assertTrue(response.getFirstHeader(HttpHeaders.LOCATION)
                    .getValue().contains("/queues/" + queueName),
                        "Incorrect location header");

    String body = responseHandler.handleResponse(response);
    QueueCreateResponse queueCreateResponse = objectMapper.readValue(body, QueueCreateResponse.class);

    Assert.assertFalse(queueCreateResponse.getMessage().isEmpty(), "Response body shouldn't be empty");

}
 
Example 8
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public List<Map<String, Object>> deleteObjects(String bucketName, List<String> objectNameList,
    boolean enableTrash) throws GalaxyFDSClientException {
  Preconditions.checkNotNull(bucketName);
  Preconditions.checkNotNull(objectNameList);

  URI uri = formatUri(fdsConfig.getBaseUri(), bucketName, (SubResource[]) null);
  ContentType contentType = ContentType.APPLICATION_JSON;
  StringEntity requestEntity = getJsonStringEntity(objectNameList, contentType);
  HashMap<String, String> params = new HashMap<String, String>();
  params.put("deleteObjects", "");
  if (!enableTrash) {
    params.put("enableTrash", String.valueOf(enableTrash));
  }
  HttpUriRequest httpRequest = fdsHttpClient.prepareRequestMethod(uri, HttpMethod.PUT,
    contentType, null, params, null, requestEntity);

  HttpResponse response = fdsHttpClient.executeHttpRequest(httpRequest, Action.DeleteObjects);

  List<Map<String, Object>> responseList = (List<Map<String, Object>>) fdsHttpClient
      .processResponse(response, List.class,
        "delete " + objectNameList.size() + " objects under bucket [" + bucketName + "]");
  return responseList;
}
 
Example 9
Source File: LoggersRestApiTest.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
@Parameters({"admin-username", "admin-password"})
@Test
public void testPositiveUpdateLogLevel(String username, String password) throws IOException {
    String loggerName = "io.ballerina.messaging.broker.core.BrokerImpl";
    String loggerLevel = "WARN";
    LoggerMetadata loggerMetadata = new LoggerMetadata().name(loggerName).level(loggerLevel);

    HttpPut httpPut = new HttpPut(apiBasePath + LOGGERS_API_PATH);
    ClientHelper.setAuthHeader(httpPut, username, password);
    String value = objectMapper.writeValueAsString(loggerMetadata);
    StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_JSON);
    httpPut.setEntity(stringEntity);

    CloseableHttpResponse response = client.execute(httpPut);

    Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_OK);

    String body = responseHandler.handleResponse(response);

    Assert.assertTrue(body.contains("Changed log level"), "Invalid success message.");


}
 
Example 10
Source File: RdbmsDiscretionaryAccessControlForExchangesTest.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
private void addUserGroupToExchange(String action, String exchangeName, String testUserGroup, String userName,
                                    String password) throws IOException {
    UserGroupList userGroupList = new UserGroupList();
    userGroupList.getUserGroups().add(testUserGroup);

    HttpPost httpPost = new HttpPost(apiBasePath + ExchangesApiDelegate.EXCHANGES_API_PATH
            + "/" + exchangeName + "/permissions/actions/" + action + "/groups");
    ClientHelper.setAuthHeader(httpPost, userName, password);
    String value = objectMapper.writeValueAsString(userGroupList);
    StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_JSON);
    httpPost.setEntity(stringEntity);

    CloseableHttpResponse response = client.execute(httpPost);
    Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_OK,
            "Incorrect status code.");
}
 
Example 11
Source File: ElasticsearchRest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
public void addDocument(String id, Map<String, Object> json) {
  try {
    HttpEntity entity =
        new StringEntity(objectMapper.writeValueAsString(json), ContentType.APPLICATION_JSON);

    esrResource
        .getClient()
        .performRequest(
            "PUT", "/" + index + "/" + type + "/" + id, Collections.emptyMap(), entity);
  } catch (IOException ioe) {
    getMonitor().error("Couldn't persist document to Elasticsearch", ioe);
  }
}
 
Example 12
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a match query for given field/value and returns the count of results.
 *
 * @param connectionConfiguration Specifies the index and type
 * @param restClient To use to execute the call
 * @param field The field to query
 * @param value The value to match
 * @return The count of documents in the search result
 * @throws IOException On error communicating with Elasticsearch
 */
static int countByMatch(
    ConnectionConfiguration connectionConfiguration,
    RestClient restClient,
    String field,
    String value)
    throws IOException {
  String requestBody =
      "{\n"
          + "  \"query\" : {\"match\": {\n"
          + "    \""
          + field
          + "\": \""
          + value
          + "\"\n"
          + "  }}\n"
          + "}\n";
  String endPoint =
      String.format(
          "/%s/%s/_search",
          connectionConfiguration.getIndex(), connectionConfiguration.getType());
  HttpEntity httpEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);

  Request request = new Request("GET", endPoint);
  request.addParameters(Collections.emptyMap());
  request.setEntity(httpEntity);
  Response response = restClient.performRequest(request);
  JsonNode searchResult = parseResponse(response.getEntity());
  if (getBackendVersion(connectionConfiguration) >= 7) {
    return searchResult.path("hits").path("total").path("value").asInt();
  } else {
    return searchResult.path("hits").path("total").asInt();
  }
}
 
Example 13
Source File: HttpsUtils.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
public static String doPostWithJson(String url, Object jsonObj) throws Exception {
	
	String jsontext = JSON.toJSONString(jsonObj, SerializerFeature.WriteSlashAsSpecial);
    logger.debug("request url = {}, content = {}",url,jsontext);
    HttpPost post = new HttpPost(url);
    HttpEntity requestEntity = new StringEntity(jsontext, ContentType.APPLICATION_JSON);
    post.setEntity(requestEntity);
    return execute(post);
}
 
Example 14
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public CopyObjectResult copyObject(FDSCopyObjectRequest request) throws GalaxyFDSClientException {
  URI uri = formatUri(fdsConfig.getBaseUri(),
    request.getDstBucketName() + "/" + request.getDstObjectName(), (SubResource[]) null);

  ContentType contentType = ContentType.APPLICATION_JSON;

  HashMap<String, String> cp = new HashMap<String, String>();
  cp.put("cp", "");

  // Prepare request entity
  Map<String, String> copySrcObject = new HashMap<String, String>();
  copySrcObject.put("srcBucketName", request.getSrcBucketName());
  copySrcObject.put("srcObjectName", request.getSrcObjectName());
  StringEntity requestEntity = getJsonStringEntity(copySrcObject, contentType);

  HttpUriRequest httpRequest = fdsHttpClient.prepareRequestMethod(uri, HttpMethod.PUT,
    contentType, null, cp, null, requestEntity);

  HttpResponse response = fdsHttpClient.executeHttpRequest(httpRequest, Action.CopyObject);

  int statusCode = response.getStatusLine().getStatusCode();

  return (CopyObjectResult) fdsHttpClient.processResponse(response, CopyObjectResult.class,
    HttpMethod.PUT.name() + " object [" + request.getDstObjectName() + "] to bucket ["
        + request.getDstBucketName() + "]");
}
 
Example 15
Source File: DcosAuthImplTest.java    From marathon-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test that signs JWT with a private key and verifies the contents
 * using the matching public key.
 *
 * @throws Exception
 */
@Test
public void testTokenCanBeVerifiedWithPublicKey() throws Exception {
    final Secret secret = PowerMockito.mock(Secret.class);
    // final payload
    final String secretText = String.format(DCOS_AUTH_JSON, testUser, RSAPrivateKeyForJSON, "RS256");

    // convert pub key string to actual publickey
    final KeyFactory         keyFactory = KeyFactory.getInstance("RSA", "BC");
    final PemReader          pemReader  = new PemReader(new StringReader(RSAPublicKey));
    final byte[]             content    = pemReader.readPemObject().getContent();
    final X509EncodedKeySpec keySpec    = new X509EncodedKeySpec(content);
    final PublicKey          publicKey  = keyFactory.generatePublic(keySpec);

    Whitebox.setInternalState(secret, "value", secretText);

    when(credentials.getSecret()).thenReturn(secret);
    when(secret.getPlainText()).thenReturn(secretText);

    final DcosAuthImpl dcosAuth = new DcosAuthImpl(credentials,
            options,
            ContentType.APPLICATION_JSON,
            builder,
            context);
    final DcosLoginPayload payload = dcosAuth.createDcosLoginPayload();
    assertNotNull("Payload is null", payload);
    assertEquals("Uid does not match", testUser, payload.getUid());
    assertNotNull("Token was not created", payload.getToken());

    // this proves we can validate the JWT with public key
    final JWTVerifier         verifier = new JWTVerifier(publicKey);
    final Map<String, Object> claims   = verifier.verify(payload.getToken());
    assertFalse("Should be populated", claims.isEmpty());
    assertEquals("Users do not match", testUser, claims.get("uid"));
}
 
Example 16
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronously deletes the target if it exists and then (re)creates it as a copy of source
 * synchronously.
 */
static void copyIndex(RestClient restClient, String source, String target) throws IOException {
  deleteIndex(restClient, target);
  HttpEntity entity =
      new NStringEntity(
          String.format(
              "{\"source\" : { \"index\" : \"%s\" }, \"dest\" : { \"index\" : \"%s\" } }",
              source, target),
          ContentType.APPLICATION_JSON);
  Request request = new Request("POST", "/_reindex");
  request.addParameters(Collections.singletonMap("refresh", "wait_for"));
  request.setEntity(entity);
  restClient.performRequest(request);
}
 
Example 17
Source File: HttpUtils.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * Create Post Request with json body
 */
public HttpPost createPost(String uri, String data) {
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setHeader("Content-Type", "application/json");
    HttpEntity entity = new StringEntity(data, ContentType.APPLICATION_JSON);
    httpPost.setEntity(entity);
    return httpPost;
}
 
Example 18
Source File: HttpEventsQueryHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() throws Exception {
    URIBuilder builder = new URIBuilder().setScheme("http")
            .setHost("127.0.0.1").setPort(9200)
            .setPath("/events/graphite_event/_query");

    HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() {
        @Override
        public String getMethod() {
            return "DELETE";
        }
    };
    delete.setURI(builder.build());

    String deletePayload = "{\"query\":{\"match_all\":{}}}";
    HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON);
    delete.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(delete);
    if(response.getStatusLine().getStatusCode() != 200)
    {
        System.out.println("Couldn't delete index after running tests.");
    }
    else {
        System.out.println("Successfully deleted index after running tests.");
    }
}
 
Example 19
Source File: ApacheHttpRequestBuilder.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public static ContentType createContentType(String contentType) {
  switch (contentType) {
    case "application/json":
      return ContentType.APPLICATION_JSON;
    case "text/plain":
      return ContentType.TEXT_PLAIN;
    default:
      throw new RuntimeException("contentType not supported: " + contentType);
  }
}
 
Example 20
Source File: ProtocolImpl.java    From jdbc-cb with Apache License 2.0 4 votes vote down vote up
public CouchResponse doQuery(String query, Map queryParameters) throws SQLException
{
    Instance endPoint = getNextEndpoint();

    // keep trying endpoints
    while(true)
    {

        try {
            String url = endPoint.getEndpointURL(ssl);

            logger.trace("Using endpoint {}", url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Accept", "application/json");

            logger.trace("do query {}", httpPost.toString());
            addOptions(queryParameters);


            String jsonParameters = JsonFactory.toJson(queryParameters);
            StringEntity entity = new StringEntity(jsonParameters, ContentType.APPLICATION_JSON);


            httpPost.setEntity(entity);

            CloseableHttpResponse response = httpClient.execute(httpPost);

            return handleResponse(query, response);

        }
        catch (ConnectTimeoutException cte)
        {
            logger.trace(cte.getLocalizedMessage());

            // this one failed, lets move on
            invalidateEndpoint(endPoint);
            // get the next one
            endPoint = getNextEndpoint();
            if (endPoint == null) {
                throw new SQLException("All endpoints have failed, giving up");
            }


        } catch (Exception ex) {
            logger.error("Error executing query [{}] {}", query, ex.getMessage());
            throw new SQLException("Error executing update", ex);
        }
    }
}