Example usage for org.apache.http.client ClientProtocolException ClientProtocolException

List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException

Introduction

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

Prototype

public ClientProtocolException(final Throwable cause) 

Source Link

Usage

From source file:no.finntech.yammer.YammerClient.java

public void sendMessage(final String group, final String message, final String... topics) throws IOException {

    HttpPost httpPost = new HttpPost(YAMMER_API_V1_MESSAGES);
    httpPost.addHeader("Authorization", "Bearer " + accessAuthToken);

    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair(MESSAGE_BODY_PARAM_NAME, message));

    if (group != null && !group.equals("")) {
        nvps.add(new BasicNameValuePair(MESSAGE_GROUP_ID_PARAM_NAME, group));
    }/*from www  .  j a v  a  2s . c  o m*/

    for (int i = 0; i < topics.length; ++i) {
        nvps.add(new BasicNameValuePair(MESSAGE_TOPIC_PARAM_NAME + (i + 1), topics[i]));
    }

    httpPost.setEntity(new UrlEncodedFormEntity(nvps, UTF8));
    HttpResponse response = httpclient.execute(httpPost);
    if (201 != response.getStatusLine().getStatusCode()) {
        throw new ClientProtocolException("failed to post message to yammer: " + response);
    }
}

From source file:com.jaspersoft.studio.server.protocol.restv2.RestV2Connection.java

private <T> T toObj(Request req, final Class<T> clazz, IProgressMonitor monitor) throws IOException {
    T obj = null;// w w  w  . j  a  va  2s .  c  om
    ConnectionManager.register(monitor, req);
    try {
        obj = exec.execute(req).handleResponse(new ResponseHandler<T>() {

            public T handleResponse(final HttpResponse response) throws IOException {
                HttpEntity entity = response.getEntity();
                InputStream in = null;
                try {
                    StatusLine statusLine = response.getStatusLine();
                    switch (statusLine.getStatusCode()) {
                    case 200:
                        in = getContent(entity);
                        return mapper.readValue(in, clazz);
                    case 204:
                        return null;
                    case 400:
                    case 404:
                        in = getContent(entity);
                        ErrorDescriptor ed = mapper.readValue(in, ErrorDescriptor.class);
                        if (ed != null)
                            throw new ClientProtocolException(
                                    MessageFormat.format(ed.getMessage(), (Object[]) ed.getParameters()));
                    default:
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }
                } finally {
                    FileUtils.closeStream(in);
                }
            }

            protected InputStream getContent(HttpEntity entity) throws ClientProtocolException, IOException {
                if (entity == null)
                    throw new ClientProtocolException("Response contains no content");
                return entity.getContent();
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } finally {
        ConnectionManager.unregister(req);
    }
    return obj;
}

From source file:net.shirayu.android.WlanLogin.MyHttpClient.java

public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
    stop_auth = false;//from   w w  w .ja v a 2 s . com
    try {
        return client.execute(request);
    } catch (RuntimeException ex) {
        throw new ClientProtocolException(ex.getMessage());
    }
}

From source file:com.scottjjohnson.finance.analysis.HttpClient.java

/**
 * Builds a simple HTTP response handler that checks for status of 2XX.
 * //  www.  ja v a2s  .  c  o m
 * @return response handler
 */
public static ResponseHandler<String> getDefaultResponseHandler() {

    // Create a custom response handler -- from httpclient docs
    return new ResponseHandler<String>() {

        @Override
        public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }
    };
}

From source file:org.sana.android.net.ssl.SimpleSSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() throws ClientProtocolException {
    try {/*from  w  ww. j a  va 2  s. co m*/
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } }, null);
        return context;
    } catch (Exception e) {
        //LOG.error(e.getMessage(), e);
        throw new ClientProtocolException(e.toString());
    }
}

From source file:javaSeed.utils.jiraConnection.Cycle.java

