Example usage for org.apache.http.client.methods HttpPost METHOD_NAME

List of usage examples for org.apache.http.client.methods HttpPost METHOD_NAME

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost METHOD_NAME.

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpPost METHOD_NAME.

Click Source Link

Usage

From source file:org.elasticsearch.client.RequestConverters.java

static Request multiGet(MultiGetRequest multiGetRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_mget");

    Params parameters = new Params(request);
    parameters.withPreference(multiGetRequest.preference());
    parameters.withRealtime(multiGetRequest.realtime());
    parameters.withRefresh(multiGetRequest.refresh());

    request.setEntity(createEntity(multiGetRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request index(IndexRequest indexRequest) {
    String method = Strings.hasLength(indexRequest.id()) ? HttpPut.METHOD_NAME : HttpPost.METHOD_NAME;
    boolean isCreate = (indexRequest.opType() == DocWriteRequest.OpType.CREATE);
    String endpoint = endpoint(indexRequest.index(), indexRequest.type(), indexRequest.id(),
            isCreate ? "_create" : null);
    Request request = new Request(method, endpoint);

    Params parameters = new Params(request);
    parameters.withRouting(indexRequest.routing());
    parameters.withTimeout(indexRequest.timeout());
    parameters.withVersion(indexRequest.version());
    parameters.withVersionType(indexRequest.versionType());
    parameters.withPipeline(indexRequest.getPipeline());
    parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
    parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());

    BytesRef source = indexRequest.source().toBytesRef();
    ContentType contentType = createContentType(indexRequest.getContentType());
    request.setEntity(new ByteArrayEntity(source.bytes, source.offset, source.length, contentType));
    return request;
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.CustomApiClientTests.java

@SuppressWarnings("deprecation")
public void testInvokeJsonEchoCallback() throws Throwable {
    final CountDownLatch latch = new CountDownLatch(1);

    // Container to store callback's results and do the asserts.
    final ResultsContainer container = new ResultsContainer();

    final JsonObject json = new JsonParser().parse("{\"message\": \"hello world\"}").getAsJsonObject();

    MobileServiceClient client = null;//  w w  w  .  ja v a 2 s.com
    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    client = client.withFilter(new EchoFilter());

    client.invokeApi("myApi", json, HttpPost.METHOD_NAME, null, new ApiJsonOperationCallback() {

        @Override
        public void onCompleted(JsonElement result, Exception exception, ServiceFilterResponse response) {
            if (exception != null) {
                container.setException(exception);
            } else if (result == null) {
                container.setException(new Exception("Expected result"));
            } else {
                container.setJsonResult(result);
            }

            latch.countDown();
        }
    });

    latch.await();

    // Asserts
    Exception exception = container.getException();
    if (exception != null) {
        fail(exception.getMessage());
    } else {
        assertEquals(1, container.getJsonResult().getAsJsonObject().entrySet().size());
        assertTrue(container.getJsonResult().getAsJsonObject().has("message"));
        assertEquals(json.get("message"), container.getJsonResult().getAsJsonObject().get("message"));
    }
}

From source file:org.elasticsearch.client.SearchIT.java

public void testSearchWithParentJoin() throws IOException {
    final String indexName = "child_example";
    StringEntity parentMapping = new StringEntity(
            "{\n" + "    \"mappings\": {\n" + "        \"qa\" : {\n" + "            \"properties\" : {\n"
                    + "                \"qa_join_field\" : {\n" + "                    \"type\" : \"join\",\n"
                    + "                    \"relations\" : { \"question\" : \"answer\" }\n"
                    + "                }\n" + "            }\n" + "        }\n" + "    }" + "}",
            ContentType.APPLICATION_JSON);
    client().performRequest(HttpPut.METHOD_NAME, "/" + indexName, Collections.emptyMap(), parentMapping);
    StringEntity questionDoc = new StringEntity("{\n"
            + "    \"body\": \"<p>I have Windows 2003 server and i bought a new Windows 2008 server...\",\n"
            + "    \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n"
            + "    \"tags\": [\n" + "        \"windows-server-2003\",\n" + "        \"windows-server-2008\",\n"
            + "        \"file-transfer\"\n" + "    ],\n" + "    \"qa_join_field\" : \"question\"\n" + "}",
            ContentType.APPLICATION_JSON);
    client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/1", Collections.emptyMap(),
            questionDoc);/*from  ww w . j av a 2 s  . co m*/
    StringEntity answerDoc1 = new StringEntity(
            "{\n" + "    \"owner\": {\n" + "        \"location\": \"Norfolk, United Kingdom\",\n"
                    + "        \"display_name\": \"Sam\",\n" + "        \"id\": 48\n" + "    },\n"
                    + "    \"body\": \"<p>Unfortunately you're pretty much limited to FTP...\",\n"
                    + "    \"qa_join_field\" : {\n" + "        \"name\" : \"answer\",\n"
                    + "        \"parent\" : \"1\"\n" + "    },\n"
                    + "    \"creation_date\": \"2009-05-04T13:45:37.030\"\n" + "}",
            ContentType.APPLICATION_JSON);
    client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/2",
            Collections.singletonMap("routing", "1"), answerDoc1);
    StringEntity answerDoc2 = new StringEntity(
            "{\n" + "    \"owner\": {\n" + "        \"location\": \"Norfolk, United Kingdom\",\n"
                    + "        \"display_name\": \"Troll\",\n" + "        \"id\": 49\n" + "    },\n"
                    + "    \"body\": \"<p>Use Linux...\",\n" + "    \"qa_join_field\" : {\n"
                    + "        \"name\" : \"answer\",\n" + "        \"parent\" : \"1\"\n" + "    },\n"
                    + "    \"creation_date\": \"2009-05-05T13:45:37.030\"\n" + "}",
            ContentType.APPLICATION_JSON);
    client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/3",
            Collections.singletonMap("routing", "1"), answerDoc2);
    client().performRequest(HttpPost.METHOD_NAME, "/_refresh");

    TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names", ValueType.STRING)
            .field("owner.display_name.keyword").size(10);
    ChildrenAggregationBuilder childrenAgg = new ChildrenAggregationBuilder("to-answers", "answer")
            .subAggregation(leafTermAgg);
    TermsAggregationBuilder termsAgg = new TermsAggregationBuilder("top-tags", ValueType.STRING)
            .field("tags.keyword").size(10).subAggregation(childrenAgg);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0).aggregation(termsAgg);
    SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search,
            highLevelClient()::searchAsync);
    assertSearchHeader(searchResponse);
    assertNull(searchResponse.getSuggest());
    assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
    assertEquals(3, searchResponse.getHits().totalHits);
    assertEquals(0, searchResponse.getHits().getHits().length);
    assertEquals(0f, searchResponse.getHits().getMaxScore(), 0f);
    assertEquals(1, searchResponse.getAggregations().asList().size());
    Terms terms = searchResponse.getAggregations().get("top-tags");
    assertEquals(0, terms.getDocCountError());
    assertEquals(0, terms.getSumOfOtherDocCounts());
    assertEquals(3, terms.getBuckets().size());
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket.getKeyAsString(), either(equalTo("file-transfer")).or(equalTo("windows-server-2003"))
                .or(equalTo("windows-server-2008")));
        assertEquals(1, bucket.getDocCount());
        assertEquals(1, bucket.getAggregations().asList().size());
        Children children = bucket.getAggregations().get("to-answers");
        assertEquals(2, children.getDocCount());
        assertEquals(1, children.getAggregations().asList().size());
        Terms leafTerms = children.getAggregations().get("top-names");
        assertEquals(0, leafTerms.getDocCountError());
        assertEquals(0, leafTerms.getSumOfOtherDocCounts());
        assertEquals(2, leafTerms.getBuckets().size());
        assertEquals(2, leafTerms.getBuckets().size());
        Terms.Bucket sam = leafTerms.getBucketByKey("Sam");
        assertEquals(1, sam.getDocCount());
        Terms.Bucket troll = leafTerms.getBucketByKey("Troll");
        assertEquals(1, troll.getDocCount());
    }
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request update(UpdateRequest updateRequest) throws IOException {
    String endpoint = endpoint(updateRequest.index(), updateRequest.type(), updateRequest.id(), "_update");
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);

    Params parameters = new Params(request);
    parameters.withRouting(updateRequest.routing());
    parameters.withTimeout(updateRequest.timeout());
    parameters.withRefreshPolicy(updateRequest.getRefreshPolicy());
    parameters.withWaitForActiveShards(updateRequest.waitForActiveShards());
    parameters.withDocAsUpsert(updateRequest.docAsUpsert());
    parameters.withFetchSourceContext(updateRequest.fetchSource());
    parameters.withRetryOnConflict(updateRequest.retryOnConflict());
    parameters.withVersion(updateRequest.version());
    parameters.withVersionType(updateRequest.versionType());

    // The Java API allows update requests with different content types
    // set for the partial document and the upsert document. This client
    // only accepts update requests that have the same content types set
    // for both doc and upsert.
    XContentType xContentType = null;//from   w w  w .ja va  2s  .  c  o m
    if (updateRequest.doc() != null) {
        xContentType = updateRequest.doc().getContentType();
    }
    if (updateRequest.upsertRequest() != null) {
        XContentType upsertContentType = updateRequest.upsertRequest().getContentType();
        if ((xContentType != null) && (xContentType != upsertContentType)) {
            throw new IllegalStateException("Update request cannot have different content types for doc ["
                    + xContentType + "]" + " and upsert [" + upsertContentType + "] documents");
        } else {
            xContentType = upsertContentType;
        }
    }
    if (xContentType == null) {
        xContentType = Requests.INDEX_CONTENT_TYPE;
    }
    request.setEntity(createEntity(updateRequest, xContentType));
    return request;
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.CustomApiClientTests.java

