Search in sources :

Example 1 with ContentHeaderProperties

use of com.swiftmq.amqp.v091.types.ContentHeaderProperties in project swiftmq-ce by iitsoftware.

the class ChannelHandler method handleHeaderFrame.

private void handleHeaderFrame(Frame frame) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", handleHeaderFrame");
    if (currentMessage != null && currentMessage.getContentHeaderProperties() == null) {
        try {
            ContentHeaderProperties contentHeaderProperties = (ContentHeaderProperties) frame.getPayloadObject();
            currentMessage.setContentHeaderProperties(contentHeaderProperties);
            if (contentHeaderProperties.getBodySize() == 0)
                publishCurrentMessage();
        } catch (Exception e) {
            amqpHandler.dispatch(new POSendChannelClose(channelNo, Constants.CHANNEL_ERROR, e.getMessage() == null ? e.toString() : e.getMessage(), currentMessage.getPublish()));
        }
    } else
        amqpHandler.dispatch(new POSendClose(Constants.UNEXPECTED_FRAME, "Unexpected: " + frame));
}
Also used : ContentHeaderProperties(com.swiftmq.amqp.v091.types.ContentHeaderProperties) JMSException(javax.jms.JMSException) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException)

Example 2 with ContentHeaderProperties

use of com.swiftmq.amqp.v091.types.ContentHeaderProperties in project swiftmq-ce by iitsoftware.

the class BasicInboundTransformer method transformHeader.

private void transformHeader(MessageImpl msg, ContentHeaderProperties prop, DestinationFactory destinationFactory) throws JMSException {
    if (prop.getContentType() != null)
        msg.setStringProperty(prefixVendor + "ContentType", prop.getContentType());
    if (prop.getContentEncoding() != null)
        msg.setStringProperty(prefixVendor + "ContentEncoding", prop.getContentEncoding());
    if (prop.getDeliveryMode() != null)
        msg.setJMSDeliveryMode(prop.getDeliveryMode() == 1 ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT);
    else
        msg.setJMSDeliveryMode(defaultDeliveryMode);
    if (prop.getPriority() != null)
        msg.setJMSPriority(prop.getPriority());
    else
        msg.setJMSPriority(defaultPriority);
    if (prop.getCorrelationId() != null)
        msg.setJMSCorrelationID(prop.getCorrelationId());
    if (prop.getReplyTo() != null)
        msg.setJMSReplyTo(destinationFactory.create(prop.getReplyTo()));
    if (prop.getExpiration() != null) {
        long value = Long.valueOf(prop.getExpiration());
        if (value > 0)
            msg.setJMSExpiration(System.currentTimeMillis() + value);
    }
    if (prop.getMessageId() != null)
        msg.setJMSMessageID(prop.getMessageId());
    else
        msg.setJMSMessageID(nextMsgId());
    if (prop.getTimestamp() != null)
        msg.setJMSTimestamp(prop.getTimestamp());
    else
        msg.setJMSTimestamp(System.currentTimeMillis());
    if (prop.getType() != null)
        msg.setStringProperty(prefixVendor + "Type", prop.getType());
    if (prop.getUserId() != null)
        msg.setStringProperty(prefixVendor + "UserId", prop.getUserId());
    if (prop.getAppId() != null)
        msg.setStringProperty(prefixVendor + "AppId", prop.getAppId());
    if (prop.getClusterId() != null)
        msg.setStringProperty(prefixVendor + "ClusterId", prop.getClusterId());
    Map<String, Object> headers = prop.getHeaders();
    if (headers != null) {
        for (Iterator iter = headers.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iter.next();
            String name = nameTranslator.translate((String) entry.getKey());
            Field field = (Field) entry.getValue();
            switch(field.getType()) {
                case 't':
                    msg.setBooleanProperty(name, (Boolean) field.getValue());
                    break;
                case 'b':
                case 'B':
                case 'U':
                case 'u':
                case 'I':
                    msg.setIntProperty(name, (Integer) field.getValue());
                    break;
                case 'i':
                case 'L':
                    msg.setLongProperty(name, (Long) field.getValue());
                    break;
                case 'l':
                    break;
                case 'f':
                case 'd':
                    msg.setFloatProperty(name, (Float) field.getValue());
                    break;
                case 'D':
                    break;
                case 's':
                    msg.setShortProperty(name, (Short) field.getValue());
                    break;
                case 'S':
                    msg.setStringProperty(name, (String) field.getStringValue());
                    break;
                case 'A':
                    break;
                case 'T':
                    msg.setLongProperty(name, (Long) field.getValue());
                    break;
                case 'F':
                    break;
                case 'V':
                    break;
            }
        }
    }
}
Also used : Field(com.swiftmq.amqp.v091.types.Field) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ContentHeaderProperties