public static Long deleteCycle(ZephyrConfigModel zephyrData) {

    Long cycleId = 0L;/*from  w  w  w. j a  v  a 2 s.c  om*/

    HttpResponse response = null;
    try {
        String deleteCycleURL = URL_DELETE_CYCLE.replace("{SERVER}", zephyrData.getRestClient().getUrl())
                .replace("{id}", zephyrData.getCycleId() + "");

        HttpDelete createCycleRequest = new HttpDelete(deleteCycleURL);

        response = zephyrData.getRestClient().getHttpclient().execute(createCycleRequest,
                zephyrData.getRestClient().getContext());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode >= 200 && statusCode < 300) {
        HttpEntity entity = response.getEntity();
        @SuppressWarnings("unused")
        String string = null;
        try {
            string = EntityUtils.toString(entity);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } else {
        try {
            throw new ClientProtocolException("Unexpected response status: " + statusCode);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }
    }

    return cycleId;
}

From source file:net.shirayu.android.WlanLogin.MyHttpClient.java

public HttpResponse execute(HttpUriRequest request, HttpContext context)
        throws IOException, ClientProtocolException {
    stop_auth = false;// w w  w .j av a  2  s.  c  o  m
    try {
        return client.execute(request, context);
    } catch (RuntimeException ex) {
        throw new ClientProtocolException(ex.getMessage());
    }
}

From source file:br.com.jbugbrasil.bot.service.jbossbooks.JBossBooksService.java

/**
 * Scheduler default que busca os livros no JBoss Books e salva em cache.
 *//*from  w w  w.  j  ava2 s  . c  o m*/
@Schedule(minute = "0/20", hour = "*", persistent = false)
public synchronized void initialize() {
    if (cache.containsKey("jsonResponse")) {
        log.fine("Cache populado, retornando");
        return;
    }
    try {
        log.fine("Buscando informaes dos livros em [" + GIT_BOOK_ENDPOINT + "]");
        HttpGet request = new HttpGet(GIT_BOOK_ENDPOINT);
        request.setHeader("Authorization", "Bearer " + gitBookToken);
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            @Override
            public String handleResponse(final HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
        ObjectMapper mapper = new ObjectMapper();
        JSONResponse jsonResponse = mapper.readValue(client().execute(request, responseHandler),
                JSONResponse.class);
        cache.put("jsonResponse", jsonResponse, 60, TimeUnit.MINUTES);
        verifyNewBook(jsonResponse.getTotal());
        verifyBookUpdates(jsonResponse);
    } catch (final Exception e) {
        e.printStackTrace();
        log.warning("Falha ao buscar informaes em [" + GIT_BOOK_ENDPOINT + "]: " + e.getMessage());
    }
}

From source file:com.thed.zephyr.jenkins.utils.rest.Cycle.java

public static Long deleteCycle(ZephyrConfigModel zephyrData) {

    Long cycleId = 0L;/*from  w  w w  .  j  a  v  a2 s. c om*/

    HttpResponse response = null;
    try {
        String deleteCycleURL = URL_DELETE_CYCLE.replace("{SERVER}", zephyrData.getRestClient().getUrl())
                .replace("{id}", zephyrData.getCycleId() + "");

        HttpDelete createCycleRequest = new HttpDelete(deleteCycleURL);

        response = zephyrData.getRestClient().getHttpclient().execute(createCycleRequest,
                zephyrData.getRestClient().getContext());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode >= 200 && statusCode < 300) {
        HttpEntity entity = response.getEntity();
        String string = null;
        try {
            string = EntityUtils.toString(entity);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } else {
        try {
            throw new ClientProtocolException("Unexpected response status: " + statusCode);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }
    }

    return cycleId;
}

From source file:com.birt.airvantage.AVRequest.java

private String getRequest(String url) {
    try {// w w w .j av a  2 s .c  o  m
        Response ex = Request.Get(url).execute();
        return ex.handleResponse(new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response) throws IOException {
                StatusLine statusLine = response.getStatusLine();
                HttpEntity entity = response.getEntity();
                if (statusLine.getStatusCode() >= 300) {
                    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
                }
                if (entity == null)
                    throw new ClientProtocolException("No data");
                return IOUtils.toString(entity.getContent());
            }
        });
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}