001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 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    
021    package org.granite.client.messaging.codec;
022    
023    import java.io.ByteArrayInputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.OutputStream;
027    import java.util.HashMap;
028    
029    import org.granite.client.configuration.Configuration;
030    import org.granite.client.messaging.channel.Channel;
031    import org.granite.context.GraniteContext;
032    import org.granite.context.SimpleGraniteContext;
033    import org.granite.messaging.amf.io.AMF3Deserializer;
034    import org.granite.messaging.amf.io.AMF3Serializer;
035    import org.granite.util.ContentType;
036    
037    import flex.messaging.messages.Message;
038    
039    /**
040     * @author Franck WOLFF
041     */
042    public class AMF3MessagingCodec implements MessagingCodec<Message[]> {
043    
044            private final Configuration config;
045            
046            public AMF3MessagingCodec(Configuration config) {
047                    this.config = config;
048            }
049    
050            @Override
051            public String getContentType() {
052                    return ContentType.AMF.mimeType();
053            }
054    
055            @Override
056            public void encode(Message[] messages, OutputStream output) throws IOException {
057                    SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), "java");
058                    try {
059                            AMF3Serializer serializer = new AMF3Serializer(output);
060                            serializer.writeObject(messages);
061                            serializer.close();
062                    }
063                    finally {
064                            GraniteContext.release();
065                    }
066            }
067    
068            @Override
069            public Message[] decode(InputStream input) throws IOException {
070                    SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), "java");
071                    try {
072                            AMF3Deserializer deserializer = new AMF3Deserializer(input);
073                            Object[] objects = (Object[])deserializer.readObject();
074                            deserializer.close();
075                            
076                            if (objects != null) {
077                                    Message[] messages = new Message[objects.length];
078                                    System.arraycopy(objects, 0, messages, 0, objects.length);
079                                    
080                                    for (Message message : messages) {
081                                            if (message != null && Boolean.TRUE.equals(message.getHeader(Channel.BYTEARRAY_BODY_HEADER))) {
082                                                    byte[] body = (byte[])message.getBody();
083                                                    deserializer = new AMF3Deserializer(new ByteArrayInputStream(body));
084                                                    message.setBody(deserializer.readObject());
085                                                    deserializer.close();
086                                            }
087                                    }
088                                    
089                                    return messages;
090                            }
091                            return new Message[0];
092                    }
093                    finally {
094                            GraniteContext.release();
095                    }
096            }
097    }