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:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.CustomApiClientTests.java

public void testInvokeTypedSingleInteger() throws Throwable {

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

    final Integer i = 42;

    MobileServiceClient client = null;// w w  w  .  j a va2 s  .c  o m

    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());

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

        Integer result = client.invokeApi("myApi", i, HttpPost.METHOD_NAME, null, Integer.class).get();

        if (result == null) {
            container.setException(new Exception("Expected one integer result"));
        } else {
            container.setCustomResult(result);
        }

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

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

From source file:net.community.chest.gitcloud.facade.frontend.git.GitController.java

private StatusLine executeRemoteRequest(HttpRequestBase request, HttpServletRequest req,
        HttpServletResponse rsp) throws IOException {
    copyRequestHeadersValues(req, request);

    final CloseableHttpResponse response;
    if (HttpPost.METHOD_NAME.equalsIgnoreCase(request.getMethod())) {
        response = transferPostedData((HttpEntityEnclosingRequestBase) request, req);
    } else {//from w w w  . j  a  v a2  s .c om
        response = client.execute(request);
    }

    try {
        HttpEntity rspEntity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if ((statusCode < HttpServletResponse.SC_OK) || (statusCode >= 300)) {
            String reason = StringUtils.trimToEmpty(statusLine.getReasonPhrase());
            logger.warn("executeRemoteRequest(" + req.getMethod() + ")[" + req.getRequestURI() + "]["
                    + req.getQueryString() + "]" + " bad response (" + statusCode + ") from remote end: "
                    + reason);
            EntityUtils.consume(rspEntity);
            rsp.sendError(statusCode, reason);
        } else {
            rsp.setStatus(statusCode);

            copyResponseHeadersValues(req, response, rsp);
            transferBackendResponse(req, rspEntity, rsp);
        }

        return statusLine;
    } finally {
        response.close();
    }
}

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

public void testInvokeTypedSingleFloat() throws Throwable {

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

    final Float f = 3.14f;

    MobileServiceClient client = null;//from   ww  w.  j  a v a2s  .  c  o  m

    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());
        client = client.withFilter(new EchoFilter());

        Float result = client.invokeApi("myApi", f, HttpPost.METHOD_NAME, null, Float.class).get();

        if (result == null) {
            container.setException(new Exception("Expected one float result"));
        } else {
            container.setCustomResult(result);
        }

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

    }

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

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

public void testInvokeTypedSingleBoolean() throws Throwable {

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

    final Boolean b = true;

    MobileServiceClient client = null;/* w  w  w.jav a 2  s . c o m*/
    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());

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

        Boolean result = client.invokeApi("myApi", b, HttpPost.METHOD_NAME, null, Boolean.class).get();

        if (result == null) {
            container.setException(new Exception("Expected one boolean result"));
        } else {
            container.setCustomResult(result);
        }

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

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

From source file:com.azure.webapi.MobileServiceClient.java

/**
 * /*w w  w.j a  v a  2s.c  o m*/
 * @param apiName
 *            The API name
 * @param content
 *            The byte array to send as the request body
 * @param httpMethod
 *            The HTTP Method used to invoke the API
 * @param requestHeaders
 *            The extra headers to send in the request
 * @param parameters
 *            The query string parameters sent in the request
 * @param callback
 *            The callback to invoke after the API execution
 */
