Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet to Servlet communication using https

 
salil sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are using https in our application. The applet within our appliction is unable to open up. We are using tunnel client implementation by Karl Moss for applet to servlet communication. How can we resolve this issue? Please advice. The piece of code used for communication is:

public DataInput _invokeMethod(byte buf[])
throws TunnelException
{
DataInput in = null;

try
{
// Get the server URL
URL url = _getURL();
if (url == null)
{
throw new IOException("Server URL has not been set");
}

// Attempt to connect to the host
URLConnection con = url.openConnection();

// Initialize the connection
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);

// Set the content that we are sending
con.setRequestProperty("Content-type",
"application/octet-stream");

// Set the length of the data buffer we are sending
con.setRequestProperty("Content-length",
"" + buf.length);

// Set the session cookie if we have one
if (sessionCookie != null)
{
con.setRequestProperty("cookie", sessionCookie);
}

// Get the output stream to the server and send our
// data buffer
DataOutputStream out =
new DataOutputStream(con.getOutputStream());
out.write(buf);

// Flush the output stream and close it
out.flush();
out.close();

// Get the input stream we can use to read the response
in = _getInputStream(con.getInputStream());

// The server will always respond with an int value
// that will either be the method ordinal that was
// invoked, or a -2 indicating an exception was thrown
// from the server
int ordinal = in.readInt();

// Check for an exception on the server.
if (ordinal == -2)
{
// Read the exception message and throw it
String msg = in.readUTF();
throw new TunnelException(msg);
}

// Get the session cookie if we don't have one yet
if (sessionCookie == null)
{
String cookie = con.getHeaderField("set-cookie");
if (cookie != null)
{
sessionCookie = parseCookie(cookie);
}
}
}
catch (IOException ex)
{
// Re-throw as a tunnel exception
ex.printStackTrace();
throw new TunnelException(ex.getMessage());
}

// Return the input stream to be used to read the rest
// of the response from the server
return in;
}
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The applet within our appliction is unable to open up.


What does this mean? What does and what doesn't happen according to your expectations? Are there any error messages? At which line of the code do things stop working correctly?
(Don't assume that people are familiar with the communication implementation you mentioned.)
 
salil sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I am getting error at following line
in = _getInputStream(con.getInputStream());

getInputStream function is as follows.
public DataInput _getInputStream(InputStream in)
throws IOException
{
// Create a new DataInputStream for reading data from
// the connection.
return new ObjectInputStream(in);
}

This code works fine if we use HTTP but throws unexpected end of file from server exception when we use HTTPS (Exception is pasted at the end). I am signing my applet jar with verisign digital code it. I think problem we are having is that it is not able to convert back to object using ObjectInputStream. I don't get exception at line "in = _getInputStream(con.getInputStream())" if I change this to in = new DatainputStream(con.getInputStream())" but I get ClassCastException at a line where code is converting back to objectInputStream to read back the Serializable object from the server. I have also tried HttpsURLConnection class as follows but I get same error.

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
con = (javax.net.ssl.HttpsURLConnection) url.openConnection();


java.net.SocketException: Unexpected end of file from server

at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)

at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

at sun.net.www.protocol.https.PluginDelegateHttpsURLConnection.getInputStream(Unknown Source)

at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

at com.omh.tunnel.client.BaseTunnelClient._invokeMethod(BaseTunnelClient.java:230)

at gov.ca.aoc.vision.courtroom.applet.service.CourtroomServiceClientImpl.invokeRemote(CourtroomServiceClientImpl.java:308)

at gov.ca.aoc.vision.courtroom.applet.service.CourtroomServiceClientImpl.refreshDocketLookup(CourtroomServiceClientImpl.java:258)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.loadDocketLookup(CourtroomApplet.java:790)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.initAppContext(CourtroomApplet.java:780)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.init(CourtroomApplet.java:83)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

com.omh.tunnel.client.TunnelException: Unexpected end of file from server

at com.omh.tunnel.client.BaseTunnelClient._invokeMethod(BaseTunnelClient.java:260)

at gov.ca.aoc.vision.courtroom.applet.service.CourtroomServiceClientImpl.invokeRemote(CourtroomServiceClientImpl.java:308)

at gov.ca.aoc.vision.courtroom.applet.service.CourtroomServiceClientImpl.refreshDocketLookup(CourtroomServiceClientImpl.java:258)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.loadDocketLookup(CourtroomApplet.java:790)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.initAppContext(CourtroomApplet.java:780)

at gov.ca.aoc.vision.courtroom.applet.ui.CourtroomApplet.init(CourtroomApplet.java:83)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you use javax.net.ssl.HttpsURLConnection instead of java.net.URLConnection? It requires Java 1.4, but would seem to do some of the things that you are coding by hand for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic