Commit 9de14187 authored by Ricco Førgaard's avatar Ricco Førgaard

Added initial files.

parent d4605e44
<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>internal</id>
<name>Internal Mirror of Central Repository</name>
<url>http://nesstar-dev.nsd.uib.no/internal/</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 wevice</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>
</build>
</project>
package com.nesstar.oaipmh;
import java.io.IOException;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.util.Properties;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.Server;
import com.nesstar.api.impl.NesstarDBImpl;
public final class NesstarServer {
private static Logger logger = Logger.getLogger(NesstarServer.class);
private static NesstarServer instance;
private static NesstarDB nesstarDB;
private static Server server;
private NesstarServer() throws Exception {
Properties props = Properties.getInstance();
String serverURL = props.getURLforServerToDisseminate();
int serverPort = props.getPortForServerToDisseminate();
try {
nesstarDB = new NesstarDBImpl();
server = nesstarDB.getServer(serverURL, serverPort);
} catch (IOException ioex) {
logger.fatal("Unable to load Nesstar server", ioex);
throw ioex;
}
}
public static NesstarServer getInstance() throws Exception {
if (null == instance) {
logger.info("Loading instance.");
instance = new NesstarServer();
}
return instance;
}
public static Server getServer() throws Exception {
return getInstance().getServerFromInstance();
}
private Server getServerFromInstance() {
return server;
}
public static NesstarDB getNesstarDB() throws Exception {
return getInstance().getNesstarDBFromInstance();
}
private NesstarDB getNesstarDBFromInstance() {
return nesstarDB;
}
}
package com.nesstar.oaipmh.dataformats;
public class MetadataFormat {
private String prefix;
private String schema;
private String namespace;
public MetadataFormat(String prefix, String schema, String namespace) {
this.prefix = prefix;
this.schema = schema;
this.namespace = namespace;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
package com.nesstar.oaipmh.exceptions;
import java.util.ArrayList;
import java.util.List;
public class BadArgumentException extends OAIException {
private List<String> missingArguments;
public BadArgumentException(List<String> missingArguments) {
this.missingArguments = missingArguments;
}
public BadArgumentException(String badArgument) {
missingArguments = new ArrayList<String>(1);
missingArguments.add(badArgument);
}
protected void setCode() {
code = "badArgument";
}
public String getMessage() {
StringBuilder message = new StringBuilder("Missing argument");
if (missingArguments.size() > 1) {
message.append('s');
}
message.append(": ");
boolean addSeparator = false;
for (String argument : missingArguments) {
if (addSeparator) {
message.append(", ");
}
addSeparator = true;
message.append(argument);
}
return message.toString();
}
public List<String> getMissingArguments() {
return missingArguments;
}
}
package com.nesstar.oaipmh.exceptions;
public class BadVerbException extends OAIException {
private String verb;
public BadVerbException(String verb) {
this.verb = verb;
}
protected void setCode() {
code = "badVerb";
}
public String getMessage() {
return new StringBuilder("Unknown or illegal verb '")
.append(verb)
.append("'")
.toString();
}
}
package com.nesstar.oaipmh.exceptions;
public class CannotDisseminateFormatException extends OAIException {
protected String metadataFormat;
public CannotDisseminateFormatException(String metadataPrefix) {
super();
metadataFormat = metadataPrefix;
}
@Override
protected void setCode() {
code = "cannotDisseminateFormat";
}
public String getMessage() {
return new StringBuilder("This server does not disseminate the format '")
.append(metadataFormat)
.append("'.").toString();
}
}
package com.nesstar.oaipmh.exceptions;
public class IdDoesNotExistException extends OAIException {
protected String unknownID;
public IdDoesNotExistException(String id) {
super();
unknownID = id;
}
protected void setCode() {
code = "idDoesNotExist";
}
public String getMessage() {
return new StringBuilder("Nothing found for the identifier '")
.append(unknownID)
.append("'.").toString();
}
}
package com.nesstar.oaipmh.exceptions;
public class NoRecordsMatchException extends OAIException {
protected void setCode() {
code = "noRecordsMatch";
}
public String getMessage() {
return "The query doesn't match any records.";
}
}
package com.nesstar.oaipmh.exceptions;
public abstract class OAIException extends Exception {
protected String message;
protected String code;
public OAIException() {
setCode();
}
public String getCode() {
return code;
}
protected abstract void setCode();
public abstract String getMessage();
}
package com.nesstar.oaipmh.request;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.result.GetRecordResult;
import com.nesstar.oaipmh.xml.DdiRecordBuilder;
import com.nesstar.oaipmh.xml.DublinCoreRecordBuilder;
import com.nesstar.oaipmh.xml.GetRecordXmlBuilder;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.BadArgumentException;
import com.nesstar.oaipmh.exceptions.CannotDisseminateFormatException;
import com.nesstar.oaipmh.exceptions.OAIException;
public class GetRecordRequest extends OAIRequest {
private static Logger logger = Logger.getLogger(GetRecordRequest.class);
public GetRecordRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() throws OAIException {
checkValidMetadataPrefix();
checkPresenceOfValidArguments();
}
public OAIXmlBuilder performRequest() throws Exception {
GetRecordResult result = new GetRecordResult(this);
verifyCorrectMetadataPrefixForEGMS(result);
GetRecordXmlBuilder xmlBuilder = null;
if (isDublinCoreRequest()) {
xmlBuilder = new DublinCoreRecordBuilder(result);
} else if (isDDIRequest()) {
xmlBuilder = new DdiRecordBuilder(result);
} else {
logger.error("Unexpected illegal metadataPrefix format");
throw new Exception("Illegal metadata prefix!");
}
xmlBuilder.buildXML();
return xmlBuilder;
}
protected boolean isDublinCoreRequest() {
return OAIRequestParser.DUBLIN_CORE_FORMAT.equalsIgnoreCase(getMetadataPrefix());
}
protected boolean isDDIRequest() {
return OAIRequestParser.DDI_FORMAT.equalsIgnoreCase(getMetadataPrefix());
}
protected void verifyCorrectMetadataPrefixForEGMS(GetRecordResult result) throws CannotDisseminateFormatException {
if (isDDIRequest() && result.isEGMS()) {
throw new CannotDisseminateFormatException(getMetadataPrefix());
}
}
public String getIdentifier() {
return getArgumentValue(OAIRequestParser.IDENTIFIER);
}
public String getMetadataPrefix() {
return getArgumentValue(OAIRequestParser.METADATA_PREFIX);
}
private void checkPresenceOfValidArguments() throws BadArgumentException {
final List<String> badArguments = new ArrayList<String>(2);
if (!arguments.containsKey(OAIRequestParser.IDENTIFIER)) {
badArguments.add(OAIRequestParser.IDENTIFIER);
}
if (!arguments.containsKey(OAIRequestParser.METADATA_PREFIX)) {
badArguments.add(OAIRequestParser.METADATA_PREFIX);
}
if (badArguments.size() > 0) {
throw new BadArgumentException(badArguments);
}
}
}
package com.nesstar.oaipmh.request;
import java.util.Map;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.result.IdentifyResult;
import com.nesstar.oaipmh.xml.IdentifyXmlBuilder;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.OAIException;
public class IdentifyRequest extends OAIRequest {
private static Logger logger = Logger.getLogger(IdentifyRequest.class);
public IdentifyRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() throws Exception {
logger.debug("Performing Identify request");
IdentifyResult oaiResult = new IdentifyResult(this);
OAIXmlBuilder xmlBuilder = new IdentifyXmlBuilder(oaiResult);
xmlBuilder.buildXML();
return xmlBuilder;
}
}
package com.nesstar.oaipmh.request;
import java.util.Map;
import com.nesstar.oaipmh.result.ListIdentifiersResult;
import com.nesstar.oaipmh.xml.ListIdentifiersXmlBuilder;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListIdentifiersRequest extends OAIRequest {
public ListIdentifiersRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() throws OAIException {
checkValidMetadataPrefix();
}
public OAIXmlBuilder performRequest() throws Exception {
ListIdentifiersResult result = new ListIdentifiersResult(this);
ListIdentifiersXmlBuilder xmlBuilder = new ListIdentifiersXmlBuilder(result);
xmlBuilder.buildXML();
return xmlBuilder;
}
}
package com.nesstar.oaipmh.request;
import java.util.Map;
import com.nesstar.oaipmh.result.ListMetadataFormatsResult;
import com.nesstar.oaipmh.xml.ListMetadataFormatsXmlBuilder;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListMetadataFormatsRequest extends OAIRequest {
public ListMetadataFormatsRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() throws Exception {
ListMetadataFormatsResult result = new ListMetadataFormatsResult(this);
OAIXmlBuilder xmlBuilder = new ListMetadataFormatsXmlBuilder(result);
xmlBuilder.buildXML();
return xmlBuilder;
}
}
package com.nesstar.oaipmh.request;
import java.util.Map;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListRecordsRequest extends OAIRequest {
public ListRecordsRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() {
return null;
}
}
package com.nesstar.oaipmh.request;
import java.util.Map;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListSetsRequest extends OAIRequest {
public ListSetsRequest(Map<String, String[]> arguments) throws OAIException {
super(arguments);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() {
return null;
}
}
package com.nesstar.oaipmh.request;
import java.util.Hashtable;
import java.util.Map;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.request.OAIRequestParser;
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(OAIRequestParser.VERB);
}
public String getIdentifier() {
return getArgumentValue(OAIRequestParser.IDENTIFIER);
}
public String getMetadataPrefix() {
return getArgumentValue(OAIRequestParser.METADATA_PREFIX);
}
public String getFrom() {
return getArgumentValue(OAIRequestParser.FROM);
}
public String getUntil() {
return getArgumentValue(OAIRequestParser.UNTIL);
}
public String getSet() {
return getArgumentValue(OAIRequestParser.SET);
}
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(OAIRequestParser.METADATA_PREFIX);
} else if (!OAIRequestParser.KNOWN_METADATA_PREFIXES.contains(prefix)) {
throw new CannotDisseminateFormatException(prefix);
}
}
public abstract OAIXmlBuilder performRequest() throws Exception;
}
package com.nesstar.oaipmh.request;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.exceptions.BadVerbException;
import com.nesstar.oaipmh.exceptions.OAIException;
public class OAIRequestParser {
private static Logger logger = Logger.getLogger(OAIRequestParser.class);
private HttpServletRequest request;
public final static String VERB = "verb";
public final static String IDENTIFIER = "identifier";
public final static String METADATA_PREFIX = "metadataPrefix";
public final static String FROM = "from";
public final static String UNTIL = "until";
public final static String SET = "set";
public static final String GET_RECORD = "GetRecord";
public static final String IDENTIFY = "Identify";
public static final String LIST_IDENTIFIERS = "ListIdentifiers";
public static final String LIST_METADATA_FORMATS = "ListMetadataFormats";
public static final String LIST_RECORDS = "ListRecords";
public static final String LIST_SETS = "ListSets";
public final static String DUBLIN_CORE_FORMAT = "oai_dc";
public final static String DDI_FORMAT = "oai_ddi";
public static final List<String> KNOWN_METADATA_PREFIXES = Arrays.asList(new String[] { "oai_dc", "oai_ddi" } );