package nlphub; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.gcube.data.analysis.nlphub.shared.Constants; import org.junit.Assert; import org.junit.Test; public class DiscoverDataMinerServiceTest { private static final Logger logger = LoggerFactory.getLogger(DiscoverDataMinerServiceTest.class); @Test public void discover() { if (Constants.TEST_ENABLE) { try { String dataMinerServiceUrl = null; String urlInformationSystem = "http://registry.d4science.org/icproxy/gcube/service/ServiceEndpoint/DataAnalysis/DataMiner?"; logger.debug("Request url: " + urlInformationSystem); urlInformationSystem += "gcube-token=" + "f57441ac-b361-4c2d-992a-40db034f1b8c-843339462"; URL url = new URL(urlInformationSystem); HttpURLConnection connection = null; connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.setDoOutput(false); connection.setUseCaches(false); try (BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { dataMinerServiceUrl = readProfileInfo(r); } logger.debug("DataMinerServiceUrl: " + dataMinerServiceUrl); Assert.assertTrue(true); } catch (Throwable e) { logger.error("Error retrieving DataMinerServiceUrl: "+e.getLocalizedMessage(),e); Assert.fail("Error retrieving DataMinerServiceUrl: "+e.getLocalizedMessage()); } } else { Assert.assertTrue(true); } } private String readProfileInfo(BufferedReader r) throws FactoryConfigurationError, XMLStreamException { String dataMinerServiceUrl = null; XMLInputFactory xmlInFact = XMLInputFactory.newInstance(); XMLStreamReader reader = xmlInFact.createXMLStreamReader(r); while (reader.hasNext()) { int eventType = reader.next(); if (eventType == XMLStreamConstants.START_ELEMENT) { logger.debug("Read Element: " + reader.getLocalName()); if (reader.getLocalName().compareToIgnoreCase("EndPoint") == 0) { logger.debug("Attribute count:" + reader.getAttributeCount()); for (int i = 0; i < reader.getAttributeCount(); i++) { logger.debug("Attribute Name: " + reader.getAttributeName(i)); logger.debug("Attribute Value: " + reader.getAttributeValue(i)); if (reader.getAttributeName(i).toString().compareToIgnoreCase("EntryName") == 0 && reader.getAttributeValue(i).toString() .compareToIgnoreCase("dataminer-prototypes.d4science.org") == 0) { dataMinerServiceUrl = reader.getElementText(); dataMinerServiceUrl = dataMinerServiceUrl.replace("\\n", "").replace("\\r", ""); logger.debug("DataMiner service url retrieved: " + dataMinerServiceUrl); return dataMinerServiceUrl; } } } } } return dataMinerServiceUrl; } }