Search in sources :

Example 6 with UnsuppportedVersionException

use of io.joynr.smrf.UnsuppportedVersionException 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 7 with UnsuppportedVersionException

use of io.joynr.smrf.UnsuppportedVersionException 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 8 with UnsuppportedVersionException

use of io.joynr.smrf.UnsuppportedVersionException 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)8 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)8 ImmutableMessage (joynr.ImmutableMessage)8 URI (java.net.URI)4 POST (javax.ws.rs.POST)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JoynrHttpException (io.joynr.communications.exceptions.JoynrHttpException)3 IOException (java.io.IOException)3 Produces (javax.ws.rs.Produces)3 Consumes (javax.ws.rs.Consumes)2 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Path (javax.ws.rs.Path)1 Broadcaster (org.atmosphere.cpr.Broadcaster)1