Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add HTTP headers (Allow, Accept-*) from OPTIONS to GET/HEAD requests
  • Loading branch information
cbeer authored and Andrew Woods committed May 4, 2014
1 parent 6236737 commit d53356e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 12 additions & 7 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -306,11 +306,21 @@ private void addResourceHttpHeaders(final HttpServletResponse servletResponse,
"<" + subjectsCanonical.getSubject(resource.getPath()) + ">;rel=\"canonical\"");
}

servletResponse.addHeader("Accept-Patch", contentTypeSPARQLUpdate);
addOptionsHttpHeaders(servletResponse);
servletResponse.addHeader("Link", "<" + LDP_NAMESPACE + "Resource>;rel=\"type\"");
servletResponse.addHeader("Link", "<" + LDP_NAMESPACE + "DirectContainer>;rel=\"type\"");
}

private void addOptionsHttpHeaders(final HttpServletResponse servletResponse) {
servletResponse.addHeader("Accept-Patch", contentTypeSPARQLUpdate);

servletResponse.addHeader("Allow", "MOVE,COPY,DELETE,POST,HEAD,GET,PUT,PATCH,OPTIONS");
final String rdfTypes = TURTLE + "," + N3 + "," + N3_ALT1 + ","
+ N3_ALT2 + "," + RDF_XML + "," + NTRIPLES;
servletResponse.addHeader("Accept-Post", rdfTypes + "," + MediaType.MULTIPART_FORM_DATA
+ "," + contentTypeSPARQLUpdate);
}

/**
* Update an object using SPARQL-UPDATE
*
Expand Down Expand Up @@ -826,12 +836,7 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,
public Response options(@PathParam("path") final List<PathSegment> pathList,
@Context final HttpServletResponse servletResponse)
throws RepositoryException {
servletResponse.addHeader("Allow", "MOVE,COPY,DELETE,POST,HEAD,GET,PUT,PATCH,OPTIONS");
final String rdfTypes = TURTLE + "," + N3 + "," + N3_ALT1 + ","
+ N3_ALT2 + "," + RDF_XML + "," + NTRIPLES;
servletResponse.addHeader("Accept-Patch", contentTypeSPARQLUpdate);
servletResponse.addHeader("Accept-Post", rdfTypes + "," + MediaType.MULTIPART_FORM_DATA
+ "," + contentTypeSPARQLUpdate);
addOptionsHttpHeaders(servletResponse);
return status(OK).build();
}

Expand Down
Expand Up @@ -366,8 +366,7 @@ public void testGetObjectGraph() throws Exception {
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
assertEquals("application/sparql-update", response.getFirstHeader(
"Accept-Patch").getValue());
testOptionsHeaders(response);

final Collection<String> links =
map(response.getHeaders("Link"), new Function<Header, String>() {
Expand Down Expand Up @@ -1045,7 +1044,11 @@ public void testOptions() throws Exception {
final HttpResponse optionsResponse = client.execute(optionsRequest);
assertEquals(OK.getStatusCode(), optionsResponse.getStatusLine().getStatusCode());

final List<String> methods = headerValues(optionsResponse,"Allow");
testOptionsHeaders(optionsResponse);
}

private void testOptionsHeaders(final HttpResponse httpResponse) {
final List<String> methods = headerValues(httpResponse,"Allow");
assertTrue("Should allow GET", methods.contains(HttpGet.METHOD_NAME));
assertTrue("Should allow POST", methods.contains(HttpPost.METHOD_NAME));
assertTrue("Should allow PUT", methods.contains(HttpPut.METHOD_NAME));
Expand All @@ -1055,10 +1058,10 @@ public void testOptions() throws Exception {
assertTrue("Should allow MOVE", methods.contains(HttpMove.METHOD_NAME));
assertTrue("Should allow COPY", methods.contains(HttpCopy.METHOD_NAME));

final List<String> patchTypes = headerValues(optionsResponse,"Accept-Patch");
final List<String> patchTypes = headerValues(httpResponse,"Accept-Patch");
assertTrue("PATCH should support application/sparql-update", patchTypes.contains(contentTypeSPARQLUpdate));

final List<String> postTypes = headerValues(optionsResponse,"Accept-Post");
final List<String> postTypes = headerValues(httpResponse,"Accept-Post");
assertTrue("POST should support application/sparql-update", postTypes.contains(contentTypeSPARQLUpdate));
assertTrue("POST should support text/turtle", postTypes.contains(contentTypeTurtle));
assertTrue("POST should support text/rdf+n3", postTypes.contains(contentTypeN3));
Expand All @@ -1068,6 +1071,7 @@ public void testOptions() throws Exception {
assertTrue("POST should support application/n-triples", postTypes.contains(contentTypeNTriples));
assertTrue("POST should support multipart/form-data", postTypes.contains("multipart/form-data"));
}

private static List<String> headerValues( HttpResponse response,
String headerName ) {
final List<String> values = new ArrayList<String>();
Expand Down

0 comments on commit d53356e

Please sign in to comment.