Skip to content

Commit

Permalink
Added new providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 16, 2013
1 parent ca58e52 commit 46d1e5d
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 76 deletions.
36 changes: 6 additions & 30 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -2,24 +2,22 @@
package org.fcrepo.api;

import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLUpdate;
import static org.fcrepo.http.RDFMediaType.N3;
import static org.fcrepo.http.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.RDFMediaType.N3_ALT2;
import static org.fcrepo.http.RDFMediaType.NTRIPLES;
import static org.fcrepo.http.RDFMediaType.POSSIBLE_RDF_VARIANTS;
import static org.fcrepo.http.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.RDFMediaType.RDF_XML;
import static org.fcrepo.http.RDFMediaType.TURTLE;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.jcr.RepositoryException;
Expand All @@ -35,13 +33,11 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
Expand All @@ -50,7 +46,6 @@
import org.fcrepo.FedoraObject;
import org.fcrepo.FedoraResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
Expand All @@ -61,6 +56,7 @@
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.update.UpdateAction;

@Component
Expand All @@ -73,40 +69,20 @@ public class FedoraNodes extends AbstractResource {
private LowLevelStorageService llStoreService;

@GET
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES})
public Response describeRdf(@PathParam("path")
@Produces({TEXT_HTML, N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON,
NTRIPLES})
public Dataset describeRdf(@PathParam("path")
final List<PathSegment> pathList, @Context
final Request request) throws RepositoryException, IOException {

final String path = toPath(pathList);
logger.trace("Getting profile for {}", path);

final Variant bestPossibleResponse =
request.selectVariant(POSSIBLE_RDF_VARIANTS);

final Session session = getAuthenticatedSession();
try {
final FedoraResource resource =
nodeService.getObject(session, path);

final Date date = resource.getLastModifiedDate();
final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);

Response.ResponseBuilder builder =
request.evaluatePreconditions(roundedDate);

if (builder == null) {
builder =
ok(new GraphStreamingOutput(resource.getGraphStore(),
bestPossibleResponse.getMediaType()));
}

final CacheControl cc = new CacheControl();

This comment has been minimized.

Copy link
@awoods

awoods May 16, 2013

Where did the caching headers end up?

cc.setMaxAge(0);
cc.setMustRevalidate(true);

return builder.cacheControl(cc).lastModified(date).build();
return resource.getGraphStore().toDataset();

} finally {
session.logout();
Expand Down
Expand Up @@ -29,7 +29,7 @@

import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
import org.fcrepo.provider.GraphStreamingOutput;
import org.fcrepo.responses.GraphStreamingOutput;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

Expand Down
63 changes: 31 additions & 32 deletions fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraNodesTest.java
Expand Up @@ -9,7 +9,6 @@
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -187,43 +186,43 @@ public void testDescribeRdfObject() throws RepositoryException, IOException {

final OutputStream mockStream = mock(OutputStream.class);
((StreamingOutput) testObj
.describeRdf(createPathList(pid), mockRequest).getEntity())
.describeRdf(createPathList(pid), mockRequest))
.write(mockStream);

verify(mockModel).write(mockStream, "N-TRIPLES");

}

@SuppressWarnings("unchecked")
@Test
public void testDescribeRdfCached() throws RepositoryException, IOException {
final String pid = "FedoraObjectsRdfTest2";
final String path = "/" + pid;
final FedoraObject mockObject = mock(FedoraObject.class);

final GraphStore mockStore = mock(GraphStore.class);
final Dataset mockDataset = mock(Dataset.class);
final Model mockModel = mock(Model.class);
when(mockStore.toDataset()).thenReturn(mockDataset);
when(mockDataset.getDefaultModel()).thenReturn(mockModel);

when(mockObject.getLastModifiedDate()).thenReturn(new Date());
when(mockObject.getGraphStore()).thenReturn(mockStore);
when(mockNodes.getObject(mockSession, path)).thenReturn(mockObject);
final Request mockRequest = mock(Request.class);

when(mockRequest.selectVariant(any(List.class))).thenReturn(
new Variant(MediaType.valueOf("application/n-triples"), null,
null));

when(mockRequest.evaluatePreconditions(any(Date.class))).thenReturn(
Response.notModified());
final Response actual =
testObj.describeRdf(createPathList(pid), mockRequest);
assertNotNull(actual);
assertEquals(Status.NOT_MODIFIED.getStatusCode(), actual.getStatus());
verify(mockSession, never()).save();
}
// ignore this test until caching is re-enabled
/*
* @SuppressWarnings("unchecked")
* @Test
* public void testDescribeRdfCached() throws RepositoryException,
* IOException {
* final String pid = "FedoraObjectsRdfTest2";
* final String path = "/" + pid;
* final FedoraObject mockObject = mock(FedoraObject.class);
* final GraphStore mockStore = mock(GraphStore.class);
* final Dataset mockDataset = mock(Dataset.class);
* final Model mockModel = mock(Model.class);
* when(mockStore.toDataset()).thenReturn(mockDataset);
* when(mockDataset.getDefaultModel()).thenReturn(mockModel);
* when(mockObject.getLastModifiedDate()).thenReturn(new Date());
* when(mockObject.getGraphStore()).thenReturn(mockStore);
* when(mockNodes.getObject(mockSession, path)).thenReturn(mockObject);
* final Request mockRequest = mock(Request.class);
* when(mockRequest.selectVariant(any(List.class))).thenReturn(
* new Variant(MediaType.valueOf("application/n-triples"), null,
* null));
* when(mockRequest.evaluatePreconditions(any(Date.class))).thenReturn(
* Response.notModified());
* final Response actual =
* testObj.describeRdf(createPathList(pid), mockRequest);
* assertNotNull(actual);
* assertEquals(Status.NOT_MODIFIED.getStatusCode(), actual.getStatus());
* verify(mockSession, never()).save();
* }
*/

@Test
public void testSparqlUpdate() throws RepositoryException, IOException {
Expand Down
@@ -1,5 +1,11 @@
package org.fcrepo.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -8,12 +14,6 @@
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class FedoraContentIT extends AbstractResourceIT {

private static final String faulkner1 =
Expand Down
Expand Up @@ -7,15 +7,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.util.EntityUtils;
Expand Down
5 changes: 5 additions & 0 deletions fcrepo-http-commons/pom.xml
Expand Up @@ -112,6 +112,11 @@
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
</dependencies>

<build>
Expand Down
@@ -1,5 +1,5 @@

package org.fcrepo.provider;
package org.fcrepo.responses;

import java.io.IOException;
import java.io.OutputStream;
Expand Down
@@ -0,0 +1,85 @@

package org.fcrepo.responses;

import static com.google.common.collect.ImmutableList.of;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.query.Dataset;

@Provider
@Component
public class HtmlProvider implements MessageBodyWriter<Dataset> {

VelocityEngine velocity = new VelocityEngine();

Template template;

@PostConstruct
void init() throws IOException {
final Properties properties = new Properties();
try (final InputStream propertiesStream =
this.getClass().getResource("velocity.properties").openStream()) {
properties.load(propertiesStream);
}
velocity.init(properties);
template = velocity.getTemplate("html-template");
}

@Override
public void writeTo(final Dataset rdf, final Class<?> type,
final Type genericType, final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream entityStream) throws IOException,
WebApplicationException {

// add a Content-type header
httpHeaders.put("Content-type", of((Object) TEXT_HTML));

final Context context = new VelocityContext();
context.put("rdf", rdf);
// the contract of MessageBodyWriter is not to close the stream
// after writing to it
template.merge(context, new OutputStreamWriter(entityStream));

}

@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return mediaType.equals(TEXT_HTML_TYPE) &&
(type.equals(Dataset.class) || genericType.getClass().equals(
Dataset.class));
}

@Override
public long getSize(final Dataset t, final Class<?> type,
final Type genericType, final Annotation[] annotations,
final MediaType mediaType) {
// we don't know in advance how large the result might be
return -1;
}

}

0 comments on commit 46d1e5d

Please sign in to comment.