Search in sources :

Example 51 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MqttMessagingSkeleton method transmit.

@Override
public void transmit(byte[] serializedMessage, FailureAction failureAction) {
    try {
        HashMap<String, Serializable> context = new HashMap<String, Serializable>();
        byte[] processedMessage = rawMessagingPreprocessor.process(serializedMessage, context);
        ImmutableMessage message = new ImmutableMessage(processedMessage);
        message.setContext(context);
        LOG.debug("<<< INCOMING <<< {}", message);
        if (messageProcessors != null) {
            for (JoynrMessageProcessor processor : messageProcessors) {
                message = processor.processIncoming(message);
            }
        }
        if (dropMessage(message)) {
            droppedMessagesCount.incrementAndGet();
            mqttStatusReceiver.notifyMessageDropped();
            return;
        }
        message.setReceivedFromGlobal(true);
        if (isRequestMessageTypeThatCanBeDropped(message.getType())) {
            requestAccepted(message.getId());
        }
        try {
            messageRouter.route(message);
        } catch (Exception e) {
            LOG.error("Error processing incoming message. Message will be dropped: {} ", e.getMessage());
            messageProcessed(message.getId());
            failureAction.execute(e);
        }
    } catch (UnsuppportedVersionException | EncodingException | NullPointerException e) {
        LOG.error("Message: \"{}\", could not be deserialized, exception: {}", serializedMessage, e.getMessage());
        failureAction.execute(e);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) EncodingException(io.joynr.smrf.EncodingException) ImmutableMessage(joynr.ImmutableMessage) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) EncodingException(io.joynr.smrf.EncodingException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException)

Example 52 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MessagingLoadDistributionTest method getMessagesFromBounceProxy.

private List<ImmutableMessage> getMessagesFromBounceProxy(BounceProxyCommunicationMock bpMock, String channelUrl, String channelId) throws IOException, EncodingException, UnsuppportedVersionException {
    String previousBaseUri = RestAssured.baseURI;
    RestAssured.baseURI = Utilities.getUrlWithoutSessionId(channelUrl, "jsessionid");
    String sessionId = Utilities.getSessionId(channelUrl, "jsessionid");
    /* @formatter:off */
    Response response = bpMock.onrequest().with().header("X-Atmosphere-tracking-id", bpMock.getReceiverId()).expect().statusCode(200).when().get(";jsessionid=" + sessionId);
    /* @formatter:on */
    List<ImmutableMessage> messagesFromResponse = bpMock.getJoynrMessagesFromResponse(response);
    RestAssured.baseURI = previousBaseUri;
    return messagesFromResponse;
}
Also used : Response(com.jayway.restassured.response.Response) ImmutableMessage(joynr.ImmutableMessage)

Example 53 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class ControlledBounceProxyServerTest method testNormalMessagingWithMultipleMessagePostsAfterChannelWasOpened.

