Example usage for org.apache.http.util EntityUtils toString

List of usage examples for org.apache.http.util EntityUtils toString

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toString.

Prototype

public static String toString(HttpEntity httpEntity) throws IOException, ParseException 

Source Link

Usage

From source file:org.kitodo.data.elasticsearch.index.type.UserGroupTypeTest.java

@Test
public void shouldCreateDocument() throws Exception {
    UserGroupType userGroupType = new UserGroupType();
    JSONParser parser = new JSONParser();

    UserGroup userGroup = prepareData().get(0);
    HttpEntity document = userGroupType.createDocument(userGroup);
    JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document));
    JSONObject expected = (JSONObject) parser
            .parse("{\"title\":\"Administrator\",\"permission\":1," + "\"users\":[{\"id\":1},{\"id\":2}]}");
    assertEquals("UserGroup JSONObject doesn't match to given JSONObject!", expected, actual);

    userGroup = prepareData().get(1);//  ww  w . j  av a2  s . c  o m
    document = userGroupType.createDocument(userGroup);
    actual = (JSONObject) parser.parse(EntityUtils.toString(document));
    expected = (JSONObject) parser.parse("{\"title\":\"Random\",\"permission\":4,\"users\":[]}");
    assertEquals("UserGroup JSONObject doesn't match to given JSONObject!", expected, actual);
}

From source file:jp.bugscontrol.github.GithubTask.java

@Override
protected Void doInBackground(final Void... p) {
    try {//from   ww w .  ja  v  a  2 s .com
        // Send the request
        final HttpClient httpClient = new DefaultHttpClient();
        final HttpGet httpGet = new HttpGet(
                method.contains("http") ? method : "https://api.github.com" + method);
        httpGet.addHeader("Authorization", "token " + server.getPassword());
        httpGet.addHeader("Accept", "application/vnd.github.v3+json");
        HttpEntity entity = httpClient.execute(httpGet).getEntity();
        response = EntityUtils.toString(entity);
    } catch (final Exception e) {
        e.printStackTrace();
    }

    listener.doInBackground(response);
    return null;
}

From source file:org.bishoph.oxdemo.util.RequestVisibleFolders.java

@Override
protected JSONObject doInBackground(Object... params) {
    try {/*  w  w w.ja  v a2 s  .  com*/
        String uri = (String) params[0];
        Log.d("OXDemo", "Execute put request " + uri);
        HttpPut httput = new HttpPut(uri);
        httput.setHeader("Accept", "application/json, text/javascript");
        HttpResponse response = httpclient.execute(httput, localcontext);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        Log.d("OXDemo", "<<<<<<<\n" + result + "\n\n");
        JSONObject jsonObj = new JSONObject(result);
        return jsonObj;
    } catch (JSONException e) {
        Log.e("OXDemo", e.getMessage());
    } catch (ClientProtocolException e) {
        Log.e("OXDemo", e.getMessage());
    } catch (IOException e) {
        Log.e("OXDemo", e.getMessage());
    }
    return null;
}

From source file:org.cvasilak.jboss.mobile.admin.net.JBossResponseHandler.java

public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
    StatusLine statusLine = response.getStatusLine();

    if (statusLine.getStatusCode() >= 300 && statusLine.getStatusCode() < 500) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*from   ww  w. j  a  v  a2s . c  o  m*/

    HttpEntity entity = response.getEntity();
    return entity == null ? null : EntityUtils.toString(entity);
}

From source file:io.wcm.maven.plugins.contentpackage.httpaction.BundleStatusCall.java

@Override
public BundleStatus execute() throws MojoExecutionException {
    if (log.isDebugEnabled()) {
        log.debug("Call URL: " + bundleStatusURL);
    }//  w w w . j ava2 s .c  o  m

    HttpGet method = new HttpGet(bundleStatusURL);
    try (CloseableHttpResponse response = httpClient.execute(method)) {

        String responseString = EntityUtils.toString(response.getEntity());
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new MojoExecutionException("Failure:\n" + responseString);
        }

        JSONObject jsonResponse = new JSONObject(responseString);
        return toBundleStatus(jsonResponse);
    } catch (IOException ex) {
        throw new MojoExecutionException("Can't determine bundles state via URL: " + bundleStatusURL, ex);
    }
}

From source file:com.redhat.refarch.microservices.warehouse.service.WarehouseService.java

public void fulfillOrder(Result result) throws Exception {

    HttpClient client = new DefaultHttpClient();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("status", "Shipped");

    URIBuilder uriBuilder = new URIBuilder("http://gateway-service:9091/customers/" + result.getCustomerId()
            + "/orders/" + result.getOrderNumber());
    HttpPatch patch = new HttpPatch(uriBuilder.build());
    patch.setEntity(new StringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON));
    logInfo("Executing " + patch);
    HttpResponse response = client.execute(patch);
    String responseString = EntityUtils.toString(response.getEntity());
    logInfo("Got response " + responseString);
}

