This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Result form main() not the same as ....

 
Taco Fleur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java Class which runs fine when I call it with main(), but when I instantiate the class and call the same method from my code it gives me an error.

The problem is on line 162 the BufferedReader is giving me:
There was an error reading the Input Stream. The error is Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

Teh java code is:
/* Author: Taco Fleur (tacofleur@gmail.com) */
package edge;
import java.io.*;
import java.net.UnknownHostException;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.*;

public class CFSSLTransportLayer
{
final String host = "edge.asic.gov.au";
final int port = 5608;
private static SSLSocket sslServerSocket;
static boolean debug = true;
final String keyStoreLocation ="C:\\Documents and Settings\\Administrator\\.keystore";
final String password = "changeit";
final int socketTimeout = 60000; // Set timeout to one minute as per specs.
SSLSocketFactory factory;
PrintWriter pw;
BufferedReader in = null;
String error = "";



public static void main(String[] args) {
CFSSLTransportLayer tl = new CFSSLTransportLayer();
System.out.println( tl.send( "XSCLOGN\nS00042S00042 ", true ) );

//System.out.println( tl.send( "ZHDASCTXID0400\nZTX777Y20050711777571\nZTRENDTXID3\nZXCthis is the certificate\nZXIthis is the namett777key\nZXSeOOofcbWkqDikY9/Cq0H2lrRMZ+GY0urZE8bDhQ8vIqufhGx+XEKWLCAscYIT4CS\nZXSlWMH+ixj89G6beKfoV7r9HABw8i55icaQCWcQ36CYoejWpUKhp2ROjZdK0h3mywo\nZXSt6v6YrHqWKYOv5QLiUtpN1sxqBeZxGCTcmPjrF2rcW0=\n", true ) );

tl.closeConnection();
}


public CFSSLTransportLayer()
{
try
{
KeyStore myKeyStore = KeyStore.getInstance( "JKS" );
myKeyStore.load( new FileInputStream( keyStoreLocation ), password.toCharArray() );
TrustManagerFactory myTrustFactory = TrustManagerFactory.getInstance( "SunX509", "SunJSSE" );
myTrustFactory.init( myKeyStore );
TrustManager[] myTrustManager = myTrustFactory.getTrustManagers();
SSLContext ctx = SSLContext.getInstance( "SSL" );
ctx.init( null, myTrustManager, null );
factory = ctx.getSocketFactory();
}
catch ( Exception e )
{
String myError = "Error creating SSL objects.";
System.err.println( myError );
setError( myError );
e.printStackTrace( System.out );
}

try
{
sslServerSocket = ( SSLSocket ) factory.createSocket( host, port );
// Set the timeout
sslServerSocket.setSoTimeout( socketTimeout );
if ( debug )
{
System.out.println( "*************************************************" );
System.out.println( "***********Secure socket made********************" );
System.out.println( "*************************************************" );
System.out.println();
}
pw = new PrintWriter( sslServerSocket.getOutputStream() );
}
catch ( UnknownHostException e )
{
String myError = "Unknown host.";
System.err.println( myError );
setError( myError );
}
catch ( java.net.SocketTimeoutException e )
{
String myError = "The Socket waited " + socketTimeout / 1000 + " seconds for a response.";
System.err.println( myError );
setError( myError );
}
catch ( IOException e )
{
String myError = "I/O Exception.";
System.err.println( myError );
setError( myError );
}
catch ( Exception e )
{
String myError = "An exception has occurred, error message: " + e.getMessage();
System.err.println( myError );
setError( myError );
}
}


private void setError( String error )
{
this.error = this.error + error + "\n";
}

private String getError()
{
return this.error;
}


public String send ( String message, boolean isLast )
{
String response;
String messageToSend = "BDAT " + message.length();
if ( isLast )
{
messageToSend = messageToSend + " LAST";
}
messageToSend = messageToSend + "\r\n" + message;
// Sent the message to EDGE
transmit( messageToSend );
response = getResponse();
if ( debug )
{
System.out.println( "Message that was sent to EDGE " + messageToSend );
System.out.println( "Message sent to EDGE." );
response = "response = " + response + getError();
}
return response;
}


public void transmit ( String message )
{
// Sent the message to EDGE
pw.println( message );
pw.flush();
}


private String getResponse()
{
String messageString = "";
try
{
if ( debug )
{
System.out.println( "*************************************************" );
System.out.println( "************Server Output************************" );
System.out.print( "Outputting reply from server: " );
}

in = new BufferedReader( new InputStreamReader( sslServerSocket.getInputStream() ) );

String header = "";
char[] myChar = new char[ 1 ];
// Making sure that we do not enter an infinite loop
int i = 0;
final int MAX_LENGTH_TO_READ = 30;
// Loop until we find the newline character, this should give us the header
while ( !header.endsWith( "\n" ) && i < MAX_LENGTH_TO_READ )
{
in.read( myChar, 0, 1 );
String myStringchar = new String( myChar );
header += myStringchar;
i++;
}
if ( i == MAX_LENGTH_TO_READ )
{
String myError = "Could not read the header!";
System.err.println( myError );
setError( myError );
}

int byteToRead = 0;
// Parse out the message length
Pattern myPattern = Pattern.compile( "\\d+" );
Matcher myMatch = myPattern.matcher( header );
if ( myMatch.find() )
{
byteToRead = Integer.parseInt( header.substring( myMatch.start(), myMatch.end() ) );
}
else
{
String myError = "Message length could not be found in the header! the header = " + header;
System.err.println( myError );
setError( myError );
// take your action in case no integer found in yourString
throw new Exception("MessageLengthNotFound", new Throwable( "The message length could not be found in the header." ) );
}

char[] myMessage = new char[ byteToRead ];
// Try and read the rest of the message
try
{
int remaining = byteToRead;
// Loop untill all bytes are read
while ( remaining > 0 )
{
remaining -= in.read( myMessage, byteToRead - remaining, remaining );
}
}
catch ( Exception e )
{
String myError = "Error reading the rest of the message, byteToRead: " + byteToRead + ". message: " + e.getMessage() + "\n";
System.err.println( myError );
setError( myError );
}
// Convert the message array to a string
messageString = new String( myMessage );
System.out.println( messageString );

if ( debug )
{
System.out.println( "***********Server output complete****************" );
System.out.println( "*************************************************" );
System.out.println();
}
}
catch ( IOException e )
{
String myError = "There was an error reading the Input Stream. The error is " + e.getMessage();
System.err.println( myError );
setError( myError );
}
catch ( Exception e )
{
e.printStackTrace( System.out );
}
return messageString;
}


public void closeConnection()
{
try
{
// Transmit log out request - receives no response
transmit( "BDAT 8\r\nXSCLOUT\n" );
in.close();
pw.close();
// close socket
sslServerSocket.close();
}
catch ( IOException e )
{
System.out.println( "Error trying to close the socket" + e.getMessage() + "\n" );
}
}

protected void finalize()
throws Throwable
{
try
{
closeConnection();
}
finally
{
super.finalize();
}
}

}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work when you instantiate it from where? It's likely that the problem is a different environment -- i.e., servlet vs. application.
 
Taco Fleur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't want to make the post more difficult than it is.
I instantiate it from ColdFusion

tl = createObject( "java", "edge.CFSSLTransportLayer11" ).init();
response = tl.send( "XSCLOGN\nS00042S00042 ", true );

In ColdFusion .init() will call the constructor explicitly, I checked and the constructor is really called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic