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 * Represents the AMQP protocol handshake packet that is sent during the 023 * initial exchange with a remote peer. 024 */ 025public class AmqpHeader { 026 027 static final Buffer PREFIX = new Buffer(new byte[] { 'A', 'M', 'Q', 'P' }); 028 029 private Buffer buffer; 030 031 public AmqpHeader() { 032 this(new Buffer(new byte[] { 'A', 'M', 'Q', 'P', 0, 1, 0, 0 })); 033 } 034 035 public AmqpHeader(Buffer buffer) { 036 this(buffer, true); 037 } 038 039 public AmqpHeader(Buffer buffer, boolean validate) { 040 setBuffer(buffer, validate); 041 } 042 043 public int getProtocolId() { 044 return buffer.get(4) & 0xFF; 045 } 046 047 public void setProtocolId(int value) { 048 buffer.data[buffer.offset + 4] = (byte) value; 049 } 050 051 public int getMajor() { 052 return buffer.get(5) & 0xFF; 053 } 054 055 public void setMajor(int value) { 056 buffer.data[buffer.offset + 5] = (byte) value; 057 } 058 059 public int getMinor() { 060 return buffer.get(6) & 0xFF; 061 } 062 063 public void setMinor(int value) { 064 buffer.data[buffer.offset + 6] = (byte) value; 065 } 066 067 public int getRevision() { 068 return buffer.get(7) & 0xFF; 069 } 070 071 public void setRevision(int value) { 072 buffer.data[buffer.offset + 7] = (byte) value; 073 } 074 075 public Buffer getBuffer() { 076 return buffer; 077 } 078 079 public void setBuffer(Buffer value) { 080 setBuffer(value, true); 081 } 082 083 public void setBuffer(Buffer value, boolean validate) { 084 if (validate && !value.startsWith(PREFIX) || value.length() != 8) { 085 throw new IllegalArgumentException("Not an AMQP header buffer"); 086 } 087 buffer = value.buffer(); 088 } 089 090 public boolean hasValidPrefix() { 091 return buffer.startsWith(PREFIX); 092 } 093 094 @Override 095 public String toString() { 096 StringBuilder builder = new StringBuilder(); 097 for (int i = 0; i < buffer.length(); ++i) { 098 char value = (char) buffer.get(i); 099 if (Character.isLetter(value)) { 100 builder.append(value); 101 } else { 102 builder.append(","); 103 builder.append((int) value); 104 } 105 } 106 return builder.toString(); 107 } 108}