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
2 changes: 2 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Changes log
- The `Range` header is now applied to GET requests only, as RFC 9110 section 14.2 requires: a HEAD request with a range was answered "206 Partial Content" (and so was OPTIONS), it is now answered as without the range. Issue #1734.
- A `CacheDirective` created by a factory, such as `CacheDirective.maxAge(60)`, now equals the same directive read from a header: `equals` and `hashCode` use the name and the value only, no longer the `digit` attribute, which has no effect on the written header. `isDigit()`, `setDigit(boolean)` and the constructor `CacheDirective(String, String, boolean)` are deprecated and will be removed in Restlet 3.0. Issue #1730.
- A 304 (Not Modified) response now carries the `Expires` header of the entity, as RFC 9110 section 15.4.5 requires, so that a revalidating cache refreshes it. Issue #1712.
- 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. Issue #1746.
- Misc
- Added conditional writes on the client side: `ClientResource` gets per-call overloads `put`, `patch` and `delete` taking `Conditions`; they apply to that call only and are never retried. `Conditions.unchangedSince(Response)` builds an "If-Match" condition from the strong entity tag that was read, or an "If-Unmodified-Since" condition if the modification date can be deduced to be strong (the response date is at least one second after it, RFC 9110 section 8.8.2.2), and refuses to build an unconditional write otherwise. `Conditions.ifAbsent()` builds "If-None-Match: *" to create a resource only if it does not exist. A failed condition is reported as a 412 `ResourceException`. Issue #100.
- Added an optional origin matcher to CORS (`CorsService`, `CorsFilter`, `setAllowedOriginMatcher(Predicate<String>)`) to accept origins that are not listed, for example any sub-domain. The response always contains the exact request origin; it has no effect while the allowed origins contain `*`. Issue #1611.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,9 @@
}

