Refactored OaiPmhServer.java (by introducing QueryBuilder.java) to get rid of the dependency on oaiharvester2 and all of its dependencies.
git-svn-id: https://oai4j-client.svn.sourceforge.net/svnroot/oai4j-client/trunk@4 201c9376-5148-0410-b534-98494c770f31master
parent
d2661f445c
commit
b45b7bb966
@ -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();
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue