Created
January 10, 2010 16:01
-
-
Save cbeer/273584 to your computer and use it in GitHub Desktop.
This is a Fedora PID generate that requests new PIDs from a webservice (like CDL's NOID)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a Fedora PID generate that requests new PIDs from a webservice (like CDL's NOID), e.g.: | |
* <module role="fedora.server.management.PIDGenerator" class="fedora.server.management.WSPIDGenerator"> | |
* <comment>The web services pid generator.</comment> | |
* <param name="url" value="http://example.org/nd/noidu_{nsID}?mint+1"/> | |
* <param name="connectionTimeout" value="5000" /> | |
* </module> | |
*/ | |
package fedora.server.management; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Map; | |
import org.apache.log4j.Logger; | |
import fedora.common.MalformedPIDException; | |
import fedora.common.PID; | |
import fedora.server.Module; | |
import fedora.server.Server; | |
import fedora.server.errors.ModuleInitializationException; | |
import fedora.server.storage.ConnectionPoolManager; | |
import org.apache.commons.httpclient.Header; | |
import org.apache.commons.httpclient.HttpClient; | |
import org.apache.commons.httpclient.HttpException; | |
import org.apache.commons.httpclient.HttpMethod; | |
import org.apache.commons.httpclient.methods.PostMethod; | |
/** | |
* A wrapper around the WSPIDGenerator class that casts it as a Module. | |
* | |
* @author Chris Beer <[email protected]> | |
*/ | |
public class WSPIDGenerator | |
extends Module | |
implements PIDGenerator { | |
/** Logger for this class. */ | |
private static final Logger LOG = | |
Logger.getLogger(WSPIDGenerator.class.getName()); | |
private HttpClient m_client; | |
private PID m_lastPid; | |
/** | |
* Constructs a WSPIDGenerator. | |
* | |
* @param moduleParameters | |
* A pre-loaded Map of name-value pairs comprising the intended | |
* configuration of this Module. | |
* @param server | |
* The <code>Server</code> instance. | |
* @param role | |
* The role this module fulfills, a java class name. | |
* @throws ModuleInitializationException | |
* If initilization values are invalid or initialization fails for | |
* some other reason. | |
*/ | |
public WSPIDGenerator(Map moduleParameters, Server server, String role) | |
throws ModuleInitializationException { | |
super(moduleParameters, server, role); | |
} | |
@Override | |
public void initModule() { | |
} | |
/** | |
* Get a reference to the HttpClient | |
*/ | |
@Override | |
public void postInitModule() throws ModuleInitializationException { | |
m_client = new HttpClient(); | |
m_client.getHttpConnectionManager().getParams().setConnectionTimeout(Integer.parseInt(getParameter("connectionTimeout"))); | |
} | |
public PID generatePID(String namespaceID) throws IOException { | |
HttpMethod method = null; | |
String url = getUrl(namespaceID); | |
method = new PostMethod(url); | |
//method.setFollowRedirects(true); | |
String responseBody = null; | |
String candidatePid = null; | |
try{ | |
m_client.executeMethod(method); | |
responseBody = method.getResponseBodyAsString(); | |
candidatePid = responseBody.substring(0, responseBody.indexOf("\n")); | |
m_lastPid = new PID(candidatePid); | |
} catch (HttpException he) { | |
System.err.println("Http error connecting to '" + url + "'"); | |
System.err.println(he.getMessage()); | |
} catch (IOException ioe){ | |
throw ioe; | |
} catch (MalformedPIDException e) { | |
throw new IOException(e.getMessage()); | |
} | |
return m_lastPid; | |
} | |
public PID getLastPID() throws IOException { | |
return m_lastPid; | |
} | |
public void neverGeneratePID(String pid) throws IOException { | |
//nop | |
} | |
private String getUrl(String namespaceID) { | |
String base = getParameter("url"); | |
String url = base.replaceAll("\\{nsID\\}", namespaceID); | |
return url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment