From e0266e25ed4bd0e6f705d857395112d7525f630a Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sat, 28 Nov 2026 15:54:27 +0100 Subject: [PATCH] Add Redirector.MODE_CLIENT_PERMANENT_STRICT answering 308 (#1746) RFC 9110 section 15.4.9: 308 makes the client repeat the request with the same method and content, unlike 301 which lets a user agent turn a POST into a GET. Status.REDIRECTION_PERMANENT_STRICT existed but nothing could answer with it. New mode (value 5) with the helpers of the other modes: Response, WrapperResponse and ServerResource redirectPermanentStrict, and Router.redirectPermanentStrict. The Javadoc of the 301 helpers no longer promises that the method is kept. Co-Authored-By: Claude Sonnet 5.5 --- changes.md | 6 ++ .../src/main/java/org/restlet/Response.java | 40 ++++++++- .../org/restlet/resource/ServerResource.java | 36 +++++++- .../java/org/restlet/routing/Redirector.java | 31 ++++++- .../main/java/org/restlet/routing/Router.java | 20 ++++- .../org/restlet/util/WrapperResponse.java | 30 ++++++- .../restlet/ResponseRedirectionTestCase.java | 62 +++++++++++++ .../PermanentRedirectWireTestCase.java | 90 +++++++++++++++++++ .../resource/ServerResourceTestCase.java | 19 ++++ .../restlet/routing/RedirectorTestCase.java | 31 +++++++ .../org/restlet/routing/RouterTestCase.java | 11 +++ .../restlet/util/WrapperResponseTestCase.java | 6 ++ 12 files changed, 365 insertions(+), 17 deletions(-) create mode 100644 org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java create mode 100644 org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java diff --git a/changes.md b/changes.md index a0baaaaaea..c34787194c 100644 --- a/changes.md +++ b/changes.md @@ -33,6 +33,12 @@ Changes log stricter than `SunX509` on unusual chains: set the trust manager algorithm to `SunX509` to get the previous behavior. - Enhancements + - `Redirector.MODE_CLIENT_PERMANENT_STRICT` answers 308 (Permanent Redirect), which makes + the client repeat the request with the same method and content, unlike + `MODE_CLIENT_PERMANENT` (301) that lets a user agent turn a POST into a GET. It comes with + `Response.redirectPermanentStrict(...)`, `ServerResource.redirectPermanentStrict(...)` and + `Router.redirectPermanentStrict(...)`. The Javadoc of the 301 helpers no longer promises + that the method is kept (#1746). - `McpServerAdapter.setJsonMapper` and `McpClientAdapter.setJsonMapper` (for the 2026-07-28 client) let the application supply the JSON mapper of the MCP messages, the SDK's Jackson 3 one by default (#1705). diff --git a/org.restlet/src/main/java/org/restlet/Response.java b/org.restlet/src/main/java/org/restlet/Response.java index 373c80eb75..2abe4bbb60 100644 --- a/org.restlet/src/main/java/org/restlet/Response.java +++ b/org.restlet/src/main/java/org/restlet/Response.java @@ -618,8 +618,9 @@ public boolean isProvisional() { } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request, for example a POST into a GET: use {@link + * #redirectPermanentStrict(Reference)} to have the same method reused. * * @param targetRef The target URI reference. */ @@ -629,8 +630,9 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request.
+ * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request, for example a POST into a GET: use {@link #redirectPermanentStrict(String)} + * to have the same method reused.
*
* If you pass a relative target URI, it will be resolved with the current base reference of the * request's resource reference (see {@link Request#getResourceRef()} and {@link @@ -643,6 +645,36 @@ public void redirectPermanent(String targetUri) { setStatus(Status.REDIRECTION_PERMANENT); } + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content, unlike with {@link + * #redirectPermanent(Reference)}. + * + * @param targetRef The target URI reference. + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public void redirectPermanentStrict(Reference targetRef) { + setLocationRef(targetRef); + setStatus(Status.REDIRECTION_PERMANENT_STRICT); + } + + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content, unlike with {@link #redirectPermanent(String)}. + *
+ *
+ * If you pass a relative target URI, it will be resolved with the current base reference of the + * request's resource reference (see {@link Request#getResourceRef()} and {@link + * Reference#getBaseRef()}. + * + * @param targetUri The target URI. + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public void redirectPermanentStrict(String targetUri) { + setLocationRef(targetUri); + setStatus(Status.REDIRECTION_PERMANENT_STRICT); + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET method on that * resource. This method exists primarily to allow the output of a POST-activated script to diff --git a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java index 128410dd07..14b9839699 100644 --- a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java +++ b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java @@ -1386,8 +1386,8 @@ protected Representation put(Representation representation, Variant variant) } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request: see {@link #redirectPermanentStrict(Reference)}. * * @param targetRef The target URI reference. */ @@ -1398,8 +1398,8 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request.
+ * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request: see {@link #redirectPermanentStrict(String)}.
*
* If you pass a relative target URI, it will be resolved with the current base reference of the * request's resource reference (see {@link Request#getResourceRef()} and {@link @@ -1413,6 +1413,34 @@ public void redirectPermanent(String targetUri) { } } + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content. + * + * @param targetRef The target URI reference. + */ + public void redirectPermanentStrict(Reference targetRef) { + if (getResponse() != null) { + getResponse().redirectPermanentStrict(targetRef); + } + } + + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content.
+ *
+ * If you pass a relative target URI, it will be resolved with the current base reference of the + * request's resource reference (see {@link Request#getResourceRef()} and {@link + * Reference#getBaseRef()}. + * + * @param targetUri The target URI. + */ + public void redirectPermanentStrict(String targetUri) { + if (getResponse() != null) { + getResponse().redirectPermanentStrict(targetUri); + } + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET method on that * resource. This method exists primarily to allow the output of a POST-activated script to diff --git a/org.restlet/src/main/java/org/restlet/routing/Redirector.java b/org.restlet/src/main/java/org/restlet/routing/Redirector.java index 9a7b7425bf..d475d5c7a8 100644 --- a/org.restlet/src/main/java/org/restlet/routing/Redirector.java +++ b/org.restlet/src/main/java/org/restlet/routing/Redirector.java @@ -24,9 +24,9 @@ /** * Rewrites URIs then redirects the call or the client to a new destination. There are various * redirection modes that you can choose from: client-side redirections ({@link #MODE_CLIENT_FOUND}, - * {@link #MODE_CLIENT_PERMANENT}, {@link #MODE_CLIENT_SEE_OTHER}, {@link #MODE_CLIENT_TEMPORARY}) - * or server-side redirections, similar to a reverse proxy ( {@link #MODE_SERVER_OUTBOUND} and - * {@link #MODE_SERVER_INBOUND}).
+ * {@link #MODE_CLIENT_PERMANENT}, {@link #MODE_CLIENT_PERMANENT_STRICT}, {@link + * #MODE_CLIENT_SEE_OTHER}, {@link #MODE_CLIENT_TEMPORARY}) or server-side redirections, similar to + * a reverse proxy ( {@link #MODE_SERVER_OUTBOUND} and {@link #MODE_SERVER_INBOUND}).
*
* When setting the redirection URIs, you can also use special URI variables to reuse most * properties from the original request as well as URI template variables. For a complete list of @@ -59,11 +59,24 @@ public class Redirector extends Restlet { * In this mode, the client is permanently redirected to the URI generated from the target URI * pattern, using the {@link Status#REDIRECTION_PERMANENT} status. Note: this is a client-side * redirection.
+ *
+ * With the 301 status, a user agent may change the method of the new request (a POST can become + * a GET): use {@link #MODE_CLIENT_PERMANENT_STRICT} to keep it. * * @see Status#REDIRECTION_PERMANENT */ public static final int MODE_CLIENT_PERMANENT = 1; + /** + * In this mode, the client is permanently redirected to the URI generated from the target URI + * pattern, using the {@link Status#REDIRECTION_PERMANENT_STRICT} status (308): the client must + * repeat the request to the new URI with the same method and content, unlike with {@link + * #MODE_CLIENT_PERMANENT}. Note: this is a client-side redirection.
+ * + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public static final int MODE_CLIENT_PERMANENT_STRICT = 5; + /** * In this mode, the client is simply redirected to the URI generated from the target URI * pattern using the {@link Status#REDIRECTION_SEE_OTHER} status. Note: this is a client-side @@ -260,6 +273,18 @@ public void handle(Request request, Response response) { response.redirectPermanent(targetRef); break; + case MODE_CLIENT_PERMANENT_STRICT: + if (request.isLoggable()) { + getLogger() + .fine( + () -> + "Permanently redirecting client, same method, to: " + + targetRef); + } + + response.redirectPermanentStrict(targetRef); + break; + case MODE_CLIENT_FOUND: if (request.isLoggable()) { getLogger().fine(() -> "Redirecting client to found location: " + targetRef); diff --git a/org.restlet/src/main/java/org/restlet/routing/Router.java b/org.restlet/src/main/java/org/restlet/routing/Router.java index 5123793405..08e8a6f218 100644 --- a/org.restlet/src/main/java/org/restlet/routing/Router.java +++ b/org.restlet/src/main/java/org/restlet/routing/Router.java @@ -561,8 +561,9 @@ protected void logRoute(Route route) { } /** - * Attaches a permanent redirection to this router based on a given URI pattern. The client is - * expected to reuse the same method for the new request. + * Attaches a permanent redirection (301) to this router based on a given URI pattern. A user + * agent may change the method of the new request: see {@link #redirectPermanentStrict(String, + * String)}. * * @param pathTemplate The URI path template that must match the relative part of the resource * URI. @@ -575,6 +576,21 @@ public TemplateRoute redirectPermanent(String pathTemplate, String targetUri) { new Redirector(getContext(), targetUri, Redirector.MODE_CLIENT_PERMANENT)); } + /** + * Attaches a permanent redirection (308) to this router based on a given URI pattern. The + * client must repeat the request to the new URI with the same method and content. + * + * @param pathTemplate The URI path template that must match the relative part of the resource + * URI. + * @param targetUri The target URI. + * @return The created route. + */ + public TemplateRoute redirectPermanentStrict(String pathTemplate, String targetUri) { + return attach( + pathTemplate, + new Redirector(getContext(), targetUri, Redirector.MODE_CLIENT_PERMANENT_STRICT)); + } + /** * Attaches a redirection to this router based on a given URI pattern. It redirects the client * to a different URI that SHOULD be retrieved using a GET method on that resource. This method diff --git a/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java b/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java index 7cc6d16c30..8c2ddadfed 100644 --- a/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java +++ b/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java @@ -275,8 +275,8 @@ public boolean isEntityAvailable() { } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request: see {@link #redirectPermanentStrict(Reference)}. * * @param targetRef The target URI reference. */ @@ -286,8 +286,8 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to reuse the same - * method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may change the method of + * the new request: see {@link #redirectPermanentStrict(String)}. * * @param targetUri The target URI. */ @@ -296,6 +296,28 @@ public void redirectPermanent(String targetUri) { getWrappedResponse().redirectPermanent(targetUri); } + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content. + * + * @param targetRef The target URI reference. + */ + @Override + public void redirectPermanentStrict(Reference targetRef) { + getWrappedResponse().redirectPermanentStrict(targetRef); + } + + /** + * Permanently redirects the client to a target URI (308). The client must repeat the request to + * the new URI with the same method and content. + * + * @param targetUri The target URI. + */ + @Override + public void redirectPermanentStrict(String targetUri) { + getWrappedResponse().redirectPermanentStrict(targetUri); + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET method on that * resource. This method exists primarily to allow the output of a POST-activated script to diff --git a/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java b/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java new file mode 100644 index 0000000000..4ab0b31172 --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java @@ -0,0 +1,62 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import org.restlet.data.Method; +import org.restlet.data.Reference; +import org.restlet.data.Status; + +/** Tests the redirection helpers of {@link Response}, including the 308 one (#1746). */ +class ResponseRedirectionTestCase { + + private static Response newResponse() { + return new Response(new Request(Method.POST, "http://localhost/old")); + } + + @Test + void redirectPermanent_answers301() { + Response response = newResponse(); + + response.redirectPermanent("http://localhost/new"); + + assertEquals(Status.REDIRECTION_PERMANENT, response.getStatus()); + } + + @Test + void redirectPermanentStrict_withUri_answers308WithTheLocation() { + Response response = newResponse(); + + response.redirectPermanentStrict("http://localhost/new"); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withReference_answers308WithTheLocation() { + Response response = newResponse(); + + response.redirectPermanentStrict(new Reference("http://localhost/new")); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withRelativeUri_isResolvedAgainstTheRequest() { + Response response = newResponse(); + + response.redirectPermanentStrict("/new"); + + assertEquals("http://localhost/new", response.getLocationRef().getTargetRef().toString()); + } +} diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java new file mode 100644 index 0000000000..e5e464441b --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java @@ -0,0 +1,90 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet.engine.connector; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.restlet.Context; +import org.restlet.Server; +import org.restlet.data.Protocol; +import org.restlet.engine.Engine; +import org.restlet.routing.Router; + +/** + * Tests, on the wire, the permanent redirections of a router: 301 may change the method of the + * request, 308 must keep it (RFC 9110 sections 15.4.2 and 15.4.9, #1746). + */ +class PermanentRedirectWireTestCase { + + private static final int TIMEOUT_MS = 10_000; + + private Server server; + + @BeforeEach + void setUp() throws Exception { + Engine.clearThreadLocalVariables(); + Engine.register(false); + Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null)); + + Context context = new Context(); + Router router = new Router(context); + router.redirectPermanent("/moved", "http://example.com/new"); + router.redirectPermanentStrict("/strict", "http://example.com/new"); + server = new Server(context, Protocol.HTTP, 0, router); + server.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (server != null) { + server.stop(); + } + Engine.clearThreadLocalVariables(); + } + + private String post(String path) throws IOException { + try (Socket socket = new Socket("localhost", server.getActualPort())) { + socket.setSoTimeout(TIMEOUT_MS); + OutputStream out = socket.getOutputStream(); + out.write( + ("POST " + + path + + " HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n" + + "Content-Type: text/plain\r\nConnection: close\r\n\r\nbody") + .getBytes(StandardCharsets.ISO_8859_1)); + out.flush(); + return new String(socket.getInputStream().readAllBytes(), StandardCharsets.ISO_8859_1); + } + } + + @Test + void post_toAStrictPermanentRedirection_answers308WithTheLocation() throws Exception { + String response = post("/strict"); + + assertTrue(response.startsWith("HTTP/1.1 308"), response); + assertTrue( + response.toLowerCase(Locale.ROOT).contains("\r\nlocation: http://example.com/new"), + response); + } + + @Test + void post_toAPermanentRedirection_stillAnswers301() throws Exception { + String response = post("/moved"); + + assertTrue(response.startsWith("HTTP/1.1 301"), response); + } +} diff --git a/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java b/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java index 0a912bd713..fc5a1f46ea 100644 --- a/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java +++ b/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java @@ -488,4 +488,23 @@ void getOnSentAndSetOnSent_roundTrip() { assertEquals(callback, resource.getOnSent()); } + + @Test + void redirectPermanentStrict_withStringUri_setsLocationAnd308() { + EchoServerResource resource = createInitializedResource(Method.GET); + + resource.redirectPermanentStrict("http://localhost/target"); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, resource.getStatus()); + assertEquals("http://localhost/target", resource.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withReference_setsLocationAnd308() { + EchoServerResource resource = createInitializedResource(Method.GET); + + resource.redirectPermanentStrict(new org.restlet.data.Reference("http://localhost/target")); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, resource.getStatus()); + } } diff --git a/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java b/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java index 2b15449f38..8da5de38d9 100644 --- a/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java +++ b/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java @@ -375,4 +375,35 @@ public void handle(Request req, Response resp) { assertEquals(List.of("http://backend/a/c/d"), seen); assertEquals("http://localhost/old", request.getResourceRef().toString()); } + + /** #1746: 308 keeps the method of the request, unlike 301. */ + @Test + void handle_clientPermanentStrictMode_redirectsWith308() { + Redirector redirector = + new Redirector(new Context(), "/new", Redirector.MODE_CLIENT_PERMANENT_STRICT); + Request request = new Request(Method.POST, "http://localhost/old"); + Response response = new Response(request); + + redirector.handle(request, response); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().getTargetRef().toString()); + assertEquals(Method.POST, request.getMethod()); + } + + @Test + void clientPermanentStrictMode_isDistinctFromTheOtherModes() { + int[] others = { + Redirector.MODE_CLIENT_PERMANENT, + Redirector.MODE_CLIENT_FOUND, + Redirector.MODE_CLIENT_SEE_OTHER, + Redirector.MODE_CLIENT_TEMPORARY, + Redirector.MODE_SERVER_INBOUND, + Redirector.MODE_SERVER_OUTBOUND + }; + for (int other : others) { + org.junit.jupiter.api.Assertions.assertNotEquals( + Redirector.MODE_CLIENT_PERMANENT_STRICT, other); + } + } } diff --git a/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java b/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java index a1421c8329..eab14a5275 100644 --- a/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java +++ b/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java @@ -398,4 +398,15 @@ private static String route(Router router, String path) { router.handle(request, response); return response.getEntityAsText(); } + + @Test + void redirectPermanentStrict_attachesRedirectorWithPermanentStrictMode() { + Router router = new Router(new Context()); + + TemplateRoute route = router.redirectPermanentStrict("/old", "/new"); + + assertTrue(route.getNext() instanceof Redirector); + assertEquals( + Redirector.MODE_CLIENT_PERMANENT_STRICT, ((Redirector) route.getNext()).getMode()); + } } diff --git a/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java b/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java index 0ec3d80d7c..386805bccf 100644 --- a/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java +++ b/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java @@ -138,6 +138,12 @@ void redirectMethods_delegateToWrappedResponse() { wrapper.redirectPermanent(new Reference("http://localhost/permanent2")); assertEquals(Status.REDIRECTION_PERMANENT, wrapped.getStatus()); + wrapper.redirectPermanentStrict("http://localhost/strict"); + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, wrapped.getStatus()); + + wrapper.redirectPermanentStrict(new Reference("http://localhost/strict2")); + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, wrapped.getStatus()); + wrapper.redirectSeeOther("http://localhost/other"); assertEquals(Status.REDIRECTION_SEE_OTHER, wrapped.getStatus());