Skip to content

Commit

Permalink
Adding support for OPTIONS requests on nodes with appropriate headers…
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Apr 15, 2014
1 parent 6106b93 commit 6153212
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
18 changes: 18 additions & 0 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -30,6 +30,7 @@
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.OK;
import static org.apache.http.HttpStatus.SC_BAD_GATEWAY;
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
import static org.apache.http.HttpStatus.SC_CONFLICT;
Expand Down Expand Up @@ -70,6 +71,7 @@
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -713,4 +715,20 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,

}

/**
* Outputs information about the supported HTTP methods, etc.
*/
@OPTIONS
@Timed
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 patchTypes = contentTypeSPARQLUpdate + "," + TURTLE + "," + N3 + ","
+ N3_ALT1 + "," + N3_ALT2 + "," + RDF_XML + "," + NTRIPLES;
servletResponse.addHeader("Accept-Patch", patchTypes);
servletResponse.addHeader("Accept-Post", patchTypes + "," + MediaType.MULTIPART_FORM_DATA);
return status(OK).build();
}

}
Expand Up @@ -62,7 +62,9 @@
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import java.util.UUID;

Expand All @@ -72,10 +74,12 @@
import nu.validator.saxtree.TreeBuilder;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
Expand Down Expand Up @@ -900,7 +904,60 @@ public void testMoveWithBadEtag() throws Exception {
request.addHeader("If-Match", "\"doesnt-match\"");
final HttpResponse moveResponse = client.execute(request);
assertEquals(412, moveResponse.getStatusLine().getStatusCode());
}
}

@Test
public void testOptions() throws Exception {
final String pid = randomUUID().toString();
final HttpPost method = postObjMethod(pid);
final HttpResponse response = client.execute(method);
assertEquals(CREATED.getStatusCode(), response.getStatusLine()
.getStatusCode());

final HttpOptions optionsRequest = new HttpOptions(serverAddress + pid);
final HttpResponse optionsResponse = client.execute(optionsRequest);
assertEquals(OK.getStatusCode(), optionsResponse.getStatusLine().getStatusCode());

final List<String> methods = headerValues(optionsResponse,"Allow");
assertTrue("Should allow GET", methods.contains("GET"));
assertTrue("Should allow POST", methods.contains("POST"));
assertTrue("Should allow PUT", methods.contains("PUT"));
assertTrue("Should allow PATCH", methods.contains("PATCH"));
assertTrue("Should allow DELETE", methods.contains("DELETE"));
assertTrue("Should allow OPTIONS", methods.contains("OPTIONS"));
assertTrue("Should allow MOVE", methods.contains("MOVE"));
assertTrue("Should allow COPY", methods.contains("COPY"));

final List<String> patchTypes = headerValues(optionsResponse,"Accept-Patch");
assertTrue("PATCH should support application/sparql-update", patchTypes.contains("application/sparql-update"));
assertTrue("PATCH should support text/turtle", patchTypes.contains("text/turtle"));
assertTrue("PATCH should support text/rdf+n3", patchTypes.contains("text/rdf+n3"));
assertTrue("PATCH should support application/n3", patchTypes.contains("application/n3"));
assertTrue("PATCH should support text/n3", patchTypes.contains("text/n3"));
assertTrue("PATCH should support application/rdf+xml", patchTypes.contains("application/rdf+xml"));
assertTrue("PATCH should support application/n-triples", patchTypes.contains("application/n-triples"));

final List<String> postTypes = headerValues(optionsResponse,"Accept-Post");
assertTrue("POST should support application/sparql-update", postTypes.contains("application/sparql-update"));
assertTrue("POST should support text/turtle", postTypes.contains("text/turtle"));
assertTrue("POST should support text/rdf+n3", postTypes.contains("text/rdf+n3"));
assertTrue("POST should support application/n3", postTypes.contains("application/n3"));
assertTrue("POST should support text/n3", postTypes.contains("text/n3"));
assertTrue("POST should support application/rdf+xml", postTypes.contains("application/rdf+xml"));
assertTrue("POST should support application/n-triples", postTypes.contains("application/n-triples"));
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>();
for ( Header header : response.getHeaders(headerName) ) {
for ( String elem : header.getValue().split(",") ) {
values.add( elem.trim() );
}
}
return values;
}


private void validateHTML(final String path) throws Exception {
final HttpGet getMethod = new HttpGet(serverAddress + path);
Expand Down

0 comments on commit 6153212

Please sign in to comment.