views:

432

answers:

3

I have to connect to a https URL with username and password to read a file. I am not able to connect to the server (see the error log below). I do not have much Java experience, so I need help with this code.

import lotus.domino.*;
import java.net.*;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class JavaAgent extends AgentBase {

public void NotesMain() {

  try {
    String username = "123";
    String password = "456";
    String input = username + ":" + password;
    String encoding = new sun.misc.BASE64Encoder().encode (input.getBytes());

    //Open the URL and read the text into a Buffer
    String urlName = "https://server.org/Export.mvc/GetMeetings?modifiedSince=4/9/2010";
    URL url = new URL(urlName);
    HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", String.valueOf (encoding.length())); 
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setAllowUserInteraction(true);
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    connection.setRequestProperty("Cookie", "LocationCode=Geneva");

    connection.connect();

    BufferedReader rd = null;
      try{
        rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      } catch (IOException e) {
        System.out.println("Read failed");
        System.exit(-1);
      }

    String line;
    while((line = rd.readLine()) != null) {
      System.out.println(line.toString());
    }
    rd.close();

    connection.disconnect();

  } catch(Exception e) {
    e.printStackTrace();
  }
}
}

Exception thrown:

java.security.AccessControlException: Access denied (java.lang.RuntimePermission exitVM.-1)
 at java.security.AccessController.checkPermission(AccessController.java:108)
 at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
 at COM.ibm.JEmpower.applet.AppletSecurity.superDotCheckPermission(AppletSecurity.java:1449)
 at COM.ibm.JEmpower.applet.AppletSecurity.checkRuntimePermission(AppletSecurity.java:1311)
 at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1611)
 at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1464)
 at java.lang.SecurityManager.checkExit(SecurityManager.java:744)
 at java.lang.Runtime.exit(Runtime.java:99)
 at java.lang.System.exit(System.java:275)
 at JavaAgent.NotesMain(Unknown Source)
 at lotus.domino.AgentBase.runNotes(Unknown Source)
 at lotus.domino.NotesThread.run(Unknown Source)
A: 

An applet may access only the server it is loaded from. Most probably your applet is not loaded from the server you are trying to connect to?

EDIT: Your stacktrace suggests, that there is a specialised security manager is installed (COM.ibm.JEmpower.applet.AppletSecurity). Googling this class reveals the problem: http://lekkimworld.com/2006/02/28/imported_java_agent.html

Arne
Domino uses this security manager to enforce its own granular security model with Java agents. It sounds, as Sio intimates below, that the agent isn't running with appropriate permissions. For example, Java code which accesses a remote resource needs "restricted" rights in the Domino world.
Ben Poole
+1  A: 

I take it this is a Java agent? Things to check.

  1. In the agent properties that the security level is set for what you want to do. Normally file access requires at least level 2.

  2. The signature of the agent or the user the agent is set to run as is allowed to run on the server.

  3. You can modify the java.policy file to allow access to certain restricted classes. (but you need to know why you are making the change).

http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html

Sio
I have set the security level in the agent to 3 and now I get this exeption:Caused by: java.security.KeyStoreException: IBMKeyManager: Problem accessing key store java.security.cert.CertificateParsingException: java.io.IOException: subject key, java.security.spec.InvalidKeySpecException: Unknown key spec.at com.ibm.jsse2.ic.a(ic.java:16) at com.ibm.jsse2.kc.g(kc.java:4) at com.ibm.jsse2.kc.<init>(kc.java:19) at java.lang.J9VMInternals.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1325) at java.security.Provider$Service.newInstance(Provider.java:880)
RMD
That looks like a new issue. Maybe key store configuration? Not an area I've played with. :/
Sio
A: 

This support thread from IBM sounds similar to your problem if you're running on an 8.5 Domino server.

giulio