Rewriting Java code to use JavaScript->Java API

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Rewriting Java code to use JavaScript->Java API

#1

Postby Segi » Wed Jun 08, 2016 6:19 pm

I am rewriting and expanding on a snippet of Java code that will check a given URL and verify if the domain is up and if the specific page on the domain is valid (I.E returns a 200 status code vs a 404).

The original Java code:

Code: Select all

try {
     HttpURLConnection.setFollowRedirects(false);
     // note : you may also need
     // HttpURLConnection.setInstanceFollowRedirects(false)
     HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
     con.setRequestMethod("HEAD");
     return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
 } catch (Exception e) {
      e.printStackTrace();
      return false;
}
This is what I have so far:

Code: Select all

function URLisValid(url) {
     try {
          var url=new java.net.URL(url);
          var socket=new java.net.Socket(url.getHost(),443);

          var httpCon=new java.net.HttpURLConnection(url.openConnection());
          httpCon.setRequestMethod("HEAD");
          alert("Resp code=" + httpCon.getResponseCode());
          return (httpCon.getResponseCode() == java.net.HttpURLConnection.HTTP_OK);
     } catch (e) {
     	  alert("Error 1 " + e);
          return false;
     } finally {
          try {
               socket.close();
          } catch(e) {
               alert("Error 2 " + e);
               return false;
          }
     } 
     
     return true;
}
The line var httpCon=new java.net.HttpURLConnection(url.openConnection()); is throwing the error:

Code: Select all


Error 1 JavaException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
This appears to be some kind of SSL certificate issue if I understand the error message but I have no idea how I can resolve it.
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#2

Postby dvanhussel » Wed Jun 08, 2016 6:29 pm

Hi,

The most common cause for this problem is that the website you are reading from, uses a SSL cert which uses a root CA cert that is not in your truststore.

You can use the Java command line tool 'keytool' to import the root CA that you need. For example in the default truststore of your JRE:

path/to/jre/lib/security/cacerts

Regards,

David
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#3

Postby Segi » Wed Jun 08, 2016 6:30 pm

David,

What would be the syntax for that ? I'm not familiar with how to do that for a public root CA certificate
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#4

Postby dvanhussel » Wed Jun 08, 2016 6:46 pm

First, you need the root ca in a plain text .crt file (open a https url on this site in FireFox, open certificate details, select the root cert and click on 'export')

Then, it should be something like:

Code: Select all

keytool -import -trustcacerts -keystore /opt/java/jre/lib/security/cacerts -storepass changeit  -alias mycert -file /tmp/examplecert.crt
'changeit' is the default password for the cacerts truststore
keytool should be in the 'bin' folder of your JRE.

After a successfull import, a restart of Tomcat is needed.

For more info see:

https://www.sslshopper.com/article-most ... mands.html

Regards,

David
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#5

Postby Segi » Wed Jun 08, 2016 8:29 pm

David,

Based on what you are telling me, you can only validate if a URL is valid if its a URL that you own. This isn't exactly what I want. I would like to be able to use my function with other sites. Is there any other way to validate a site that uses SSL ? All I care about is if the URL is valid. I don't care about the contents whatsoever.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#6

Postby Jon » Thu Jun 09, 2016 8:18 am

Segi,

As an alternative, there is an FPL function that will do this for you, but it has to be installed first using Server Admin - see http://www.ebasetech.com/ebase/doc/cust ... c252439253. Then you can call it from Javascript with something like:

var url = "http://www.google.com";
var resp = system.executeCustomFunction("testurlconnection", [url]);
if (resp == "Y")
..

You should only need to add a cert to your keystore if it's unsigned.

Regards
Jon
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#7

Postby Segi » Thu Jun 09, 2016 4:37 pm

Jon,

Thanks,

That function does not work with SSL based URLs that use https. I have found that URLS that redirect http to https still work.

I made a change to our server after this problem had already started but I'm not sure if it matters or not. I didn't want to use myserver.com/ebase/FORMNAME so I set the default app to ROOT so I can use the URL myserver.com/MYFORM.eb without referencing any web app.

When I enter the test server parameters now, it reads localhost,80,ROOT under external server.

I can manually connect to the server admin app by entering the URL http://myserver/ebaseAdmin.eb
Last edited by Segi on Fri Jun 10, 2016 3:36 pm, edited 1 time in total.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#8

Postby Jon » Fri Jun 10, 2016 3:32 pm

This works for me with an https URL e.g. https://www.google.com. Are you sure that the URL you are checking is using a signed certificate?
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#9

Postby Segi » Fri Jun 10, 2016 3:37 pm

Jon,

When the URL is "http://www.google.com" resp='Y'
When the URL is "https://www.google.com" resp="N"
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#10

Postby Jon » Fri Jun 10, 2016 4:14 pm

That's a bit weird because both return a 'Y' for me. I suspect it's throwing some sort of exception that is being swallowed - but I'm guessing. I have no idea why your system is any different from mine.
0 x


Who is online

Users browsing this forum: No registered users and 14 guests