deserialize and back java objects send via URLConnection

205 views
Skip to first unread message

iannos

unread,
Mar 16, 2011, 5:51:27 PM3/16/11
to Lift
Greetings

I have a situation where I need to deserialize from Lift arbitrary
java objects (the modelObject in the following example) send via a
URLConnection from a java applet. The old implementation used a
servlet (gamesservlet1) to accept the applet request and deserialize
the object.

Basically I want to be able to deserialize java objects send this way
and also serialize and send back with a LiftResponse java objects back
to the applet.

Calling code example from the applet:

String servletURL = webServerStr2;
URL gamesservlet1 = new URL( servletURL);
URLConnection servletConnection1 = gamesservlet1.openConnection();

servletConnection1.setDoInput(true);
servletConnection1.setDoOutput(true);
servletConnection1.setUseCaches (false);
servletConnection1.setRequestProperty ("Content-Type", "application/
octet-stream");
sendModelToServlet1(servletConnection1, modelObject);
inputFromServlet = new
ObjectInputStream(servletConnection1.getInputStream());
...


Thanks in advance

Jack Dimas
Athens, Hellas

David Pollak

unread,
Mar 17, 2011, 6:55:05 PM3/17/11
to lif...@googlegroups.com
You can send an Array[Byte] as a response using InMemoryResponse see http://exploring.liftweb.net/master/index-9.html

To respond to a specific URL, Lift's REST Helper will help you.  See http://stable.simply.liftweb.net/#toc-Chapter-5


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net

iannos

unread,
Mar 18, 2011, 8:26:28 AM3/18/11
to Lift
Thanks a lot David, I'll give it a try later today.

On Mar 18, 12:55 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> You can send an Array[Byte] as a response using InMemoryResponse seehttp://exploring.liftweb.net/master/index-9.html
>
> To respond to a specific URL, Lift's REST Helper will help you.  Seehttp://stable.simply.liftweb.net/#toc-Chapter-5
> Beginning Scalahttp://www.apress.com/book/view/1430219890

iannos

unread,
Mar 20, 2011, 1:41:51 PM3/20/11
to Lift
Guys ... I love you! David many thanks for pointing to the right
direction. Just for the record:

in boot.scala:
// stateful dispatch
LiftRules.dispatch.append {
// simulate legacy app servlets
LegacyServletInterceptor
}

in RestHelper object:
object LegacyServletInterceptor extends RestHelper
{
serve {
case "webprog" :: "PlayServletStoreVect" :: _ Post req => {
var inputFromApplet: ObjectInputStream = null
var wireObject: WireObject = null
req.body match {
case Full(bytes: Array[Byte]) => {
inputFromApplet = new ObjectInputStream(new
ByteArrayInputStream(bytes))
wireObject =
inputFromApplet.readObject().asInstanceOf[WireObject]
println("WireObject get something "+ wireObject.getMethod() )
inputFromApplet.close()
storeToDB(wireObject)
}
case _ => println("empty request!")
}

// now respond back to applet a dummy object
val baos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(baos)
oos.writeObject(DummyWireObject)
oos.close()
Full(InMemoryResponse(baos.toByteArray,
("Content-Type" -> "application/octet-stream") :: Nil,
Nil,
200))

}
}
...
...
@serializable object DummyWireObject

Please excuse my code style, I'm just converting from classic ASP to
Scala-Lift so it is quite a shock.

Jack Dimas
Athens, Greece
Reply all
Reply to author
Forward
0 new messages