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 package org.apache.qpid.framing;
022
023 import org.apache.mina.common.ByteBuffer;
024 import org.apache.qpid.protocol.AMQVersionAwareProtocolSession;
025 import org.apache.qpid.AMQException;
026
027 public class ContentHeaderBody implements AMQBody
028 {
029 public static final byte TYPE = 2;
030
031 public int classId;
032
033 public int weight;
034
035 /** unsigned long but java can't handle that anyway when allocating byte array */
036 public long bodySize;
037
038 /** must never be null */
039 public ContentHeaderProperties properties;
040
041 public ContentHeaderBody()
042 {
043 }
044
045 public ContentHeaderBody(ByteBuffer buffer, long size) throws AMQFrameDecodingException
046 {
047 classId = buffer.getUnsignedShort();
048 weight = buffer.getUnsignedShort();
049 bodySize = buffer.getLong();
050 int propertyFlags = buffer.getUnsignedShort();
051 ContentHeaderPropertiesFactory factory = ContentHeaderPropertiesFactory.getInstance();
052 properties = factory.createContentHeaderProperties(classId, propertyFlags, buffer, (int)size - 14);
053
054 }
055
056
057 public ContentHeaderBody(ContentHeaderProperties props, int classId)
058 {
059 properties = props;
060 this.classId = classId;
061 }
062
063 public ContentHeaderBody(int classId, int weight, ContentHeaderProperties props, long bodySize)
064 {
065 this(props, classId);
066 this.weight = weight;
067 this.bodySize = bodySize;
068 }
069
070 public byte getFrameType()
071 {
072 return TYPE;
073 }
074
075 protected void populateFromBuffer(ByteBuffer buffer, long size)
076 throws AMQFrameDecodingException, AMQProtocolVersionException
077 {
078 classId = buffer.getUnsignedShort();
079 weight = buffer.getUnsignedShort();
080 bodySize = buffer.getLong();
081 int propertyFlags = buffer.getUnsignedShort();
082 ContentHeaderPropertiesFactory factory = ContentHeaderPropertiesFactory.getInstance();
083 properties = factory.createContentHeaderProperties(classId, propertyFlags, buffer, (int)size - 14);
084 }
085
086 /**
087 * Helper method that is used currently by the persistence layer (by BDB at the moment).
088 * @param buffer
089 * @param size
090 * @return
091 * @throws AMQFrameDecodingException
092 */
093 public static ContentHeaderBody createFromBuffer(ByteBuffer buffer, long size)
094 throws AMQFrameDecodingException, AMQProtocolVersionException
095 {
096 ContentHeaderBody body = new ContentHeaderBody(buffer, size);
097
098 return body;
099 }
100
101 public int getSize()
102 {
103 return 2 + 2 + 8 + 2 + properties.getPropertyListSize();
104 }
105
106 public void writePayload(ByteBuffer buffer)
107 {
108 EncodingUtils.writeUnsignedShort(buffer, classId);
109 EncodingUtils.writeUnsignedShort(buffer, weight);
110 buffer.putLong(bodySize);
111 EncodingUtils.writeUnsignedShort(buffer, properties.getPropertyFlags());
112 properties.writePropertyListPayload(buffer);
113 }
114
115 public void handle(final int channelId, final AMQVersionAwareProtocolSession session)
116 throws AMQException
117 {
118 session.contentHeaderReceived(channelId, this);
119 }
120
121 public static AMQFrame createAMQFrame(int channelId, int classId, int weight, BasicContentHeaderProperties properties,
122 long bodySize)
123 {
124 return new AMQFrame(channelId, new ContentHeaderBody(classId, weight, properties, bodySize));
125 }
126
127 public static AMQFrame createAMQFrame(int channelId, ContentHeaderBody body)
128 {
129 return new AMQFrame(channelId, body);
130 }
131 }
|