Example usage for org.apache.http.client ResponseHandler handleResponse

List of usage examples for org.apache.http.client ResponseHandler handleResponse

Introduction

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

Prototype

T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;

Source Link

Document

Processes an HttpResponse and returns some value corresponding to that response.

Usage

From source file:cn.org.once.cstack.cli.rest.RestUtils.java

/**
 * sendPostCommand/*from  ww w . j a v a  2 s  .  c o m*/
 *
 * @param url
 * @param credentials
 * @param entity
 * @return
 * @throws ClientProtocolException
 */
public Map<String, Object> sendPostCommand(String url, Map<String, Object> credentials, String entity)
        throws ManagerResponseException {
    Map<String, Object> response = new HashMap<String, Object>();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    try {
        StringEntity stringEntity = new StringEntity(entity);
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext);
        ResponseHandler<String> handler = new CustomResponseErrorHandler();
        String body = handler.handleResponse(httpResponse);
        response.put(BODY, body);
        httpResponse.close();
    } catch (Exception e) {
        throw new ManagerResponseException(e.getMessage(), e);
    }

    return response;
}

From source file:cn.org.once.cstack.cli.rest.RestUtils.java

public Map<String, String> connect(String url, Map<String, Object> parameters) throws ManagerResponseException {

    Map<String, String> response = new HashMap<String, String>();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login")));
    nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password")));
    localContext = HttpClientContext.create();
    localContext.setCookieStore(new BasicCookieStore());
    HttpPost httpPost = new HttpPost(url);
    try {//from w  w  w. j  a  va 2 s .  c o m
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext);
        ResponseHandler<String> handler = new CustomResponseErrorHandler();
        String body = handler.handleResponse(httpResponse);
        response.put(BODY, body);
        httpResponse.close();
    } catch (Exception e) {
        authentificationUtils.getMap().clear();
        throw new ManagerResponseException(e.getMessage(), e);
    }

    return response;
}

From source file:cn.org.once.cstack.cli.rest.RestUtils.java

/**
 * sendPutCommand//w w  w .  j  av  a2 s . c om
 *
 * @param url
 * @param parameters
 * @return
 * @throws ClientProtocolException
 */
public Map<String, Object> sendPutCommand(String url, Map<String, Object> credentials,
        Map<String, String> parameters) throws ManagerResponseException {
    Map<String, Object> response = new HashMap<String, Object>();
    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPut httpPut = new HttpPut(url);
    httpPut.setHeader("Accept", "application/json");
    httpPut.setHeader("Content-type", "application/json");

    try {
        ObjectMapper mapper = new ObjectMapper();
        StringEntity entity = new StringEntity(mapper.writeValueAsString(parameters));
        httpPut.setEntity(entity);
        CloseableHttpResponse httpResponse = httpclient.execute(httpPut, localContext);
        ResponseHandler<String> handler = new CustomResponseErrorHandler();
        String body = handler.handleResponse(httpResponse);
        response.put(BODY, body);

        httpResponse.close();
    } catch (Exception e) {
        throw new ManagerResponseException(e.getMessage(), e);
    }

    return response;
}

From source file:org.labkey.freezerpro.export.GetFreezersCommand.java

public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
    HttpPost post = new HttpPost(_url);

    try {/*from ww w.  j  a v  a2s .  c o  m*/
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("method", "freezers"));
        params.add(new BasicNameValuePair("username", _username));
        params.add(new BasicNameValuePair("password", _password));

        post.setEntity(new UrlEncodedFormEntity(params));

        ResponseHandler<String> handler = new BasicResponseHandler();

        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            return new GetFreezersResponse(_export, handler.handleResponse(response), status.getStatusCode(),
                    job);
        else
            return new GetFreezersResponse(_export, status.getReasonPhrase(), status.getStatusCode(), job);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.labkey.freezerpro.export.ExportLocationCommand.java

public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
    HttpPost post = new HttpPost(_url);

    try {//w w  w. j a  v a  2s  .c om
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("method", "vials_sample"));
        params.add(new BasicNameValuePair("username", _username));
        params.add(new BasicNameValuePair("password", _password));
        params.add(new BasicNameValuePair("start", String.valueOf(_start)));
        params.add(new BasicNameValuePair("limit", String.valueOf(_limit)));

        post.setEntity(new UrlEncodedFormEntity(params));

        ResponseHandler<String> handler = new BasicResponseHandler();

        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            return new ExportLocationResponse(_export, handler.handleResponse(response), status.getStatusCode(),
                    job);
        else
            return new ExportLocationResponse(_export, status.getReasonPhrase(), status.getStatusCode(), job);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.labkey.freezerpro.export.ExportSampleUserFieldsCommand.java

