Search code examples
javahttpjakarta-eehttpsftp4j

javax.net.ssl.SSLException on HttpsURLConnection request


I am trying to call an https url inside my java application just after renaming a file in a ftp server directory.

The file is renamed and the https url request is succesfull however i am receiving a weird exception related to a certificate imported in my cacerts file.

The code that renames the file in ftp directory and then makes the https url request is the following:

...
FTPClient client = null;
try {
    client = Ftp4jUtility.ftpsConnect1(SERVER_MACHINE, PORT, SERVER_USERNAME, SERVER_PASSWORD);          
    client.rename(oldDir, newDir);
    renameStatus=true;
    String url = config.getExtractZipUrl()+stdDet.getStudyDetailsCaseId(tmpNewFile.trim());
    try {
            JavaHttpsUrlConnectionReader(url);
        } catch (Exception ex) {
            Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
        } 

        client.logout();
        client.disconnect(true);

    } catch (IllegalStateException ex) {
        log.info("Failed to rename: " + oldDir);
        Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);

    } catch (IOException | FTPIllegalReplyException | FTPException ex) {
        log.info("Failed to rename: " + oldDir);
        Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);

    } finally {
        if (client.isConnected()) {
            try {
                try {
                    client.logout();
                    client.disconnect(true);
                } catch (IllegalStateException | FTPIllegalReplyException | FTPException ex) {
                    Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
                }                    
            } catch (IOException ex) {
                Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
            }
        }

    }
return renameStatus;

In my log i reveice:

2015-03-05 13:09:04 ERROR TranferFileFtp4j:398 - 
javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)    at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:565)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:911)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.disconnect(FTPClient.java:1133)
at com.npap.network.TranferFileFtp4j.renameFileFtps1(TranferFileFtp4j.java:390)
at com.npap.utils.ProcessDicomFiles.sendZippFiles(ProcessDicomFiles.java:195)
at com.npap.scheduler.MainJob.execute(MainJob.java:84)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
2015-03-05 13:09:04 ERROR TranferFileFtp4j:415 - 
java.lang.IllegalStateException: Client not authenticated
at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)
at com.npap.network.TranferFileFtp4j.renameFileFtps1(TranferFileFtp4j.java:412)
at com.npap.utils.ProcessDicomFiles.sendZippFiles(ProcessDicomFiles.java:195)
at com.npap.scheduler.MainJob.execute(MainJob.java:84)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

and here is also the code (pretty simple) which is making the https url request:

public void JavaHttpsUrlConnectionReader(String myUrl) throws Exception {
    URL url = null;
    BufferedReader reader = null;
    StringBuilder stringBuilder;
    try {
        url = new URL(myUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        // just want to do an HTTP GET here
        connection.setRequestMethod("GET");
        // give it 15 seconds to respond
        connection.setReadTimeout(15*1000);
        connection.connect();
        // read the output from the server
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
          stringBuilder.append(line + "\n");
        }
        //return stringBuilder.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
        // close the reader; this can throw an exception too, so
        // wrap it in another try/catch block.
        if (reader != null)
        {
          try
          {
            reader.close();
          }
          catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
        }
    }

}

Why am i receiving:

javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)    at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)

and if you check further down:

java.lang.IllegalStateException: Client not authenticated
at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)

actually the line 390 in my code reffered to the log is where i am trying to:

client.disconnect(true);

So i am receiving the exception while i am trying to disconnect from the ftp session. Anybody has an idea why am i receiving this exception?


Solution

  • Finally it seems that

    client.logout();
    

    before:

    client.disconnet(true);
    

    was causing the exception:

    java.lang.IllegalStateException: Client not authenticated
    at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)
    

    the:

    javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
    at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)        at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)
    

    was due to an https request (HttpsURLConnection) with insufficient readTimeOut:

    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setReadTimeout(15*1000);
    connection.connect();
    

    15 second was not enough. Increased to 60 and it was solved...