Search in sources :

Example 6 with EncodingException

use of io.joynr.smrf.EncodingException in project joynr by bmwcarit.

the class DispatcherImpl method messageArrived.

@Override
public void messageArrived(final ImmutableMessage message) {
    if (message == null) {
        logger.error("received message was null");
        return;
    }
    if (!message.isTtlAbsolute()) {
        logger.error("received message with relative ttl (not supported)");
        return;
    }
    final long expiryDate = message.getTtlMs();
    final Map<String, String> customHeaders = message.getCustomHeaders();
    if (DispatcherUtils.isExpired(expiryDate)) {
        logger.debug("TTL expired, discarding message : {}", message);
        return;
    }
    String payload;
    try {
        payload = new String(message.getUnencryptedBody(), Charsets.UTF_8);
    } catch (EncodingException e) {
        logger.error("Error reading SMRF message. msgId: {}. from: {} to: {}. Reason: {}. Discarding joynr message.", new Object[] { message.getSender(), message.getRecipient(), message.getId(), e.getMessage() });
        return;
    }
    String type = message.getType();
    try {
        if (Message.VALUE_MESSAGE_TYPE_REPLY.equals(type)) {
            Reply reply = objectMapper.readValue(payload, Reply.class);
            logger.trace("Parsed reply from message payload :" + payload);
            handle(reply);
        } else if (Message.VALUE_MESSAGE_TYPE_SUBSCRIPTION_REPLY.equals(type)) {
            SubscriptionReply subscriptionReply = objectMapper.readValue(payload, SubscriptionReply.class);
            logger.trace("Parsed subscription reply from message payload :" + payload);
            handle(subscriptionReply);
        } else if (Message.VALUE_MESSAGE_TYPE_REQUEST.equals(type)) {
            final Request request = objectMapper.readValue(payload, Request.class);
            request.setCreatorUserId(message.getCreatorUserId());
            request.setContext(message.getContext());
            logger.trace("Parsed request from message payload :" + payload);
            handle(request, message.getSender(), message.getRecipient(), expiryDate, customHeaders, message.isCompressed());
        } else if (Message.VALUE_MESSAGE_TYPE_ONE_WAY.equals(type)) {
            OneWayRequest oneWayRequest = objectMapper.readValue(payload, OneWayRequest.class);
            oneWayRequest.setCreatorUserId(message.getCreatorUserId());
            oneWayRequest.setContext(message.getContext());
            logger.trace("Parsed one way request from message payload :" + payload);
            handle(oneWayRequest, message.getRecipient(), expiryDate);
        } else if (Message.VALUE_MESSAGE_TYPE_SUBSCRIPTION_REQUEST.equals(type) || Message.VALUE_MESSAGE_TYPE_BROADCAST_SUBSCRIPTION_REQUEST.equals(type) || Message.VALUE_MESSAGE_TYPE_MULTICAST_SUBSCRIPTION_REQUEST.equals(type)) {
            SubscriptionRequest subscriptionRequest = objectMapper.readValue(payload, SubscriptionRequest.class);
            logger.trace("Parsed subscription request from message payload :" + payload);
            handle(subscriptionRequest, message.getSender(), message.getRecipient());
        } else if (Message.VALUE_MESSAGE_TYPE_SUBSCRIPTION_STOP.equals(type)) {
            SubscriptionStop subscriptionStop = objectMapper.readValue(payload, SubscriptionStop.class);
            logger.trace("Parsed subscription stop from message payload :" + payload);
            handle(subscriptionStop);
        } else if (Message.VALUE_MESSAGE_TYPE_PUBLICATION.equals(type)) {
            SubscriptionPublication publication = objectMapper.readValue(payload, SubscriptionPublication.class);
            logger.trace("Parsed publication from message payload :" + payload);
            handle(publication);
        } else if (Message.VALUE_MESSAGE_TYPE_MULTICAST.equals(type)) {
            MulticastPublication multicastPublication = objectMapper.readValue(payload, MulticastPublication.class);
            logger.trace("Parsed multicast publication from message payload: {}", payload);
            handle(multicastPublication);
        }
    } catch (IOException e) {
        logger.error("Error parsing payload. msgId: {}. from: {} to: {}. Reason: {}. Discarding joynr message.", message.getId(), message.getSender(), message.getRecipient(), e.getMessage());
        return;
    }
}
Also used : SubscriptionReply(joynr.SubscriptionReply) EncodingException(io.joynr.smrf.EncodingException) OneWayRequest(joynr.OneWayRequest) MulticastSubscriptionRequest(joynr.MulticastSubscriptionRequest) SubscriptionRequest(joynr.SubscriptionRequest) Request(joynr.Request) IOException(java.io.IOException) OneWayRequest(joynr.OneWayRequest) MulticastSubscriptionRequest(joynr.MulticastSubscriptionRequest) SubscriptionRequest(joynr.SubscriptionRequest) MulticastPublication(joynr.MulticastPublication) SubscriptionPublication(joynr.SubscriptionPublication) SubscriptionReply(joynr.SubscriptionReply) Reply(joynr.Reply) SubscriptionStop(joynr.SubscriptionStop)

