Example usage for org.apache.http.entity StringEntity setContentType

List of usage examples for org.apache.http.entity StringEntity setContentType

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity setContentType.

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetFormVerifyDelete() throws Exception {
    System.out.println("postGetFormVerifyDelete starts");

    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getJson);
    input.setContentType("application/json");
    httpPost.setEntity(input);//from  w  ww .ja va  2s .  c  o  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        assertTrue(json.contains("cannot be found"));

        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
    System.out.println("postGetFormVerifyDelete ends");
}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetUserByEmail() throws Exception {
    System.out.println("postGetUserByEmail starts");
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getUserByEmail);
    input.setContentType("application/json");
    httpPost.setEntity(input);//from   www. j a va 2s.c  o  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
    System.out.println("postGetUserByEmail ends");
}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetForm() throws Exception {
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getJson);
    input.setContentType("application/json");
    httpPost.setEntity(input);/*from  w  ww  . j  a v  a  2  s . co m*/
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        ObjectMapper mapper = ServiceLocator.getInstance().getMapper();
        Map<String, Object> jsonMap = mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {
        });
        Map<String, Object> result = (Map<String, Object>) jsonMap.get("data");
        assertTrue(result.size() == 3);
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }

}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetUserByUserId() throws Exception {
    System.out.println("postGetUserByUserId starts");
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getUserByUserId);
    input.setContentType("application/json");
    httpPost.setEntity(input);//  w  w  w  .  j  a  va  2s . c  o  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
    System.out.println("postGetUserByUserId ends");
}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

/**
 * Set ownerToken in case any action uses it to do update. For example delUser
 * @throws Exception/*from ww  w . jav a  2 s .  c om*/
 */
private void signInOwner() throws Exception {
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(signInOwner);
    input.setContentType("application/json");
    httpPost.setEntity(input);
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        Map<String, Object> jsonMap = ServiceLocator.getInstance().getMapper().readValue(json,
                new TypeReference<HashMap<String, Object>>() {
                });
        ownerToken = "Bearer " + (String) jsonMap.get("accessToken");
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

/**
 * Login as the new test user/*  ww  w  . ja  v  a2  s.com*/
 * @throws Exception
 */
private void signInUser() throws Exception {
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(signInByUserId);
    input.setContentType("application/json");
    httpPost.setEntity(input);
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        // make sure there is an accessToken in the json.
        assertTrue(json.contains("accessToken"));
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetMenu() throws Exception {
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getMenuJson);
    input.setContentType("application/json");
    httpPost.setEntity(input);//ww w.j a  va  2 s.  c  o  m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        ObjectMapper mapper = ServiceLocator.getInstance().getMapper();
        Map<String, Object> jsonMap = mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {
        });
        List<Map<String, Object>> result = (List<Map<String, Object>>) jsonMap.get("data");
        assertTrue(result.size() == 5);
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }

}

From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java

private void postGetFormVerifyUpdate() throws Exception {
    HttpPost httpPost = new HttpPost("http://example:8080/api/rs");
    StringEntity input = new StringEntity(getJson);
    input.setContentType("application/json");
    httpPost.setEntity(input);//w  ww  .  j a va  2 s .c  o m
    CloseableHttpResponse response = httpclient.execute(httpPost);

    try {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String json = "";
        String line = "";
        while ((line = rd.readLine()) != null) {
            json = json + line;
        }
        System.out.println("json = " + json);
        ObjectMapper mapper = ServiceLocator.getInstance().getMapper();
        Map<String, Object> jsonMap = mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {
        });
        Map<String, Object> result = (Map<String, Object>) jsonMap.get("data");
        assertTrue(result.size() == 3);

        Map<String, Object> schema = (Map<String, Object>) result.get("schema");
        String title = (String) schema.get("title");
        assertEquals(title, "Updated Comment");

        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
}

From source file:org.wso2.carbon.appmgt.impl.publishers.WSO2ExternalAppStorePublisher.java

/**
 * Add tags to web app created in the external store.
 *
 * @param webApp        Web App in the current store
 * @param assetId       UUID of web app created in the external store
 * @param storeEndpoint Publisher url of external store
 * @param httpContext   HttpContext/*  ww w  .  j a va 2 s  . c  o  m*/
 * @param httpClient
 * @throws AppManagementException
 */
private void addTags(WebApp webApp, String assetId, String storeEndpoint, HttpContext httpContext,
        HttpClient httpClient) throws AppManagementException {

    if (log.isDebugEnabled()) {
        String msg = String.format("Adding tags to web app : %s in external store ", webApp.getApiName());
        log.debug(msg);
    }

    try {
        storeEndpoint = storeEndpoint + AppMConstants.APP_STORE_ADD_TAGS_URL + assetId;
        HttpPut httpPut = new HttpPut(storeEndpoint);

        String tags = getTagsAsJsonString(webApp);
        if (tags != null) { //web app have  tags
            StringEntity input = new StringEntity(tags);
            input.setContentType("application/json");
            httpPut.setEntity(input);
            HttpResponse response = httpClient.execute(httpPut, httpContext);
            HttpEntity entity = response.getEntity();
            String responseString = "";
            if (entity != null) {
                //{"status" : 200, "ok" : true}
                responseString = EntityUtils.toString(entity, "UTF-8");
                EntityUtils.consume(entity);
            }
            JSONObject responseJson = (JSONObject) JSONValue.parse(responseString);
            boolean status = false;
            if (responseJson != null) {
                Object statusOk = responseJson.get("ok");
                if (statusOk != null)
                    status = Boolean.parseBoolean(statusOk.toString().trim());
            }

            if (!status) { //If tags successfully added
                throw new AppManagementException("Error while adding tags to web app :" + webApp.getApiName()
                        + ". Reason :" + responseString);
            }
        }
    } catch (IOException e) {
        throw new AppManagementException("Error while adding  tags to web app :" + webApp.getApiName(), e);
    }

}

From source file:zswi.protocols.communication.core.HTTPSConnection.java

/**
   This method provides adding VCard to contacts.
   @param card VCard object/*from  ww  w . j a va  2s. c  o  m*/
   @return true if adding was successful, otherwise return false
 */
public boolean addVCard(VCard card) throws ClientProtocolException, IOException, URISyntaxException {
    VCardWriter wr = new VCardWriter();
    wr.setVCard(card);

    StringEntity se = new StringEntity(wr.buildVCardString(), "UTF-8");
    se.setContentType(TYPE_VCARD);
    return this.addVCard(se, defaultContactsPath);
}