use of com.swiftmq.amqp.v091.types.ContentHeaderProperties in project swiftmq-ce by iitsoftware.

the class BasicInboundTransformer method transform.

public MessageImpl transform(MessageWrap messageWrap, DestinationFactory destinationFactory) throws JMSException {
    MessageImpl msg = null;
    ContentHeaderProperties prop = messageWrap.getContentHeaderProperties();
    byte[] payload = getPayload(prop.getBodySize().intValue(), messageWrap.getBodyParts());
    if (prop.getContentType() != null && prop.getContentType().startsWith("text/")) {
        TextMessageImpl textMessage = new TextMessageImpl();
        try {
            textMessage.setText(new String(payload, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            throw new JMSException(e.toString());
        }
        msg = textMessage;
    } else
        msg = new BytesMessageImpl(payload, payload.length);
    msg.setLongProperty(messageFormat, Util.MESSAGE_FORMAT);
    transformHeader(msg, prop, destinationFactory);
    return msg;
}
Also used : BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl) TextMessageImpl(com.swiftmq.jms.TextMessageImpl) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JMSException(javax.jms.JMSException) ContentHeaderProperties(com.swiftmq.amqp.v091.types.ContentHeaderProperties) MessageImpl(com.swiftmq.jms.MessageImpl) TextMessageImpl(com.swiftmq.jms.TextMessageImpl) BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl)

Example 4 with ContentHeaderProperties

use of com.swiftmq.amqp.v091.types.ContentHeaderProperties in project swiftmq-ce by iitsoftware.

the class BasicOutboundTransformer method transform.

public Delivery transform(MessageImpl jmsMessage) throws Exception {
    jmsMessage.reset();
    ContentHeaderProperties prop = new ContentHeaderProperties();
    byte[] body = null;
    if (jmsMessage instanceof TextMessageImpl) {
        String text = ((TextMessageImpl) jmsMessage).getText();
        if (text != null)
            body = text.getBytes("utf-8");
    } else if (jmsMessage instanceof BytesMessageImpl) {
        BytesMessageImpl bytesMessage = (BytesMessageImpl) jmsMessage;
        if (bytesMessage.getBodyLength() > 0) {
            body = new byte[(int) bytesMessage.getBodyLength()];
            bytesMessage.readBytes(body);
        }
    } else
        throw new Exception("Unable to tranform (only Text/BytesMessages supported): " + jmsMessage);
    // Fill props
    prop.setWeight(0);
    prop.setBodySize(Long.valueOf(body.length));
    prop.setContentType(jmsMessage.getStringProperty(prefixVendor + "ContentType"));
    prop.setContentEncoding(jmsMessage.getStringProperty(prefixVendor + "ContentEncoding"));
    prop.setDeliveryMode(jmsMessage.getJMSDeliveryMode() == DeliveryMode.NON_PERSISTENT ? 1 : 2);
    prop.setPriority(jmsMessage.getJMSPriority());
    prop.setCorrelationId(jmsMessage.getJMSCorrelationID());
    Destination replyTo = jmsMessage.getJMSReplyTo();
    if (replyTo != null)
        prop.setReplyTo(replyTo.toString());
    prop.setExpiration(String.valueOf(jmsMessage.getJMSExpiration()));
    prop.setMessageId(jmsMessage.getJMSMessageID());
    prop.setTimestamp(jmsMessage.getJMSTimestamp());
    prop.setType(jmsMessage.getStringProperty(prefixVendor + "Type"));
    prop.setUserId(jmsMessage.getStringProperty(prefixVendor + "UserId"));
    prop.setAppId(jmsMessage.getStringProperty(prefixVendor + "AppId"));
    prop.setClusterId(jmsMessage.getStringProperty(prefixVendor + "ClusterId"));
    Map<String, Object> headers = new HashMap<String, Object>();
    for (Enumeration _enum = jmsMessage.getPropertyNames(); _enum.hasMoreElements(); ) {
        String name = (String) _enum.nextElement();
        if (!name.startsWith(prefixVendor)) {
            Object value = jmsMessage.getObjectProperty(name);
            char type = toFieldType(value);
            if (value instanceof String)
                value = ((String) value).getBytes("utf-8");
            headers.put(nameTranslator.translate(name), new Field(type, value));
        }
    }
    if (headers.size() > 0)
        prop.setHeaders(headers);
    return new Delivery(prop, body);
}
Also used : Destination(javax.jms.Destination) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Field(com.swiftmq.amqp.v091.types.Field) BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl) TextMessageImpl(com.swiftmq.jms.TextMessageImpl) ContentHeaderProperties(com.swiftmq.amqp.v091.types.ContentHeaderProperties) Delivery(com.swiftmq.impl.amqp.amqp.v00_09_01.Delivery)

