diff --git a/pom.xml b/pom.xml index e1b4d1d..f3b14fc 100644 --- a/pom.xml +++ b/pom.xml @@ -15,21 +15,11 @@ 4.1 test - - org.oclc - oaiharvester2 - 0.1.10 - dom4j dom4j 1.6.1 - - log4j - log4j - 1.2.14 - jaxen jaxen diff --git a/src/main/java/se/kb/oai/pmh/Identification.java b/src/main/java/se/kb/oai/pmh/Identification.java index 7d89b9b..502984a 100644 --- a/src/main/java/se/kb/oai/pmh/Identification.java +++ b/src/main/java/se/kb/oai/pmh/Identification.java @@ -19,9 +19,9 @@ package se.kb.oai.pmh; import java.util.LinkedList; import java.util.List; +import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; -import org.w3c.dom.Document; public class Identification extends ResponseBase { diff --git a/src/main/java/se/kb/oai/pmh/IdentifiersList.java b/src/main/java/se/kb/oai/pmh/IdentifiersList.java index df51b2d..3b923dd 100644 --- a/src/main/java/se/kb/oai/pmh/IdentifiersList.java +++ b/src/main/java/se/kb/oai/pmh/IdentifiersList.java @@ -19,8 +19,8 @@ package se.kb.oai.pmh; import java.util.LinkedList; import java.util.List; +import org.dom4j.Document; import org.dom4j.Node; -import org.w3c.dom.Document; public class IdentifiersList extends ResponseBase { diff --git a/src/main/java/se/kb/oai/pmh/MetadataFormatsList.java b/src/main/java/se/kb/oai/pmh/MetadataFormatsList.java index 438dfb3..39695e6 100644 --- a/src/main/java/se/kb/oai/pmh/MetadataFormatsList.java +++ b/src/main/java/se/kb/oai/pmh/MetadataFormatsList.java @@ -19,8 +19,8 @@ package se.kb.oai.pmh; import java.util.LinkedList; import java.util.List; +import org.dom4j.Document; import org.dom4j.Node; -import org.w3c.dom.Document; public class MetadataFormatsList extends ResponseBase { diff --git a/src/main/java/se/kb/oai/pmh/OaiPmhServer.java b/src/main/java/se/kb/oai/pmh/OaiPmhServer.java index 559e8f6..73191f9 100644 --- a/src/main/java/se/kb/oai/pmh/OaiPmhServer.java +++ b/src/main/java/se/kb/oai/pmh/OaiPmhServer.java @@ -18,14 +18,10 @@ package se.kb.oai.pmh; import java.net.URL; -import se.kb.oai.OAIException; +import org.dom4j.Document; +import org.dom4j.io.SAXReader; -import ORG.oclc.oai.harvester2.verb.GetRecord; -import ORG.oclc.oai.harvester2.verb.Identify; -import ORG.oclc.oai.harvester2.verb.ListIdentifiers; -import ORG.oclc.oai.harvester2.verb.ListMetadataFormats; -import ORG.oclc.oai.harvester2.verb.ListRecords; -import ORG.oclc.oai.harvester2.verb.ListSets; +import se.kb.oai.OAIException; /** * Class that acts as a facade for an OAI-PMH server. @@ -38,7 +34,8 @@ import ORG.oclc.oai.harvester2.verb.ListSets; */ public class OaiPmhServer { - private String baseurl; + private QueryBuilder builder; + private SAXReader reader; /** * Creates an OaiPmhServer with the given base URL. @@ -46,7 +43,8 @@ public class OaiPmhServer { * @param url Base URL that points to an OAI-PMH server. */ public OaiPmhServer(String url) { - this.baseurl = url; + this.builder = new QueryBuilder(url); + this.reader = new SAXReader(); } /** @@ -59,13 +57,14 @@ public class OaiPmhServer { } public String getBaseUrl() { - return baseurl; + return builder.getBasesurl(); } public Record getRecord(String identifier, String metadataPrefix) throws OAIException { try { - GetRecord getRecord = new GetRecord(baseurl, identifier, metadataPrefix); - return new Record(getRecord.getDocument()); + String query = builder.buildGetRecordQuery(identifier, metadataPrefix); + Document document = reader.read(query); + return new Record(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -75,8 +74,9 @@ public class OaiPmhServer { public Identification identify() throws OAIException { try { - Identify identify = new Identify(baseurl); - return new Identification(identify.getDocument()); + String query = builder.buildIdentifyQuery(); + Document document = reader.read(query); + return new Identification(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -85,13 +85,14 @@ public class OaiPmhServer { } public IdentifiersList listIdentifiers(String metadataPrefix) throws OAIException { - return listIdentifiers(null, null, null, metadataPrefix); + return listIdentifiers(metadataPrefix, null, null, null); } - public IdentifiersList listIdentifiers(String from, String until, String set, String metadataPrefix) throws OAIException { + public IdentifiersList listIdentifiers(String metadataPrefix, String from, String until, String set) throws OAIException { try { - ListIdentifiers list = new ListIdentifiers(baseurl, from, until, set, metadataPrefix); - return new IdentifiersList(list.getDocument()); + String query = builder.buildListIdentifiersQuery(metadataPrefix, from, until, set); + Document document = reader.read(query); + return new IdentifiersList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -101,8 +102,9 @@ public class OaiPmhServer { public IdentifiersList listIdentifiers(ResumptionToken resumptionToken) throws OAIException { try { - ListIdentifiers list = new ListIdentifiers(baseurl, resumptionToken.getId()); - return new IdentifiersList(list.getDocument()); + String query = builder.buildListIdentifiersQuery(resumptionToken); + Document document = reader.read(query); + return new IdentifiersList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -111,13 +113,14 @@ public class OaiPmhServer { } public RecordsList listRecords(String metadataPrefix) throws OAIException { - return listRecords(null, null, null, metadataPrefix); + return listRecords(metadataPrefix, null, null, null); } - public RecordsList listRecords(String from, String until, String set, String metadataPrefix) throws OAIException { + public RecordsList listRecords(String metadataPrefix, String from, String until, String set) throws OAIException { try { - ListRecords list = new ListRecords(baseurl, from, until, set, metadataPrefix); - return new RecordsList(list.getDocument()); + String query = builder.buildListRecordsQuery(metadataPrefix, from, until, set); + Document document = reader.read(query); + return new RecordsList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -127,8 +130,9 @@ public class OaiPmhServer { public RecordsList listRecords(ResumptionToken resumptionToken) throws OAIException { try { - ListRecords list = new ListRecords(baseurl, resumptionToken.getId()); - return new RecordsList(list.getDocument()); + String query = builder.buildListRecordsQuery(resumptionToken); + Document document = reader.read(query); + return new RecordsList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -142,8 +146,9 @@ public class OaiPmhServer { public MetadataFormatsList listMetadataFormats(String identifier) throws OAIException { try { - ListMetadataFormats list = new ListMetadataFormats(baseurl, identifier); - return new MetadataFormatsList(list.getDocument()); + String query = builder.buildListMetadataFormatsQuery(identifier); + Document document = reader.read(query); + return new MetadataFormatsList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { @@ -153,12 +158,25 @@ public class OaiPmhServer { public SetsList listSets() throws OAIException { try { - ListSets list = new ListSets(baseurl); - return new SetsList(list.getDocument()); + String query = builder.buildListSetsQuery(); + Document document = reader.read(query); + return new SetsList(document); } catch (ErrorResponseException e) { throw e; } catch (Exception e) { throw new OAIException(e); } } + + public SetsList listSets(ResumptionToken resumptionToken) throws OAIException { + try { + String query = builder.buildListSetsQuery(resumptionToken); + Document document = reader.read(query); + return new SetsList(document); + } catch (ErrorResponseException e) { + throw e; + } catch (Exception e) { + throw new OAIException(e); + } + } } diff --git a/src/main/java/se/kb/oai/pmh/QueryBuilder.java b/src/main/java/se/kb/oai/pmh/QueryBuilder.java new file mode 100644 index 0000000..b957643 --- /dev/null +++ b/src/main/java/se/kb/oai/pmh/QueryBuilder.java @@ -0,0 +1,119 @@ +/* + * Copyright 2008 National Library of Sweden + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package se.kb.oai.pmh; + +public class QueryBuilder { + + private enum Verb { Identify, GetRecord, ListIdentifiers, ListMetadataFormats, ListRecords, ListSets } + + private static final String VERB = "verb"; + private static final String QUESTION_MARK = "?"; + private static final String AMPERSAND = "&"; + private static final String EQUAL_SIGN = "="; + + private static final String IDENTIFIER = "identifier"; + private static final String METADATA_PREFIX = "metadataPrefix"; + private static final String FROM = "from"; + private static final String UNTIL = "until"; + private static final String SET = "set"; + private static final String RESUMPTION_TOKEN = "resumptionToken"; + + private String basesurl; + + public QueryBuilder(String baseurl) { + this.basesurl = baseurl + QUESTION_MARK + VERB + EQUAL_SIGN; + } + + public String getBasesurl() { + return basesurl; + } + + public String buildGetRecordQuery(String identifier, String metadataPrefix) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.GetRecord); + buffer.append(AMPERSAND).append(IDENTIFIER).append(EQUAL_SIGN).append(identifier); + buffer.append(AMPERSAND).append(METADATA_PREFIX).append(EQUAL_SIGN).append(metadataPrefix); + return buffer.toString(); + } + + public String buildIdentifyQuery() { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.Identify); + return buffer.toString(); + } + + public String buildListIdentifiersQuery(String metadataPrefix, String from, String until, String set) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListIdentifiers); + buffer.append(AMPERSAND).append(METADATA_PREFIX).append(EQUAL_SIGN).append(metadataPrefix); + if (from != null) + buffer.append(AMPERSAND).append(FROM).append(EQUAL_SIGN).append(from); + if (until != null) + buffer.append(AMPERSAND).append(UNTIL).append(EQUAL_SIGN).append(until); + if (set != null) + buffer.append(AMPERSAND).append(SET).append(EQUAL_SIGN).append(set); + return buffer.toString(); + } + + public String buildListIdentifiersQuery(ResumptionToken token) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListIdentifiers); + buffer.append(AMPERSAND).append(RESUMPTION_TOKEN).append(EQUAL_SIGN).append(token.getId()); + return buffer.toString(); + } + + public String buildListMetadataFormatsQuery(String identifier) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListMetadataFormats); + if (identifier != null) + buffer.append(AMPERSAND).append(IDENTIFIER).append(EQUAL_SIGN).append(identifier); + return buffer.toString(); + } + + public String buildListRecordsQuery(String metadataPrefix, String from, String until, String set) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListRecords); + buffer.append(AMPERSAND).append(METADATA_PREFIX).append(EQUAL_SIGN).append(metadataPrefix); + if (from != null) + buffer.append(AMPERSAND).append(FROM).append(EQUAL_SIGN).append(from); + if (until != null) + buffer.append(AMPERSAND).append(UNTIL).append(EQUAL_SIGN).append(until); + if (set != null) + buffer.append(AMPERSAND).append(SET).append(EQUAL_SIGN).append(set); + return buffer.toString(); + } + + public String buildListRecordsQuery(ResumptionToken token) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListRecords); + buffer.append(AMPERSAND).append(RESUMPTION_TOKEN).append(EQUAL_SIGN).append(token.getId()); + return buffer.toString(); + } + + public String buildListSetsQuery() { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListSets); + return buffer.toString(); + } + + public String buildListSetsQuery(ResumptionToken token) { + StringBuffer buffer = new StringBuffer(basesurl); + buffer.append(Verb.ListSets); + buffer.append(AMPERSAND).append(RESUMPTION_TOKEN).append(EQUAL_SIGN).append(token.getId()); + return buffer.toString(); + } +} diff --git a/src/main/java/se/kb/oai/pmh/Record.java b/src/main/java/se/kb/oai/pmh/Record.java index b6a327f..2b0438d 100644 --- a/src/main/java/se/kb/oai/pmh/Record.java +++ b/src/main/java/se/kb/oai/pmh/Record.java @@ -1,29 +1,26 @@ /* - * Created on 17 Aug 2007 - * - * Copyright (C) 2007 Royal Library of Sweden. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * Copyright 2008 National Library of Sweden + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + package se.kb.oai.pmh; import java.io.IOException; +import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; -import org.w3c.dom.Document; import se.kb.xml.XMLUtils; import se.kb.xml.XPathWrapper; diff --git a/src/main/java/se/kb/oai/pmh/RecordsList.java b/src/main/java/se/kb/oai/pmh/RecordsList.java index 11c34c5..b4b1e3d 100644 --- a/src/main/java/se/kb/oai/pmh/RecordsList.java +++ b/src/main/java/se/kb/oai/pmh/RecordsList.java @@ -1,29 +1,26 @@ /* - * Created on 17 Aug 2007 - * - * Copyright (C) 2007 Royal Library of Sweden. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * Copyright 2008 National Library of Sweden + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + package se.kb.oai.pmh; import java.util.LinkedList; import java.util.List; +import org.dom4j.Document; import org.dom4j.Node; -import org.w3c.dom.Document; public class RecordsList extends ResponseBase { diff --git a/src/main/java/se/kb/oai/pmh/ResponseBase.java b/src/main/java/se/kb/oai/pmh/ResponseBase.java index 900ecd1..2583c34 100644 --- a/src/main/java/se/kb/oai/pmh/ResponseBase.java +++ b/src/main/java/se/kb/oai/pmh/ResponseBase.java @@ -1,27 +1,23 @@ /* - * Created on 17 Aug 2007 - * - * Copyright (C) 2007 Royal Library of Sweden. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * Copyright 2008 National Library of Sweden + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + package se.kb.oai.pmh; +import org.dom4j.Document; import org.dom4j.Element; -import org.dom4j.io.DOMReader; -import org.w3c.dom.Document; import se.kb.xml.XPathWrapper; @@ -41,8 +37,7 @@ public abstract class ResponseBase { protected ResumptionToken resumptionToken; public ResponseBase(Document document) throws ErrorResponseException { - DOMReader reader = new DOMReader(); - Element root = reader.read(document).getRootElement(); + Element root = document.getRootElement(); this.xpath = new XPathWrapper(root); xpath.addNamespace(OAI_NS_PREFIX, OAI_NS_URI); diff --git a/src/main/java/se/kb/oai/pmh/SetsList.java b/src/main/java/se/kb/oai/pmh/SetsList.java index c6f2592..e265174 100644 --- a/src/main/java/se/kb/oai/pmh/SetsList.java +++ b/src/main/java/se/kb/oai/pmh/SetsList.java @@ -19,8 +19,8 @@ package se.kb.oai.pmh; import java.util.LinkedList; import java.util.List; +import org.dom4j.Document; import org.dom4j.Node; -import org.w3c.dom.Document; public class SetsList extends ResponseBase { diff --git a/src/test/java/se/kb/oai/ore/ORETest.java b/src/test/java/se/kb/oai/ore/ORETest.java index 89cf242..15c2fe7 100644 --- a/src/test/java/se/kb/oai/ore/ORETest.java +++ b/src/test/java/se/kb/oai/ore/ORETest.java @@ -18,4 +18,6 @@ public class ORETest { ResourceMap map = new ResourceMap(new URI("http://test.kb.se/rem/")); assertEquals("http://test.kb.se/rem/#aggregation", map.getAggregation().getId().toString()); } + + } diff --git a/src/test/java/se/kb/oai/pmh/PMHTest.java b/src/test/java/se/kb/oai/pmh/PMHTest.java new file mode 100644 index 0000000..de274c7 --- /dev/null +++ b/src/test/java/se/kb/oai/pmh/PMHTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2008 National Library of Sweden + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package se.kb.oai.pmh; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class PMHTest { + + private QueryBuilder builder; + private String baseurl = "http://lauren.kb.se:8080/oaiprovider"; + + @Before + public void clearCalculator() { + this.builder = new QueryBuilder(baseurl); + } + + @Test + public void testBuildIdentifyQuery() + { + assertEquals(baseurl + "?verb=Identify", builder.buildIdentifyQuery()); + } + + @Test + public void testBuildGetRecordyQuery() + { + assertEquals(baseurl + "?verb=GetRecord&identifier=kb:1&metadataPrefix=mods", builder.buildGetRecordQuery("kb:1", "mods")); + } + + @Test + public void testBuildListIdentifiersQuery() { + assertEquals(baseurl + "?verb=ListIdentifiers&metadataPrefix=mods&from=2001-01-01&until=2002-02-02", builder.buildListIdentifiersQuery("mods", "2001-01-01", "2002-02-02", null)); + } + + @Test + public void testBuildListMetadataFormatsQuery() { + assertEquals(baseurl + "?verb=ListMetadataFormats&identifier=kb:1", builder.buildListMetadataFormatsQuery("kb:1")); + } + + @Test + public void testBuildListRecordsQuery() { + assertEquals(baseurl + "?verb=ListRecords&metadataPrefix=mods&from=2001-01-01&until=2002-02-02&set=sot", builder.buildListRecordsQuery("mods", "2001-01-01", "2002-02-02", "sot")); + } +}