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.protocol; 018 019import org.apache.activemq.command.WireFormatInfo; 020import org.apache.activemq.openwire.OpenWireFormat; 021import org.apache.activemq.openwire.OpenWireFormatFactory; 022 023/** 024 * 025 * 026 */ 027public class OpenWireProtocolVerifier implements ProtocolVerifier { 028 029 protected final OpenWireFormatFactory wireFormatFactory; 030 031 public OpenWireProtocolVerifier(OpenWireFormatFactory wireFormatFactory) { 032 this.wireFormatFactory = wireFormatFactory; 033 } 034 035 /* (non-Javadoc) 036 * @see org.apache.activemq.broker.transport.protocol.ProtocolVerifier#isProtocol(byte[]) 037 */ 038 @Override 039 public boolean isProtocol(byte[] value) { 040 if (value.length < 8) { 041 throw new IllegalArgumentException("Protocol header length changed " 042 + value.length); 043 } 044 045 int start = !((OpenWireFormat)wireFormatFactory.createWireFormat()).isSizePrefixDisabled() ? 4 : 0; 046 int j = 0; 047 // type 048 if (value[start] != WireFormatInfo.DATA_STRUCTURE_TYPE) { 049 return false; 050 } 051 start++; 052 WireFormatInfo info = new WireFormatInfo(); 053 final byte[] magic = info.getMagic(); 054 int remainingLen = value.length - start; 055 int useLen = remainingLen > magic.length ? magic.length : remainingLen; 056 useLen += start; 057 // magic 058 for (int i = start; i < useLen; i++) { 059 if (value[i] != magic[j]) { 060 return false; 061 } 062 j++; 063 } 064 return true; 065 } 066 067}