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:org.pentaho.di.ui.core.dialog.ErrorDialogTest.java

@Test
public void setErrorTextWithCauseMessageException() {
    ClientProtocolException cpe = new ClientProtocolException("causeMessage");
    Exception e = new KettleException("kettleMessage", cpe);

    StringBuilder text = new StringBuilder();
    StringBuilder details = new StringBuilder();

    ErrorDialog dialog = mock(ErrorDialog.class);
    doCallRealMethod().when(dialog).handleException(anyString(), any(Exception.class), any(StringBuilder.class),
            any(StringBuilder.class));

    dialog.handleException("argMessage", e, text, details);

    Throwable cause = e.getCause();

    assertEquals(text.toString(), cause.getMessage().toString());

}

From source file:org.stem.api.JsonResponseHandler.java

@Override
public StemResponse handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    StatusLine status = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();

    if (status.getStatusCode() >= 300) {
        try {/*from  w  ww.  j  ava  2 s.  c  o m*/
            ErrorResponse errorResponse = JsonUtils.decode(content, ErrorResponse.class);

            String message = String.format("%s [%s]: Code: %s, Message: %s", status.getReasonPhrase(),
                    status.getStatusCode(), errorResponse.getErrorCode(), errorResponse.getError());

            throw new HttpResponseException(status.getStatusCode(), message);
        } catch (RuntimeException e) {
            throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase());
        }
    }

    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    //SimpleType simpleType = SimpleType.construct(clazz);
    return JsonUtils.decode(content, clazz);
}

From source file:it.av.fac.driver.APIClient.java

public String queryRequest(String userToken, String resource) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        HttpPost httpPost = new HttpPost(endpoint + QUERY_PATH);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("request", URLEncoder.encode(resource, "UTF-8")));
        nvps.add(new BasicNameValuePair("token", URLEncoder.encode(userToken, "UTF-8")));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        // Create a custom response handler
        ResponseHandler<String> responseHandler = (final HttpResponse response) -> {
            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);
            }//w  ww.j a  va 2s .  c  o m
        };

        return httpclient.execute(httpPost, responseHandler);
    } catch (IOException ex) {
        Logger.getLogger(APIClient.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:cn.org.once.cstack.maven.plugin.handler.ResponseErrorHandler.java

@Override
public String handleResponse(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 {/* w  ww .j  av a2 s  . c om*/
        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            throw new ClientProtocolException(jsonStringBuilder.toString());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            throw new ClientProtocolException(
                    "Status 404 - The server can treat the request, please contact an admin");
        default:
            throw new ClientProtocolException("Cloudunit server does not response. Please contact an admin");
        }
    }

}

From source file:org.yamj.api.common.http.HttpClientWrapper.java

protected static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException {
    HttpHost target = null;/*  w  w w  . j  a v a2s.  co  m*/
    URI requestURI = request.getURI();
    if (requestURI.isAbsolute()) {
        target = URIUtils.extractHost(requestURI);
        if (target == null) {
            throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI);
        }
    }
    return target;
}

From source file:com.helger.httpclient.response.ResponseHandlerJson.java

@Nullable
public IJson handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sJson = StringHelper.trim(EntityUtils.toString(aEntity, aCharset));

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got JSON: <" + sJson + ">");

        final IJson ret = JsonReader.readFromString(sJson);
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as JSON: " + sJson);
        return ret;
    }//from   ww w  .j a v  a 2 s  . com

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    return JsonReader.readFromReader(aReader);
}

From source file:com.helger.httpclient.response.ResponseHandlerXml.java

@Nullable
public Document handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sXML = EntityUtils.toString(aEntity, aCharset);

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got XML: <" + sXML + ">");

        Document ret = null;/*from  w  w w  .j a  v a2s.  c o  m*/
        try {
            ret = DOMReader.readXMLDOM(sXML);
        } catch (final SAXException ex) {
            // Ignore
        }
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as XML: " + sXML);
        return ret;
    }

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    try {
        return DOMReader.readXMLDOM(aReader);
    } catch (final SAXException ex) {
        throw new IllegalArgumentException("Failed to parse as XML", ex);
    }
}

From source file:com.helger.httpclient.response.ResponseHandlerMicroDom.java

@Nullable
public IMicroDocument handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sXML = EntityUtils.toString(aEntity, aCharset);

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got XML: <" + sXML + ">");

        final IMicroDocument ret = MicroReader.readMicroXML(sXML);
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as XML: " + sXML);
        return ret;
    }/* w  w  w  .j a  v a2 s  . c o m*/

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    return MicroReader.readMicroXML(aReader);
}

From source file:eu.seaclouds.platform.dashboard.http.HttpRequestBuilder.java

public HttpRequestBuilder() {
    this.scheme = DEFAULT_SCHEME;
    this.responseHandler = new ResponseHandler<String>() {
        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);
            }/*from  w  w w. ja v  a  2s .  com*/
        }

    };
}

From source file:cn.org.once.cstack.cli.exception.CustomResponseErrorHandler.java

@Override
public String handleResponse(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 {// w w w.  j a  va  2 s . c o  m

        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            HttpErrorServer error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        default:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        }
    }

}