public void testInvokeRandomByteEcho() throws Throwable {

    // Container to store callback's results and do the asserts.
    final ResultsContainer container = new ResultsContainer();

    final byte[] content = UUID.randomUUID().toString().getBytes(MobileServiceClient.UTF8_ENCODING);

    MobileServiceClient client = null;/*from w  ww  .  j av a  2  s.  c om*/
    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());

        client = client.withFilter(new EchoFilter());

        List<Pair<String, String>> mockHeaders = new ArrayList<Pair<String, String>>();
        List<Pair<String, String>> mockParameters = new ArrayList<Pair<String, String>>();

        ServiceFilterResponse response = client
                .invokeApi("myApi", content, HttpPost.METHOD_NAME, mockHeaders, mockParameters).get();

        if (response == null || response.getRawContent() == null) {
            container.setException(new Exception("Expected response"));
        } else {
            container.setRawResponseContent(response.getRawContent());
        }

    } catch (Exception exception) {
        container.setException(exception);
    }

    // Asserts
    Exception exception = container.getException();
    if (exception != null) {
        fail(exception.getMessage());
    } else {
        assertEquals(content, container.getRawResponseContent());
    }
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request search(SearchRequest searchRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME,
            endpoint(searchRequest.indices(), searchRequest.types(), "_search"));

    Params params = new Params(request);
    addSearchRequestParams(params, searchRequest);

    if (searchRequest.source() != null) {
        request.setEntity(createEntity(searchRequest.source(), REQUEST_BODY_CONTENT_TYPE));
    }/*from   w  ww.ja v a  2s  .  co  m*/
    return request;
}

From source file:com.wudaosoft.net.httpclient.Request.java

public WorkerBuilder post(String url) {
    return new WorkerBuilder(this, HttpPost.METHOD_NAME, url);
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request searchScroll(SearchScrollRequest searchScrollRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_search/scroll");
    request.setEntity(createEntity(searchScrollRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}