From source file:guru.nidi.ramltester.HttpCommonsTest.java

@Test
public void testServletOk() throws IOException {
    final HttpGet get = new HttpGet(url("base/data"));
    final HttpResponse response = client.execute(get);
    assertEquals("\"json string\"", EntityUtils.toString(response.getEntity()));
    assertTrue(client.getLastReport().isEmpty());
}

From source file:io.wcm.maven.plugins.contentpackage.httpaction.PackageManagerJsonCall.java

@Override
public JSONObject execute() throws MojoExecutionException {
    if (log.isDebugEnabled()) {
        log.debug("Call URL: " + method.getURI());
    }/*  w w  w. jav a2 s.c o m*/

    try (CloseableHttpResponse response = httpClient.execute(method)) {
        JSONObject jsonResponse = null;

        String responseString = EntityUtils.toString(response.getEntity());
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            // get response JSON
            if (responseString != null) {
                try {
                    jsonResponse = new JSONObject(responseString);
                } catch (JSONException ex) {
                    throw new MojoExecutionException("Error parsing JSON response.\n" + responseString, ex);
                }
            }
            if (jsonResponse == null) {
                jsonResponse = new JSONObject();
                jsonResponse.put("success", false);
                jsonResponse.put("msg", "Invalid response (null).");
            }

        } else {
            throw new MojoExecutionException(
                    "Call failed with HTTP status " + response.getStatusLine().getStatusCode() + " "
                            + response.getStatusLine().getReasonPhrase() + "\n" + responseString);
        }

        return jsonResponse;
    } catch (IOException ex) {
        throw new MojoExecutionException("Http method failed.", ex);
    }
}

From source file:com.odo.kcl.mobileminer.OpenBmapCellRequest.java

@Override
protected Object doInBackground(Object... cellSpec) {
    if (cellSpec.length < 4)
        return null;
    String Mcc, Mnc, Lac, Id;//www.j  av  a  2s .c  om
    Mcc = (String) cellSpec[0];
    Mnc = (String) cellSpec[1];
    Lac = (String) cellSpec[2];
    Id = (String) cellSpec[3];
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.openbmap.org/api/getGPSfromGSM.php");
    List<NameValuePair> postData = new ArrayList<NameValuePair>(4);
    postData.add(new BasicNameValuePair("mcc", Mcc));
    postData.add(new BasicNameValuePair("mnc", Mnc));
    postData.add(new BasicNameValuePair("lac", Lac));
    postData.add(new BasicNameValuePair("cell_id", Id));
    try {
        String XMLdump, Lat, Long, poly, polyDump;
        Lat = null;
        Long = null;
        poly = null;
        post.setEntity(new UrlEncodedFormEntity(postData));
        XMLdump = EntityUtils.toString(client.execute(post).getEntity());
        //Log.i("MobileMiner",XMLdump);
        Matcher latMatch = latPattern.matcher(XMLdump);
        while (latMatch.find())
            Lat = latMatch.group(1);
        Matcher longMatch = longPattern.matcher(XMLdump);
        while (longMatch.find())
            Long = longMatch.group(1);
        Matcher polyMatch = polyPattern.matcher(XMLdump);
        while (polyMatch.find())
            poly = polyMatch.group(1);
        //Log.i("MobileMiner","Lat "+Lat);
        //Log.i("MobileMiner","Long "+Long);
        //Log.i("MobileMiner","Poly "+poly);
        if (poly != null) {
            List<String> points = new ArrayList<String>();
            String[] point;
            String[] polyChunks = poly.split(",");
            for (String chunk : polyChunks) {
                point = chunk.split("\\s+");
                points.add("[" + point[1] + "," + point[0] + "]");
            }
            polyDump = "[" + TextUtils.join(",", points.subList(0, points.size() - 1)) + "]";
            //Log.i("MobileMiner",polyDump);
        } else {
            polyDump = null;
        }
        if (Lat != null && Long != null) {
            return new String[] { Lat, Long, polyDump };
        } else {
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    // TODO Auto-generated method stub
    return null;
}

From source file:bit.changepurse.wdk.util.CheckedExceptionMethods.java

public static String toString(HttpEntity entity) {
    try {/*from w  ww  .  j a va 2 s .  c  o  m*/
        return entity != null ? EntityUtils.toString(entity) : "";
    } catch (ParseException | IOException e) {
        throw new UncheckedException(e);
    }
}