2008-03-14 10:00:56 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-03-13 14:43:08 +01:00
|
|
|
package se.kb.oai.pmh;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
2008-03-14 11:24:18 +01:00
|
|
|
import org.dom4j.Document;
|
|
|
|
import org.dom4j.io.SAXReader;
|
2008-03-13 14:43:08 +01:00
|
|
|
|
2008-03-14 11:24:18 +01:00
|
|
|
import se.kb.oai.OAIException;
|
2008-03-13 14:43:08 +01:00
|
|
|
|
|
|
|
/**
|
2008-03-14 16:45:59 +01:00
|
|
|
* Class that acts as a facade for an OAI-PMH server. Has methods
|
|
|
|
* that corresponds to the different verbs in the OAI-PMH specification
|
|
|
|
* and that will return appropriate objects based on the response.
|
|
|
|
* <p>
|
|
|
|
* For more about the different verbs, requests and responses in the OAI-PMH specification,
|
|
|
|
* see <a href="http://www.openarchives.org/OAI/openarchivesprotocol.html">
|
|
|
|
* http://www.openarchives.org/OAI/openarchivesprotocol.html</a>.
|
2008-03-13 14:43:08 +01:00
|
|
|
*
|
2008-03-14 16:45:59 +01:00
|
|
|
* @author Oskar Grenholm, National Library of Sweden
|
2008-03-13 14:43:08 +01:00
|
|
|
*/
|
|
|
|
public class OaiPmhServer {
|
|
|
|
|
2008-03-14 11:24:18 +01:00
|
|
|
private QueryBuilder builder;
|
|
|
|
private SAXReader reader;
|
2008-03-13 14:43:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an <code>OaiPmhServer</code> with the given base URL.
|
|
|
|
*
|
2008-03-14 16:45:59 +01:00
|
|
|
* @param url base URL that points to an OAI-PMH server
|
2008-03-13 14:43:08 +01:00
|
|
|
*/
|
|
|
|
public OaiPmhServer(String url) {
|
2008-03-14 11:24:18 +01:00
|
|
|
this.builder = new QueryBuilder(url);
|
|
|
|
this.reader = new SAXReader();
|
2008-03-13 14:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an <code>OaiPmhServer</code> with the given base URL.
|
|
|
|
*
|
2008-03-14 16:45:59 +01:00
|
|
|
* @param url base URL that points to an OAI-PMH server
|
2008-03-13 14:43:08 +01:00
|
|
|
*/
|
|
|
|
public OaiPmhServer(URL url) {
|
|
|
|
this(url.toString());
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Get the base URL to the OAI-PMH server.
|
|
|
|
*
|
|
|
|
* @return the base URL
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public String getBaseUrl() {
|
2008-03-14 16:45:59 +01:00
|
|
|
return builder.getBaseUrl();
|
2008-03-13 14:43:08 +01:00
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a GetRecord request to the OAI-PMH server with
|
|
|
|
* the specified parameters.
|
|
|
|
*
|
|
|
|
* @param identifier id to get a Record for
|
|
|
|
* @param metadataPrefix which metadata format
|
|
|
|
*
|
|
|
|
* @return the response from the server
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public Record getRecord(String identifier, String metadataPrefix) throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildGetRecordQuery(identifier, metadataPrefix);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new Record(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a request for the OAI-PMH server to Identify it self.
|
|
|
|
*
|
|
|
|
* @return the response from the server
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public Identification identify() throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildIdentifyQuery();
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new Identification(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a request to the OAI-PMH server that it should list all
|
|
|
|
* identifiers that has metadata in the specified format.
|
|
|
|
*
|
|
|
|
* @param metadataPrefix which metadata format
|
|
|
|
*
|
|
|
|
* @return a list of identifiers
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public IdentifiersList listIdentifiers(String metadataPrefix) throws OAIException {
|
2008-03-14 11:24:18 +01:00
|
|
|
return listIdentifiers(metadataPrefix, null, null, null);
|
2008-03-13 14:43:08 +01:00
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a request to the OAI-PMH server that it should list all
|
|
|
|
* identifiers that matches the given parameters.
|
|
|
|
*
|
|
|
|
* @param metadataPrefix which metadata format
|
|
|
|
* @param from a start date, optional (may be <code>null</code>)
|
|
|
|
* @param until a stop date, optional (may be <code>null</code>)
|
|
|
|
* @param set a specific set, optional (may be <code>null</code>)
|
|
|
|
*
|
|
|
|
* @return a list of identifiers
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-14 11:24:18 +01:00
|
|
|
public IdentifiersList listIdentifiers(String metadataPrefix, String from, String until, String set) throws OAIException {
|
2008-03-13 14:43:08 +01:00
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListIdentifiersQuery(metadataPrefix, from, until, set);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new IdentifiersList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* List next set of identifiers not returned in the previous response from
|
|
|
|
* a call to listIdentifiers().
|
|
|
|
*
|
|
|
|
* @param resumptionToken a resumption token returned from a previous call
|
|
|
|
*
|
|
|
|
* @return a list of identifiers
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public IdentifiersList listIdentifiers(ResumptionToken resumptionToken) throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListIdentifiersQuery(resumptionToken);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new IdentifiersList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a request for the OAI-PMH server to return a list of Records.
|
|
|
|
*
|
|
|
|
* @param metadataPrefix which metadata format
|
|
|
|
*
|
|
|
|
* @return a list of records
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public RecordsList listRecords(String metadataPrefix) throws OAIException {
|
2008-03-14 11:24:18 +01:00
|
|
|
return listRecords(metadataPrefix, null, null, null);
|
2008-03-13 14:43:08 +01:00
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Send a request for the OAI-PMH server to return a list of Records.
|
|
|
|
*
|
|
|
|
* @param metadataPrefix which metadata format
|
|
|
|
* @param from a start date, optional (may be <code>null</code>)
|
|
|
|
* @param until a stop date, optional (may be <code>null</code>)
|
|
|
|
* @param set a specific set, optional (may be <code>null</code>)
|
|
|
|
*
|
|
|
|
* @return a lsit of records
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-14 11:24:18 +01:00
|
|
|
public RecordsList listRecords(String metadataPrefix, String from, String until, String set) throws OAIException {
|
2008-03-13 14:43:08 +01:00
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListRecordsQuery(metadataPrefix, from, until, set);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new RecordsList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* List next set of records not returned in the previous response from
|
|
|
|
* a call to listRecords().
|
|
|
|
*
|
|
|
|
* @param resumptionToken a resumption token returned from a previous call
|
|
|
|
*
|
|
|
|
* @return a list of records
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public RecordsList listRecords(ResumptionToken resumptionToken) throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListRecordsQuery(resumptionToken);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new RecordsList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Ask the OAI-PMH server to list all metadata formats it holds.
|
|
|
|
*
|
|
|
|
* @return a list of available metadata formats
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public MetadataFormatsList listMetadataFormats() throws OAIException {
|
|
|
|
return listMetadataFormats(null);
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* Ask the OAI-PMH server to list all metadata formats it holds
|
|
|
|
* for the specified identifier.
|
|
|
|
*
|
|
|
|
* @return a list of available metadata formats
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public MetadataFormatsList listMetadataFormats(String identifier) throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListMetadataFormatsQuery(identifier);
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new MetadataFormatsList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* List all sets the OAI-PMH server has.
|
|
|
|
*
|
|
|
|
* @return a list of sets
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-13 14:43:08 +01:00
|
|
|
public SetsList listSets() throws OAIException {
|
|
|
|
try {
|
2008-03-14 11:24:18 +01:00
|
|
|
String query = builder.buildListSetsQuery();
|
|
|
|
Document document = reader.read(query);
|
|
|
|
return new SetsList(document);
|
2008-03-14 10:00:56 +01:00
|
|
|
} catch (ErrorResponseException e) {
|
2008-03-13 14:43:08 +01:00
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new OAIException(e);
|
|
|
|
}
|
|
|
|
}
|
2008-03-14 11:24:18 +01:00
|
|
|
|
2008-03-14 16:45:59 +01:00
|
|
|
/**
|
|
|
|
* List next set of sets not returned in the previous response from
|
|
|
|
* a call to listSets().
|
|
|
|
*
|
|
|
|
* @param resumptionToken
|
|
|
|
*
|
|
|
|
* @return a list of sets
|
|
|
|
* @throws OAIException
|
|
|
|
*/
|
2008-03-14 11:24:18 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2008-03-13 14:43:08 +01:00
|
|
|
}
|