Example 7 with EncodingException

use of io.joynr.smrf.EncodingException in project joynr by bmwcarit.

the class DispatcherImpl method error.

@Override
public void error(ImmutableMessage message, Throwable error) {
    if (message == null) {
        logger.error("error: ", error);
        return;
    }
    String type = message.getType();
    String payload;
    try {
        payload = new String(message.getUnencryptedBody(), Charsets.UTF_8);
    } catch (EncodingException e) {
        logger.error("Error extracting payload for message {}. Reason: {}", new Object[] { message.getId(), e.getMessage() });
        return;
    }
    try {
        if (type.equals(Message.VALUE_MESSAGE_TYPE_REQUEST)) {
            Request request = objectMapper.readValue(payload, Request.class);
            requestReplyManager.handleError(request, error);
        }
    } catch (IOException e) {
        logger.error("Error extracting payload for message " + message.getId() + ", raw payload: " + payload, e.getMessage());
    }
}
Also used : EncodingException(io.joynr.smrf.EncodingException) OneWayRequest(joynr.OneWayRequest) MulticastSubscriptionRequest(joynr.MulticastSubscriptionRequest) SubscriptionRequest(joynr.SubscriptionRequest) Request(joynr.Request) IOException(java.io.IOException)

Example 8 with EncodingException

use of io.joynr.smrf.EncodingException in project joynr by bmwcarit.

