Support Questions
Find answers, ask questions, and share your expertise

Nifi xml parsing when SSL is enabled.

Nifi xml parsing when SSL is enabled.

Explorer

I'm trying to write a java program that takes the XML file generated by a nifi-api url and generates an email if the queue size of the nifi flow exceeds a certain threshold. But the problem is that since SSL is enabled, the XML file is not being accessed and it gives me the below error:

java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:851) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:848) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at java.net.URL.openStream(URL.java:1045) at ReadXMLFile.main(ReadXMLFile.java:21)

When I manually try to access the link I had to select a certificate and I think that is the problem in accessing the XML file from code. Can someone tell me how to sign or select a certificate through java code?

My Program:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.InputStream;
import java.net.URL;

public class ReadXMLFile {
  public static void main(String argv[]) {
    try {
	URL url = new URL("https://172.22.2.6:9079/nifi-api/controller/process-groups/root/status?recursive=true");

	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

	InputStream stream = url.openStream();

	Document doc = dBuilder.parse(stream);

	doc.getDocumentElement().normalize();
	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
	NodeList nList = doc.getElementsByTagName("processGroupStatus");
	System.out.println("----------------------------");
	for (int temp = 0; temp < nList.getLength(); temp++) {
		System.out.println(nList.getLength());
		Node nNode = nList.item(temp);
		System.out.println("\nCurrent Element :" + nNode.getNodeName());
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			Element eElement = (Element) nNode;

			int lastElement = eElement.getElementsByTagName("queuedSize").getLength() -1;

			System.out.println("queuedSize : " + eElement.getElementsByTagName("queuedSize").item(lastElement).getTextContent());

			System.out.println("queuedCount : " + eElement.getElementsByTagName("queuedCount").item(lastElement).getTextContent());




		}

	}

    } catch (Exception e) {

    	System.out.println(e.getMessage());

	e.printStackTrace();

    }

  }




}
1 REPLY 1

Re: Nifi xml parsing when SSL is enabled.

If you are using client certificates as an authentication mechanism, you will need to provide a valid client certificate in the Java invocation of the API. You can do this by loading a Java Keystore (*.jks) file containing the private key of the client certificate and presenting that. An example of the code to do so (using Apache httpclient and JRE variables) is here. You can also do this explicitly by writing code to parse an arbitrary keystore and load a key via SSLContext and SSLSockets (see here).