Example 5 with ContentHeaderProperties

use of com.swiftmq.amqp.v091.types.ContentHeaderProperties in project swiftmq-ce by iitsoftware.

the class ChannelHandler method visit.

public void visit(POSendMessages po) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " ...");
    try {
        SourceMessageProcessor mp = po.getSourceMessageProcessor();
        Consumer consumer = mp.getConsumer();
        if (mp.getException() != null)
            amqpHandler.dispatch(new POSendChannelClose(channelNo, Constants.CHANNEL_ERROR, mp.getException().toString(), null));
        else {
            if (!consumer.isNoAck() && waitingSends.size() > 0) {
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " sends waiting, adding to list");
                waitingSends.add(po);
                return;
            }
            Delivery[] deliveries = po.getDeliveries() != null ? po.getDeliveries() : mp.getTransformedMessages();
            for (int i = po.getDeliveryStart(); i < deliveries.length; i++) {
                Delivery delivery = deliveries[i];
                if (!consumer.isNoAck() && !isWithinPrefetchLimits(delivery.getBody().length)) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " not within prefetch limit, adding to list");
                    po.setDeliveryStart(i);
                    po.setDeliveries(deliveries);
                    waitingSends.add(po);
                    return;
                }
                long deliveryTag = getNextDeliveryTag();
                if (!consumer.isNoAck())
                    addUnacked(deliveryTag, consumer, delivery.getMessageIndex(), delivery.getBody().length);
                else
                    consumer.ack(delivery.getMessageIndex());
                Deliver deliver = new Deliver();
                deliver.setConsumerTag(mp.getConsumer().getConsumerTag());
                deliver.setDeliveryTag(deliveryTag);
                deliver.setExchange("");
                deliver.setRedelivered(delivery.isRedelivered());
                deliver.setRoutingKey(mp.getConsumer().getOriginalQueueName());
                Frame frame = new Frame(Frame.TYPE_METHOD, channelNo, 0, null);
                amqpHandler.toPayload(frame, deliver);
                versionedConnection.send(frame);
                frame = new Frame(Frame.TYPE_HEADER, channelNo, 0, null);
                ContentHeaderProperties contentHeaderProperties = delivery.getContentHeaderProperties();
                contentHeaderProperties.setClassId(deliver._getClassId());
                amqpHandler.toPayload(frame, contentHeaderProperties);
                versionedConnection.send(frame);
                byte[] body = delivery.getBody();
                int maxFrameSize = amqpHandler.getMaxFrameSize();
                int pos = 0;
                while (pos < body.length) {
                    byte[] part = new byte[Math.min(body.length - pos, maxFrameSize)];
                    System.arraycopy(body, pos, part, 0, part.length);
                    pos += part.length;
                    frame = new Frame(Frame.TYPE_BODY, channelNo, 0, null);
                    frame.setPayload(part);
                    frame.setSize(part.length);
                    versionedConnection.send(frame);
                }
            }
            mp.getConsumer().startMessageProcessor(mp);
        }
    } catch (Exception e) {
        amqpHandler.dispatch(new POSendChannelClose(channelNo, Constants.CHANNEL_ERROR, e.toString(), null));
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " done");
}
Also used : Frame(com.swiftmq.amqp.v091.types.Frame) ContentHeaderProperties(com.swiftmq.amqp.v091.types.ContentHeaderProperties) JMSException(javax.jms.JMSException) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException)

Aggregations

ContentHeaderProperties (com.swiftmq.amqp.v091.types.ContentHeaderProperties)4 JMSException (javax.jms.JMSException)3 Field (com.swiftmq.amqp.v091.types.Field)2 BytesMessageImpl (com.swiftmq.jms.BytesMessageImpl)2 TextMessageImpl (com.swiftmq.jms.TextMessageImpl)2 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)2 HashMap (java.util.HashMap)2 Frame (com.swiftmq.amqp.v091.types.Frame)1 Delivery (com.swiftmq.impl.amqp.amqp.v00_09_01.Delivery)1 MessageImpl (com.swiftmq.jms.MessageImpl)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Destination (javax.jms.Destination)1