A couple of months ago I wrote a Java Agent that synchs documents in Notes databases with tickets in a Redmine system. The agent opens a Notes document through an HttpURLConnection and it worked perfectly until two weeks ago (no changes to the code or server were made). Now a NullPointerException is thrown when conn.getResponseCode() is called and I can't figure out why, since the URL used for the connection works fine when I copy and paste it into my browser (using the same credentials as the agent does). As you can see in the code block below I also tried printing out the header fields of the connection object, but it returns an empty map, {}. The stack trace I get looks like the one pasted in this question and I tried all the solutions suggested there, but it didn't solve the problem. Interestingly, when I try to save the document to Redmine, the method creating a new issue there (using the Redmine Java API) also fails because it cannot establish an HTTP connection. Again: this used to work flawlessly before. I have been trying to fix this for two days now with no success. The agent is allowed to run restricted operations with full administration rights (3) and the databases it accesses are on the same server as the DB containig the agent, and the default access in the ACL is "author". Any ideas what could be causing this issue? Thanks in advance!
String issueBody = "";
Session session = getSession();
Name serverName = session.createName(db.getServer());
URL url = null;
java.net.HttpURLConnection conn = null;
InputStream is;
BufferedReader rd;
String line;
String urlString = "http://"+serverName.getCommon()+":80/"+db.getFilePath()+"/"+VIEW_SPRS_BY_SPRNO+"/" + issue.getUniversalID() + "/Body?OpenField";
urlString = urlString.replace("\\", "/");
String userpass = httpUser + ":" + httpPass;
BASE64Encoder encoder = new BASE64Encoder();
String encodedStr = encoder.encode(userpass.getBytes());
String basicAuth = "Basic " + encodedStr;
try {
url = new URL(urlString);
conn = (java.net.HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setAllowUserInteraction(false);
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Host", url.getHost());
conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.connect();
System.out.println("Headers: " + conn.getHeaderFields());
int responseCode = conn.getResponseCode();
String responseMessage = conn.getResponseMessage();
System.out.println("response code: " + responseCode + ", response message: " + responseMessage);
if (responseCode >= 400) {
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
String errorString = "";
while ((line = rd.readLine()) != null) {
errorString += line;
}
rd.close();
System.out.println("error stream: " + errorString);
} else {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
issueBody += line;
}
rd.close();
}
} catch (UnknownHostException e) {
OpenLogItem.logError(session, e, "java.net.UnknownHostException: Can't find specified host: "+urlString, Level.SEVERE, null);
issueBody = issue.getItemValueString("Body");
} catch (MalformedURLException e) {
OpenLogItem.logError(session, e, "java.net.MalformedURLException: Can't connect to invalid URL: "+urlString, Level.SEVERE, null);
issueBody = issue.getItemValueString("Body");
} catch (IOException e) {
OpenLogItem.logError(session, e, "java.io.IOException: Failed to read stream at: "+urlString, Level.SEVERE, null);
issueBody = issue.getItemValueString("Body");
} catch (NullPointerException e) {
OpenLogItem.logError(session, e, "java.lang.NullPointerException. Failed to establish HTTP connection. Header fields: "+conn.getHeaderFields()+" URL: "+urlString, Level.SEVERE, null);
issueBody = issue.getItemValueString("Body");
} catch (Exception e) {
OpenLogItem.logError(session, e, "Unknown error. Failed to establish HTTP connection: "+urlString, Level.SEVERE, null);
issueBody = issue.getItemValueString("Body");
} finally {
conn.disconnect();
}
Here is the stack trace:
java.lang.NullPointerException
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:719)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:646)
at COM.ibm.JEmpower.applet.http.HttpURLConnection.getInputStream(HttpURLConnection.java:411)
at COM.ibm.JEmpower.applet.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:692)
at java.net.URLConnection.getContentType(URLConnection.java:509)
at JavaAgent.saveIssueToRedmine(Unknown Source)
at JavaAgent.pushIssueFromNotesToRedmine(Unknown Source)
at JavaAgent.NotesMain(Unknown Source)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(Unknown Source)