Skip to content

Commit 0309d69

Browse files
authored
Use correct content negotiation for NonRDF resources (#1282)
1 parent df5004a commit 0309d69

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

core/http/src/main/java/org/trellisldp/http/impl/GetHandler.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import java.util.stream.Stream;
7676

7777
import javax.ws.rs.ClientErrorException;
78+
import javax.ws.rs.NotAcceptableException;
7879
import javax.ws.rs.NotFoundException;
7980
import javax.ws.rs.RedirectionException;
8081
import javax.ws.rs.core.EntityTag;
@@ -147,11 +148,16 @@ public ResponseBuilder initialize(final Resource resource) {
147148

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

150-
if (!LDP.NonRDFSource.equals(resource.getInteractionModel()) || getRequest().getExt() != null) {
151-
this.syntax = getSyntax(getServices().getIOService(),
152-
getRequest().getAcceptableMediaTypes(), resource.getBinaryMetadata()
153-
.filter(b -> !DESCRIPTION.equals(getRequest().getExt()))
154-
.map(b -> b.getMimeType().orElse(APPLICATION_OCTET_STREAM)).orElse(null));
151+
// Get the requested syntax
152+
this.syntax = getSyntax(getServices().getIOService(),
153+
getRequest().getAcceptableMediaTypes(), resource.getBinaryMetadata()
154+
.filter(b -> !DESCRIPTION.equals(getRequest().getExt()))
155+
.map(b -> b.getMimeType().orElse(APPLICATION_OCTET_STREAM)).orElse(null));
156+
157+
// For LDP-NRs, if there is a negotiated RDF syntax, throw a 406 error
158+
if (LDP.NonRDFSource.equals(resource.getInteractionModel()) && getRequest().getExt() == null &&
159+
this.syntax != null) {
160+
throw new NotAcceptableException();
155161
}
156162

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

364370
// Add standard headers
365-
builder.header(ACCEPT_RANGES, "bytes").tag(etag)
366-
.header(ALLOW, isMemento ? join(",", GET, HEAD, OPTIONS) : join(",", GET, HEAD, OPTIONS, PUT, DELETE));
371+
builder.header(ACCEPT_RANGES, "bytes").tag(etag);
372+
addAllowHeaders(builder);
367373

368374
// Short circuit HEAD requests
369375
if (HEAD.equals(getRequest().getMethod())) {

core/http/src/test/java/org/trellisldp/http/AbstractTrellisHttpResourceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ void testGetBinary() throws IOException {
431431
}
432432
}
433433

434+
@Test
435+
void testGetBinaryBadConneg() throws IOException {
436+
try (final Response res = target(BINARY_PATH).request().header(ACCEPT, "text/turtle").get()) {
437+
assertEquals(SC_NOT_ACCEPTABLE, res.getStatus(), ERR_RESPONSE_CODE);
438+
}
439+
}
440+
434441
@Test
435442
void testGetBinaryHeaders() {
436443
try (final Response res = target(BINARY_PATH).request().head()) {

0 commit comments

Comments
 (0)