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

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

Introduction

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

Prototype

public HttpPost(final String uri) 

Source Link

Usage

From source file:com.procleus.brime.utils.JSONParser.java

public JSONObject getJSONFromUrl(String url) {

    try {//from ww  w. ja v  a2 s . c  o  m
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    return jObj;
}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Register a template that is passed to the web-server where the it is
 * stored using the ID as the key. This is used later when setting up a mock
 * response.//w w w  . j av  a2 s. co m
 * 
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param template
 *            => the template that is to be registered for later use
 * @param id
 *            => key used to indicate which template is to be used when
 *            mocking a response
 * @return => returns the response
 * @throws Exception
 */
public static String registerTemplate(String server, int port, String template, String id) throws Exception {
    HttpPost post = null;
    JSONObject object = new JSONObject();

    URIBuilder builder = new URIBuilder();

    builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/template");

    post = new HttpPost(builder.toString());
    object.put("template", template);
    object.put("id", id);

    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(post);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("POST failed. Error code: " + resp.getStatusLine().getStatusCode());
    }
    return EntityUtils.toString(resp.getEntity());

}

From source file:org.activiti.rest.dmn.service.api.repository.DmnDeploymentCollectionResourceTest.java

/**
 * Test deploying single DMN file/*from   ww  w .  j  a v  a  2  s . c om*/
 */
public void testPostNewDeploymentDMNFile() throws Exception {

    try {
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX
                + DmnRestUrls.createRelativeResourceUrl(DmnRestUrls.URL_DEPLOYMENT_COLLECTION));

        httpPost.setEntity(HttpMultipartHelper.getMultiPartEntity("simple.dmn", "application/xml",
                this.getClass().getClassLoader()
                        .getResourceAsStream("org/activiti/rest/dmn/service/api/repository/simple.dmn"),
                null));
        CloseableHttpResponse response = executeBinaryRequest(httpPost, HttpStatus.SC_CREATED);

        // Check deployment
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);

        String deploymentId = responseNode.get("id").textValue();
        String name = responseNode.get("name").textValue();
        String category = responseNode.get("category").textValue();
        String deployTime = responseNode.get("deploymentTime").textValue();
        String url = responseNode.get("url").textValue();
        String tenantId = responseNode.get("tenantId").textValue();

        assertEquals("", tenantId);

        assertNotNull(deploymentId);
        assertEquals(1L, dmnRepositoryService.createDeploymentQuery().deploymentId(deploymentId).count());

        assertNotNull(name);
        assertEquals("simple.dmn", name);

        assertNotNull(url);
        assertTrue(
                url.endsWith(DmnRestUrls.createRelativeResourceUrl(DmnRestUrls.URL_DEPLOYMENT, deploymentId)));

        // No deployment-category should have been set
        assertNull(category);
        assertNotNull(deployTime);

        // Check if process is actually deployed in the deployment
        List<String> resources = dmnRepositoryService.getDeploymentResourceNames(deploymentId);
        assertEquals(1L, resources.size());
        assertEquals("simple.dmn", resources.get(0));
        assertEquals(1L, dmnRepositoryService.createDeploymentQuery().deploymentId(deploymentId).count());
    } finally {
        // Always cleanup any created deployments, even if the test failed
        List<DmnDeployment> deployments = dmnRepositoryService.createDeploymentQuery().list();
        for (DmnDeployment deployment : deployments) {
            dmnRepositoryService.deleteDeployment(deployment.getId());
        }
    }
}

From source file:org.flowable.rest.dmn.service.api.repository.DmnDeploymentCollectionResourceTest.java

/**
 * Test deploying single DMN file//from  w w w .ja va2s .c  om
 */
