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:com.mashape.galileo.agent.network.AnalyticsResponseHandler.java

@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new ClientProtocolException("failed to save the batch");
    }//from w  w  w. ja  v a  2s .  c o m

    StatusLine statusLine = response.getStatusLine();
    String responseBody = EntityUtils.toString(entity);
    if (statusLine.getStatusCode() == 200) {
        LOGGER.debug(String.format("successfully saved the batch. (%s)", responseBody));
        return true;
    }
    if (statusLine.getStatusCode() == 207) {
        LOGGER.warn(String.format("collector could not save all ALFs from the batch. (%s)", responseBody));
        return true;
    }
    if (statusLine.getStatusCode() == 400) {
        LOGGER.error(String.format(
                "collector refused to save the batch, dropping batch. Status: (%d) Reason: (%s) Error: (%s)",
                statusLine.getStatusCode(), statusLine.getReasonPhrase(), responseBody));
        return true;
    }
    if (statusLine.getStatusCode() == 413) {
        LOGGER.error(
                String.format("collector refused to save the batch, dropping batch. Status: (%d)  Error: (%s)",
                        statusLine.getStatusCode(), responseBody));
        return true;
    }
    LOGGER.error(String.format("failed to save the batch. Status: (%d)  Error: (%s)",
            statusLine.getStatusCode(), responseBody));
    return false;
}

From source file:org.pentaho.di.ui.spoon.SpoonSlaveTest.java

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

    SpoonSlave spoonSlave = mock(SpoonSlave.class);
    doCallRealMethod().when(spoonSlave).setExceptionMessage(any(Exception.class));

    String message = spoonSlave.setExceptionMessage(e);

    Throwable cause = e.getCause();

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

}

From source file:com.wudaosoft.net.httpclient.JsonResponseHandler.java

@Override
public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        Charset charset = ContentType.getOrDefault(entity).getCharset();
        if (charset == null) {
            charset = Consts.UTF_8;/*from   w w  w.j  a v a2  s  .co  m*/
        }
        try {
            return entity != null ? JSON.parseObject(EntityUtils.toString(entity, charset)) : new JSONObject();
        } catch (JSONException e) {
            throw new ClientProtocolException("Json format error: " + e.getMessage());
        }
    } else {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

From source file:io.nirvagi.utils.node.helper.HttpGetHelper.java

private ResponseHandler<String> createHttpResponseHandler() {
    return new ResponseHandler<String>() {

        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new ClientProtocolException(String.format(HTTP_GET_FAILED_MESSAGE,
                        httpGet.getURI().toString(), statusCode, response.getStatusLine().getReasonPhrase()));
            }//from   w  w  w .  j a  v a 2s  .c o  m
            return "SUCCESS";
        }
    };

}

From source file:org.jwifisd.httpclient.ByteResponseHandler.java

@Override
public byte[] handleResponse(HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= START_HTTP_OK_RESPONSE_RANGE && status < END_HTTP_OK_RESPONSE_RANGE) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toByteArray(entity) : null;
    } else {//w  w w.  j  av a2 s  . co m
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

From source file:de.tudarmstadt.ukp.shibhttpclient.Utils.java

/**
 * Helper method that deserializes and unmarshalls the message from the given stream. This method has been adapted from
 * {@code org.opensaml.ws.message.decoder.BaseMessageDecoder}.
 * //ww  w  .  j  av a 2  s  .  c  o  m
 * @param messageStream
 *            input stream containing the message
 * 
 * @return the inbound message
 * 
 * @throws MessageDecodingException
 *             thrown if there is a problem deserializing and unmarshalling the message
 */
public static XMLObject unmarshallMessage(ParserPool parserPool, InputStream messageStream)
        throws ClientProtocolException {
    try {
        Document messageDoc = parserPool.parse(messageStream);
        Element messageElem = messageDoc.getDocumentElement();

        Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
        if (unmarshaller == null) {
            throw new ClientProtocolException(
                    "Unable to unmarshall message, no unmarshaller registered for message element "
                            + XMLHelper.getNodeQName(messageElem));
        }

        XMLObject message = unmarshaller.unmarshall(messageElem);

        return message;
    } catch (XMLParserException e) {
        throw new ClientProtocolException("Encountered error parsing message into its DOM representation", e);
    } catch (UnmarshallingException e) {
        throw new ClientProtocolException("Encountered error unmarshalling message from its DOM representation",
                e);
    }
}

From source file:com.vmware.bdd.plugin.clouderamgr.poller.host.handler.DefaultResponseHandler.java

@Override
public ParseResult handleResponse(HttpResponse response) throws IOException {

    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*from  ww  w .java2s  .  c  om*/
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    return contentParser.parse(EntityUtils.toString(entity));
}

From source file:uk.ac.diamond.shibbolethecpauthclient.Utils.java

/**
 * Helper method that deserializes and unmarshalls the message from the given stream. This
 * method has been adapted from {@code org.opensaml.ws.message.decoder.BaseMessageDecoder}.
 * //from w w w  .j  av  a2  s .c  o  m
 * @param parserPool
 *The parser to use for unmarshalling the message
 * @param messageStream
 *Input stream containing the message
 * 
 * @return The in-bound message
 * 
 * @throws ClientProtocolException
 * thrown if there is a problem deserializing and unmarshalling the message
 */
static XMLObject unmarshallMessage(ParserPool parserPool, InputStream messageStream)
        throws ClientProtocolException {

    try {
        Document messageDoc = parserPool.parse(messageStream);
        Element messageElem = messageDoc.getDocumentElement();

        Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
        if (unmarshaller == null) {
            throw new ClientProtocolException(
                    "Unable to unmarshall message, no unmarshaller registered for message element "
                            + XMLHelper.getNodeQName(messageElem));
        }

        XMLObject message = unmarshaller.unmarshall(messageElem);

        return message;
    } catch (XMLParserException e) {
        throw new ClientProtocolException("Encountered error parsing message into its DOM representation", e);
    } catch (UnmarshallingException e) {
        throw new ClientProtocolException("Encountered error unmarshalling message from its DOM representation",
                e);
    }
}

From source file:org.apache.droids.protocol.http.AdvancedHttpProtocol.java

@Override
public AdvancedManagedContentEntity load(URI uri) throws IOException {
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = getHttpClient().execute(httpget);
    StatusLine statusline = response.getStatusLine();
    if (statusline.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
        httpget.abort();//from   w ww  .  ja  v a  2  s  .  co m
        throw new HttpResponseException(statusline.getStatusCode(), statusline.getReasonPhrase());
    }
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        // Should _almost_ never happen with HTTP GET requests.
        throw new ClientProtocolException("Empty entity");
    }
    long maxlen = getHttpClient().getParams().getLongParameter(DroidsHttpClient.MAX_BODY_LENGTH, 0);
    return new AdvancedHttpContentEntity(entity, response.getAllHeaders(), maxlen);
}