/**
* 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 @@ -579,8 +580,9 @@
}

/**
* 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
Expand All @@ -593,6 +595,36 @@
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
Expand Down Expand Up @@ -730,7 +762,7 @@
* @param accessControlAllowOrigin The origin allowed by the requested resource.
*/
public void setAccessControlAllowOrigin(String accessControlAllowOrigin) {
// TODO Add some input validation here.

Check warning on line 765 in org.restlet.java/org.restlet/src/main/java/org/restlet/Response.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this TODO comment.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQa23dePIV2HLCpu&open=AaIiqQa23dePIV2HLCpu&pullRequest=1749
this.accessControlAllowOrigin = accessControlAllowOrigin;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
* {@link #init(Context, Request, Response)}() method will be invoked right
* after the creation of the resource.
*/
public ServerResource() {

Check warning on line 117 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Change the visibility of this constructor to "protected".

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpZ&open=AaIiqQOo3dePIV2HLCpZ&pullRequest=1749
}

/**
Expand Down Expand Up @@ -527,7 +527,7 @@
* @return The response entity.
* @throws ResourceException
*/
protected Representation doHandle(MethodAnnotationInfo annotationInfo, Variant variant) throws ResourceException {

Check failure on line 530 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 26 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpa&open=AaIiqQOo3dePIV2HLCpa&pullRequest=1749
Representation result = null;
Class<?>[] parameterTypes = annotationInfo.getJavaInputTypes();

Expand Down Expand Up @@ -635,7 +635,7 @@
* @return The response entity.
* @throws ResourceException
*/
protected Representation doHandle(Variant variant) throws ResourceException {

Check failure on line 638 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 31 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpb&open=AaIiqQOo3dePIV2HLCpb&pullRequest=1749
Representation result = null;
Method method = getMethod();

Expand Down Expand Up @@ -961,10 +961,10 @@
return this.variants;
}

private List<Variant> getVariantsFromAnnotations(Method method) {

Check failure on line 964 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpc&open=AaIiqQOo3dePIV2HLCpc&pullRequest=1749
final List<Variant> result = new ArrayList<>();

for (AnnotationInfo annotationInfo : getAnnotations()) {

Check warning on line 967 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce the total number of break and continue statements in this loop to use at most one.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpY&open=AaIiqQOo3dePIV2HLCpY&pullRequest=1749
if (!(annotationInfo instanceof MethodAnnotationInfo methodAnnotationInfo)) {
continue;
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@
getResponse().setEntity(result);
}

} catch (Throwable t) {

Check warning on line 1062 in org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Throwable.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQOo3dePIV2HLCpd&open=AaIiqQOo3dePIV2HLCpd&pullRequest=1749
doCatch(t);
} finally {
finishHandling();
Expand Down Expand Up @@ -1443,8 +1443,9 @@
}

/**
* 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 @@ -1455,8 +1456,9 @@
}

/**
* 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
Expand All @@ -1470,6 +1472,34 @@
}
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
* 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
* {@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>
Expand Down Expand Up @@ -60,11 +61,27 @@
* 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.
Expand Down Expand Up @@ -259,18 +276,26 @@
// Generate the target reference
Reference targetRef = getTargetRef(request, response);

switch (this.mode) {

Check failure on line 279 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a default case to this switch.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpm&open=AaIiqQZO3dePIV2HLCpm&pullRequest=1749
case MODE_CLIENT_PERMANENT:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Permanently redirecting client to: " + targetRef);

Check warning on line 282 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpe&open=AaIiqQZO3dePIV2HLCpe&pullRequest=1749

Check warning on line 282 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpn&open=AaIiqQZO3dePIV2HLCpn&pullRequest=1749
}

response.redirectPermanent(targetRef);
break;

case MODE_CLIENT_PERMANENT_STRICT:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Permanently redirecting client, same method, to: " + targetRef);

Check warning on line 290 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpf&open=AaIiqQZO3dePIV2HLCpf&pullRequest=1749

Check warning on line 290 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpo&open=AaIiqQZO3dePIV2HLCpo&pullRequest=1749
}

response.redirectPermanentStrict(targetRef);
break;

case MODE_CLIENT_FOUND:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Redirecting client to found location: " + targetRef);

Check warning on line 298 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpp&open=AaIiqQZO3dePIV2HLCpp&pullRequest=1749

Check warning on line 298 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpg&open=AaIiqQZO3dePIV2HLCpg&pullRequest=1749
}

response.setLocationRef(targetRef);
Expand All @@ -279,7 +304,7 @@

case MODE_CLIENT_SEE_OTHER:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Redirecting client to another location: " + targetRef);

Check warning on line 307 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpq&open=AaIiqQZO3dePIV2HLCpq&pullRequest=1749

Check warning on line 307 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCph&open=AaIiqQZO3dePIV2HLCph&pullRequest=1749
}

response.redirectSeeOther(targetRef);
Expand All @@ -287,7 +312,7 @@

case MODE_CLIENT_TEMPORARY:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Temporarily redirecting client to: " + targetRef);

Check warning on line 315 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpi&open=AaIiqQZO3dePIV2HLCpi&pullRequest=1749

Check warning on line 315 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpr&open=AaIiqQZO3dePIV2HLCpr&pullRequest=1749
}

response.redirectTemporary(targetRef);
Expand All @@ -295,7 +320,7 @@

case MODE_SERVER_OUTBOUND:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Redirecting via client dispatcher to: " + targetRef);

Check warning on line 323 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpj&open=AaIiqQZO3dePIV2HLCpj&pullRequest=1749

Check warning on line 323 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCps&open=AaIiqQZO3dePIV2HLCps&pullRequest=1749
}

outboundServerRedirect(targetRef, request, response);
Expand All @@ -303,7 +328,7 @@

case MODE_SERVER_INBOUND:
if (request.isLoggable()) {
getLogger().log(Level.FINE, "Redirecting via server dispatcher to: " + targetRef);

Check warning on line 331 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpt&open=AaIiqQZO3dePIV2HLCpt&pullRequest=1749

Check warning on line 331 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpk&open=AaIiqQZO3dePIV2HLCpk&pullRequest=1749
}

inboundServerRedirect(targetRef, request, response);
Expand Down Expand Up @@ -444,7 +469,7 @@
*/
protected void serverRedirect(Restlet next, Reference targetRef, Request request, Response response) {
if (next == null) {
getLogger().warning("No next Restlet provided for server redirection to " + targetRef);

Check warning on line 472 in org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiqQZO3dePIV2HLCpl&open=AaIiqQZO3dePIV2HLCpl&pullRequest=1749
} else {
// Save the base URI if it exists as we might need it for
// redirections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,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 @@ -602,6 +603,21 @@ public TemplateRoute redirectPermanent(String pathTemplate, String targetUri) {
return attach(pathTemplate, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ 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 @@ -264,8 +265,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: see
* {@link #redirectPermanentStrict(String)}.
*
* @param targetUri The target URI.
*/
Expand All @@ -274,6 +276,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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 static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.resource.ServerResource;
import org.restlet.routing.Redirector;
import org.restlet.routing.Router;
import org.restlet.routing.TemplateRoute;
import org.restlet.util.WrapperResponse;

/**
* Tests the redirection helpers of {@link Response}, {@link WrapperResponse},
* {@link ServerResource} and {@link Router}, including the 308 ones (#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());
}

@Test
void wrapperResponse_delegatesRedirectPermanentStrict() {
Response wrapped = newResponse();
WrapperResponse wrapper = new WrapperResponse(wrapped);

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());
assertEquals("http://localhost/strict2", wrapped.getLocationRef().toString());
}

@Test
void serverResource_redirectPermanentStrict_setsLocationAnd308() {
ServerResource resource = new ServerResource() {
};
Response response = newResponse();
resource.init(new Context(), response.getRequest(), response);

resource.redirectPermanentStrict("http://localhost/target");
assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus());
assertEquals("http://localhost/target", response.getLocationRef().toString());

resource.redirectPermanentStrict(new Reference("http://localhost/target2"));
assertEquals("http://localhost/target2", response.getLocationRef().toString());
}

@Test
void router_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());
}
}
Loading
Loading