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

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

Introduction

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

Prototype

public HttpResponseException(final int statusCode, final String s) 

Source Link

Usage

From source file:com.splunk.shuttl.archiver.archive.ArchiveRestHandler.java

private void handleResponseFromDoingArchiveBucketRequest(HttpResponse response, Bucket bucket)
        throws HttpResponseException {
    switch (response.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
    case HttpStatus.SC_NO_CONTENT:
        logSuccess(response, bucket, response.getStatusLine().getStatusCode());
        break;// w  w w  . j  a v  a2s.co m
    default:
        throw new HttpResponseException(response.getStatusLine().getStatusCode(),
                "Unexpected response when archiving bucket.");
    }
}

From source file:cn.com.loopj.android.http.BinaryHttpResponseHandler.java

@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders(AsyncHttpClient.HEADER_CONTENT_TYPE);
    if (contentTypeHeaders.length != 1) {
        //malformed/ambiguous HTTP Header, ABORT!
        sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(
                status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
        return;//from   w  ww.j  a  v  a  2s. c  o m
    }
    Header contentTypeHeader = contentTypeHeaders[0];
    boolean foundAllowedContentType = false;
    for (String anAllowedContentType : getAllowedContentTypes()) {
        try {
            if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
                foundAllowedContentType = true;
            }
        } catch (PatternSyntaxException e) {
            AsyncHttpClient.log.e(LOG_TAG, "Given pattern is not valid: " + anAllowedContentType, e);
        }
    }
    if (!foundAllowedContentType) {
        //Content-Type not in allowed list, ABORT!
        sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(
                status.getStatusCode(), "Content-Type (" + contentTypeHeader.getValue() + ") not allowed!"));
        return;
    }
    super.sendResponseMessage(response);
}

From source file:com.unifonic.sdk.resources.http.AccountResourceImpl.java

@Override
public SenderID addSenderId(String senderID) throws IOException {
    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair(PARAM_SENDER_ID, senderID));

    OTSRestResponse response = sendRequest(accountUrl.urlAddSenderID(), param);
    if (response.getStatusCode() < 400) {
        Type type = new TypeToken<ResponseModel<SenderID>>() {
        }.getType();/*from  w w w.  j  a v  a2  s .c  o m*/
        ResponseModel<SenderID> respData = GSON.fromJson(response.getData(), type);
        SenderID sender = respData.create();
        sender.setSenderID(senderID);
        return sender;
    } else if (response.getStatusCode() == 400) {
        SenderID resp = GSON.fromJson(response.getData(), SenderID.class);
        return resp;
    } else {
        throw new HttpResponseException(response.getStatusCode(), response.getReasonPhrase());
    }
}

From source file:de.incoherent.suseconferenceclient.app.HTTPWrapper.java

public static JSONObject get(String url) throws IllegalStateException, SocketException,
        UnsupportedEncodingException, IOException, JSONException {
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    DefaultHttpClient client = new DefaultHttpClient();
    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    HttpGet get = new HttpGet(url);
    HttpResponse response = httpClient.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode >= 200 && statusCode <= 299) {
        return HTTPWrapper.parseResponse(response);
    } else {// w  ww  .  j  a  va 2 s. c  o  m
        throw new HttpResponseException(statusCode, statusLine.getReasonPhrase());
    }
}

From source file:com.unifonic.sdk.resources.http.VoiceResourceImpl.java

@Override
public CallStatusResponse getCallIDStatus(Map<String, String> param) throws IOException {
    OTSRestResponse response = sendRequest(voiceUrl.urlGetCallIDStatus(), param);
    if (response.getStatusCode() < 400) {
        Type type = new TypeToken<ResponseModel<CallStatusResponse>>() {
        }.getType();/* w w  w .j a  va 2  s  .  com*/
        ResponseModel<CallStatusResponse> respData = GSON.fromJson(response.getData(), type);
        return respData.create();
    } else if (response.getStatusCode() == 400) {
        CallStatusResponse resp = GSON.fromJson(response.getData(), CallStatusResponse.class);
        return resp;
    } else {
        throw new HttpResponseException(response.getStatusCode(), response.getReasonPhrase());
    }
}

From source file:siarhei.luskanau.gps.tracker.free.service.sync.tracking.json.SendJsonForm.java

private static InputStream requestInputStream(HttpUriRequest httpUriRequest) throws Exception {
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);

    HttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpResponse httpResponse = httpClient.execute(httpUriRequest);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if (statusCode != 200 && statusCode != 201) {
        String responseString = null;
        try {/*from  w w w. j a  va 2 s. c o  m*/
            responseString = read(httpResponse.getEntity().getContent());
        } catch (Throwable t) {
            responseString = null;
        }
        throw new HttpResponseException(statusCode,
                statusCode + " " + httpResponse.getStatusLine().getReasonPhrase() + "\n" + responseString);
    }
    return httpResponse.getEntity().getContent();
}

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;//ww w  .  jav  a2 s . 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:com.esri.geoportal.commons.ckan.client.Client.java

private Response execute(HttpUriRequest req) throws IOException, URISyntaxException {
    try (CloseableHttpResponse httpResponse = httpClient.execute(req);
            InputStream contentStream = httpResponse.getEntity().getContent();) {
        String reasonMessage = httpResponse.getStatusLine().getReasonPhrase();
        String responseContent = IOUtils.toString(contentStream, "UTF-8");
        LOG.trace(String.format("RESPONSE: %s, %s", responseContent, reasonMessage));

        if (httpResponse.getStatusLine().getStatusCode() >= 400) {
            Response value = null;
            try {
                value = mapper.readValue(responseContent, Response.class);
            } catch (Exception ex) {
                throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase());
            }/*  w w w. ja  va2  s .c o m*/
            if (value == null) {
                throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase());
            }
            return value;
        }

        return mapper.readValue(responseContent, Response.class);
    }
}

From source file:cn.edu.zzu.wemall.http.BinaryHttpResponseHandler.java

@Override
protected void sendResponseMessage(HttpResponse response) throws IOException {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders("Content-Type");
    if (contentTypeHeaders.length != 1) {
        //malformed/ambiguous HTTP Header, ABORT!
        sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(
                status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
        return;/*from  ww w .ja v  a  2  s .c om*/
    }
    Header contentTypeHeader = contentTypeHeaders[0];
    boolean foundAllowedContentType = false;
    for (String anAllowedContentType : mAllowedContentTypes) {
        if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
            foundAllowedContentType = true;
        }
    }
    if (!foundAllowedContentType) {
        //Content-Type not in allowed list, ABORT!
        sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null,
                new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"));
        return;
    }
    super.sendResponseMessage(response);
}

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

private String getRequest(String url) {
    try {//from   w w  w . j a  v a2  s.c  om
        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;
}