Example usage for org.apache.http.impl.client BasicResponseHandler handleResponse

List of usage examples for org.apache.http.impl.client BasicResponseHandler handleResponse

Introduction

In this page you can find the example usage for org.apache.http.impl.client BasicResponseHandler handleResponse.

Prototype

public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException 

Source Link

Document

Returns the response body as a String if the response was successful (a 2xx status code).

Usage

From source file:com.sanjaydalvi.spacestationlocator.LoadISSLocation.java

@Override
protected String doInBackground(HttpUriRequest... params) {
    try {//from   www. j av  a2 s .  c om
        HttpUriRequest request = params[0];
        HttpResponse serverResponse = mClient.execute(request);
        BasicResponseHandler handler = new BasicResponseHandler();
        return handler.handleResponse(serverResponse);
    } catch (Exception e) {
        // TODO handle this properly
        e.printStackTrace();
        return "";
    }
}

From source file:com.zextras.zimbradrive.statustest.ConnectionTestUtils.java

public String assertHttpGetRequestResponse(URL url) throws URISyntaxException, IOException {
    HttpGet httpGet = new HttpGet(url.toURI());

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse httpResponse;//from w  w  w  .  ja  va2s  .  c o  m
    httpResponse = client.execute(httpGet);

    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    return basicResponseHandler.handleResponse(httpResponse);
}

From source file:com.zextras.zimbradrive.soap.GetAllFoldersHdlr.java

private void privateHandleRequest(ZimbraContext zimbraContext, SoapResponse soapResponse) throws IOException {
    HttpResponse response = queryDriveOnCloudServerServiceFolder(zimbraContext);
    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    String responseBody = basicResponseHandler.handleResponse(response);

    soapResponse.setQName(RESPONSE_QNAME);

    appendSoapResponseFromDriveResponseFolder(soapResponse, responseBody);
}

From source file:com.zextras.zimbradrive.soap.NewDirectoryHdlr.java

private void privateHandleRequest(ZimbraContext zimbraContext, SoapResponse soapResponse) throws IOException {

    String path = zimbraContext.getParameter(ZimbraDriveItem.F_PATH, "");
    HttpResponse response = sendNewDirectoryToDriveOnCloudServerService(zimbraContext, path);
    soapResponse.setQName(RESPONSE_QNAME);
    final int responseStatusCode = response.getStatusLine().getStatusCode();
    if (responseStatusCode >= HTTP_LOWEST_ERROR_STATUS) {
        throw new RuntimeException(Integer.toString(responseStatusCode));
    }//from w  w  w  . j a  va  2s  . c om

    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    String responseBody = basicResponseHandler.handleResponse(response);

    appendSoapResponseFromDriveResponseFolder(soapResponse, responseBody);
}

From source file:com.mlohr.hvvgti.ApiClient.java

private BaseResponse executeApiRequest(BaseRequest apiRequest) throws ApiException {
    HttpPost httpRequest = new HttpPost(getBaseUri() + apiRequest.getUriPart());
    httpRequest.setHeader(new BasicHeader("Content-Type", "application/json"));
    httpRequest.setHeader(new BasicHeader("Accept", "application/json"));
    // TODO configure user agent
    httpRequest.setHeader(new BasicHeader("geofox-auth-type", getSignatureAlgorithm().getAlgorithmString()));
    httpRequest.setHeader(new BasicHeader("geofox-auth-user", authUser));
    httpRequest.setHeader(new BasicHeader("geofox-auth-signature", generateSignature(apiRequest.getBody())));
    httpRequest.setEntity(new ByteArrayEntity(apiRequest.getBody().toString().getBytes()));
    try {//w w w .  j  a v a 2  s . com
        HttpResponse httpResponse = httpClient.execute(httpRequest);
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String responseBody = responseHandler.handleResponse(httpResponse);
        return new BaseResponse(responseBody);
    } catch (HttpResponseException e) {
        throw new ApiException("Error sending HTTP request", e.getStatusCode(), e);
    } catch (ClientProtocolException e) {
        throw new ApiException("Error sending HTTP request", e);
    } catch (JSONException e) {
        throw new ApiException("Error parsing JSON response", e);
    } catch (IOException e) {
        throw new ApiException(e);
    }
}

From source file:com.zextras.zimbradrive.soap.SearchRequestHdlr.java

