Skip to content

Commit

Permalink
Changing out PidMinter for generic Java 8 Supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jun 25, 2015
1 parent f13ca21 commit a240d99
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 93 deletions.
Expand Up @@ -598,7 +598,7 @@ private String mintNewPid(final String slug) {
if (slug != null && !slug.isEmpty()) {
pid = slug;
} else {
pid = pidMinter.mintPid();
pid = pidMinter.get();
}
// reverse translate the proffered or created identifier
LOGGER.trace("Using external identifier {} to create new resource.", pid);
Expand Down
Expand Up @@ -29,6 +29,7 @@

import java.net.URISyntaxException;
import java.util.Date;
import java.util.function.Supplier;

import javax.jcr.ItemExistsException;
import javax.jcr.Node;
Expand All @@ -50,11 +51,11 @@

import org.fcrepo.http.commons.api.rdf.HttpResourceConverter;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.mint.PidMinter;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.services.VersionService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class FedoraNodesTest {
private HttpServletResponse mockResponse;

@Mock
private PidMinter mockPidMinter;
private Supplier<String> mockPidMinter;

@Mock
private Date mockDate;
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.fcrepo.http.commons;

import java.util.function.Supplier;

import javax.inject.Inject;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
Expand All @@ -25,11 +27,11 @@
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.mint.PidMinter;
import org.fcrepo.kernel.services.BinaryService;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.services.VersionService;

import org.jvnet.hk2.annotations.Optional;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -98,7 +100,7 @@ public class AbstractResource {
* A resource that can mint new Fedora PIDs.
*/
@Autowired
protected PidMinter pidMinter;
protected Supplier<String> pidMinter;

/**
* Convert a JAX-RS list of PathSegments to a JCR path
Expand Down
Expand Up @@ -19,11 +19,13 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.function.Supplier;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.mint.PidMinter;
import org.fcrepo.kernel.services.NodeService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -42,7 +44,7 @@ public class AbstractResourceTest {
private NodeService mockNodes;

@Mock
private PidMinter mockPids;
private Supplier<String> mockPids;

@Mock
private UriInfo mockUris;
Expand Down
Expand Up @@ -35,6 +35,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
Expand All @@ -50,7 +51,7 @@
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import org.fcrepo.mint.PidMinter;

import org.fcrepo.kernel.impl.services.AbstractService;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
Expand All @@ -61,6 +62,7 @@
import org.fcrepo.kernel.impl.rdf.converters.ValueConverter;
import org.fcrepo.kernel.impl.utils.NodePropertiesTools;
import org.fcrepo.mint.UUIDPathMinter;

import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;

Expand Down Expand Up @@ -106,7 +108,7 @@ public class JcrRdfTools {

private static final Model m = createDefaultModel();

private static final PidMinter pidMinter = new UUIDPathMinter();
private static final Supplier<String> pidMinter = new UUIDPathMinter();

/**
* Constructor with even more context.
Expand Down Expand Up @@ -409,7 +411,7 @@ private Resource getSkolemizedResource(final IdentifierConverter<Resource, Fedor

if (!skolemizedBnodeMap.containsKey(id)) {
jcrTools.findOrCreateNode(session, skolemizedPrefix());
final String pid = pidMinter.mintPid();
final String pid = pidMinter.get();
final String path = skolemizedPrefix() + pid;
final Node preexistingNode = getClosestExistingAncestor(session, path);
final Node orCreateNode = jcrTools.findOrCreateNode(session, path);
Expand Down
Expand Up @@ -27,14 +27,13 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;

import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.utils.EventType;

import org.fcrepo.mint.PidMinter;
import org.fcrepo.mint.UUIDPathMinter;

import com.google.common.base.Function;
Expand All @@ -56,7 +55,7 @@ public class FedoraEvent {
private Set<Integer> eventTypes = new HashSet<>();
private Set<String> eventProperties = new HashSet<>();

private static final PidMinter pidMinter = new UUIDPathMinter();
private static final Supplier<String> pidMinter = new UUIDPathMinter();

/**
* Wrap a JCR Event with our FedoraEvent decorators
Expand All @@ -65,7 +64,7 @@ public class FedoraEvent {
*/
public FedoraEvent(final Event e) {
checkArgument(e != null, "null cannot support a FedoraEvent!");
eventID = pidMinter.mintPid();
eventID = pidMinter.get();
this.e = e;
}

Expand Down
41 changes: 21 additions & 20 deletions fcrepo-mint/src/main/java/org/fcrepo/mint/HttpPidMinter.java
Expand Up @@ -16,16 +16,16 @@
package org.fcrepo.mint;

import static org.slf4j.LoggerFactory.getLogger;
import static org.apache.commons.lang.StringUtils.isBlank;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import org.slf4j.Logger;

import com.codahale.metrics.annotation.Timed;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.function.Supplier;

import org.w3c.dom.Document;

Expand Down Expand Up @@ -59,8 +59,9 @@
* @author escowles
* @since 04/28/2014
*/
public class HttpPidMinter implements PidMinter {
public class HttpPidMinter implements Supplier<String> {

private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
private static final Logger LOGGER = getLogger(HttpPidMinter.class);
protected final String url;
protected final String method;
Expand All @@ -70,6 +71,7 @@ public class HttpPidMinter implements PidMinter {
private XPathExpression xpath;

protected HttpClient client;
private final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

/**
* Create a new HttpPidMinter.
Expand All @@ -88,17 +90,17 @@ public class HttpPidMinter implements PidMinter {
public HttpPidMinter( final String url, final String method, final String username,
final String password, final String regex, final String xpath ) {

checkArgument( !isBlank(url), "Minter URL must be specified!" );
checkArgument( !isNullOrEmpty(url), "Minter URL must be specified!" );

this.url = url;
this.method = (method == null ? "post" : method);
this.username = username;
this.password = password;
this.regex = regex;
if ( !isBlank(xpath) ) {
if ( !isNullOrEmpty(xpath) ) {
try {
this.xpath = XPathFactory.newInstance().newXPath().compile(xpath);
} catch ( XPathException ex ) {
} catch ( final XPathException ex ) {
LOGGER.warn("Error parsing xpath ({}): {}", xpath, ex );
throw new IllegalArgumentException("Error parsing xpath" + xpath, ex);
}
Expand All @@ -111,9 +113,8 @@ public HttpPidMinter( final String url, final String method, final String userna
* @return the setup of authentication
**/
protected HttpClient buildClient() {
HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties().setConnectionManager(
new PoolingHttpClientConnectionManager());
if (!isBlank(username) && !isBlank(password)) {
HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties().setConnectionManager(connManager);
if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
final URI uri = URI.create(url);
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
Expand All @@ -127,10 +128,10 @@ protected HttpClient buildClient() {
* Instantiate a request object based on the method variable.
**/
private HttpUriRequest minterRequest() {
switch (method) {
case "GET": case "get":
switch (method.toUpperCase()) {
case "GET":
return new HttpGet(url);
case "PUT": case "put":
case "PUT":
return new HttpPut(url);
default:
return new HttpPost(url);
Expand All @@ -146,12 +147,12 @@ private HttpUriRequest minterRequest() {
**/
protected String responseToPid( final String responseText ) throws IOException {
LOGGER.debug("responseToPid({})", responseText);
if ( !isBlank(regex) ) {
if ( !isNullOrEmpty(regex) ) {
return responseText.replaceFirst(regex,"");
} else if ( xpath != null ) {
try {
return xpath( responseText, xpath );
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
} catch (ParserConfigurationException | SAXException | XPathExpressionException e) {
throw new IOException(e);
}
} else {
Expand All @@ -164,7 +165,7 @@ protected String responseToPid( final String responseText ) throws IOException {
**/
private static String xpath( final String xml, final XPathExpression xpath )
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
final Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
return xpath.evaluate(doc);
}
Expand All @@ -175,17 +176,17 @@ private static String xpath( final String xml, final XPathExpression xpath )
*/
@Timed
@Override
public String mintPid() {
public String get() {
try {
LOGGER.debug("mintPid()");
final HttpResponse resp = client.execute( minterRequest() );
return responseToPid( EntityUtils.toString(resp.getEntity()) );
} catch ( IOException ex ) {
} catch ( final IOException ex ) {
LOGGER.warn("Error minting pid from {}: {}", url, ex);
throw new PidMinterException("Error minting pid", ex);
} catch ( Exception ex ) {
throw new PidMintingException("Error minting pid", ex);
} catch ( final Exception ex ) {
LOGGER.warn("Error processing minter response", ex);
throw new PidMinterException("Error processing minter response", ex);
throw new PidMintingException("Error processing minter response", ex);
}
}
}
34 changes: 0 additions & 34 deletions fcrepo-mint/src/main/java/org/fcrepo/mint/PidMinter.java

This file was deleted.

Expand Up @@ -21,23 +21,23 @@
* @since 2015-06-05
* @author escowles
*/
public class PidMinterException extends RuntimeException {
public class PidMintingException extends RuntimeException {

private static final long serialVersionUID = 1L;

/**
* Constructor with message.
* @param msg Exception message.
**/
public PidMinterException(final String msg) {
public PidMintingException(final String msg) {
super(msg);
}

/**
* Constructor with cause.
* @param cause Original exception.
**/
public PidMinterException(final Throwable cause) {
public PidMintingException(final Throwable cause) {
super(cause);
}

Expand All @@ -46,7 +46,7 @@ public PidMinterException(final Throwable cause) {
* @param msg Exception message.
* @param cause Original exception.
**/
public PidMinterException(final String msg, final Throwable cause) {
public PidMintingException(final String msg, final Throwable cause) {
super(msg, cause);
}
}

0 comments on commit a240d99

Please sign in to comment.