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

* Updated to nesstar-api 0.2

     * Better enforcement of MVC pattern (and renamed classes to make this more
    obvious)
     * Added a web based installation/configuration module that reads and
     * writes to a property file
     * If the server isn't configured, it won't work (but throw Internal
     * Server Error)
parent 23f0ff82
src/main/webapp/WEB-INF/nesstar-oai.properties
.classpath
.project
.settings
target
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>app-server</id>
<formats>
<format>war</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>package</scope>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
\ No newline at end of file
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>nesstar</id>
<formats>
<format>war</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>package</scope>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>activation:activation</exclude>
<exclude>commons-lang:commons-lang</exclude>
<exclude>commons-logging:commons-logging</exclude>
<exclude>commons-io:commons-io</exclude>
<exclude>commons-httpclient:commons-httpclient</exclude>
<exclude>commons-collections:commons-collections</exclude>
<exclude>commons-codec:commons-codec</exclude>
<exclude>ezmorph:ezmorph</exclude>
<exclude>log4j:log4j</exclude>
<exclude>nesstar_legacy_api:nesstar_legacy_api</exclude>
<exclude>rdf_parser:rdf_parser</exclude>
<exclude>stax-api:stax-api</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
\ No newline at end of file
package com.nesstar.oaipmh;
import java.io.IOException;
import java.net.URI;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.util.Properties;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NesstarObject;
import com.nesstar.api.NesstarTreeNode;
import com.nesstar.api.NotAuthorizedException;
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;
}
import com.nesstar.oaipmh.util.OAIProperties;
public class NesstarServer {
private static Logger logger = Logger.getLogger(NesstarServer.class);
private NesstarDB nesstarDB;
private Server server;
public NesstarServer() {
init();
}
public void init() {
OAIProperties props = OAIProperties.getInstance();
String serverURL = props.getURLforServerToDisseminate();
if (serverURL.startsWith("http://")) {
serverURL = serverURL.substring(7);
} else if (serverURL.startsWith("https://")) {
serverURL = serverURL.substring(8);
}
int serverPort = props.getPortForServerToDisseminate();
try {
nesstarDB = NesstarDBFactory.getInstance();
server = nesstarDB.getServer(serverURL, serverPort);
} catch (IOException ioex) {
logger.fatal("Unable to load Nesstar server", ioex);
throw new RuntimeException();
}
login();
}
private void login() {
if (server != null) {
OAIProperties props = OAIProperties.getInstance();
String username = props.getUsername();
String password = props.getPassword();
server.login(username, password);
}
}
public NesstarObject lookup(URI uri) throws NotAuthorizedException, IOException {
return nesstarDB.lookup(uri);
}
public NesstarTreeNode getTreeRoot() throws NotAuthorizedException, IOException {
return server.getTreeRoot();
}
public Server getServer() throws Exception {
return server;
}
public NesstarDB getNesstarDB() throws Exception {
return nesstarDB;
}
}
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;
}
}
......@@ -2,24 +2,23 @@ package com.nesstar.oaipmh.controller;
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.view.GetRecordView;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.view.builder.DdiRecordBuilder;
import com.nesstar.oaipmh.view.builder.DublinCoreRecordBuilder;
import com.nesstar.oaipmh.exceptions.BadArgumentException;
import com.nesstar.oaipmh.exceptions.CannotDisseminateFormatException;
import com.nesstar.oaipmh.exceptions.OAIException;
public class GetRecordController extends OAIRequest {
public class GetRecordController extends OAIController {
private static Logger logger = Logger.getLogger(GetRecordController.class);
public GetRecordController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public GetRecordController(RequestParser parser) throws OAIException {
super(parser);
}
protected void identifyInvalidArguments() throws OAIException {
......@@ -27,22 +26,22 @@ public class GetRecordController extends OAIRequest {
checkPresenceOfValidArguments();
}
public OAIXmlBuilder performRequest() throws Exception {
GetRecordResult result = new GetRecordResult(getIdentifier());
public OAIview performRequest() throws Exception {
GetRecordResult result = new GetRecordResult(request.getParameter(RequestParser.IDENTIFIER));
setRequestValues(result);
verifyCorrectMetadataPrefixForEGMS(result);
GetRecordXmlBuilder xmlBuilder = null;
GetRecordView view = new GetRecordView(result);
if (isDublinCoreRequest()) {
xmlBuilder = new DublinCoreRecordBuilder(result);
view.setRecordBuilder(new DublinCoreRecordBuilder(result));
} else if (isDDIRequest()) {
xmlBuilder = new DdiRecordBuilder(result);
view.setRecordBuilder(new DdiRecordBuilder(result));
} else {
logger.error("Unexpected illegal metadataPrefix format");
throw new Exception("Illegal metadata prefix!");
}
xmlBuilder.buildXML();
return xmlBuilder;
view.buildXML();
return view;
}
protected boolean isDublinCoreRequest() {
......@@ -59,20 +58,16 @@ public class GetRecordController extends OAIRequest {
}
}
public String getIdentifier() {
return getArgumentValue(RequestParser.IDENTIFIER);
}
public String getMetadataPrefix() {
return getArgumentValue(RequestParser.METADATA_PREFIX);
return request.getParameter(RequestParser.METADATA_PREFIX);
}
private void checkPresenceOfValidArguments() throws BadArgumentException {
final List<String> badArguments = new ArrayList<String>(2);
if (!arguments.containsKey(RequestParser.IDENTIFIER)) {
if ("".equals(request.getIdentifier())) {
badArguments.add(RequestParser.IDENTIFIER);
}
if (!arguments.containsKey(RequestParser.METADATA_PREFIX)) {
if ("".equals(getMetadataPrefix())) {
badArguments.add(RequestParser.METADATA_PREFIX);
}
if (badArguments.size() > 0) {
......
package com.nesstar.oaipmh.controller;
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.view.IdentifyView;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.exceptions.OAIException;
public class IdentifyController extends OAIRequest {
public class IdentifyController extends OAIController {
private static Logger logger = Logger.getLogger(IdentifyController.class);
public IdentifyController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public IdentifyController(RequestParser request) throws OAIException {
super(request);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() throws Exception {
public OAIview performRequest() throws Exception {
logger.debug("Performing Identify request");
IdentifyResult oaiResult = new IdentifyResult();
setRequestValues(oaiResult);
OAIXmlBuilder xmlBuilder = new IdentifyXmlBuilder(oaiResult);
OAIview xmlBuilder = new IdentifyView(oaiResult);
xmlBuilder.buildXML();
return xmlBuilder;
}
......
package com.nesstar.oaipmh.controller;
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.view.ListIdentifiersView;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListIdentifiersController extends OAIRequest {
public ListIdentifiersController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public class ListIdentifiersController extends OAIController {
public ListIdentifiersController(RequestParser parser) throws OAIException {
super(parser);
}
protected void identifyInvalidArguments() throws OAIException {
checkValidMetadataPrefix();
}
public OAIXmlBuilder performRequest() throws Exception {
ListIdentifiersResult result = new ListIdentifiersResult(getVerb(), getMetadataPrefix());
public OAIview performRequest() throws Exception {
ListIdentifiersResult result = new ListIdentifiersResult(request.getVerb(), request.getMetadataPrefix());
ListIdentifiersXmlBuilder xmlBuilder = new ListIdentifiersXmlBuilder(result);
ListIdentifiersView xmlBuilder = new ListIdentifiersView(result);
xmlBuilder.buildXML();
return xmlBuilder;
}
......
package com.nesstar.oaipmh.controller;
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.view.ListMetadataFormatsView;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListMetadataFormatsController extends OAIRequest {
public ListMetadataFormatsController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public class ListMetadataFormatsController extends OAIController {
public ListMetadataFormatsController(RequestParser request) throws OAIException {
super(request);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() throws Exception {
public OAIview performRequest() throws Exception {
ListMetadataFormatsResult result = new ListMetadataFormatsResult();
setRequestValues(result);
OAIXmlBuilder xmlBuilder = new ListMetadataFormatsXmlBuilder(result);
OAIview xmlBuilder = new ListMetadataFormatsView(result);
xmlBuilder.buildXML();
return xmlBuilder;
}
......
package com.nesstar.oaipmh.controller;
import java.util.Map;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListRecordsController extends OAIRequest {
public ListRecordsController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public class ListRecordsController extends OAIController {
public ListRecordsController(RequestParser request) throws OAIException {
super(request);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() {
public OAIview performRequest() {
return null;
}
}
package com.nesstar.oaipmh.controller;
import java.util.Map;
import com.nesstar.oaipmh.xml.OAIXmlBuilder;
import com.nesstar.oaipmh.view.OAIview;
import com.nesstar.oaipmh.exceptions.OAIException;
public class ListSetsController extends OAIRequest {
public ListSetsController(Map<String, String[]> arguments) throws OAIException {
super(arguments);
public class ListSetsController extends OAIController {
public ListSetsController(RequestParser request) throws OAIException {
super(request);
}
protected void identifyInvalidArguments() {
return;
}
public OAIXmlBuilder performRequest() {
public OAIview performRequest() {
return null;
}
}
package com.nesstar.oaipmh.controller;
import com.nesstar.oaipmh.result.OAIResult;
import com.nesstar.oaipmh.view.OAIview;
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 OAIController {
protected RequestParser request;
protected OAIResult result;
protected OAIController() throws OAIException {
}
protected OAIController(RequestParser request) throws OAIException {
this.request = request;
identifyInvalidArguments();
}
protected abstract void identifyInvalidArguments() throws OAIException;
protected void setRequestValues(OAIResult someResult) {
someResult.setVerb(request.getParameter(RequestParser.VERB));
someResult.setIdentifier(request.getParameter(RequestParser.IDENTIFIER));
someResult.setMetadataPrefix(request.getParameter(RequestParser.METADATA_PREFIX));
someResult.setDateRangeFrom(request.getParameter(RequestParser.FROM));
someResult.setDateRangeTo(request.getParameter(RequestParser.UNTIL));
someResult.setSetName(request.getParameter(RequestParser.SET));
}
protected void checkValidMetadataPrefix() throws CannotDisseminateFormatException, BadArgumentException {
final String prefix = request.getMetadataPrefix();
if ("".equals(prefix)) {
throw new BadArgumentException(RequestParser.METADATA_PREFIX);
} else if (!RequestParser.KNOWN_METADATA_PREFIXES.contains(prefix)) {
throw new CannotDisseminateFormatException(prefix);
}
}
public abstract OAIview performRequest() throws Exception;
public OAIResult getResult() {
return result;
}
}
package com.nesstar.oaipmh.controller;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.exceptions.BadVerbException;
import com.nesstar.oaipmh.exceptions.OAIException;
public class RequestParser {
private static Logger logger = Logger.getLogger(RequestParser.class);
private HttpServletRequest request;
public final static String VERB = "verb";
public final static String IDENTIFIER = "identifier";
public final static String METADATA_PREFIX = "metadataPrefix";
......@@ -31,45 +31,73 @@ public class RequestParser {
public static final List<String> KNOWN_METADATA_PREFIXES = Arrays.asList(new String[] { "oai_dc", "oai_ddi" } );
public RequestParser(HttpServletRequest request) {
this.request = request;
private Map<String, String[]> parameters;
public RequestParser(Map<String, String[]> parameters) {
retrieveParametersForMethod(parameters);
}
private void retrieveParametersForMethod(Map<String, String[]> incomingParameters) {
logger.debug("Retrieving arguments for OAI call");
parameters = new HashMap<String, String[]>();
Set<Entry<String, String[]>> entries = incomingParameters.entrySet();
for (Entry<String, String[]> pair : entries) {
parameters.put(pair.getKey(), pair.getValue());
}
}
public OAIRequest getOAIRequest() throws OAIException {
String verb = request.getParameter(VERB);
Map<String, String[]> arguments = retrieveArgumentsForMethod();
OAIRequest oaiRequest = identifyRequest(verb, arguments);
return oaiRequest;
public boolean isGetRecordRequest() {
return isRequestVerb(GET_RECORD);
}
private OAIRequest identifyRequest(String verb, Map<String, String[]> arguments) throws OAIException {
logger.debug("Identifying OAI request");
OAIRequest oaiRequest = null;
if (GET_RECORD.equalsIgnoreCase(verb)) {
oaiRequest = new GetRecordController(arguments);
} else if (IDENTIFY.equalsIgnoreCase(verb)) {
oaiRequest = new IdentifyController(arguments);
} else if (LIST_IDENTIFIERS.equalsIgnoreCase(verb)) {
oaiRequest = new ListIdentifiersController(arguments);
} else if (LIST_METADATA_FORMATS.equalsIgnoreCase(verb)) {
oaiRequest = new ListMetadataFormatsController(arguments);
} else if (LIST_RECORDS.equalsIgnoreCase(verb)) {
oaiRequest = new ListRecordsController(arguments);
} else if (LIST_SETS.equalsIgnoreCase(verb)) {
oaiRequest = new ListSetsController(arguments);
} else {
throw new BadVerbException(verb);
}
return oaiRequest;
public boolean isIdentifyRequest() {
return isRequestVerb(IDENTIFY);
}
@SuppressWarnings("unchecked")
private Map<String, String[]> retrieveArgumentsForMethod() {
logger.debug("Retrieving arguments for OAI call");
Map<String, String[]> arguments = request.getParameterMap();
return arguments;
public boolean isListIdentifiersRequest() {
return isRequestVerb(LIST_IDENTIFIERS);
}
public boolean isListMetadataFormatsRequest() {
return isRequestVerb(LIST_METADATA_FORMATS);
}
public boolean isListRecordsRequest() {
return isRequestVerb(LIST_RECORDS);
}
public boolean isListSetsRequest() {
return isRequestVerb(LIST_SETS);
}
private boolean isRequestVerb(String verbCandidate) {
String verb = getVerb();
return verb.equalsIgnoreCase(verbCandidate);
}
public String getVerb() {
return getParameter(VERB);
}
public String getIdentifier() {
return getParameter(IDENTIFIER);
}
public String getMetadataPrefix() {
return getParameter(METADATA_PREFIX);
}
public Map<String, String[]> getParameters() {
return parameters;
}
public String getParameter(String name) {
if (parameters.containsKey(name)) {
String[] values = parameters.get(name);
if (values.length > 0 && values[0] != null) {
return values[0];
}
}
return "";
}
}
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();
}
private String verb;
public BadVerbException(String verb) {
this.verb = verb;
}
@Override
protected void setCode() {
code = "badVerb";
}
@Override
public String getMessage() {
if (verb == null || "".equals(verb)) {