public void testPostNewDeploymentDMNFile() throws Exception {

    try {
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX
                + DmnRestUrls.createRelativeResourceUrl(DmnRestUrls.URL_DEPLOYMENT_COLLECTION));

        httpPost.setEntity(HttpMultipartHelper.getMultiPartEntity("simple.dmn", "application/xml",
                this.getClass().getClassLoader()
                        .getResourceAsStream("org/flowable/rest/dmn/service/api/repository/simple.dmn"),
                null));
        CloseableHttpResponse response = executeBinaryRequest(httpPost, HttpStatus.SC_CREATED);

        // Check deployment
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);

        String deploymentId = responseNode.get("id").textValue();
        String name = responseNode.get("name").textValue();
        String category = responseNode.get("category").textValue();
        String deployTime = responseNode.get("deploymentTime").textValue();
        String url = responseNode.get("url").textValue();
        String tenantId = responseNode.get("tenantId").textValue();

        assertEquals("", tenantId);

        assertNotNull(deploymentId);
        assertEquals(1L, dmnRepositoryService.createDeploymentQuery().deploymentId(deploymentId).count());

        assertNotNull(name);
        assertEquals("simple.dmn", name);

        assertNotNull(url);
        assertTrue(
                url.endsWith(DmnRestUrls.createRelativeResourceUrl(DmnRestUrls.URL_DEPLOYMENT, deploymentId)));

        // No deployment-category should have been set
        assertNull(category);
        assertNotNull(deployTime);

        // Check if process is actually deployed in the deployment
        List<String> resources = dmnRepositoryService.getDeploymentResourceNames(deploymentId);
        assertEquals(1L, resources.size());
        assertEquals("simple.dmn", resources.get(0));
        assertEquals(1L, dmnRepositoryService.createDeploymentQuery().deploymentId(deploymentId).count());
    } finally {
        // Always cleanup any created deployments, even if the test failed
        List<DmnDeployment> deployments = dmnRepositoryService.createDeploymentQuery().list();
        for (DmnDeployment deployment : deployments) {
            dmnRepositoryService.deleteDeployment(deployment.getId());
        }
    }
}

From source file:org.opentravel.otm.forum2016.am.CreateAPIOperation.java

/**
 * @see org.opentravel.otm.forum2016.am.RESTClientOperation#execute()
 *//*from w ww.  j  ava  2 s .  c om*/
@Override
public APIDetails execute() throws IOException {
    HttpPost request = new HttpPost(APIPublisherConfig.getWSO2PublisherApiBaseUrl());

    request.setHeader("Content-Type", "application/json");
    request.setEntity(new StringEntity(new Gson().toJson(api.toJson())));
    return execute(request);
}

From source file:me.kaidul.uhunt.JSONDownloader.java

public InputStreamReader getJSONStringFromUrl(String url) {

    try {//from   www  .  j  a  v  a2 s.c  o  m
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }

    try {
        return new InputStreamReader(is, "UTF-8");
    } catch (Exception e) {
        if (CommonUtils.isDebuggable) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        return null;
    }
}

From source file:ch.asadzia.cognitive.SituationAnalysis.java

public ServiceResult process() {

    HttpClient httpclient = HttpClients.createDefault();

    try {//from  w  ww.  j ava2 s .co  m
        URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/vision/v1.0/analyze");

        builder.setParameter("visualFeatures", "Categories,Tags,Description,Faces,Adult");

        URI uri = builder.build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/octet-stream");
        request.setHeader("Ocp-Apim-Subscription-Key", apikey);

        // Request body
        FileEntity reqEntity = new FileEntity(imageData);
        request.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            String responseStr = EntityUtils.toString(entity);

            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                System.err.println(responseStr);
                return null;
            }

            ServiceResult result = translateSituation(responseStr);

            System.out.println(responseStr);

            return result;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return null;
}

From source file:org.dasein.cloud.aws.storage.GlacierAction.java

public HttpRequestBase getMethod(String url) throws InternalException {

    switch (GlacierAction.this) {
    case DELETE_VAULT:
    case DELETE_ARCHIVE:
        return new HttpDelete(url);
    case LIST_VAULTS:
    case DESCRIBE_VAULT:
    case DESCRIBE_JOB:
    case LIST_JOBS:
    case GET_JOB_OUTPUT:
        return new HttpGet(url);
    case CREATE_VAULT:
        return new HttpPut(url);
    case CREATE_ARCHIVE:
    case CREATE_JOB:
        return new HttpPost(url);
    }//from   w w w  .j  av  a 2  s.c o  m
    throw new InternalException("failed to build method");
}

From source file:org.dataconservancy.ui.it.support.ListCollectionsRequest.java

public HttpPost asHttpPost() {

    HttpPost post = null;/*from  www .ja va 2 s.  c  o  m*/
    try {
        post = new HttpPost(urlConfig.getListCollectionsUrl().toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(STRIPES_EVENT, "List Collections"));

    HttpEntity entity = null;
    try {
        entity = new UrlEncodedFormEntity(params, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    post.setEntity(entity);

    return post;
}

From source file:com.servoy.extensions.plugins.http.PostRequest.java

public PostRequest(String url, DefaultHttpClient hc, IClientPluginAccess plugin) {
    super(url, hc, new HttpPost(url), plugin);
}