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;
}
}
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());
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations