001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022/* 023 * www.openamf.org 024 * 025 * Distributable under LGPL license. 026 * See terms of license at gnu.org. 027 */ 028 029package org.granite.messaging.amf; 030 031import java.util.ArrayList; 032import java.util.Iterator; 033import java.util.List; 034import java.io.Serializable; 035 036/** 037 * AMF Message 038 * 039 * @author Jason Calabrese <jasonc@missionvi.com> 040 * @author Pat Maddox <pergesu@users.sourceforge.net> 041 * @see AMF0Header 042 * @see AMF0Body 043 * @version $Revision: 1.13 $, $Date: 2003/11/30 02:25:00 $ 044 */ 045public class AMF0Message implements Serializable { 046 047 private static final long serialVersionUID = 1L; 048 049 public static final int CURRENT_VERSION = 3; 050 051 protected int version = CURRENT_VERSION; 052 protected final List<AMF0Header> headers = new ArrayList<AMF0Header>(); 053 protected final List<AMF0Body> bodies = new ArrayList<AMF0Body>(); 054 055 public void addHeader(String key, boolean required, Object value) { 056 addHeader(new AMF0Header(key, required, value)); 057 } 058 059 public void addHeader(AMF0Header header) { 060 headers.add(header); 061 } 062 063 public int getHeaderCount() { 064 return headers.size(); 065 } 066 067 public AMF0Header getHeader(int index) { 068 return headers.get(index); 069 } 070 071 /** 072 * 073 * @return a List that contains zero or more {@link AMF0Header} objects 074 * 075 */ 076 public List<AMF0Header> getHeaders() { 077 return headers; 078 } 079 080 public AMF0Body addBody(String target, String response, Object value, byte type) { 081 return addBody(new AMF0Body(target, response, value, type)); 082 } 083 084 public AMF0Body addBody(AMF0Body body) { 085 bodies.add(body); 086 return body; 087 } 088 089 public int getBodyCount() { 090 return bodies.size(); 091 } 092 093 public AMF0Body getBody(int index) { 094 return bodies.get(index); 095 } 096 097 public Iterator<AMF0Body> getBodies() { 098 return bodies.iterator(); 099 } 100 101 public boolean isFirstMessage() { 102 if (bodies.size() == 1) 103 return bodies.get(0).isFirstBody(); 104 105 for (AMF0Body body : bodies) { 106 if (body.isFirstBody()) 107 return true; 108 } 109 110 return false; 111 } 112 113 public int getVersion() { 114 return version; 115 } 116 117 public void setVersion(int version) { 118 this.version = version; 119 } 120 121 public String getBodiesString() { 122 StringBuffer sb = new StringBuffer(); 123 for (int i = 0; i < bodies.size(); i++) { 124 if (i > 0) { 125 sb.append('\n'); 126 } 127 AMF0Body amfBody = bodies.get(i); 128 sb.append(amfBody); 129 } 130 return sb.toString(); 131 } 132 133 /* 134 * AMFMessage content 135 */ 136 137 @Override 138 public String toString() { 139 return toString(""); 140 } 141 142 public String toString(String indent) { 143 final String innerIndent = indent + " "; 144 145 StringBuilder sb = new StringBuilder(2048); 146 sb.append('\n').append(indent).append(AMF0Message.class.getName()).append(" {"); 147 148 // Print version. 149 sb.append('\n').append(indent).append(" version = ").append(version); 150 151 // Print headers. 152 sb.append('\n').append(indent).append(" headers = ["); 153 for (int i = 0; i < headers.size(); i++) { 154 AMF0Header amfHeader = headers.get(i); 155 sb.append(amfHeader.toString(innerIndent)); 156 } 157 if (headers.size() > 0) 158 sb.append('\n').append(indent).append(" "); 159 sb.append(']'); 160 161 // Print bodies. 162 sb.append('\n').append(indent).append(" bodies = ["); 163 for (int i = 0; i < bodies.size(); i++) { 164 if (i > 0) 165 sb.append(','); 166 AMF0Body amfBody = bodies.get(i); 167 sb.append(amfBody.toString(innerIndent)); 168 } 169 if (bodies.size() > 0) 170 sb.append('\n').append(indent).append(" "); 171 sb.append(']'); 172 173 sb.append('\n').append(indent).append("}"); 174 175 return sb.toString(); 176 } 177 178}