Search in sources :

Example 6 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class LongPollChannel method longPoll.

private void longPoll() {
    String responseBody = null;
    if (shutdown) {
        return;
    }
    final String asciiString = httpget.getURI().toASCIIString();
    try {
        responseBody = httpclient.execute(httpget, new ResponseHandler<String>() {

            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                HttpEntity entity = response.getEntity();
                String body = entity == null ? null : EntityUtils.toString(entity, "UTF-8");
                statusCode = response.getStatusLine().getStatusCode();
                statusText = response.getStatusLine().getReasonPhrase();
                logger.debug("Long poll returned: {} reason: url {}", statusCode, asciiString);
                return body;
            }
        });
    } catch (IllegalStateException e) {
        logger.error("IllegalStateException in long poll: {} message: {}", asciiString, e.getMessage());
        throw new JoynrShutdownException(e.getMessage(), e);
    } catch (Exception e) {
        logger.debug("Exception in long poll: " + asciiString, e);
        delay();
        return;
    }
    switch(statusCode) {
        case HttpStatus.SC_OK:
            notifyDispatcher(responseBody);
            break;
        case HttpStatus.SC_NOT_FOUND:
            logger.error(responseBody);
            delay();
            throw new JoynrChannelMissingException("Not found");
        case HttpStatus.SC_BAD_REQUEST:
            if (responseBody != null) {
                try {
                    JoynrMessagingError error = objectMapper.readValue(responseBody, JoynrMessagingError.class);
                    JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
                    logger.error(error.toString());
                    switch(joynrMessagingErrorCode) {
                        case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
                            throw new JoynrChannelMissingException(error.getReason());
                        default:
                            throw new JoynrCommunicationException(error.getReason());
                    }
                } catch (IOException e) {
                    throw new JoynrCommunicationException(statusText, e);
                }
            }
        default:
            delay();
            break;
    }
}
Also used : JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) HttpResponse(org.apache.http.HttpResponse) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) EncodingException(io.joynr.smrf.EncodingException) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException)

Aggregations

JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)6 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)4 Test (org.junit.Test)4 Response (com.jayway.restassured.response.Response)2 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)2 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)2 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)2 IOException (java.io.IOException)2 ImmutableMessage (joynr.ImmutableMessage)2 HttpEntity (org.apache.http.HttpEntity)2 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 EncodingException (io.joynr.smrf.EncodingException)1 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 ResponseHandler (org.apache.http.client.ResponseHandler)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1