MethodConverter_0_9.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 
022 package org.apache.qpid.framing.amqp_0_9;
023 
024 import org.apache.mina.common.ByteBuffer;
025 
026 import org.apache.qpid.framing.abstraction.AbstractMethodConverter;
027 import org.apache.qpid.framing.abstraction.ProtocolVersionMethodConverter;
028 import org.apache.qpid.framing.abstraction.ContentChunk;
029 import org.apache.qpid.framing.abstraction.MessagePublishInfo;
030 import org.apache.qpid.framing.abstraction.MessagePublishInfoImpl;
031 import org.apache.qpid.framing.*;
032 import org.apache.qpid.framing.amqp_0_9.*;
033 import org.apache.qpid.framing.amqp_0_9.BasicPublishBodyImpl;
034 
035 public class MethodConverter_0_9 extends AbstractMethodConverter implements ProtocolVersionMethodConverter
036 {
037     private int _basicPublishClassId;
038     private int _basicPublishMethodId;
039 
040     public MethodConverter_0_9()
041     {
042         super((byte)0,(byte)9);
043 
044 
045     }
046 
047     public AMQBody convertToBody(ContentChunk contentChunk)
048     {
049         if(contentChunk instanceof ContentChunk_0_9)
050         {
051             return ((ContentChunk_0_9)contentChunk).toBody();
052         }
053         else
054         {
055             return new ContentBody(contentChunk.getData());
056         }
057     }
058 
059     public ContentChunk convertToContentChunk(AMQBody body)
060     {
061         final ContentBody contentBodyChunk = (ContentBodybody;
062 
063         return new ContentChunk_0_9(contentBodyChunk);
064 
065     }
066 
067     public void configure()
068     {
069 
070         _basicPublishClassId = org.apache.qpid.framing.amqp_0_9.BasicPublishBodyImpl.CLASS_ID;
071         _basicPublishMethodId = BasicPublishBodyImpl.METHOD_ID;
072 
073     }
074 
075     public MessagePublishInfo convertToInfo(AMQMethodBody methodBody)
076     {
077         final BasicPublishBody publishBody = ((BasicPublishBodymethodBody);
078 
079         final AMQShortString exchange = publishBody.getExchange();
080         final AMQShortString routingKey = publishBody.getRoutingKey();
081 
082         return new MessagePublishInfoImpl(exchange,
083                                           publishBody.getImmediate(),
084                                           publishBody.getMandatory(),
085                                           routingKey);
086 
087     }
088 
089     public AMQMethodBody convertToBody(MessagePublishInfo info)
090     {
091 
092         return new BasicPublishBodyImpl(0,
093                                     info.getExchange(),
094                                     info.getRoutingKey(),
095                                     info.isMandatory(),
096                                     info.isImmediate()) ;
097 
098     }
099 
100     private static class ContentChunk_0_9 implements ContentChunk
101     {
102         private final ContentBody _contentBodyChunk;
103 
104         public ContentChunk_0_9(final ContentBody contentBodyChunk)
105         {
106             _contentBodyChunk = contentBodyChunk;
107         }
108 
109         public int getSize()
110         {
111             return _contentBodyChunk.getSize();
112         }
113 
114         public ByteBuffer getData()
115         {
116             return _contentBodyChunk.payload;
117         }
118 
119         public void reduceToFit()
120         {
121             _contentBodyChunk.reduceBufferToFit();
122         }
123 
124         public AMQBody toBody()
125         {
126             return _contentBodyChunk;
127         }
128     }
129 }