Scheduled XAgent -SSL Encrypted

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import javax.net.ssl.SSLSocketFactory;

import lotus.domino.AgentBase;

public class JavaAgent extends AgentBase {

	// // Change these settings below to your setup as required.
	static final String	hostName	= "DNS_HOST_NAME_OF_YOUR_SERVER";
	static final String	urlFilepath	= "FILEPATH_ON_HOST_TO_YOUR_XPAGE_AND_OPTIONAL_QUERYSTRING";
	static final int	sslPort	    = 443;


	public void NotesMain() {

		try {
			final SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			final Socket socket = factory.createSocket(JavaAgent.hostName, JavaAgent.sslPort);

			final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			final StringBuilder sb = new StringBuilder();
			sb.append("GET ");
			sb.append(JavaAgent.urlFilepath);
			sb.append(" HTTP/1.1\n");
			final String command = sb.toString();

			sb.setLength(0);
			sb.append("Host: ");
			sb.append(JavaAgent.hostName);
			sb.append("\n\n");
			final String hostinfo = sb.toString();

			out.write(command);
			out.write(hostinfo);
			out.flush();

			in.close();
			out.close();
			socket.close();

		} catch (final Exception e) {
			// YOUR_EXCEPTION_HANDLING_CODE
		}
	}
}





Java Agent Code for hitting an XPage via SSL encrypted connection.
Hitting the XPage from a scheduled agent allows us to "trigger" the XAgent code on a scheduled basis.  
Note that for Anonymous ACL to work on the database containing the XAgent XPage, it must be set to at minimum of READER.
Also note that the scheduled agent code must be at minimum of security level 2 (Allow restricted operations), or it will not be able to connect to a URL.

Java
Devin Olson
June 25, 2015 10:39 AM
Rating
24

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



1 comment(s)Login first to comment...
Andrew Grabovetskyi
(at 12:21 on 01.08.2015)
Hi! Thank you for the solution! ...unfortunately it is not working for me. I am getting exception: "javax.net.ssl.SSLKeyException: RSA premaster secret error":

08/01/2015 10:53:26 AM Agent Manager: Agent error: javax.net.ssl.SSLKeyException: RSA premaster secret error
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.jb.<init>(jb.java:76)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.lb.a(lb.java:256)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.lb.a(lb.java:214)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.kb.s(kb.java:413)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.kb.a(kb.java:452)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:109)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.SSLSocketImpl.h(SSLSocketImpl.java:723)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:108)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.k.write(k.java:35)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:355)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java:425)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:429)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:175)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:274)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at java.io.BufferedWriter.flush(BufferedWriter.java:247)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at JavaAgent.NotesMain(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at lotus.domino.AgentBase.runNotes(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at lotus.domino.NotesThread.run(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: Caused by:
08/01/2015 10:53:26 AM Agent Manager: Agent error: java.security.InvalidKeyException: Illegal key size or default parameters
08/01/2015 10:53:26 AM Agent Manager: Agent error: at javax.crypto.Cipher.a(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at javax.crypto.Cipher.a(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at javax.crypto.Cipher.a(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at javax.crypto.Cipher.init(Unknown Source)
08/01/2015 10:53:26 AM Agent Manager: Agent error: at com.ibm.jsse2.jb.<init>(jb.java:6)
08/01/2015 10:53:26 AM Agent Manager: Agent error: ... 17 more

Server version: "IBM Domino (r) Server (Release 9.0.1FP3HF237 for UNIX)".
Any ideas how to solve it? Looks like it is due to latest updates for SSL...

Thanks in advance!