private void privateHandleRequest(ZimbraContext zimbraContext, SoapResponse soapResponse) throws IOException {
    soapResponse.setQName(RESPONSE_QNAME);

    String query = zimbraContext.getParameter("query", "");
    soapResponse.setValue("query", query);

    String requestedTypesCsv = zimbraContext.getParameter("types", "");
    soapResponse.setValue("types", requestedTypesCsv);

    Boolean isCaseSensitive = false;
    if (zimbraContext.getParameterMap().containsKey(ZimbraDriveItem.F_CASESENSITIVE)) {
        isCaseSensitive = true;/* w  ww  . j a  v a2  s.c  o m*/
        soapResponse.setValue(ZimbraDriveItem.F_CASESENSITIVE, "");
    }

    if (query.equals("")) {
        return;
    }
    String parsedQuery = getStandardQuery(query);

    String[] requestedTypesArray = requestedTypesCsv.split(",");
    if (requestedTypesArray.length == 1 && requestedTypesArray[0].length() == 0) {
        requestedTypesArray = new String[] { ZimbraDriveItem.F_NODE_TYPE_FILE,
                ZimbraDriveItem.F_NODE_TYPE_FOLDER };
    }

    JSONArray defaultTypesJsonArray = new JSONArray(requestedTypesArray);
    requestedTypesCsv = defaultTypesJsonArray.toString();

    HttpResponse response = queryDriveOnCloudServerService(zimbraContext, parsedQuery, isCaseSensitive,
            requestedTypesCsv);
    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    String responseBody = basicResponseHandler.handleResponse(response); //throw HttpResponseException if status code >= 300

    mCloudHttpRequestUtils.appendArrayNodesAttributeToSoapResponse(soapResponse, responseBody);
}

From source file:com.redblackit.war.X509HttpClientTest.java

/**
 * Do request and validate response/*from  w ww  . j av a2 s  .  c o m*/
 * 
 * @param httpClientToTest
 * @param url
 * @param expectedTitle
 */
private void validateResponseToRequest(HttpClient httpClientToTest, String url, String expectedTitle)
        throws Exception {
    HttpGet request = new HttpGet(url);
    HttpResponse response = httpClientToTest.execute(request);

    logger.info("request:" + request);

    StatusLine status = response.getStatusLine();
    Assert.assertEquals("Status code:" + status, HttpStatus.SC_OK, status.getStatusCode());
    logger.info(status);

    BasicResponseHandler responseHandler = new BasicResponseHandler();
    String responseBody = responseHandler.handleResponse(response).trim();
    final int xmlstart = responseBody.indexOf("<?xml");
    if (xmlstart > 0) {
        responseBody = responseBody.substring(xmlstart);
    }
    logger.debug("responseBody*>>");
    logger.debug(responseBody);
    logger.debug("responseBody*<<");

    Pattern titlePattern = Pattern.compile("(<title>)([^<]+)(</title>)");
    Matcher matcher = titlePattern.matcher(responseBody);
    Assert.assertTrue("title element found", matcher.find());

    String title = matcher.group(2);
    Assert.assertEquals("title", expectedTitle, title);
}

From source file:com.redblackit.war.X509HttpClientTest.java

/**
 * Validate request fails (for bad certificates when server only supports
 * clientAuthMandatory e.g. GlassFish//from  w ww.  j  av  a 2  s  . c  o  m
 * 
 * @param httpClientToTest
 * @param url
 */
private void validateRequestFails(HttpClient httpClientToTest, String url) throws Exception {

    HttpGet request = new HttpGet(url);
    logger.info("request:" + request);
    try {
        HttpResponse response = httpClientToTest.execute(request);

        StatusLine status = response.getStatusLine();
        logger.error(status);

        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String responseBody = responseHandler.handleResponse(response).trim();
        final int xmlstart = responseBody.indexOf("<?xml");
        if (xmlstart > 0) {
            responseBody = responseBody.substring(xmlstart);
        }
        logger.error("responseBody*>>");
        logger.error(responseBody);
        logger.error("responseBody*<<");

        Assert.fail("expected exception for bad certifiate:but got response");
    } catch (SSLPeerUnverifiedException pue) {
        logger.debug("expected exception", pue);
    }

}

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

@Override
protected JsonElement doInBackground(File... objects) {
    if (client == null) {
        return null;
    }//w w w.  j  a  v a2 s.co  m

    final File file = objects[0];
    final long length = file.length();

    try {
        CustomMultiPartEntity multipart = new CustomMultiPartEntity(
                new CustomMultiPartEntity.ProgressListener() {
                    @Override
                    public void transferred(long num) {
                        //Log.d(TAG, "length=" + length + " num=" + num);
                        publishProgress((int) ((num * 100) / length));
                    }
                });

        multipart.addPart(file.getName(), new FileBody(file));

        HttpPost httpRequest = new HttpPost(server.getHostPort() + "/management/add-content");
        httpRequest.setEntity(multipart);

        HttpResponse serverResponse = client.execute(httpRequest);

        BasicResponseHandler handler = new BasicResponseHandler();
        String response = handler.handleResponse(serverResponse);

        Log.d(TAG, "<-------- " + response);

        return parser.parse(response).getAsJsonObject();

    } catch (Exception e) {
        this.exception = e;
        cancel(true);
    }

    return null;
}

From source file:com.zextras.zimbradrive.UploadFileHttpHandler.java

private String createResponseForZimlet(HttpResponse fileRequestResponseFromDrive) {
    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    String responseBody;// w ww  .j  a  v  a  2  s. com
    try {
        responseBody = basicResponseHandler.handleResponse(fileRequestResponseFromDrive);
    } catch (IOException e) {
        ZimbraLog.extensions.debug("Unable to create response for zimlet", e);
        throw new RuntimeException();
    }

    JSONObject jsonResponse = new JSONObject(responseBody);

    int responseCode = fileRequestResponseFromDrive.getStatusLine().getStatusCode();

    return htmlResponse(jsonResponse.toString(), responseCode);
}