Java Objects serialization corruption

Hi,

I'm trying to serialize a Java Object from a Java client application toward a JIRA plugin (servlet) and in the reverse direction too.

Whereas the serialization works from the client to the server, it looks like corrupted when serialized from the plugin toward the client application:

 

invalid stream header: 3F3F0005

 

This is the code at the servlet:

OutputStream os = httpServletResponse.getOutputStream();
                oos = new ObjectOutputStream(os);
                oos.writeObject("World!");
                oos.flush();
                httpServletResponse.flushBuffer();

 

And this is the code at the client:

 

// ... This works ...
            oos = new ObjectOutputStream(conn.getOutputStream());
            oos.writeObject("Hello");
            oos.flush();
            // Connect ...
            conn.connect();

            // This does NOT work
            ois = new ObjectInputStream(conn.getInputStream());
            Object result = ois.readObject();

 

The "Hello" object sent from the client is properly read from the plugin, whereas the "World" object sent by the plugin is not read by the client app.

 

Note: conn is an HttpClient (3.x) connection object. The example code works fine when used in an standard web app on Tomcat rather on a JIRA plugin.

 

The error occurs just in the line below:

ois = new ObjectInputStream(conn.getInputStream());

 

which verifies some special headers at the beginning. As I saw some Atlassian's Java classes wrapping the servlet output, I wonder whether that code might impact on this.

 

Any suggestion?

 

Thanks!

Pablo.

1 answer

0 vote
Daniel Wester Community Champion Apr 04, 2015

More than likely it's the osgi container that's causing your headaches. Whenever you need to do stuff with class loading in p2 - you're asking for a lot of pain (personal experience here wink ). Don't know if this helps or not:

http://stackoverflow.com/questions/13861342/how-do-you-deserialize-an-object-from-bytes-in-osgi

I would probably suggest stepping back and see if you can't get away from the serialization/deserialization and maybe using json or other language neutral transport mechanism. It will be more code to write, but I suspect you'll not be banging your head against a wall as much.

@Danie, thanks for the answer. Unfortunately, due the nature of the project, any alternative serialization is not an option :(. I would say the OSGi environment is not a problem (yet) because the serialized objects were Java's basic types (java.lang.String) which are available in any JVM and therefore no problem with packages visibility. I found this post: http://stackoverflow.com/questions/21635198/objectinputstream-invalid-stream-header-00000000 pointing to an outputstream related issue (just the same raised error: "invalid stream header"). While debugging, I saw some Atlassian's wrapper classes for the plugin servlet outputstream channel furthermore some sanitize tasks removing '/r' and '/n' characters. Perhaps (this is my guess) the Atlassian's Plugin Framework makes some http header manipulation, i.e: by adding some custom or "inoffensive" header in any servlet response. This would cause the "invalid stream header" error as the ObjectInputStream would read the Atlassian's injected header rather than the expected one.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

172 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you