Skip to content

Commit

Permalink
Use correct content negotiation for NonRDF resources (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn authored Mar 12, 2021
1 parent df5004a commit 0309d69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 13 additions & 7 deletions core/http/src/main/java/org/trellisldp/http/impl/GetHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.stream.Stream;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.core.EntityTag;
Expand Down Expand Up @@ -147,11 +148,16 @@ public ResponseBuilder initialize(final Resource resource) {

LOGGER.debug("Acceptable media types: {}", getRequest().getAcceptableMediaTypes());

if (!LDP.NonRDFSource.equals(resource.getInteractionModel()) || getRequest().getExt() != null) {
this.syntax = getSyntax(getServices().getIOService(),
getRequest().getAcceptableMediaTypes(), resource.getBinaryMetadata()
.filter(b -> !DESCRIPTION.equals(getRequest().getExt()))
.map(b -> b.getMimeType().orElse(APPLICATION_OCTET_STREAM)).orElse(null));
// Get the requested syntax
this.syntax = getSyntax(getServices().getIOService(),
getRequest().getAcceptableMediaTypes(), resource.getBinaryMetadata()
.filter(b -> !DESCRIPTION.equals(getRequest().getExt()))
.map(b -> b.getMimeType().orElse(APPLICATION_OCTET_STREAM)).orElse(null));

// For LDP-NRs, if there is a negotiated RDF syntax, throw a 406 error
if (LDP.NonRDFSource.equals(resource.getInteractionModel()) && getRequest().getExt() == null &&
this.syntax != null) {
throw new NotAcceptableException();
}

final IRI ext = getExtensionGraphName();
Expand Down Expand Up @@ -362,8 +368,8 @@ private CompletionStage<ResponseBuilder> getLdpNr(final ResponseBuilder builder)
final IRI dsid = getResource().getBinaryMetadata().map(BinaryMetadata::getIdentifier).orElse(null);

// Add standard headers
builder.header(ACCEPT_RANGES, "bytes").tag(etag)
.header(ALLOW, isMemento ? join(",", GET, HEAD, OPTIONS) : join(",", GET, HEAD, OPTIONS, PUT, DELETE));
builder.header(ACCEPT_RANGES, "bytes").tag(etag);
addAllowHeaders(builder);

// Short circuit HEAD requests
if (HEAD.equals(getRequest().getMethod())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ void testGetBinary() throws IOException {
}
}

@Test
void testGetBinaryBadConneg() throws IOException {
try (final Response res = target(BINARY_PATH).request().header(ACCEPT, "text/turtle").get()) {
assertEquals(SC_NOT_ACCEPTABLE, res.getStatus(), ERR_RESPONSE_CODE);
}
}

@Test
void testGetBinaryHeaders() {
try (final Response res = target(BINARY_PATH).request().head()) {
Expand Down

0 comments on commit 0309d69

Please sign in to comment.