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.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.context.GraniteContext;
030    import org.granite.context.SimpleGraniteContext;
031    import org.granite.messaging.amf.AMF0Message;
032    import org.granite.messaging.amf.io.AMF0Deserializer;
033    import org.granite.messaging.amf.io.AMF0Serializer;
034    import org.granite.util.ContentType;
035    
036    /**
037     * @author Franck WOLFF
038     */
039    public class AMF0MessagingCodec implements MessagingCodec<AMF0Message> {
040    
041            private final Configuration config;
042            
043            public AMF0MessagingCodec(Configuration config) {
044                    this.config = config;
045            }
046    
047            @Override
048            public String getContentType() {
049                    return ContentType.AMF.mimeType();
050            }
051    
052            @Override
053            public void encode(AMF0Message message, OutputStream output) throws IOException {
054                    SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), "java");
055                    try {
056                            AMF0Serializer serializer = new AMF0Serializer(output);
057                            serializer.serializeMessage(message);
058                    }
059                    finally {
060                            GraniteContext.release();
061                    }
062            }
063    
064            @Override
065            public AMF0Message decode(InputStream input) throws IOException {
066                    SimpleGraniteContext.createThreadInstance(config.getGraniteConfig(), config.getServicesConfig(), new HashMap<String, Object>(0), "java");
067                    try {
068                            AMF0Deserializer deserializer = new AMF0Deserializer(input);
069                            return deserializer.getAMFMessage();
070                    }
071                    finally {
072                            GraniteContext.release();
073                    }
074            }
075    }