Jan
25
Arcadio January 25, 2009 7:05 pm

URLLoader class allow to interchange data with the server as text, variables encoded and binary but it donesn’t allow to use encoding options like others channels as RemoteObject in URL access. Nevertheless, it’s possible to use Java serialization capabilities embedding objects (writeObject) in binary format to deserializate it on the client side with Flex (readObject). This operation can be done as following:

  1. URLLoader object send request by GET/POST to the server waiting for a binary format response.
  2. Servlet (HttpServlet) get request and its parameters.
  3. Build the object to send.
  4. Use Amf3Output class from Flex Messaging libraries given with BlazeDS to convert the object in binary format AMF and write it in the response.
  5. Flex client get the response as ByteArray and deserialize it with the readObject() method.

Here are client and server codes of the process. (BlazeDS libraries must be included in the server project):

SerializeServlet.java

package com.servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

@SuppressWarnings("serial")
public class SerializeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        processRequest(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        processRequest(req, res);
    }

    @SuppressWarnings("unchecked")
    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            List object = new ArrayList();
            object.add("Mercury");
            object.add("Venus");
            object.add("Earth");
            object.add("Mars");
            object.add("Jupiter");
            object.add("Saturn");
            object.add("Uranus");
            object.add("Neptune");
            object.add("Pluto");

             byte[] data = getBytesAMF(object);
             ServletOutputStream out = response.getOutputStream();
             out.write(data);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] getBytesAMF(Object obj) throws java.io.IOException {
        SerializationContext context = SerializationContext.getSerializationContext();
        Amf3Output amf3Output = new Amf3Output(context);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        amf3Output.setOutputStream(bos);
        amf3Output.writeObject(obj);
        amf3Output.flush();
        amf3Output.close();
        byte[] data = bos.toByteArray();
        return data;
    }

    public static byte[] getBytes(Object obj) throws java.io.IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        bos.close();
        byte[] data = bos.toByteArray();
        return data;
    }
}

MySerialize.mxlm

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="onCreationComplete()">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;

			[Bindable]
			private var myPlanet:ArrayCollection;

			private function onCreationComplete():void {
				mySend();
			}

			private function mySend():void {
				var loader:URLLoader=new URLLoader();
				loader.dataFormat=URLLoaderDataFormat.BINARY;
				loader.addEventListener(Event.COMPLETE,onComplete);
				loader.load(new URLRequest('http://localhost:8400/DynamicWeb/serialize'));
			}

			private function onComplete(event:Event):void {
				var data : ByteArray = ByteArray(event.target.data);
				var obj : Object = data.readObject();
				myPlanet=ArrayCollection(obj);
			}
		]]>
	</mx:Script>
	<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
		<mx:List dataProvider="{myPlanet}" width="200"/>
	</mx:VBox>
</mx:Application>
  • Share/Bookmark

Comments (2)

  1. Arcadio said:

    Script for other output formats:

    XML
    ServletOutputStream out = response.getOutputStream();
    response.setContentType(”application/binary”);
    out.println(”NAMEID“);

    Object
    ObjectOutput out = new ObjectOutputStream(response.getOutputStream());
    out.writeObject(object);
    out.close();

    Binary
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(object);
    oos.flush();
    byte[] serializedBytes = baos.toByteArray();

Post a comment



  • Share/Bookmark
Creative Commons License
This blog is under Creative Commons licence, unless indicated otherwise.
Special thanks to Mark James for the icon set used in this blog.