public void invokeApi(String apiName, byte[] content, String httpMethod,
        List<Pair<String, String>> requestHeaders, List<Pair<String, String>> parameters,
        final ServiceFilterResponseCallback callback) {

    if (apiName == null || apiName.trim().equals("")) {
        if (callback != null) {
            callback.onResponse(null, new IllegalArgumentException("apiName cannot be null"));
        }
        return;
    }

    if (httpMethod == null || httpMethod.trim().equals("")) {
        if (callback != null) {
            callback.onResponse(null, new IllegalArgumentException("httpMethod cannot be null"));
        }
        return;
    }

    Uri.Builder uriBuilder = Uri.parse(getAppUrl().toString()).buildUpon();
    uriBuilder.path(CUSTOM_API_URL + apiName);

    if (parameters != null && parameters.size() > 0) {
        for (Pair<String, String> parameter : parameters) {
            uriBuilder.appendQueryParameter(parameter.first, parameter.second);
        }
    }

    ServiceFilterRequest request;
    String url = uriBuilder.build().toString();

    if (httpMethod.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpGet(url));
    } else if (httpMethod.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPost(url));
    } else if (httpMethod.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPut(url));
    } else if (httpMethod.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpPatch(url));
    } else if (httpMethod.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
        request = new ServiceFilterRequestImpl(new HttpDelete(url));
    } else {
        if (callback != null) {
            callback.onResponse(null, new IllegalArgumentException("httpMethod not supported"));
        }
        return;
    }

    if (requestHeaders != null && requestHeaders.size() > 0) {
        for (Pair<String, String> header : requestHeaders) {
            request.addHeader(header.first, header.second);
        }
    }

    if (content != null) {
        try {
            request.setContent(content);
        } catch (Exception e) {
            if (callback != null) {
                callback.onResponse(null, e);
            }
            return;
        }
    }

    MobileServiceConnection conn = createConnection();

    // Create AsyncTask to execute the request and parse the results
    new RequestAsyncTask(request, conn) {
        @Override
        protected void onPostExecute(ServiceFilterResponse response) {
            if (callback != null) {
                callback.onResponse(response, mTaskException);
            }
        }
    }.execute();
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static int resetFailedTransfers() {
    int failed = 0;
    failed += resetFailedTransfers(HttpPost.METHOD_NAME);
    failed += resetFailedTransfers(HttpGet.METHOD_NAME);
    return failed;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static int resetFailedTransfers(String method) {
    String ongoing;/*from   w  w  w  .  ja  v  a 2  s.co  m*/
    String failed;
    if (HttpPost.METHOD_NAME.equals(method)) {
        ongoing = ResourceState.STATE_POSTING;
        failed = ResourceState.STATE_POSTING_FAILED;
    } else if (HttpGet.METHOD_NAME.equals(method)) {
        ongoing = ResourceState.STATE_GETTING;
        failed = ResourceState.STATE_GETTING_FAILED;
    } else {
        Log.e(TAG, "Bad method name: " + method);
        return 0;
    }

    final ContentValues values = new ContentValues(1);
    values.put(Nodes.NODE_RESOURCE_STATE, failed);
    final String where = Nodes.NODE_RESOURCE_STATE + "=?";
    final String[] selectionArgs = new String[] { ongoing };
    return sResolver.update(Nodes.CONTENT_URI, values, where, selectionArgs);
}

From source file:org.trancecode.xproc.step.RequestParser.java

private HttpRequestBase constructMethod(final String method, final URI hrefUri) {
    final HttpEntity httpEntity = request.getEntity();
    final HeaderGroup headers = request.getHeaders();
    if (StringUtils.equalsIgnoreCase(HttpPost.METHOD_NAME, method)) {
        final HttpPost httpPost = new HttpPost(hrefUri);
        for (final Header h : headers.getAllHeaders()) {
            if (!StringUtils.equalsIgnoreCase("Content-Type", h.getName())) {
                httpPost.addHeader(h);//from w  w  w .ja v a2  s.  c  o m
            }
        }
        httpPost.setEntity(httpEntity);
        return httpPost;
    } else if (StringUtils.equalsIgnoreCase(HttpPut.METHOD_NAME, method)) {
        final HttpPut httpPut = new HttpPut(hrefUri);
        httpPut.setEntity(httpEntity);
        return httpPut;
    } else if (StringUtils.equalsIgnoreCase(HttpDelete.METHOD_NAME, method)) {
        final HttpDelete httpDelete = new HttpDelete(hrefUri);
        httpDelete.setHeaders(headers.getAllHeaders());
        return httpDelete;
    } else if (StringUtils.equalsIgnoreCase(HttpGet.METHOD_NAME, method)) {
        final HttpGet httpGet = new HttpGet(hrefUri);
        httpGet.setHeaders(headers.getAllHeaders());
        return httpGet;

    } else if (StringUtils.equalsIgnoreCase(HttpHead.METHOD_NAME, method)) {
        final HttpHead httpHead = new HttpHead(hrefUri);
        httpHead.setHeaders(headers.getAllHeaders());
        return httpHead;
    }
    return null;
}

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

public void testInvokeTypedMultipleObject() throws Throwable {

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

    final PersonTestObject p1 = new PersonTestObject("john", "doe", 30);
    final PersonTestObject p2 = new PersonTestObject("jane", "does", 31);

    MobileServiceClient client = null;/*from   w  w  w .jav a  2  s .c o m*/
    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());

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

        List<PersonTestObject> people = new ArrayList<PersonTestObject>();
        people.add(p1);
        people.add(p2);

        PersonTestObject[] entities = client
                .invokeApi("myApi", people, HttpPost.METHOD_NAME, null, PersonTestObject[].class).get();

        if (entities == null || entities.length != 2) {
            container.setException(new Exception("Expected two person result"));
        } else {
            container.setPeopleResult(entities);
        }

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

    // Asserts
    Exception exception = container.getException();
    if (exception != null) {
        fail(exception.getMessage());
    } else {
        assertEquals(p1.getFirstName(), container.getPeopleResult().get(0).getFirstName());
        assertEquals(p1.getLastName(), container.getPeopleResult().get(0).getLastName());
        assertEquals(p1.getAge(), container.getPeopleResult().get(0).getAge());

        assertEquals(p2.getFirstName(), container.getPeopleResult().get(1).getFirstName());
        assertEquals(p2.getLastName(), container.getPeopleResult().get(1).getLastName());
        assertEquals(p2.getAge(), container.getPeopleResult().get(1).getAge());
    }
}

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

public void testInvokeJsonEcho() throws Throwable {

    // 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;//from   ww  w  .j  av  a  2s  . c  o  m
    try {
        client = new MobileServiceClient(appUrl, appKey, getInstrumentation().getTargetContext());

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

        JsonElement result = client.invokeApi("myApi", json, HttpPost.METHOD_NAME, null).get();

        if (result == null) {
            container.setException(new Exception("Expected result"));
        } else {
            container.setJsonResult(result);
        }

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

    // 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"));
    }
}