Skip to content

Commit e19194a

Browse files
committed
Enforce trailing slash with redirect
Graph `PUT` method returns `308 Permanent Redirect` with `Locatioon` instead of `422 Unprocessable Entity`
1 parent 21f2bbb commit e19194a

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/main/java/com/atomgraph/linkeddatahub/resource/Graph.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import jakarta.ws.rs.core.Request;
6363
import jakarta.ws.rs.core.Response;
6464
import jakarta.ws.rs.core.Response.ResponseBuilder;
65+
import static jakarta.ws.rs.core.Response.Status.PERMANENT_REDIRECT;
6566
import jakarta.ws.rs.core.SecurityContext;
6667
import jakarta.ws.rs.core.UriInfo;
6768
import jakarta.ws.rs.ext.MessageBodyReader;
@@ -212,11 +213,16 @@ public Response put(Model model, @QueryParam("default") @DefaultValue("false") B
212213
throw new WebApplicationException("Method '" + HttpMethod.PUT + "' is not allowed on document URI <" + getURI() + ">", Response.status(Response.Status.METHOD_NOT_ALLOWED).allow(getAllowedMethods()).build());
213214
}
214215

215-
// enforce that request URI always end with a slash and is present in the RDF document
216+
// enforce that request URI always end with a slash - by redirecting to it if doesn't not already
216217
if (!getURI().toString().endsWith("/"))
217218
{
218-
if (log.isErrorEnabled()) log.error("Document URI <{}> does not end with a slash", getURI());
219-
throw new WebApplicationException("Document URI <" + getURI() + "> does not end with a slash", UNPROCESSABLE_ENTITY.getStatusCode()); // 422 Unprocessable Entity
219+
String uriWithSlash = getURI().toString() + "/";
220+
221+
if (log.isDebugEnabled()) log.debug("Redirecting document URI <> to <{}> in order to enforce trailing a slash", getURI(), uriWithSlash);
222+
223+
return Response.status(PERMANENT_REDIRECT).
224+
location(URI.create(uriWithSlash)).
225+
build();
220226
}
221227

222228
new Skolemizer(getURI().toString()).apply(model);

src/main/java/com/atomgraph/linkeddatahub/server/exception/ResourceExistsException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
2828
*/
29+
@Deprecated
2930
public class ResourceExistsException extends ModelException
3031
{
3132

src/main/java/com/atomgraph/linkeddatahub/server/mapper/ResourceExistsExceptionMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
3434
*/
35+
@Deprecated
3536
public class ResourceExistsExceptionMapper extends ExceptionMapperBase implements ExceptionMapper<ResourceExistsException>
3637
{
3738

0 commit comments

Comments
 (0)