@Test
@Ignore("need cleanup of other tests (i.e. implementation of delete channel")
public void testNormalMessagingWithMultipleMessagePostsAfterChannelWasOpened() throws Exception {
    final String channelId = "channel-testNormalMessagingWithMultipleMessagePostsAfterChannelWasOpened";
    final String trackingId = "trackingId-testNormalMessagingWithMultipleMessagePostsAfterChannelWasOpened";
    // create channel on bounce proxy
    /* @formatter:off */
    Response responseCreateChannel = given().header(X_ATMOSPHERE_TRACKING_ID, trackingId).post("channels?ccid=" + channelId);
    /* @formatter:on */
    assertEquals(201, /* Created */
    responseCreateChannel.getStatusCode());
    assertNotNull(responseCreateChannel.getHeader(HEADER_BOUNCEPROXY_ID));
    final String channelUrl = responseCreateChannel.getHeader(HEADER_LOCATION);
    String bpId = responseCreateChannel.getHeader(HEADER_BOUNCEPROXY_ID);
    String bpUrl = configuration.getBounceProxyUrl(bpId);
    assertThat(channelUrl, isChannelUrlwithJsessionId(bpUrl, channelId, SESSIONID_NAME));
    String sessionId = Utilities.getSessionId(channelUrl, SESSIONID_NAME);
    RestAssured.baseURI = Utilities.getUrlWithoutSessionId(channelUrl, SESSIONID_NAME);
    // open long polling channel first in separate thread
    Future<?> longPollingChannelFuture = Executors.newSingleThreadExecutor().submit(new Callable<Response>() {

        @Override
        public Response call() throws Exception {
            /* @formatter:off */
            return given().when().contentType(ContentType.BINARY).header(X_ATMOSPHERE_TRACKING_ID, trackingId).get("");
        /* @formatter:on */
        }
    });
    // post messages to long polling channel after opening channel
    String[] msgIds = { "message-123", "message-456", "message-789" };
    for (String msgId : msgIds) {
        byte[] serializedMessage = bpMock.createImmutableMessage(100000l, msgId.getBytes(Charsets.UTF_8)).getSerializedMessage();
        /* @formatter:off */
        Response responsePostMessage = given().when().contentType(ContentType.BINARY).body(serializedMessage).post("message/" + SESSIONID_APPENDIX + sessionId);
        /* @formatter:on */
        assertEquals(201, /* Created */
        responsePostMessage.getStatusCode());
        String messageUrl = responsePostMessage.getHeader(HEADER_LOCATION);
        assertThat(messageUrl, isMessageUrlwithJsessionId(bpUrl, msgId, sessionId, SESSIONID_NAME));
        assertEquals(msgId, responsePostMessage.getHeader(HEADER_MSG_ID));
    }
    Response responseOpenChannel = (Response) longPollingChannelFuture.get(10, TimeUnit.SECONDS);
    assertEquals(200, /* OK */
    responseOpenChannel.getStatusCode());
    List<ImmutableMessage> messages = bpMock.getJoynrMessagesFromResponse(responseOpenChannel);
    assertEquals(1, messages.size());
    assertThat(messages, containsPayload("message-123"));
}
Also used : Response(com.jayway.restassured.response.Response) ImmutableMessage(joynr.ImmutableMessage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 54 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MessageWithoutContentTypeTest method testSendAndReceiveMessage.

@Test
public void testSendAndReceiveMessage() throws Exception {
    String channelId = "MessageWithoutContentTypeTest_" + UUID.randomUUID().toString();
    bpMock.createChannel(channelId);
    byte[] postPayload = ("payload-" + UUID.randomUUID().toString()).getBytes(Charsets.UTF_8);
    ImmutableMessage messageToSend = bpMock.createImmutableMessage(100000l, postPayload);
    given().contentType(ContentType.BINARY).content(messageToSend.getSerializedMessage()).log().all().expect().response().statusCode(201).header("Location", RestAssured.baseURI + "messages/" + messageToSend.getId()).header("msgId", messageToSend.getId()).when().post("/channels/" + channelId + "/messageWithoutContentType");
    ScheduledFuture<Response> longPollConsumer = bpMock.longPollInOwnThread(channelId, 30000);
    Response responseLongPoll = longPollConsumer.get();
    List<ImmutableMessage> messagesFromResponse = bpMock.getJoynrMessagesFromResponse(responseLongPoll);
    Assert.assertEquals(1, messagesFromResponse.size());
    ImmutableMessage message = messagesFromResponse.get(0);
    Assert.assertEquals(postPayload, message.getUnencryptedBody());
}
Also used : Response(com.jayway.restassured.response.Response) ImmutableMessage(joynr.ImmutableMessage) Test(org.junit.Test)

Example 55 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class MessagingServiceResponseMatchers method containsPayload.

public static Matcher<List<ImmutableMessage>> containsPayload(final String payload) {
    return new BaseMatcher<List<ImmutableMessage>>() {

        @Override
        public boolean matches(Object item) {
            @SuppressWarnings("unchecked") List<ImmutableMessage> messages = (List<ImmutableMessage>) item;
            byte[] binaryPayload = payload.getBytes(Charsets.UTF_8);
            for (ImmutableMessage message : messages) {
                try {
                    if (Arrays.equals(message.getUnencryptedBody(), binaryPayload)) {
                        return true;
                    }
                } catch (EncodingException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            return false;
        }

        @Override
        public void describeTo(Description arg0) {
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) EncodingException(io.joynr.smrf.EncodingException) ImmutableMessage(joynr.ImmutableMessage) List(java.util.List)

Aggregations

ImmutableMessage (joynr.ImmutableMessage)63 Test (org.junit.Test)42 Response (com.jayway.restassured.response.Response)12 FailureAction (io.joynr.messaging.FailureAction)11 SuccessAction (io.joynr.messaging.SuccessAction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 EncodingException (io.joynr.smrf.EncodingException)9 MutableMessage (joynr.MutableMessage)9 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)8 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)8 Semaphore (java.util.concurrent.Semaphore)7 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)6 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)6 MessagingQos (io.joynr.messaging.MessagingQos)6 Ignore (org.junit.Ignore)6 AbstractModule (com.google.inject.AbstractModule)5 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 CcMessageRouter (io.joynr.messaging.routing.CcMessageRouter)4