From beb4c665ce42b1cfe06cd2c620f0d3aa0c2d2386 Mon Sep 17 00:00:00 2001 From: qadan Date: Fri, 7 Apr 2017 13:13:56 -0300 Subject: [PATCH 01/12] json to xml --- pom.xml | 7 +++- .../gsearch_extensions/JSONToXML.java | 36 ++++++++++++++++ .../gsearch_extensions/JSONToXMLTest.java | 41 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java create mode 100644 src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java diff --git a/pom.xml b/pom.xml index e13df1c..27cb43e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ ca.discoverygarden gsearch_extensions - 0.1.1 + 0.1.2 jar @@ -35,6 +35,11 @@ commons-lang3 3.3.2 + + org.json + json + 20160810 + diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java new file mode 100644 index 0000000..d71e05a --- /dev/null +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java @@ -0,0 +1,36 @@ +package ca.discoverygarden.gsearch_extensions; + +import org.json.JSONObject; +import org.json.XML; + +public class JSONToXML { + + /** + * Converts a JSON string to an XML string enclosed in a 'json' tag. + * + * @param in + * The JSON to convert. + * + * @return + * XML string. + */ + public static String convertJSONToXML(String in) { + return convertJSONToXML(in, "json"); + } + + /** + * Convert a JSON string to an XML string. + * + * @param in + * The JSON to convert. + * @param enclosing_tag + * The tag to enclose the results in. + * + * @return + * XML string. + */ + public static String convertJSONToXML(String in, String enclosing_tag) { + JSONObject json = new JSONObject(in); + return XML.toString(json, enclosing_tag); + } +} diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java new file mode 100644 index 0000000..b992035 --- /dev/null +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java @@ -0,0 +1,41 @@ +package ca.discoverygarden.gsearch_extensions; + +import junit.framework.TestCase; + +/** + * Tests that we can get XML from JSON. + */ +public class JSONToXMLTest extends TestCase { + + // Our test JSON we use twice. + protected static final String testJson = "{\"thing_1\":{\"subthing_1.1\":\"value_1.1\",\"subthing_1.2\":\"value_1.2\"},\"thing_2\":\"value_2\"}"; + + /** + * Set up the thingy. + */ + protected void setUp() throws Exception { + super.setUp(); + } + + /** + * Tests conversion with the default root element. + */ + public void testDefaultRootElement() { + // Should be XML enclosed in a 'json' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson); + + assertEquals(dest, transformed); + } + + /** + * Tests conversion with a custom root element. + */ + public void testCustomRootElement() { + // Should be XML enclosed in a 'custom' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson, "custom"); + + assertEquals(dest, transformed); + } +} From b165ab5f962e39556e2428722a3593797f6299af Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 10 Apr 2017 17:13:16 -0300 Subject: [PATCH 02/12] json-to-xml --- pom.xml | 15 ++++++++++ .../gsearch_extensions/JSONToXML.java | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/pom.xml b/pom.xml index 27cb43e..c157363 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,21 @@ json 20160810 + + javax.xml.parsers + jaxp-api + 1.4.5 + + + org.w3c + dom + 2.3.0-jaxb-1.0.6 + + + org.fcrepo + fcrepo-server + 3.8.1 + diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java index d71e05a..9f5908d 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java @@ -3,8 +3,18 @@ import org.json.JSONObject; import org.json.XML; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.w3c.dom.Document; +import java.io.StringReader; +import java.io.IOException; + public class JSONToXML { + /** * Converts a JSON string to an XML string enclosed in a 'json' tag. * @@ -33,4 +43,23 @@ public static String convertJSONToXML(String in, String enclosing_tag) { JSONObject json = new JSONObject(in); return XML.toString(json, enclosing_tag); } + + /** + * Convert a JSON string to an XML document. + * + * @param in + * JSON to convert. + * + * @return + * XML Node. + */ + public static Document convertJSONToDocument(String in) + throws ParserConfigurationException, SAXException, IOException + { + StringReader xmlStringReader = new StringReader(convertJSONToXML(in)); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + return builder.parse(new InputSource(xmlStringReader)); + } } From c8f56d5e8f37138691999f9823269946a4a63caa Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 10 Apr 2017 17:18:55 -0300 Subject: [PATCH 03/12] file pls --- .../gsearch_extensions/FedoraUtils.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java new file mode 100644 index 0000000..b33f46c --- /dev/null +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -0,0 +1,38 @@ +package ca.discoverygarden.gsearch_extensions; + +import org.apache.log4j.Logger; + +import dk.defxws.fedoragsearch.server.GenericOperationsImpl; + +import org.fcrepo.client.FedoraClient; +import org.fcrepo.server.access.FedoraAPIA; +import org.fcrepo.server.types.gen.Datastream; +import org.fcrepo.server.types.gen.MIMETypedStream; + +/** + * Utilities for interfacing with Fedora in ways that GSearch can't. + */ +public class FedoraUtils { + + protected static final Logger logger = Logger.getLogger(FedoraUtils.class); + + /** + * Gets the raw text of a datastream. + */ + public static String getRawDatastreamText(String pid, String repositoryName, String dsId, String fedoraSoap, String fedoraUser, String fedoraPass, String trustStorePath, String trustStorePass) { + dsBuffer = new StringBuffer(); + FedoraAPIA apia = GenericOperationsImpl.getAPIA(repositoryName, fedoraSoap, fedoraUser, fedoraPass, trustStorePath, trustStorePass); + // Get the DS; getDatastreamDissemination could be null. + MIMETypedStream dsStream = apia.getDatastreamDissemination(getRealPID(pid), dsId, null); + if (dsStream == null) { + logger.warn(String.format("Failed to get datastream dissemination of '%s' for '%s'", dsId, pid)); + return ""; + } + // Get the stream. + ds = dsStream.getStream(); + if (ds == null) { + logger.warn(String.format("Failed to get stream from '%s' for '%s'", dsId, pid)); + return ""; + } + } +} From 8e7ea3ded1006fc79a6290c83e2d586d2e66b711 Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 10 Apr 2017 19:24:10 -0300 Subject: [PATCH 04/12] getting text of stuff --- pom.xml | 5 + .../gsearch_extensions/FedoraUtils.java | 98 +++++++++++++++---- .../gsearch_extensions/JSONToXML.java | 2 +- .../gsearch_extensions/FedoraUtilsTest.java | 33 +++++++ .../gsearch_extensions/JSONToXMLTest.java | 18 ++++ 5 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java diff --git a/pom.xml b/pom.xml index c157363..0396376 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,11 @@ fcrepo-server 3.8.1 + + org.fcrepo + fcrepo-client-admin + 3.8.1 + diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index b33f46c..85df7ff 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -1,13 +1,14 @@ package ca.discoverygarden.gsearch_extensions; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; -import dk.defxws.fedoragsearch.server.GenericOperationsImpl; - -import org.fcrepo.client.FedoraClient; -import org.fcrepo.server.access.FedoraAPIA; -import org.fcrepo.server.types.gen.Datastream; -import org.fcrepo.server.types.gen.MIMETypedStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; /** * Utilities for interfacing with Fedora in ways that GSearch can't. @@ -16,23 +17,80 @@ public class FedoraUtils { protected static final Logger logger = Logger.getLogger(FedoraUtils.class); + /** + * Gets an InputStream for a datastream. + * + * @param pid + * The PID of the object that has the datastream. + * @param dsId + * The ID of the datastream to get. + * @param fedoraBase + * The base URL of fedora, e.g., localhost:8080/fedora. + * @param fedoraUser + * The Fedora username to connect with. + * @param fedoraPass + * The Fedora password to connect with. + * + * @return InputStream + * An InputStream at the constructed dissemination point. + */ + public static InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + URL url; + // Attempt to generate the URL from input. + try { + url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + return url.openStream(); + } + catch (MalformedURLException e) { + logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); + } + catch (IOException e) { + logger.warn(String.format("Failed to open stream: %s", e.getMessage())); + } + return new ByteArrayInputStream("".getBytes()); + } + /** * Gets the raw text of a datastream. + * + * @param pid + * The PID of the object that has the datastream. + * @param dsId + * The ID of the datastream to get. + * @param fedoraBase + * The base URL of fedora, e.g., localhost:8080/fedora. + * @param fedoraUser + * The Fedora username to connect with. + * @param fedoraPass + * The Fedora password to connect with. + * + * @return String + * The text of the given datastream. */ - public static String getRawDatastreamText(String pid, String repositoryName, String dsId, String fedoraSoap, String fedoraUser, String fedoraPass, String trustStorePath, String trustStorePass) { - dsBuffer = new StringBuffer(); - FedoraAPIA apia = GenericOperationsImpl.getAPIA(repositoryName, fedoraSoap, fedoraUser, fedoraPass, trustStorePath, trustStorePass); - // Get the DS; getDatastreamDissemination could be null. - MIMETypedStream dsStream = apia.getDatastreamDissemination(getRealPID(pid), dsId, null); - if (dsStream == null) { - logger.warn(String.format("Failed to get datastream dissemination of '%s' for '%s'", dsId, pid)); + public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + try { + URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + URLConnection connection = url.openConnection(); + InputStream dsStream = connection.getInputStream(); + String encoding = connection.getContentEncoding(); + encoding = encoding == null ? "UTF=8" : encoding; + return IOUtils.toString(dsStream, encoding); + } catch (MalformedURLException e) { + logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); + } catch (IOException e) { + logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); + } return ""; - } - // Get the stream. - ds = dsStream.getStream(); - if (ds == null) { - logger.warn(String.format("Failed to get stream from '%s' for '%s'", dsId, pid)); - return ""; - } + } + + /** + * Turns some parameters into a datastream dissemination URL. + * + * @return URL + * A URL object using the given parameters. + */ + protected static URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws MalformedURLException { + String formattedUrl = String.format("http://%s:%s@%s/objects/%s/datastreams/%s/content", fedoraUser, fedoraPass, fedoraBase, pid, dsId); + return new URL(formattedUrl); } } diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java index 9f5908d..cb8f75a 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java @@ -51,7 +51,7 @@ public static String convertJSONToXML(String in, String enclosing_tag) { * JSON to convert. * * @return - * XML Node. + * XML Document. */ public static Document convertJSONToDocument(String in) throws ParserConfigurationException, SAXException, IOException diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java new file mode 100644 index 0000000..625fd00 --- /dev/null +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java @@ -0,0 +1,33 @@ +package ca.discoverygarden.gsearch_extensions; + +import junit.framework.TestCase; + +import java.net.MalformedURLException; +import java.net.URL; + +public class FedoraUtilsTest extends TestCase { + + /** + * Set that thing up. + */ + protected void setUp() throws Exception { + super.setUp(); + } + + /** + * Tests building a URL. + */ + public void testURLBuild() throws MalformedURLException { + String pid = "test:pid"; + String dsId = "TEST_DSID"; + String fedoraBase = "localhost:8080/fedora"; + String fedoraUser = "test_user"; + String fedoraPass = "test_pass"; + URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + + assertEquals(url.getPort(), 8080); + assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); + assertEquals(url.getUserInfo(), "test_user:test_pass"); + } + +} diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java index b992035..b0dd71c 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java @@ -2,6 +2,14 @@ import junit.framework.TestCase; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + /** * Tests that we can get XML from JSON. */ @@ -38,4 +46,14 @@ public void testCustomRootElement() { assertEquals(dest, transformed); } + + /** + * Tests conversion to a Document object. + */ + public void testDocumentConversion() throws ParserConfigurationException, SAXException, IOException { + Document transformed = JSONToXML.convertJSONToDocument(testJson); + NodeList thingList = transformed.getElementsByTagName("thing_1"); + + assertEquals(thingList.getLength(), 1); + } } From 4139f1f636646ef4375234c037920c1eca064840 Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 10 Apr 2017 23:14:43 -0300 Subject: [PATCH 05/12] maybe --- pom.xml | 10 --- .../gsearch_extensions/FedoraUtils.java | 61 ++++++++++++++----- .../gsearch_extensions/FedoraUtilsTest.java | 3 +- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 0396376..9e4e64e 100644 --- a/pom.xml +++ b/pom.xml @@ -50,16 +50,6 @@ dom 2.3.0-jaxb-1.0.6 - - org.fcrepo - fcrepo-server - 3.8.1 - - - org.fcrepo - fcrepo-client-admin - 3.8.1 - diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index 85df7ff..dc2d583 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -1,6 +1,5 @@ package ca.discoverygarden.gsearch_extensions; -import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import java.io.IOException; @@ -8,7 +7,31 @@ import java.io.ByteArrayInputStream; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.util.Scanner; + +/** + * Authenticator for connecting to Fedora. + */ +class FedoraAuthenticator extends Authenticator { + + protected String fedoraUser; + protected String fedoraPass; + + public void setCredentials(String username, String password) { + fedoraUser = username; + fedoraPass = password; + } + + /** + * Overloaded password authenticator. + */ + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(fedoraUser, fedoraPass.toCharArray()); + } + +} /** * Utilities for interfacing with Fedora in ways that GSearch can't. @@ -25,7 +48,8 @@ public class FedoraUtils { * @param dsId * The ID of the datastream to get. * @param fedoraBase - * The base URL of fedora, e.g., localhost:8080/fedora. + * The base URL of fedora, including protocol; e.g., + * http://localhost:8080/fedora. * @param fedoraUser * The Fedora username to connect with. * @param fedoraPass @@ -34,7 +58,7 @@ public class FedoraUtils { * @return InputStream * An InputStream at the constructed dissemination point. */ - public static InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + public InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { URL url; // Attempt to generate the URL from input. try { @@ -58,7 +82,8 @@ public static InputStream getDatastreamDisseminationInputStream(String pid, Stri * @param dsId * The ID of the datastream to get. * @param fedoraBase - * The base URL of fedora, e.g., localhost:8080/fedora. + * The base URL of fedora, including protocol; e.g., + * http://localhost:8080/fedora. * @param fedoraUser * The Fedora username to connect with. * @param fedoraPass @@ -67,20 +92,20 @@ public static InputStream getDatastreamDisseminationInputStream(String pid, Stri * @return String * The text of the given datastream. */ - public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + public String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { try { URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - URLConnection connection = url.openConnection(); - InputStream dsStream = connection.getInputStream(); - String encoding = connection.getContentEncoding(); - encoding = encoding == null ? "UTF=8" : encoding; - return IOUtils.toString(dsStream, encoding); + InputStream dsStream = url.openStream(); + Scanner scanner = new Scanner(dsStream); + String dsString = scanner.hasNext() ? scanner.next() : ""; + logger.debug(String.format("getRawDatastreamDissemination (pid: %s, DSID: %s): %s", pid, dsId, dsString)); + return dsString; } catch (MalformedURLException e) { logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); } catch (IOException e) { - logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); + logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); } - return ""; + return ""; } /** @@ -89,8 +114,12 @@ public static String getRawDatastreamDissemination(String pid, String dsId, Stri * @return URL * A URL object using the given parameters. */ - protected static URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws MalformedURLException { - String formattedUrl = String.format("http://%s:%s@%s/objects/%s/datastreams/%s/content", fedoraUser, fedoraPass, fedoraBase, pid, dsId); - return new URL(formattedUrl); + protected static final URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws MalformedURLException { + FedoraAuthenticator auth = new FedoraAuthenticator(); + auth.setCredentials(fedoraUser, fedoraPass); + Authenticator.setDefault(auth); + String url = String.format("%s/objects/%s/datastreams/%s/content", fedoraBase, pid, dsId); + logger.debug(String.format("Building URL for %s", url)); + return new URL(url); } } diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java index 625fd00..6e09ee4 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java @@ -20,14 +20,13 @@ protected void setUp() throws Exception { public void testURLBuild() throws MalformedURLException { String pid = "test:pid"; String dsId = "TEST_DSID"; - String fedoraBase = "localhost:8080/fedora"; + String fedoraBase = "http://localhost:8080/fedora"; String fedoraUser = "test_user"; String fedoraPass = "test_pass"; URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); assertEquals(url.getPort(), 8080); assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); - assertEquals(url.getUserInfo(), "test_user:test_pass"); } } From d120095e45631390d910fb7577756309a49cef35 Mon Sep 17 00:00:00 2001 From: qadan Date: Tue, 11 Apr 2017 02:07:51 -0300 Subject: [PATCH 06/12] cleanup, documentation --- README.md | 170 +++++++++++++----- .../gsearch_extensions/FedoraUtils.java | 34 +++- .../gsearch_extensions/JSONToXML.java | 21 ++- .../gsearch_extensions/FedoraUtilsTest.java | 18 +- .../gsearch_extensions/JSONToXMLTest.java | 25 +-- 5 files changed, 194 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 5c4e4b4..2938774 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,43 @@ -SUMMARY -------- +# DGI GSearch Extensions -discoverygarden's GSearch extensions +## Introduction -Currently provides a thin wrapper around the Joda date/time library, in -order to more reliably transform dates for Solr. +discoverygarden's GSearch extensions, providing extended functionality available to GSearch XSLTs that would otherwise be extremely difficult or impossible to recreate in XSLT 1.0. -REQUIREMENTS ------------- +## Requirements -For build: -- Maven 2/3 +* [Apache Maven](https://maven.apache.org) 2 or 3 to build. -INSTALLATION ------------- +## Installation -Build the extensions with `mvn package`, and copy the created jar into GSearch's -lib directory (`$CATALINA_HOME/webapps/fedoragsearch/WEB-INF/lib`). +Build the extensions with `mvn package`, and copy the created jar into GSearch's lib directory (`$CATALINA_HOME/webapps/fedoragsearch/WEB-INF/lib`). -If you want to include Joda Time manually use gsearch_extensions-0.1.0.jar otherwise use -gsearch_extensions-0.1.0-jar-with-dependencies.jar +If providing package libraries yourself, use gsearch_extensions-0.1.2.jar; otherwise use gsearch_extensions-0.1.2-jar-with-dependencies.jar. -CONFIGURATION -------------- +## Usage +### Namespacing -USAGE -------------- +Extensions are available to the java namespace of your XSLT parser. In Xalan, this should be: + +`xmlns:java="http://xml.apache.org/xalan/java"` + +From there, extension functions can be called at that namespace. + +### Functions Available + +#### `ca.discoverygarden.gsearch_extensions.JodaAdapter` + +##### `transformForSolr($date, $pid, $datastream)` + +Attempts to parse dates to a Solr-appropriate format in the following default set and order, assuming UTC if no timezone is provided: + +* `M/d/y`, e.g., `7/23/2013` would become `2013-07-23T00:00:00.000Z` +* `M/d/y H:m`, e.g., `7/23/2013 11:36` would become `2013-07-23T11:36:00.000Z` +* ISO Date Time, as per [Joda-Time ISODateTimeFormat](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTimeParser%28%29). Timezone offsets will be transformed off, so `2013-07-23T02:36-03:00` would be transformed to `2013-07-23T11:36:00.000Z`. + +For example: -In Xalan, it should be possible to call the code like: ``` 08/13/2013 ``` -For better logging, one could also call like: -``` - -``` -where `$pid` and `$datastream` are the identifiers of the object and datastream, respectively. +More parser formats can be added with `addDateParser()`. The list of formats can be reset with `resetParsers()`. + +Variable|Description +--------|----------- +`$date`|A string date formatted like one of the `JodaAdapter`'s parsers. +`$pid`|(Optional) The string PID of the object being processed, for potential logging purposes. +`$datastream`|(Optional; required if `$pid` is given) The string datastream ID of the datastream being processed, for potential logging purposes. + +##### `addDateParser($position, $format)` + +Adds a parsing format pattern to the list of patterns to attempt when running `transformForSolr()`, optionally at the provided position. + +Variable|Description +--------|----------- +`$position`|(Optional) An integer position to place the parser format at. +`$format`|A string format to add to the parser list, e.g., `Y-m-d`. + +##### `resetParsers()` + +Resets the list of parsers `transformForSolr()` will attempt when converting a date. + +#### `ca.discoverygarden.gsearch_extensions.XMLStringUtils` -The three base parsers assume that they are given dates in UTC if no timezone is provided, and are attempted in order: -- `M/d/y` - So `7/23/2013` should result in `2013-07-23T00:00:00.000Z`. -- `M/d/y H:m` - So `7/23/2013 11:36` should result in `2013-07-23T11:36:00.000Z`. -- ISO Date Time, as per http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTimeParser%28%29 - Timezone offsets will be transformed off, so `2013-07-23T02:36-03:00` will be transformed to `2013-07-23T11:36:00.000Z`. +##### `escapeForXML($input, $replacement)` -CUSTOMIZATION -------------- +Escapes a string for inclusion in XML, for example, in cases where the contents of a plaintext datastream are being provided to Solr. + +The list of characters being replaced are based off of the Apache Commons lang3 library's [escapeXML10](https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/StringEscapeUtils.html#escapeXml10-java.lang.String-) list of replaced characters. + +Variable|Description +`$input`|The string to sanitize. +`$replacement`|(Optional) The string to use when replacing invalid characters; otherwise, invalid characters will be replaced with Unicode U+FFFD (the Unicode replacement character). + +#### `ca.discoverygarden.gsearch_extensions.FedoraUtils` + +##### `getDatastreamDisseminationInputStream($pid, $dsId, $fedoraBase, $fedoraUser, $fedoraPass)` + +Gets the dissemination of a datastream as an InputStream object. + +Variable|Description +--------|----------- +`$pid`|The PID of the object to get a datastream from. +`$dsId`|The ID of the datastream to get the dissemination for. +`$fedoraBase`|The base URL of Fedora, including the protocol; e.g., `http://localhost:8080/fedora`. +`$fedoraUser`|The username to log into Fedora with. +`$fedoraPass`|The password for the given username. + +##### `getRawDatastreamDissemination($pid, $dsId, $fedoraBase, $fedoraUser, $fedoraPass)` + +Gets the dissemination of a datastream as a string. Useful in cases where GSearch refuses to return the text of a datastream, i.e., most cases. + +Variable|Description +--------|----------- +`$pid`|The PID of the object to get a datastream from. +`$dsId`|The ID of the datastream to get the dissemination for. +`$fedoraBase`|The base URL of Fedora, including the protocol; e.g., `http://localhost:8080/fedora`. +`$fedoraUser`|The username to log into Fedora with. +`$fedoraPass`|The password for the given username. + +#### `ca.discoverygarden.gsearch_extensions.JSONToXML` + +##### `convertJSONToXML($input, $enclosing_tag)` + +Converts a JSON string to an XML string. + +Variable|Description +--------|----------- +`$input`|The input JSON string. +`$enclosing_tag`|(Optional) The top-level element to wrap resultant XML in, to prevent invalid XML from being written. If not provided, defaults to an element called 'json'. + +##### `convertJSONToDocument($input, $enclosing_tag)` + +Converts a JSON string to an XML Document object. + +The resultant document can be interpreted as a Node-Set by Xalan, for example: + +``` +{"something": "has content"} + + + +``` +Variable|Description +--------|----------- +`$input`|The input JSON string. +`$enclosing_tag|(Optional) The top-level element to wrap resultant XML in, to prevent invalid XML from being written. If not provided, defaults to an element called 'json'. -TROUBLESHOOTING ---------------- +## Troubleshooting/Issues +Having problems or solved a problem? Contact [discoverygarden](http://support.discoverygarden.ca). -F.A.Q. ------- +## Maintainers/Sponsors +Current maintainers: -CONTACT -------- +* [discoverygarden](http://www.discoverygarden.ca) +## Development -SPONSORS --------- +If you would like to contribute to this module, please check out our helpful +[Documentation for Developers](https://github.com/Islandora/islandora/wiki#wiki-documentation-for-developers) +info, [Developers](http://islandora.ca/developers) section on Islandora.ca and +contact [discoverygarden](http://support.discoverygarden.ca). diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index dc2d583..fb80d23 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -19,6 +19,14 @@ class FedoraAuthenticator extends Authenticator { protected String fedoraUser; protected String fedoraPass; + /** + * Set the username and password for this authenticator. + * + * @param username + * The username to set. + * @param password + * The password to set. + */ public void setCredentials(String username, String password) { fedoraUser = username; fedoraPass = password; @@ -26,6 +34,9 @@ public void setCredentials(String username, String password) { /** * Overloaded password authenticator. + * + * @return PasswordAuthentication + * Authentication for this authenticator. */ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(fedoraUser, fedoraPass.toCharArray()); @@ -58,18 +69,20 @@ public class FedoraUtils { * @return InputStream * An InputStream at the constructed dissemination point. */ - public InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + public static InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { URL url; // Attempt to generate the URL from input. try { - url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); return url.openStream(); } + // On exception, log and return a stream with no content so the caller + // doesn't get messed up. catch (MalformedURLException e) { - logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); + logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); } catch (IOException e) { - logger.warn(String.format("Failed to open stream: %s", e.getMessage())); + logger.warn(String.format("Failed to open stream: %s", e.getMessage())); } return new ByteArrayInputStream("".getBytes()); } @@ -92,17 +105,22 @@ public InputStream getDatastreamDisseminationInputStream(String pid, String dsId * @return String * The text of the given datastream. */ - public String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { + public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws FileNotFoundException { try { URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); InputStream dsStream = url.openStream(); Scanner scanner = new Scanner(dsStream); + // If no content, return an empty string. String dsString = scanner.hasNext() ? scanner.next() : ""; logger.debug(String.format("getRawDatastreamDissemination (pid: %s, DSID: %s): %s", pid, dsId, dsString)); return dsString; - } catch (MalformedURLException e) { + } + // On exception, log and return an empty string so the caller doesn't get + // messed up. + catch (MalformedURLException e) { logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); - } catch (IOException e) { + } + catch (IOException e) { logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); } return ""; @@ -115,9 +133,11 @@ public String getRawDatastreamDissemination(String pid, String dsId, String fedo * A URL object using the given parameters. */ protected static final URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws MalformedURLException { + // Set the authenticator. FedoraAuthenticator auth = new FedoraAuthenticator(); auth.setCredentials(fedoraUser, fedoraPass); Authenticator.setDefault(auth); + // Build the URL. String url = String.format("%s/objects/%s/datastreams/%s/content", fedoraBase, pid, dsId); logger.debug(String.format("Building URL for %s", url)); return new URL(url); diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java index cb8f75a..ac9f82b 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java @@ -51,12 +51,29 @@ public static String convertJSONToXML(String in, String enclosing_tag) { * JSON to convert. * * @return + * XML document. + */ + public static Document convertJSONToDocument(String in, String enclosing_tag) + throws ParserConfigurationException, SAXException, IOException + { + return convertJSONToDocument(in, "json"); + } + + /** + * Convert a JSON string to an XML document. + * + * @param in + * JSON to convert. + * @param enclosing_tag + * The tag to enclose the results in. + * + * @return * XML Document. */ - public static Document convertJSONToDocument(String in) + public static Document convertJSONToDocument(String in, String enclosing_tag) throws ParserConfigurationException, SAXException, IOException { - StringReader xmlStringReader = new StringReader(convertJSONToXML(in)); + StringReader xmlStringReader = new StringReader(convertJSONToXML(in, enclosing_tag)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java index 6e09ee4..436c870 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java @@ -11,22 +11,22 @@ public class FedoraUtilsTest extends TestCase { * Set that thing up. */ protected void setUp() throws Exception { - super.setUp(); + super.setUp(); } /** * Tests building a URL. */ public void testURLBuild() throws MalformedURLException { - String pid = "test:pid"; - String dsId = "TEST_DSID"; - String fedoraBase = "http://localhost:8080/fedora"; - String fedoraUser = "test_user"; - String fedoraPass = "test_pass"; - URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + String pid = "test:pid"; + String dsId = "TEST_DSID"; + String fedoraBase = "http://localhost:8080/fedora"; + String fedoraUser = "test_user"; + String fedoraPass = "test_pass"; + URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - assertEquals(url.getPort(), 8080); - assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); + assertEquals(url.getPort(), 8080); + assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); } } diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java index b0dd71c..d08b3e8 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java @@ -22,38 +22,39 @@ public class JSONToXMLTest extends TestCase { * Set up the thingy. */ protected void setUp() throws Exception { - super.setUp(); + super.setUp(); } /** * Tests conversion with the default root element. */ public void testDefaultRootElement() { - // Should be XML enclosed in a 'json' tag. - String dest = "value_1.2value_1.1value_2"; - String transformed = JSONToXML.convertJSONToXML(testJson); + // Should be XML enclosed in a 'json' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson); - assertEquals(dest, transformed); + assertEquals(dest, transformed); } /** * Tests conversion with a custom root element. */ public void testCustomRootElement() { - // Should be XML enclosed in a 'custom' tag. - String dest = "value_1.2value_1.1value_2"; - String transformed = JSONToXML.convertJSONToXML(testJson, "custom"); + // Should be XML enclosed in a 'custom' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson, "custom"); - assertEquals(dest, transformed); + assertEquals(dest, transformed); } /** * Tests conversion to a Document object. */ public void testDocumentConversion() throws ParserConfigurationException, SAXException, IOException { - Document transformed = JSONToXML.convertJSONToDocument(testJson); - NodeList thingList = transformed.getElementsByTagName("thing_1"); + Document transformed = JSONToXML.convertJSONToDocument(testJson); + NodeList thingList = transformed.getElementsByTagName("thing_1"); - assertEquals(thingList.getLength(), 1); + // We should have a thing_1 node in our loaded document. + assertEquals(thingList.getLength(), 1); } } From e85d4d247afd03ed32ed76547fea75dca0b5484b Mon Sep 17 00:00:00 2001 From: qadan Date: Tue, 11 Apr 2017 02:09:39 -0300 Subject: [PATCH 07/12] tyop --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2938774..598fcb8 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ The resultant document can be interpreted as a Node-Set by Xalan, for example: Variable|Description --------|----------- `$input`|The input JSON string. -`$enclosing_tag|(Optional) The top-level element to wrap resultant XML in, to prevent invalid XML from being written. If not provided, defaults to an element called 'json'. +`$enclosing_tag`|(Optional) The top-level element to wrap resultant XML in, to prevent invalid XML from being written. If not provided, defaults to an element called 'json'. ## Troubleshooting/Issues From c497936d81354a6784dd02f2238a97767d8eaa9b Mon Sep 17 00:00:00 2001 From: qadan Date: Tue, 11 Apr 2017 10:52:13 -0300 Subject: [PATCH 08/12] travis failing' --- .../java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java | 2 +- .../java/ca/discoverygarden/gsearch_extensions/JSONToXML.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index fb80d23..dff2eaa 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -105,7 +105,7 @@ public static InputStream getDatastreamDisseminationInputStream(String pid, Stri * @return String * The text of the given datastream. */ - public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws FileNotFoundException { + public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { try { URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); InputStream dsStream = url.openStream(); diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java index ac9f82b..5fe9bad 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/JSONToXML.java @@ -53,7 +53,7 @@ public static String convertJSONToXML(String in, String enclosing_tag) { * @return * XML document. */ - public static Document convertJSONToDocument(String in, String enclosing_tag) + public static Document convertJSONToDocument(String in) throws ParserConfigurationException, SAXException, IOException { return convertJSONToDocument(in, "json"); From 4fd5ab8b79ac439f9bdb5d1fd09f6ed3b44bb33f Mon Sep 17 00:00:00 2001 From: qadan Date: Tue, 11 Apr 2017 12:07:42 -0300 Subject: [PATCH 09/12] accidentally a thing --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 598fcb8..dd4fcb2 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Escapes a string for inclusion in XML, for example, in cases where the contents The list of characters being replaced are based off of the Apache Commons lang3 library's [escapeXML10](https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/StringEscapeUtils.html#escapeXml10-java.lang.String-) list of replaced characters. Variable|Description +--------|----------- `$input`|The string to sanitize. `$replacement`|(Optional) The string to use when replacing invalid characters; otherwise, invalid characters will be replaced with Unicode U+FFFD (the Unicode replacement character). From 2b7da068072a07739e239fdd0aacbbfe37d28450 Mon Sep 17 00:00:00 2001 From: qadan Date: Tue, 11 Apr 2017 16:33:08 -0300 Subject: [PATCH 10/12] eclipse how could u --- .../gsearch_extensions/FedoraUtils.java | 54 +++++++++---------- .../gsearch_extensions/FedoraUtilsTest.java | 42 +++++++-------- .../gsearch_extensions/JSONToXMLTest.java | 30 +++++------ 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index dff2eaa..ed05778 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -70,21 +70,21 @@ public class FedoraUtils { * An InputStream at the constructed dissemination point. */ public static InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { - URL url; - // Attempt to generate the URL from input. - try { - url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - return url.openStream(); - } + URL url; + // Attempt to generate the URL from input. + try { + url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + return url.openStream(); + } // On exception, log and return a stream with no content so the caller // doesn't get messed up. - catch (MalformedURLException e) { - logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); - } - catch (IOException e) { - logger.warn(String.format("Failed to open stream: %s", e.getMessage())); - } - return new ByteArrayInputStream("".getBytes()); + catch (MalformedURLException e) { + logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); + } + catch (IOException e) { + logger.warn(String.format("Failed to open stream: %s", e.getMessage())); + } + return new ByteArrayInputStream("".getBytes()); } /** @@ -106,23 +106,23 @@ public static InputStream getDatastreamDisseminationInputStream(String pid, Stri * The text of the given datastream. */ public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { - try { - URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - InputStream dsStream = url.openStream(); - Scanner scanner = new Scanner(dsStream); - // If no content, return an empty string. - String dsString = scanner.hasNext() ? scanner.next() : ""; - logger.debug(String.format("getRawDatastreamDissemination (pid: %s, DSID: %s): %s", pid, dsId, dsString)); - return dsString; - } + try { + URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + InputStream dsStream = url.openStream(); + Scanner scanner = new Scanner(dsStream); + // If no content, return an empty string. + String dsString = scanner.hasNext() ? scanner.next() : ""; + logger.debug(String.format("getRawDatastreamDissemination (pid: %s, DSID: %s): %s", pid, dsId, dsString)); + return dsString; + } // On exception, log and return an empty string so the caller doesn't get // messed up. catch (MalformedURLException e) { - logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); - } + logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); + } catch (IOException e) { - logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); - } + logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); + } return ""; } @@ -140,6 +140,6 @@ protected static final URL getDatastreamDisseminationURL(String pid, String dsId // Build the URL. String url = String.format("%s/objects/%s/datastreams/%s/content", fedoraBase, pid, dsId); logger.debug(String.format("Building URL for %s", url)); - return new URL(url); + return new URL(url); } } diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java index 436c870..39f2cde 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java @@ -7,26 +7,26 @@ public class FedoraUtilsTest extends TestCase { - /** - * Set that thing up. - */ - protected void setUp() throws Exception { - super.setUp(); - } - - /** - * Tests building a URL. - */ - public void testURLBuild() throws MalformedURLException { - String pid = "test:pid"; - String dsId = "TEST_DSID"; - String fedoraBase = "http://localhost:8080/fedora"; - String fedoraUser = "test_user"; - String fedoraPass = "test_pass"; - URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - - assertEquals(url.getPort(), 8080); - assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); - } + /** + * Set that thing up. + */ + protected void setUp() throws Exception { + super.setUp(); + } + + /** + * Tests building a URL. + */ + public void testURLBuild() throws MalformedURLException { + String pid = "test:pid"; + String dsId = "TEST_DSID"; + String fedoraBase = "http://localhost:8080/fedora"; + String fedoraUser = "test_user"; + String fedoraPass = "test_pass"; + URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + + assertEquals(url.getPort(), 8080); + assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); + } } diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java index d08b3e8..ca4b677 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java @@ -22,39 +22,39 @@ public class JSONToXMLTest extends TestCase { * Set up the thingy. */ protected void setUp() throws Exception { - super.setUp(); + super.setUp(); } /** * Tests conversion with the default root element. */ public void testDefaultRootElement() { - // Should be XML enclosed in a 'json' tag. - String dest = "value_1.2value_1.1value_2"; - String transformed = JSONToXML.convertJSONToXML(testJson); + // Should be XML enclosed in a 'json' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson); - assertEquals(dest, transformed); + assertEquals(dest, transformed); } /** * Tests conversion with a custom root element. */ public void testCustomRootElement() { - // Should be XML enclosed in a 'custom' tag. - String dest = "value_1.2value_1.1value_2"; - String transformed = JSONToXML.convertJSONToXML(testJson, "custom"); + // Should be XML enclosed in a 'custom' tag. + String dest = "value_1.2value_1.1value_2"; + String transformed = JSONToXML.convertJSONToXML(testJson, "custom"); - assertEquals(dest, transformed); + assertEquals(dest, transformed); } - + /** * Tests conversion to a Document object. */ public void testDocumentConversion() throws ParserConfigurationException, SAXException, IOException { - Document transformed = JSONToXML.convertJSONToDocument(testJson); - NodeList thingList = transformed.getElementsByTagName("thing_1"); - - // We should have a thing_1 node in our loaded document. - assertEquals(thingList.getLength(), 1); + Document transformed = JSONToXML.convertJSONToDocument(testJson); + NodeList thingList = transformed.getElementsByTagName("thing_1"); + + // We should have a thing_1 node in our loaded document. + assertEquals(thingList.getLength(), 1); } } From a465083d0e14624a8325d41e24137fbce22ee630 Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 12 Apr 2017 14:35:36 -0300 Subject: [PATCH 11/12] fixies --- .../gsearch_extensions/FedoraUtils.java | 98 +++++++++---------- .../gsearch_extensions/FedoraUtilsTest.java | 11 +-- .../gsearch_extensions/JSONToXMLTest.java | 7 -- 3 files changed, 46 insertions(+), 70 deletions(-) diff --git a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java index ed05778..95ee178 100644 --- a/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java +++ b/src/main/java/ca/discoverygarden/gsearch_extensions/FedoraUtils.java @@ -12,44 +12,44 @@ import java.util.Scanner; /** - * Authenticator for connecting to Fedora. + * Utilities for interfacing with Fedora in ways that GSearch can't. */ -class FedoraAuthenticator extends Authenticator { +public class FedoraUtils { - protected String fedoraUser; - protected String fedoraPass; + protected static final Logger logger = Logger.getLogger(FedoraUtils.class); /** - * Set the username and password for this authenticator. - * - * @param username - * The username to set. - * @param password - * The password to set. + * Authenticator for connecting to Fedora. */ - public void setCredentials(String username, String password) { - fedoraUser = username; - fedoraPass = password; - } + static class FedoraAuthenticator extends Authenticator { - /** - * Overloaded password authenticator. - * - * @return PasswordAuthentication - * Authentication for this authenticator. - */ - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(fedoraUser, fedoraPass.toCharArray()); - } + protected String fedoraUser; + protected String fedoraPass; -} + /** + * Constructor; sets the username and password for this authenticator. + * + * @param username + * The username to set. + * @param password + * The password to set. + */ + FedoraAuthenticator(String username, String password) { + fedoraUser = username; + fedoraPass = password; + } -/** - * Utilities for interfacing with Fedora in ways that GSearch can't. - */ -public class FedoraUtils { + /** + * Overloaded password authenticator. + * + * @return PasswordAuthentication + * Authentication for this authenticator. + */ + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(fedoraUser, fedoraPass.toCharArray()); + } - protected static final Logger logger = Logger.getLogger(FedoraUtils.class); + } /** * Gets an InputStream for a datastream. @@ -70,10 +70,12 @@ public class FedoraUtils { * An InputStream at the constructed dissemination point. */ public static InputStream getDatastreamDisseminationInputStream(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { - URL url; + // Set the authenticator. + FedoraAuthenticator auth = new FedoraAuthenticator(fedoraUser, fedoraPass); + Authenticator.setDefault(auth); // Attempt to generate the URL from input. try { - url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase); return url.openStream(); } // On exception, log and return a stream with no content so the caller @@ -106,24 +108,16 @@ public static InputStream getDatastreamDisseminationInputStream(String pid, Stri * The text of the given datastream. */ public static String getRawDatastreamDissemination(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) { - try { - URL url = getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); - InputStream dsStream = url.openStream(); - Scanner scanner = new Scanner(dsStream); - // If no content, return an empty string. - String dsString = scanner.hasNext() ? scanner.next() : ""; + InputStream dsStream = getDatastreamDisseminationInputStream(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + // Scan the InputStream, delimited by the start of string marker, so the + // whole string gets scanned. + Scanner scanner = new Scanner(dsStream).useDelimiter("\\A"); + // If no content, return an empty string. + String dsString = scanner.hasNext() ? scanner.next() : ""; + if (logger.isDebugEnabled()) { logger.debug(String.format("getRawDatastreamDissemination (pid: %s, DSID: %s): %s", pid, dsId, dsString)); - return dsString; - } - // On exception, log and return an empty string so the caller doesn't get - // messed up. - catch (MalformedURLException e) { - logger.warn(String.format("Attempt to generate URL for datastream dissemination failed: %s", e.getMessage())); - } - catch (IOException e) { - logger.warn(String.format("Failed to open connection to datastream dissemination: %s", e.getMessage())); } - return ""; + return dsString; } /** @@ -132,14 +126,12 @@ public static String getRawDatastreamDissemination(String pid, String dsId, Stri * @return URL * A URL object using the given parameters. */ - protected static final URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase, String fedoraUser, String fedoraPass) throws MalformedURLException { - // Set the authenticator. - FedoraAuthenticator auth = new FedoraAuthenticator(); - auth.setCredentials(fedoraUser, fedoraPass); - Authenticator.setDefault(auth); + protected static final URL getDatastreamDisseminationURL(String pid, String dsId, String fedoraBase) throws MalformedURLException { // Build the URL. String url = String.format("%s/objects/%s/datastreams/%s/content", fedoraBase, pid, dsId); - logger.debug(String.format("Building URL for %s", url)); + if (logger.isDebugEnabled()) { + logger.debug(String.format("Building URL for %s", url)); + } return new URL(url); } } diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java index 39f2cde..f8aa4ea 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/FedoraUtilsTest.java @@ -7,13 +7,6 @@ public class FedoraUtilsTest extends TestCase { - /** - * Set that thing up. - */ - protected void setUp() throws Exception { - super.setUp(); - } - /** * Tests building a URL. */ @@ -21,9 +14,7 @@ public void testURLBuild() throws MalformedURLException { String pid = "test:pid"; String dsId = "TEST_DSID"; String fedoraBase = "http://localhost:8080/fedora"; - String fedoraUser = "test_user"; - String fedoraPass = "test_pass"; - URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase, fedoraUser, fedoraPass); + URL url = FedoraUtils.getDatastreamDisseminationURL(pid, dsId, fedoraBase); assertEquals(url.getPort(), 8080); assertEquals(url.getPath(), "/fedora/objects/test:pid/datastreams/TEST_DSID/content"); diff --git a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java index ca4b677..8d33e20 100644 --- a/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java +++ b/src/test/java/ca/discoverygarden/gsearch_extensions/JSONToXMLTest.java @@ -18,13 +18,6 @@ public class JSONToXMLTest extends TestCase { // Our test JSON we use twice. protected static final String testJson = "{\"thing_1\":{\"subthing_1.1\":\"value_1.1\",\"subthing_1.2\":\"value_1.2\"},\"thing_2\":\"value_2\"}"; - /** - * Set up the thingy. - */ - protected void setUp() throws Exception { - super.setUp(); - } - /** * Tests conversion with the default root element. */ From a60e81dc4bdfa9f80912e8d180c28965b9005be0 Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 12 Apr 2017 17:34:42 -0300 Subject: [PATCH 12/12] rely on the jar in gsearch --- pom.xml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 9e4e64e..6d79e48 100644 --- a/pom.xml +++ b/pom.xml @@ -41,14 +41,10 @@ 20160810 - javax.xml.parsers - jaxp-api - 1.4.5 - - - org.w3c - dom - 2.3.0-jaxb-1.0.6 + dom4j + dom4j + 1.6.1 + provided