Search in sources :

Example 1 with JoynrChannelMissingException

use of io.joynr.exceptions.JoynrChannelMissingException in project joynr by bmwcarit.

the class SerializationTest method serializeReplyWithJoynrChannelMissingException.

@Test
public void serializeReplyWithJoynrChannelMissingException() throws IOException {
    JoynrChannelMissingException error = new JoynrChannelMissingException("detail message: JoynrChannelMissingException");
    Reply reply = new Reply(UUID.randomUUID().toString(), error);
    String writeValueAsString = objectMapper.writeValueAsString(reply);
    System.out.println(writeValueAsString);
    Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
    Assert.assertEquals(reply, receivedReply);
}
Also used : JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) Reply(joynr.Reply) Test(org.junit.Test)

Example 2 with JoynrChannelMissingException

use of io.joynr.exceptions.JoynrChannelMissingException in project joynr by bmwcarit.

the class LongPollingChannelLifecycle method createChannel.

/**
 * Create a new channel for the given cluster controller id. If a channel already exists, it is returned instead.
 *
 * @return whether channel was created
 */
private synchronized boolean createChannel() {
    final String url = settings.getBounceProxyUrl().buildCreateChannelUrl(channelId);
    HttpPost postCreateChannel = httpRequestFactory.createHttpPost(URI.create(url.trim()));
    postCreateChannel.setConfig(defaultRequestConfig);
    postCreateChannel.addHeader(httpConstants.getHEADER_X_ATMOSPHERE_TRACKING_ID(), receiverId);
    postCreateChannel.addHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON());
    channelUrl = null;
    CloseableHttpResponse response = null;
    boolean created = false;
    try {
        response = httpclient.execute(postCreateChannel);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        String reasonPhrase = statusLine.getReasonPhrase();
        switch(statusCode) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
                try {
                    Header locationHeader = response.getFirstHeader(httpConstants.getHEADER_LOCATION());
                    channelUrl = locationHeader != null ? locationHeader.getValue() : null;
                } catch (Exception e) {
                    throw new JoynrChannelMissingException("channel url was null");
                }
                break;
            default:
                logger.error("createChannel channelId: {} failed. status: {} reason: {}", channelId, statusCode, reasonPhrase);
                throw new JoynrChannelMissingException("channel url was null");
        }
        created = true;
        logger.info("createChannel channelId: {} returned channelUrl {}", channelId, channelUrl);
    } catch (ClientProtocolException e) {
        logger.error("createChannel ERROR reason: {}", e.getMessage());
    } catch (IOException e) {
        logger.error("createChannel ERROR reason: {}", e.getMessage());
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                logger.error("createChannel ERROR reason: {}", e.getMessage());
            }
        }
    }
    return created;
}
Also used : StatusLine(org.apache.http.StatusLine) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 3 with JoynrChannelMissingException

use of io.joynr.exceptions.JoynrChannelMissingException in project joynr by bmwcarit.

the class HttpMessageSender method sendMessage.

public void sendMessage(ChannelAddress address, byte[] serializedMessage, SuccessAction successAction, FailureAction failureAction) {
    // check if messageReceiver is ready to receive replies otherwise delay request by at least 100 ms
    if (!messageReceiver.isReady()) {
        long delay_ms = DELAY_RECEIVER_NOT_STARTED_MS;
        failureAction.execute(new JoynrDelayMessageException(delay_ms, RECEIVER_NOT_STARTED_REASON));
    }
    String sendUrl = urlResolver.getSendUrl(address.getMessagingEndpointUrl());
    logger.trace("SENDING: channelId: {} message: {}", sendUrl, serializedMessage);
    HttpContext context = new BasicHttpContext();
    // execute http command to send
    CloseableHttpResponse response = null;
    try {
        HttpPost httpPost = httpRequestFactory.createHttpPost(URI.create(sendUrl));
        httpPost.addHeader(new BasicHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON() + ";charset=UTF-8"));
        httpPost.setEntity(new ByteArrayEntity(serializedMessage));
        // Clone the default config
        Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig);
        requestConfigBuilder.setConnectionRequestTimeout(httpConstants.getSEND_MESSAGE_REQUEST_TIMEOUT());
        httpPost.setConfig(requestConfigBuilder.build());
        response = httpclient.execute(httpPost, context);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        String statusText = statusLine.getReasonPhrase();
        switch(statusCode) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
                logger.trace("SENT: channelId {} message: {}", sendUrl, serializedMessage);
                successAction.execute();
                break;
            case HttpURLConnection.HTTP_BAD_REQUEST:
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender. No further reason found in message body"));
                    return;
                }
                String body = EntityUtils.toString(entity, "UTF-8");
                JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
                JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
                logger.error(error.toString());
                switch(joynrMessagingErrorCode) {
                    case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
                        failureAction.execute(new JoynrChannelMissingException("Channel does not exist. Status: " + statusCode + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                    default:
                        failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender: " + statusText + body + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                }
                break;
            default:
                failureAction.execute(new JoynrCommunicationException("Unknown Error in HttpMessageSender: " + statusText + " statusCode: " + statusCode));
                break;
        }
    } catch (JoynrShutdownException e) {
        failureAction.execute(new JoynrMessageNotSentException("Message not sent to: " + address, e));
    } catch (Exception e) {
        // An exception occured - this could still be a communication error (e.g Connection refused)
        failureAction.execute(new JoynrCommunicationException(e.getClass().getName() + "Exception while communicating. error: " + e.getMessage()));
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : HttpPost(io.joynr.messaging.http.operation.HttpPost) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Builder(org.apache.http.client.config.RequestConfig.Builder) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) IOException(java.io.IOException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) StatusLine(org.apache.http.StatusLine) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException)

Example 4 with JoynrChannelMissingException

use of io.joynr.exceptions.JoynrChannelMissingException 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

JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)4 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)3 IOException (java.io.IOException)3 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)2 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)2 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)2 HttpEntity (org.apache.http.HttpEntity)2 StatusLine (org.apache.http.StatusLine)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)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 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Reply (joynr.Reply)1 Header (org.apache.http.Header)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 ResponseHandler (org.apache.http.client.ResponseHandler)1