Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
40 changes: 36 additions & 4 deletions org.restlet/src/main/java/org/restlet/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.<br>
* 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.<br>
* <br>
* 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
Expand All @@ -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)}.
* <br>
* <br>
* 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
Expand Down
36 changes: 32 additions & 4 deletions org.restlet/src/main/java/org/restlet/resource/ServerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.<br>
* Permanently redirects the client to a target URI (301). A user agent may change the method of
* the new request: see {@link #redirectPermanentStrict(String)}.<br>
* <br>
* 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
Expand All @@ -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.<br>
* <br>
* 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
Expand Down
31 changes: 28 additions & 3 deletions org.restlet/src/main/java/org/restlet/routing/Redirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}).<br>
* {@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}).<br>
* <br>
* 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
Expand Down Expand Up @@ -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.<br>
* <br>
* 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.<br>
*
* @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
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 18 additions & 2 deletions org.restlet/src/main/java/org/restlet/routing/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
30 changes: 26 additions & 4 deletions org.restlet/src/main/java/org/restlet/util/WrapperResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* 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
*<p>
* 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());
}
}
Loading