001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.amqp; 018 019import org.fusesource.hawtbuf.Buffer; 020 021/** 022 */ 023public class AmqpHeader { 024 025 static final Buffer PREFIX = new Buffer(new byte[] { 'A', 'M', 'Q', 'P' }); 026 027 private Buffer buffer; 028 029 public AmqpHeader() { 030 this(new Buffer(new byte[] { 'A', 'M', 'Q', 'P', 0, 1, 0, 0 })); 031 } 032 033 public AmqpHeader(Buffer buffer) { 034 this(buffer, true); 035 } 036 037 public AmqpHeader(Buffer buffer, boolean validate) { 038 setBuffer(buffer, validate); 039 } 040 041 public int getProtocolId() { 042 return buffer.get(4) & 0xFF; 043 } 044 045 public void setProtocolId(int value) { 046 buffer.data[buffer.offset + 4] = (byte) value; 047 } 048 049 public int getMajor() { 050 return buffer.get(5) & 0xFF; 051 } 052 053 public void setMajor(int value) { 054 buffer.data[buffer.offset + 5] = (byte) value; 055 } 056 057 public int getMinor() { 058 return buffer.get(6) & 0xFF; 059 } 060 061 public void setMinor(int value) { 062 buffer.data[buffer.offset + 6] = (byte) value; 063 } 064 065 public int getRevision() { 066 return buffer.get(7) & 0xFF; 067 } 068 069 public void setRevision(int value) { 070 buffer.data[buffer.offset + 7] = (byte) value; 071 } 072 073 public Buffer getBuffer() { 074 return buffer; 075 } 076 077 public void setBuffer(Buffer value) { 078 setBuffer(value, true); 079 } 080 081 public void setBuffer(Buffer value, boolean validate) { 082 if (validate && !value.startsWith(PREFIX) || value.length() != 8) { 083 throw new IllegalArgumentException("Not an AMQP header buffer"); 084 } 085 buffer = value.buffer(); 086 } 087 088 public boolean hasValidPrefix() { 089 return buffer.startsWith(PREFIX); 090 } 091 092 @Override 093 public String toString() { 094 StringBuilder builder = new StringBuilder(); 095 for (int i = 0; i < buffer.length(); ++i) { 096 char value = (char) buffer.get(i); 097 if (Character.isLetter(value)) { 098 builder.append(value); 099 } else { 100 builder.append(","); 101 builder.append((int) value); 102 } 103 } 104 return builder.toString(); 105 } 106}