the class AttachmentSenderService method postMessageWithAttachment.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postMessageWithAttachment(@PathParam("ccid") String ccid, @FormDataParam("wrapper") String serializedMessage, @FormDataParam("attachment") InputStream attachment) {
    try {
        byte[] serializedMessageBytes = serializedMessage.getBytes(Charsets.UTF_8);
        ImmutableMessage message = new ImmutableMessage(serializedMessageBytes);
        log.debug("Message with attachment received! Expiry Date : " + message.getTtlMs());
        int bytesRead = 0;
        int bytesToRead = 1024;
        byte[] input = new byte[bytesToRead];
        while (bytesRead < bytesToRead) {
            int result = attachment.read(input, bytesRead, bytesToRead - bytesRead);
            if (result == -1)
                break;
            bytesRead += result;
        }
        attachmentStorage.put(message.getId(), input);
        log.debug("Uploaded attachment : " + new String(input, 0, Math.min(50, input.length), "UTF-8"));
        // post message
        try {
            String msgId = message.getId();
            log.debug("******POST message {} to cluster controller: {}", msgId, ccid);
            log.trace("******POST message {} to cluster controller: {} extended info: \r\n {}", ccid, message);
            // the location that can be queried to get the message
            // status
            // TODO REST URL for message status?
            String path = longPollingDelegate.postMessage(ccid, serializedMessageBytes);
            URI location = ui.getBaseUriBuilder().path(path).build();
            // return the message status location to the sender.
            return Response.created(location).header("msgId", msgId).build();
        } catch (WebApplicationException e) {
            log.error("Invalid request from host {}", request.getRemoteHost());
            throw e;
        } catch (Exception e) {
            log.error("POST message for cluster controller: error: {}", e.getMessage());
            throw new WebApplicationException(e);
        }
    } catch (IOException | EncodingException | UnsuppportedVersionException e) {
        log.error("POST message for cluster controller: error: {}", e.getMessage(), e);
        return Response.serverError().build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) EncodingException(io.joynr.smrf.EncodingException) ImmutableMessage(joynr.ImmutableMessage) IOException(java.io.IOException) URI(java.net.URI) EncodingException(io.joynr.smrf.EncodingException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 9 with EncodingException

use of io.joynr.smrf.EncodingException in project joynr by bmwcarit.

the class MessagingWithoutContentTypeService method postMessageWithoutContentType.

/**
 * Send a message to the given cluster controller like the above method
 * postMessage
 * @param ccid the channel id
 * @param serializedMessage a serialized SMRF message to be sent
 * @return response builder object with the URL that can be queried to get the message
 * @throws IOException on I/O error
 * @throws JsonParseException on parsing problems due to non-well formed content
 * @throws JsonMappingException on fatal problems with mapping of content
 */
@POST
@Consumes({ MediaType.APPLICATION_OCTET_STREAM })
public Response postMessageWithoutContentType(@PathParam("ccid") String ccid, byte[] serializedMessage) throws IOException {
    ImmutableMessage message;
    try {
        message = new ImmutableMessage(serializedMessage);
    } catch (EncodingException | UnsuppportedVersionException e) {
        log.error("Failed to deserialize SMRF message: {}", e.getMessage());
        throw new WebApplicationException(e);
    }
    try {
        String msgId = message.getId();
        log.debug("******POST message {} to cluster controller: {}", msgId, ccid);
        log.trace("******POST message {} to cluster controller: {} extended info: \r\n {}", ccid, message);
        response.setCharacterEncoding("UTF-8");
        // the location that can be queried to get the message
        // status
        // TODO REST URL for message status?
        String path = longPollingDelegate.postMessage(ccid, serializedMessage);
        URI location = ui.getBaseUriBuilder().path(path).build();
        // return the message status location to the sender.
        return Response.created(location).header("msgId", msgId).build();
    } catch (WebApplicationException e) {
        log.error("Invalid request from host {}", request.getRemoteHost());
        throw e;
    } catch (Exception e) {
        log.error("POST message for cluster controller: error: {}", e.getMessage());
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) EncodingException(io.joynr.smrf.EncodingException) ImmutableMessage(joynr.ImmutableMessage) URI(java.net.URI) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) EncodingException(io.joynr.smrf.EncodingException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsuppportedVersionException(io.joynr.smrf.UnsuppportedVersionException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 10 with EncodingException

use of io.joynr.smrf.EncodingException 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)

Aggregations

EncodingException (io.joynr.smrf.EncodingException)11 ImmutableMessage (joynr.ImmutableMessage)9 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)8 IOException (java.io.IOException)5 URI (java.net.URI)4 POST (javax.ws.rs.POST)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JoynrHttpException (io.joynr.communications.exceptions.JoynrHttpException)3 Produces (javax.ws.rs.Produces)3 Consumes (javax.ws.rs.Consumes)2 MulticastSubscriptionRequest (joynr.MulticastSubscriptionRequest)2 OneWayRequest (joynr.OneWayRequest)2 Request (joynr.Request)2 SubscriptionRequest (joynr.SubscriptionRequest)2 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Path (javax.ws.rs.Path)1