Skip to content

Commit

Permalink
Minor fcrepo-http-commons code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f authored and Andrew Woods committed Jun 30, 2015
1 parent 1f29124 commit e8b3f63
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 190 deletions.
Expand Up @@ -34,13 +34,11 @@

import org.jvnet.hk2.annotations.Optional;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.eventbus.EventBus;

/**
* Superclass for Fedora JAX-RS Resources, providing convenience fields
* and methods.
* Superclass for Fedora JAX-RS Resources, providing convenience fields and methods.
*
* @author ajs6f
*/
Expand All @@ -65,31 +63,31 @@ public class AbstractResource {
@Context
protected HttpHeaders headers;

@Autowired
@Inject
protected SessionFactory sessions;

/**
* The fcrepo node service
* The JCR node service
*/
@Autowired
@Inject
protected NodeService nodeService;

/**
* The fcrepo object service
* The repository object service
*/
@Autowired
@Inject
protected ContainerService containerService;

/**
* The fcrepo datastream service
* The bitstream service
*/
@Autowired
@Inject
protected BinaryService binaryService;

/**
* The fcrepo version service
* The version service
*/
@Autowired
@Inject
protected VersionService versionService;

@Inject
Expand All @@ -99,7 +97,7 @@ public class AbstractResource {
/**
* A resource that can mint new Fedora PIDs.
*/
@Autowired
@Inject
protected Supplier<String> pidMinter;

/**
Expand All @@ -116,10 +114,7 @@ public static final String toPath(final IdentifierConverter<Resource, FedoraReso

final String path = idTranslator.asString(resource);

if (path.isEmpty()) {
return "/";
}
return path;
return path.isEmpty() ? "/" : path;
}

}
Expand Up @@ -15,11 +15,9 @@
*/
package org.fcrepo.http.commons.api.rdf;

import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Lists.newArrayList;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static java.util.Collections.singleton;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.replaceOnce;
import static org.fcrepo.kernel.FedoraJcrTypes.FCR_METADATA;
Expand All @@ -35,6 +33,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,7 +62,6 @@
import org.springframework.context.ApplicationContext;

import com.google.common.base.Converter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.hp.hpl.jena.rdf.model.Resource;

Expand Down Expand Up @@ -136,7 +134,7 @@ protected FedoraResource doForward(final Resource resource) {
if (TombstoneImpl.hasMixin(preexistingNode)) {
throw new TombstoneException(new TombstoneImpl(preexistingNode));
}
} catch (RepositoryException inner) {
} catch (final RepositoryException inner) {
LOGGER.debug("Error checking for parent tombstones", inner);
}
}
Expand Down Expand Up @@ -379,14 +377,10 @@ private String doBackwardPathOnly(final FedoraResource resource) {
protected void resetTranslationChain() {
if (translationChain == null) {
translationChain = getTranslationChain();

final Converter<String,String> transactionIdentifierConverter = new TransactionIdentifierConverter(session);

@SuppressWarnings("unchecked")
final ImmutableList<Converter<String, String>> chain = copyOf(
concat(newArrayList(transactionIdentifierConverter),
translationChain));
setTranslationChain(chain);
final List<Converter<String, String>> newChain =
new ArrayList<>(singleton(new TransactionIdentifierConverter(session)));
newChain.addAll(translationChain);
setTranslationChain(newChain);
}
}

Expand All @@ -403,9 +397,8 @@ private void setTranslationChain(final List<Converter<String, String>> chained)
}


private static final List<Converter<String, String>> minimalTranslationChain = of(
new NamespaceConverter(), (Converter<String, String>) new HashConverter()
);
private static final List<Converter<String, String>> minimalTranslationChain =
of(new NamespaceConverter(), new HashConverter());

protected List<Converter<String,String>> getTranslationChain() {
final ApplicationContext context = getApplicationContext();
Expand Down
Expand Up @@ -15,16 +15,19 @@
*/
package org.fcrepo.http.commons.api.rdf;

import static org.fcrepo.kernel.utils.iterators.RdfStream.fromModel;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Map;

import javax.ws.rs.core.UriInfo;

import com.hp.hpl.jena.rdf.model.Resource;

import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.utils.iterators.RdfStream;

import org.slf4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -62,19 +65,11 @@ public void addHttpComponentModelsForResourceToStream(final RdfStream rdfStream,
final IdentifierConverter<Resource,FedoraResource> idTranslator) {

LOGGER.debug("Adding additional HTTP context triples to stream");
for (final Map.Entry<String, UriAwareResourceModelFactory> e : getUriAwareTripleFactories()
.entrySet()) {
final String beanName = e.getKey();
final UriAwareResourceModelFactory uriAwareResourceModelFactory =
e.getValue();
LOGGER.debug("Adding response information using: {}", beanName);

final Model m =
uriAwareResourceModelFactory.createModelForResource(
resource, uriInfo, idTranslator);
rdfStream.concat(RdfStream.fromModel(m));
}

getUriAwareTripleFactories().forEach((bean, factory) -> {
LOGGER.debug("Adding response information using: {}", bean);
final Model m = factory.createModelForResource(resource, uriInfo, idTranslator);
rdfStream.concat(fromModel(m));
});
}

private Map<String, UriAwareResourceModelFactory> getUriAwareTripleFactories() {
Expand Down
Expand Up @@ -16,22 +16,24 @@
package org.fcrepo.http.commons.domain;

import com.google.common.annotations.VisibleForTesting;

import org.fcrepo.kernel.services.ExternalContentService;

import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import static java.util.Arrays.stream;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;

/**
Expand All @@ -47,17 +49,15 @@ public class ContentLocationMessageBodyReader implements MessageBodyReader<Input
@Autowired
private ExternalContentService contentService;

private static final Class contentLocationClass = ContentLocation.class;
private static final Class<ContentLocation> contentLocationClass = ContentLocation.class;

@Override
public boolean isReadable(final Class<?> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {
return InputStream.class.isAssignableFrom(type) &&
Arrays.asList(annotations).stream()
.map(Annotation::annotationType)
.anyMatch(contentLocationClass::equals);
stream(annotations).map(Annotation::annotationType).anyMatch(contentLocationClass::equals);
}

@Override
Expand All @@ -73,7 +73,7 @@ public InputStream readFrom(final Class<InputStream> type,

try {
return contentService.retrieveExternalContent(new URI(location));
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
throw new WebApplicationException(e, BAD_REQUEST);
}

Expand Down
Expand Up @@ -42,8 +42,6 @@ public MultiPrefer(final String header) throws ParseException {
*/
public MultiPrefer(final @HeaderParam("Prefer") Set<SinglePrefer> prefers) throws ParseException {
super("");
for (SinglePrefer prefer : prefers) {
preferTags().addAll(prefer.preferTags());
}
prefers.forEach(p -> preferTags().addAll(p.preferTags()));
}
}
Expand Up @@ -146,17 +146,15 @@ public int compareTo(final PreferTag otherTag) {
public boolean equals(final Object obj) {
if ((obj != null) && (obj instanceof PreferTag)) {
return getTag().equals(((PreferTag) obj).getTag());
} else {
return false;
}
return false;
}

@Override
public int hashCode() {
if (getTag() == null) {
return 0;
}

return getTag().hashCode();
}
}
Expand Up @@ -108,7 +108,6 @@ public static Range convert(final String source) {
return new Range();
}

matcher.matches();
final String from = matcher.group(1);
final String to = matcher.group(2);

Expand Down
Expand Up @@ -41,7 +41,7 @@ public class SinglePrefer {
* @throws ParseException if parse exception occurred
*/
public SinglePrefer(final String header) throws ParseException {
preferTags.addAll(HttpHeaderReader.readList(PREFER_CREATOR, header));
preferTags.addAll(HttpHeaderReader.readList(PreferTag::new, header));
}

/**
Expand Down Expand Up @@ -84,16 +84,6 @@ public PreferTag getHandling() {
.findFirst().orElse(emptyTag());
}

private static final HttpHeaderReader.ListElementCreator<PreferTag> PREFER_CREATOR =
new HttpHeaderReader.ListElementCreator<PreferTag>() {

@Override
public PreferTag create(final HttpHeaderReader reader) throws ParseException {
return new PreferTag(reader);
}
};


protected Set<PreferTag> preferTags() {
return preferTags;
}
Expand Down
Expand Up @@ -15,59 +15,28 @@
*/
package org.fcrepo.http.commons.responses;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BoundedInputStream;
import org.apache.commons.io.input.ProxyInputStream;

/**
* An InputStream wrapper that skips N bytes and only returns
* the data up to a certain length
* An {@link InputStream} that skips bytes and only returns the data up to a certain limit
*
* @author awoods
* @author ajs6f
*/
public class RangeRequestInputStream extends FilterInputStream {
public class RangeRequestInputStream extends BoundedInputStream {

/**
* Creates a <code>FilterInputStream</code>
* by assigning the argument <code>in</code>
* to the field <code>this.in</code> so as
* to remember it for later use.
*
* @param in the underlying input stream, or <code>null</code> if
* this instance is to be created without an underlying stream.
* @param skip the number of bytes to skip at the beginning of the stream
* @param length the number of bytes from the inputstream to read
* @throws IOException if IO exception occurred
*/
public RangeRequestInputStream(final InputStream in,
final long skip,
final long length) throws IOException {
super(new BoundedInputStream(new SkipInputStream(in, skip), length));
}


/**
* An InputStream wrapper that skips bytes
* @param in
* @param skip
* @throws IOException
*/
private static class SkipInputStream extends ProxyInputStream {

/**
* An InputStream wrapper that always skips the first N bytes
* @param in
* @param skip
* @throws IOException
*/
public SkipInputStream(final InputStream in,
final long skip) throws IOException {
super(in);
IOUtils.skip(in, skip);
}
public RangeRequestInputStream(final InputStream in, final long skip, final long length) throws IOException {
super(in, length);
in.skip(skip);
}
}

0 comments on commit e8b3f63

Please sign in to comment.