public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
    HttpPost post = new HttpPost(_url);

    try {/*from   ww  w.jav a2  s  .  co m*/
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("method", "sample_userfields"));
        params.add(new BasicNameValuePair("username", _username));
        params.add(new BasicNameValuePair("password", _password));
        params.add(new BasicNameValuePair("id", _sampleId));

        post.setEntity(new UrlEncodedFormEntity(params));

        ResponseHandler<String> handler = new BasicResponseHandler();

        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            return new ExportUserFieldsResponse(_export, handler.handleResponse(response),
                    status.getStatusCode(), job);
        else
            return new ExportUserFieldsResponse(_export, status.getReasonPhrase(), status.getStatusCode(), job);
    } catch (Exception e) {
        _export.getJob().error(
                "An error was encountered trying to get user defined fields for sample : " + _sampleId, e);
        return null;
    }
}

From source file:org.labkey.freezerpro.export.GetFreezerSamplesCommand.java

public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
    HttpPost post = new HttpPost(_url);

    try {/*  w w  w. ja va  2  s.  com*/
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("method", "freezer_samples"));
        params.add(new BasicNameValuePair("username", _username));
        params.add(new BasicNameValuePair("password", _password));
        params.add(new BasicNameValuePair("id", _typeId));
        params.add(new BasicNameValuePair("limit", "10000"));

        post.setEntity(new UrlEncodedFormEntity(params));

        ResponseHandler<String> handler = new BasicResponseHandler();

        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            return new GetFreezerSamplesResponse(_export, handler.handleResponse(response),
                    status.getStatusCode(), job);
        else
            return new GetFreezerSamplesResponse(_export, status.getReasonPhrase(), status.getStatusCode(),
                    job);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.labkey.freezerpro.export.ExportSamplesCommand.java

public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
    HttpPost post = new HttpPost(_url);

    try {//w  w  w.j  a  v a2  s .co  m
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("method", "search_samples"));
        params.add(new BasicNameValuePair("username", _username));
        params.add(new BasicNameValuePair("password", _password));

        if (_searchFilterString != null)
            params.add(new BasicNameValuePair("query", _searchFilterString));
        else
            params.add(new BasicNameValuePair("query", ""));

        params.add(new BasicNameValuePair("start", String.valueOf(_startRecord)));
        params.add(new BasicNameValuePair("limit", String.valueOf(_limit)));

        post.setEntity(new UrlEncodedFormEntity(params));

        ResponseHandler<String> handler = new BasicResponseHandler();
        HttpResponse response = client.execute(post);
        StatusLine status = response.getStatusLine();

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            return new ExportSamplesResponse(_export, handler.handleResponse(response), status.getStatusCode(),
                    job);
        else
            return new ExportSamplesResponse(_export, status.getReasonPhrase(), status.getStatusCode(), job);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.juddi.v3.client.mapping.wsdl.WSDLLocatorImpl.java

private InputSource getImportFromUrl(String url) {
    InputSource inputSource = null;
    DefaultHttpClient httpclient = null;
    try {/*from w ww .  j  a v  a 2 s.com*/
        URL url2 = new URL(url);
        if (!url.toLowerCase().startsWith("http")) {
            return getImportFromFile(url);
        }
        boolean usessl = false;
        int port = 80;
        if (url.toLowerCase().startsWith("https://")) {
            port = 443;
            usessl = true;
        }

        if (url2.getPort() > 0) {
            port = url2.getPort();
        }

        if (ignoreSSLErrors && usessl) {
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("https", port, new MockSSLSocketFactory()));
            ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
            httpclient = new DefaultHttpClient(cm);
        } else {
            httpclient = new DefaultHttpClient();
        }

        if (username != null && username.length() > 0 && password != null && password.length() > 0) {

            httpclient.getCredentialsProvider().setCredentials(new AuthScope(url2.getHost(), port),
                    new UsernamePasswordCredentials(username, password));
        }
        HttpGet httpGet = new HttpGet(url);
        try {

            HttpResponse response1 = httpclient.execute(httpGet);
            //System.out.println(response1.getStatusLine());
            // HttpEntity entity1 = response1.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String handleResponse = responseHandler.handleResponse(response1);
            StringReader sr = new StringReader(handleResponse);
            inputSource = new InputSource(sr);

        } finally {
            httpGet.releaseConnection();

        }

        //  InputStream inputStream = importUrl.openStream();
        //inputSource = new InputSource(inputStream);
        latestImportURI = url;
    } catch (Exception e) {
        log.error(e.getMessage());
        log.debug(e.getMessage(), e);
        lastException = e;
    } finally {
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return inputSource;
}

From source file:edu.vt.alerts.android.library.tasks.TermsOfServiceTask.java

/**
 * {@inheritDoc}//from  www  .  j  av  a 2  s.  c om
 */
@Override
protected TaskResult<String> doInBackground(Void... params) {
    if (PreferenceUtil.hasAcceptedTerms(context, alertsEnvironment)) {
        return new TaskResult<String>(null, null);
    }

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(alertsEnvironment.getTermsOfUseUrl());
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        HttpResponse response = httpClient.execute(get);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode >= 200 && statusCode < 300) {
            String tos = responseHandler.handleResponse(response);
            return new TaskResult<String>(tos, null);
        }

        throw new UnexpectedNetworkResponseException(
                "Unexpected response when attempting to fetch terms of service", statusCode,
                response.getStatusLine().getReasonPhrase());
    } catch (Exception e) {
        return new TaskResult<String>(null, e);
    }
}