Commit af0f8683 authored by Ricco Førgaard's avatar Ricco Førgaard

Left out some files in the previous commit.

parent f9540f6b
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- maven-compiler-plugin configuration -->
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>Nesstar</id>
<name>Nesstar Maven Repository</name>
<url>http://nesstar-dev.nsd.uib.no/nexus/content/groups/public/</url>
</repository>
</repositories>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nesstar.oaipmh</groupId>
<artifactId>oai-pmh</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>oai-pmh web service</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nesstar</groupId>
<artifactId>nesstar_public_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.concordion</groupId>
<artifactId>concordion</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-servlet-tester</artifactId>
<version>7.0.0.pre5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.3</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>oai-pmh</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<projectNameTemplate>oai-pmh</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
<manifest>${basedir}/src/main/resources/META-INF/MANIFEST.MF</manifest>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
<configuration>
<archive>
<compress>false</compress>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>concordion.output.dir</name>
<value>target/concordion</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}</directory>
<filtering>false</filtering>
<includes>
<include>*.txt</include>
</includes>
</resource>
</resources>
</build>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- maven-compiler-plugin configuration -->
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>Nesstar</id>
<name>Nesstar Maven Repository</name>
<url>http://nesstar-dev.nsd.uib.no/nexus/content/groups/public/</url>
</repository>
</repositories>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nesstar.oaipmh</groupId>
<artifactId>oai-pmh</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>oai-pmh web service</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nesstar</groupId>
<artifactId>nesstar-api</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.concordion</groupId>
<artifactId>concordion</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-servlet-tester</artifactId>
<version>7.0.0.pre5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.3</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.8.2.2</version>
</dependency>
</dependencies>
<build>
<finalName>oai-pmh</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<projectNameTemplate>oai-pmh</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
<manifest>${basedir}/src/main/resources/META-INF/MANIFEST.MF</manifest>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
<configuration>
<archive>
<compress>false</compress>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemProperties>
<property>
<name>concordion.output.dir</name>
<value>target/concordion</value>
</property>
</systemProperties>
<parallel>methods</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>nesstar-enabled</finalName>
<descriptors>
<descriptor>src/main/assembly/nesstar.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.2.v20120308</version>
<configuration>
<reload>automatic</reload>
<scanIntervalSeconds>5</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/target/oai-pmh</webAppSourceDirectory>
<webAppConfig>
<contextPath>/oai-pmh</contextPath>
</webAppConfig>
<systemProperties>
<systemProperty>
<name>catalina.base</name>
<value>target</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}</directory>
<filtering>false</filtering>
<includes>
<include>*.txt</include>
</includes>
</resource>
</resources>
</build>
</project>
\ No newline at end of file
......@@ -2,21 +2,20 @@ package com.nesstar.oaipmh;
public final class NesstarServerSingleton {
private static NesstarServer instance;
private NesstarServerSingleton() {
}
public static synchronized NesstarServer getInstance() {
if (instance == null) {
instance = new NesstarServer();
}
return instance;
}
public static void setNesstarServer(NesstarServer server) {
instance = server;
}
}
package com.nesstar.oaipmh.controller;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
import com.nesstar.oaipmh.util.Properties;
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = -8646231713260917250L;
public void init() throws ServletException {
super.init();
initializeLogging();
initializeProperties();
}
private void initializeLogging() {
logToStandardOutput("Initializing OAI PMH Logging System");
File propertyFile = getLogPropertyFile();
configureLoggingSystem(propertyFile);
}
private File getLogPropertyFile() {
final ServletContext context = getServletContext();
String path = buildPathForPropertyFile(context);
return new File(path);
}
private String buildPathForPropertyFile(ServletContext context) {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(context.getRealPath("/"));
pathBuilder.append("/WEB-INF/log4j.properties");
return pathBuilder.toString();
}
private void configureLoggingSystem(File propertyFile) {
if (propertyFile.exists()) {
logToStandardOutput("Initializing log4j with " + propertyFile.getAbsolutePath());
PropertyConfigurator.configure(propertyFile.getAbsolutePath());
} else {
logToStandardError("Unable to find log4j properties. Using defaults.");
BasicConfigurator.configure();
}
}
private void initializeProperties() {
Properties.init(getServletConfig());
}
private void logToStandardOutput(String message) {
System.out.println(message);
}
private void logToStandardError(String message) {
System.err.println(message);
}
}
package com.nesstar.oaipmh.controller;
import java.util.Hashtable;
import java.util.Map;
import com.nesstar.oaipmh.result.OAIResult;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.controller.RequestParser;
import com.nesstar.oaipmh.exceptions.BadArgumentException;
import com.nesstar.oaipmh.exceptions.CannotDisseminateFormatException;
import com.nesstar.oaipmh.exceptions.OAIException;
public abstract class OAIRequest {
protected Map<String, String[]> arguments;
protected OAIRequest() throws OAIException {
arguments = new Hashtable<String, String[]>();
}
protected OAIRequest(Map<String, String[]> args) throws OAIException {
arguments = args;
identifyInvalidArguments();
}
protected abstract void identifyInvalidArguments() throws OAIException;
public String getVerb() {
return getArgumentValue(RequestParser.VERB);
}
public String getIdentifier() {
return getArgumentValue(RequestParser.IDENTIFIER);
}
public String getMetadataPrefix() {
return getArgumentValue(RequestParser.METADATA_PREFIX);
}
public String getFrom() {
return getArgumentValue(RequestParser.FROM);
}
public String getUntil() {
return getArgumentValue(RequestParser.UNTIL);
}
public String getSet() {
return getArgumentValue(RequestParser.SET);
}
protected void setRequestValues(OAIResult someResult) {
someResult.setVerb(getVerb());
someResult.setIdentifier(getIdentifier());
someResult.setMetadataPrefix(getMetadataPrefix());
someResult.setDateRangeFrom(getFrom());
someResult.setDateRangeTo(getUntil());
someResult.setSetName(getSet());
}
protected String getArgumentValue(String argument) {
String[] values = arguments.get(argument);
String returnValue = "";
if (values != null && values.length > 0) {
returnValue = values[0];
}
return returnValue;
}
protected void checkValidMetadataPrefix() throws CannotDisseminateFormatException, BadArgumentException {
final String prefix = getMetadataPrefix();
if ("".equals(prefix)) {
throw new BadArgumentException(RequestParser.METADATA_PREFIX);
} else if (!RequestParser.KNOWN_METADATA_PREFIXES.contains(prefix)) {
throw new CannotDisseminateFormatException(prefix);
}
}
public abstract OAIXmlBuilder performRequest() throws Exception;
}
package com.nesstar.oaipmh.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.exceptions.OAIException;
import com.nesstar.oaipmh.controller.OAIRequest;
import com.nesstar.oaipmh.controller.RequestParser;
import com.nesstar.oaipmh.xml.ExceptionXmlBuilder;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
public class OaiPmhServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(OaiPmhServlet.class);
protected HttpServletResponse response;
public OaiPmhServlet() {
super();
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
logger.info("Initializing OAI PMH Servlet...");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug("Got request");
this.response = response;
try {
handleOAIRequest(request);
} catch (Exception exception) {
handleErrors(exception);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void handleOAIRequest(HttpServletRequest request) throws Exception {
logger.debug("Handling request");
RequestParser parser = new RequestParser(request);
OAIRequest oaiRequest = parser.getOAIRequest();
OAIXmlBuilder oaiXML = oaiRequest.performRequest();
writeXMLtoOutput(oaiXML);
//writeXMLtoFile(oaiXML);
}
protected void handleErrors(Exception exception) {
// The HTTP status code should always be 200 OK
// according to the OAI-PMH spec.
if (exception instanceof OAIException) {
handleKnownErrors((OAIException) exception);
} else {
handleUnknownErrors(exception);
}
}
protected void handleKnownErrors(OAIException exception) {
logger.debug("Handling error " + exception.getMessage());
try {
ExceptionXmlBuilder xmlBuilder = new ExceptionXmlBuilder(exception);
xmlBuilder.buildXML();
writeXMLtoOutput(xmlBuilder);
} catch (Exception e) {
logger.fatal("Could not build XML for error handling.", e);
}
}
protected void handleUnknownErrors(Exception exception) {
response.setStatus(500);
response.setContentType("text/html");
try {
PrintWriter out = response.getWriter();
out.write("<h1>Internal Server Error</h1>");
out.write("<p>An error occurred in the server. This incident has been logged.</p>");
out.flush();
out.close();
} catch (IOException ioex) {
logger.fatal("Could not write error message to client.", ioex);
}
}
protected File writeXMLtoFile(OAIXmlBuilder oaiXML) {
File tmp = null;
try {
tmp = File.createTempFile("oaipmh", ".xml");
oaiXML.writeXMLtoFile(tmp);
} catch (Exception exception) {
logger.fatal("Failed writing XML to file on disk.", exception);
}
return tmp;
}
protected void writeXMLtoOutput(OAIXmlBuilder oaiXML) {
response.setContentType("text/xml");
response.setCharacterEncoding("utf-8");
try {
PrintWriter out = response.getWriter();
oaiXML.writeXMLtoOutput(out);
out.flush();
out.close();
} catch (Exception exception) {
logger.fatal("Failed writing XML to output", exception);
}
}
}
package com.nesstar.oaipmh.result;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import com.nesstar.api.Catalog;
import com.nesstar.api.IllegalTreeOperationException;
import com.nesstar.api.NesstarList;
import com.nesstar.api.NesstarTreeNode;
import com.nesstar.api.NesstarTreeObject;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Study;
import com.nesstar.oaipmh.NesstarServer;
import com.nesstar.oaipmh.NesstarServerSingleton;
import com.nesstar.oaipmh.util.OAIProperties;
public class IdentifyResult extends OAIResult {
private static Logger logger = Logger.getLogger(IdentifyResult.class);
private String repositoryName;
private String baseURL;
private String protocolVersion = "2.0";
private List<String> administratorEmails;
private Date earliestDate;
private String deletedRecord = "transient";
private String granularity = "YYYY-MM-DDThh:mm:ssZ";
private String compression = "deflate";
public IdentifyResult() {
super();
administratorEmails = OAIProperties.getInstance().getAdministratorEmails();
logger.debug("New IdentifyResult");
}
public String getRepositoryName() throws Exception {
if (null == repositoryName) {
repositoryName = NesstarServerSingleton.getInstance().getServer().getLabel();
}
return repositoryName;
}
public String getBaseURL() {
if (null == baseURL) {
baseURL = OAIProperties.getInstance().getThisServerURL();
}
return baseURL;
}
public String getProtocolVersion() {
return protocolVersion;
}
public List<String> getAdministratorEmails() {
return administratorEmails;
}
public Date getEarliestDateStamp() throws Exception, IOException {
//TODO: caching her? JA! Kanskje tilogmed sjekk ved server startup...
if (earliestDate == null) {
NesstarTreeNode root = getRootFromServer();
searchCatalogTreeAndSetEarliestDate(root);
}
return earliestDate;
}
protected void searchCatalogTreeAndSetEarliestDate(NesstarTreeNode node) throws NotAuthorizedException, IOException, IllegalTreeOperationException {
List<NesstarTreeNode> children = node.getChildren();
for (NesstarTreeNode child : children) {
NesstarTreeObject payload = child.getUserObject();
if (payload instanceof Study) {
setEarliestDateFromStudy((Study) payload);
} else if (child.allowsChildren()){
searchCatalogTreeAndSetEarliestDate(child);
}
}
}
protected void setEarliestDateFromStudy(Study study) throws NotAuthorizedException, IOException {
Date found = study.getTimeStamp();
setEarliestDate(found);
}
public void setEarliestDate(Date found) {
if (earliestDate == null || (found != null && found.compareTo(earliestDate) < 0)) {
earliestDate = found;
}
}
public String getDeletedRecord() {
return deletedRecord;
}
public String getGranularity() {
return granularity;
}
public String getCompression() {
return compression;
}
protected NesstarTreeNode getRootFromServer() throws Exception {
return NesstarServerSingleton.getInstance().getTreeRoot();
}
private static Logger logger = Logger.getLogger(IdentifyResult.class);
private String repositoryName;
private String baseURL;
private String protocolVersion = "2.0";
private List<String> administratorEmails;
private Date earliestDate;
private String deletedRecord = "transient";
private String granularity = "YYYY-MM-DDThh:mm:ssZ";
private String compression = "deflate";
public IdentifyResult() {
super();
administratorEmails = OAIProperties.getInstance().getAdministratorEmails();
logger.debug("New IdentifyResult");
}
public String getRepositoryName() throws Exception {
if (null == repositoryName) {
repositoryName = NesstarServerSingleton.getInstance().getServer().getLabel();
}
return repositoryName;
}
public String getBaseURL() {
if (null == baseURL) {
baseURL = OAIProperties.getInstance().getThisServerURL();
}
return baseURL;
}
public String getProtocolVersion() {
return protocolVersion;
}