001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of Granite Data Services.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or (at your
010 * option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful, but
013 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 * for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020 package org.granite.client.messaging.codec;
021
022 import java.io.ByteArrayInputStream;
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.OutputStream;
026 import java.util.HashMap;
027
028 import org.granite.client.configuration.Configuration;
029 import org.granite.client.messaging.channel.Channel;
030 import org.granite.context.GraniteContext;
031 import org.granite.context.SimpleGraniteContext;
032 import org.granite.messaging.amf.io.AMF3Deserializer;
033 import org.granite.messaging.amf.io.AMF3Serializer;
034 import org.granite.util.ContentType;
035
036 import flex.messaging.messages.Message;
037
038 /**
039 * @author Franck WOLFF
040 */
041 public class AMF3MessagingCodec implements MessagingCodec<Message[]> {
042
043 private final Configuration config;
044
045 public AMF3MessagingCodec(Configuration config) {
046 this.config = config;
047 }
048
049 @Override
050 public ClientType getClientType() {
051 return config.getClientType();
052 }
053
054 @Override
055 public String getContentType() {
056 return ContentType.AMF.mimeType();
057 }
058
059 @Override
060 public void encode(Message[] messages, OutputStream output) throws IOException {
061 SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), getClientType().toString());
062 try {
063 AMF3Serializer serializer = new AMF3Serializer(output);
064 serializer.writeObject(messages);
065 serializer.close();
066 }
067 finally {
068 GraniteContext.release();
069 }
070 }
071
072 @Override
073 public Message[] decode(InputStream input) throws IOException {
074 SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), getClientType().toString());
075 try {
076 AMF3Deserializer deserializer = new AMF3Deserializer(input);
077 Object[] objects = (Object[])deserializer.readObject();
078 deserializer.close();
079
080 if (objects != null) {
081 Message[] messages = new Message[objects.length];
082 System.arraycopy(objects, 0, messages, 0, objects.length);
083
084 for (Message message : messages) {
085 if (message != null && Boolean.TRUE.equals(message.getHeader(Channel.BYTEARRAY_BODY_HEADER))) {
086 byte[] body = (byte[])message.getBody();
087 deserializer = new AMF3Deserializer(new ByteArrayInputStream(body));
088 message.setBody(deserializer.readObject());
089 deserializer.close();
090 }
091 }
092
093 return messages;
094 }
095 return new Message[0];
096 }
097 finally {
098 GraniteContext.release();
099 }
100 }
101 }