Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from futures/DublinCoreIndexer
Create initial Dublin Core indexer framework and REST API endpoint.
- Loading branch information
Showing
14 changed files
with
348 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/fcrepo/modeshape/indexer/DublinCore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.fcrepo.modeshape.indexer; | ||
|
||
import org.fcrepo.modeshape.AbstractResource; | ||
import org.fcrepo.modeshape.indexer.dublincore.AbstractIndexer; | ||
import org.fcrepo.modeshape.indexer.dublincore.IndexFromWellKnownPath; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import static javax.ws.rs.core.MediaType.TEXT_XML; | ||
import static javax.ws.rs.core.Response.ok; | ||
|
||
|
||
@Path("/objects/{pid}/oai_dc") | ||
public class DublinCore extends AbstractResource { | ||
|
||
@Inject | ||
List<AbstractIndexer> indexers; | ||
|
||
@GET | ||
@Produces({ TEXT_XML }) | ||
public Response getObjectAsDublinCore(@PathParam("pid") final String pid) throws RepositoryException { | ||
final Session session = repo.login(); | ||
|
||
try { | ||
if (session.nodeExists("/objects/" + pid)) { | ||
final Node obj = session.getNode("/objects/" + pid); | ||
|
||
for(AbstractIndexer indexer : indexers) { | ||
InputStream inputStream = indexer.getStream(obj); | ||
|
||
if(inputStream != null) { | ||
return ok(inputStream, TEXT_XML).build(); | ||
} | ||
} | ||
|
||
return four04; | ||
} else { | ||
return four04; | ||
} | ||
|
||
} finally { | ||
session.logout(); | ||
} | ||
|
||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/fcrepo/modeshape/indexer/dublincore/AbstractIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.fcrepo.modeshape.indexer.dublincore; | ||
|
||
|
||
import javax.jcr.Node; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public abstract class AbstractIndexer { | ||
|
||
public AbstractIndexer() { | ||
} | ||
|
||
public abstract InputStream getStream(Node node); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/fcrepo/modeshape/indexer/dublincore/IndexFromJcrProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.fcrepo.modeshape.indexer.dublincore; | ||
|
||
import javax.jcr.*; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public class IndexFromJcrProperties extends AbstractIndexer { | ||
|
||
@Override | ||
public InputStream getStream(Node node) { | ||
|
||
|
||
String str = "<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"; | ||
|
||
try { | ||
final String[] nameGlobs = new String[] { "dc:*"}; | ||
PropertyIterator iter = node.getProperties(nameGlobs); | ||
|
||
while(iter.hasNext()) { | ||
Property property = iter.nextProperty(); | ||
if(property.isMultiple()) { | ||
for(final Value v : property.getValues()) { | ||
str += "\t<" + property.getName() + ">" + v.getString() + "</" + property.getName() + ">\n"; | ||
} | ||
} | ||
else { | ||
str += "\t<" + property.getName() + ">" + property.getValue().getString() + "</" + property.getName() + ">\n"; | ||
|
||
} | ||
} | ||
|
||
str += "</oai_dc:dc>"; | ||
|
||
try { | ||
return new ByteArrayInputStream(str.getBytes("UTF-8")); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} catch (RepositoryException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/fcrepo/modeshape/indexer/dublincore/IndexFromWellKnownPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.fcrepo.modeshape.indexer.dublincore; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Binary; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
|
||
import java.io.InputStream; | ||
|
||
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT; | ||
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA; | ||
|
||
public class IndexFromWellKnownPath extends AbstractIndexer { | ||
|
||
private String wellKnownPath; | ||
|
||
@Override | ||
public InputStream getStream(Node node) { | ||
|
||
try { | ||
return getContentInputStream(node); | ||
} catch (RepositoryException e) { | ||
e.printStackTrace(); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
private InputStream getContentInputStream(Node node) throws RepositoryException { | ||
if(node.hasNode(this.wellKnownPath)) { | ||
final Node dc = node.getNode(this.wellKnownPath); | ||
|
||
Binary binary = dc.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary(); | ||
|
||
return binary.getStream(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public void setWellKnownPath(String wellKnownPath) { | ||
this.wellKnownPath = wellKnownPath; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/fcrepo/modeshape/indexer/dublincore/WorstCaseIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.fcrepo.modeshape.indexer.dublincore; | ||
|
||
import javax.jcr.Node; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public class WorstCaseIndexer extends AbstractIndexer { | ||
|
||
@Override | ||
public InputStream getStream(Node node) { | ||
String str = "<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ></oai_dc:dc>"; | ||
|
||
try { | ||
return new ByteArrayInputStream(str.getBytes("UTF-8")); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | ||
|
||
|
||
<context:annotation-config/> | ||
|
||
<bean class="org.springframework.beans.factory.config.ListFactoryBean"> | ||
<property name="targetListClass"> | ||
<value>java.util.LinkedList</value> | ||
</property> | ||
<property name="sourceList"> | ||
<list> | ||
<ref local="DCfromJcrPropertiesIndexer"/> | ||
<ref local="wkpIndexer"/> | ||
</list> | ||
</property> | ||
</bean> | ||
|
||
<bean id="wkpIndexer" class="org.fcrepo.modeshape.indexer.dublincore.IndexFromWellKnownPath"> | ||
<property name="wellKnownPath" value="DC" /> | ||
</bean> | ||
|
||
|
||
<bean id="DCfromJcrPropertiesIndexer" class="org.fcrepo.modeshape.indexer.dublincore.IndexFromJcrProperties" /> | ||
|
||
|
||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/org/fcrepo/modeshape/indexer/DublinCoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.fcrepo.modeshape.indexer; | ||
|
||
|
||
import org.apache.commons.httpclient.HttpClient; | ||
import org.apache.commons.httpclient.methods.GetMethod; | ||
import org.apache.commons.httpclient.methods.PostMethod; | ||
import org.apache.commons.httpclient.methods.StringRequestEntity; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import static java.util.regex.Pattern.DOTALL; | ||
import static java.util.regex.Pattern.compile; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration({ "/spring-test/rest.xml", "/spring-test/repo.xml", "/spring-test/indexer.xml" }) | ||
public class DublinCoreTest { | ||
private static final int SERVER_PORT = 8080; | ||
private static final String HOSTNAME = "localhost"; | ||
private static final String serverAddress = "http://" + HOSTNAME + ":" | ||
+ SERVER_PORT + "/"; | ||
|
||
final private Logger logger = LoggerFactory | ||
.getLogger(DublinCoreTest.class); | ||
|
||
final private HttpClient client = new HttpClient(); | ||
|
||
@Test | ||
public void testJcrPropertiesBasedOaiDc() throws Exception { | ||
PostMethod createObjMethod = new PostMethod(serverAddress | ||
+ "rest/objects/fdsa"); | ||
client.executeMethod(createObjMethod); | ||
|
||
GetMethod getWorstCaseOaiMethod = new GetMethod(serverAddress | ||
+ "rest/objects/fdsa/oai_dc"); | ||
int status = client.executeMethod(getWorstCaseOaiMethod); | ||
assertEquals(200, status); | ||
|
||
final String response = getWorstCaseOaiMethod.getResponseBodyAsString(); | ||
assertTrue("Didn't find oai_dc!", | ||
compile("oai_dc", DOTALL).matcher(response).find()); | ||
|
||
assertTrue("Didn't find dc:identifier!", | ||
compile("dc:identifier", DOTALL).matcher(response).find()); | ||
} | ||
|
||
@Test | ||
public void testWellKnownPathOaiDc() throws Exception { | ||
PostMethod createObjMethod = new PostMethod(serverAddress | ||
+ "rest/objects/lkjh"); | ||
client.executeMethod(createObjMethod); | ||
|
||
|
||
PostMethod createDSMethod = new PostMethod(serverAddress | ||
+ "rest/objects/lkjh/datastreams/DC"); | ||
|
||
createDSMethod.setRequestEntity(new StringRequestEntity( | ||
"marbles for everyone", null, null)); | ||
|
||
client.executeMethod(createDSMethod); | ||
|
||
GetMethod getWorstCaseOaiMethod = new GetMethod(serverAddress | ||
+ "rest/objects/lkjh/oai_dc"); | ||
int status = client.executeMethod(getWorstCaseOaiMethod); | ||
assertEquals(200, status); | ||
|
||
final String response = getWorstCaseOaiMethod.getResponseBodyAsString(); | ||
assertTrue("Didn't find our datastream!", | ||
compile("marbles for everyone", DOTALL).matcher(response).find()); | ||
} | ||
} |
Oops, something went wrong.