Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HTTP 307/308 redirect status codes #14092

Open
wants to merge 3 commits into
base: 7.0.x
Choose a base branch
from

Conversation

dakshmehta007
Copy link

Added support for HTTP 307/308 status codes in the ResponseRedirector by:

Fixes #11625

  1. Extending the redirect logic to check for a tempRedirect flag in the statusConfig

  2. Using appropriate status codes based on permanent + tempRedirect flags:

    • permanent: false, tempRedirect: false -> 302 (default)
    • permanent: true, tempRedirect: false -> 301
    • permanent: false, tempRedirect: true -> 307
    • permanent: true, tempRedirect: true -> 308
  3. Added test cases to verify both temporary (307) and permanent (308) redirect behavior

Usage##

**For 307 Temporary Redirect**
request.setAttribute('statusConfig', [tempRedirect: true])
redirect(action: 'foo')

**For 308 Permanent Redirect**
request.setAttribute('statusConfig', [tempRedirect: true])
redirect(action: 'foo', permanent: true)

@@ -53,6 +53,16 @@ class RedirectController {
redirect(action:"${prefix}oo")
}

def toActionTemporaryRedirect() {
request.setAttribute('statusConfig', [tempRedirect: true])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code seems to add a dedicated parameter for these purposes. Can you please update your PR to do so? i.e. ARGUMENT_PERMANENT - there should be one for ARGUMENT_TEMPORARY

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@dakshmehta007
Copy link
Author

@jdaugherty I've updated the implementation as suggested to use a dedicated argument instead of request attributes. Now the implementation changes:

  • Adds a new ARGUMENT_TEMPORARY constant following the pattern of ARGUMENT_PERMANENT
  • Uses arguments instead of request attributes for controlling redirect behavior
  • Updates the usage to be more consistent with existing patterns: groovy

// For 307 Temporary Redirect
redirect(action:'foo', temporary:true)

// For 308 Permanent Redirect
redirect(action:'foo', permanent:true, temporary:true)

@jdaugherty
Copy link
Contributor

@dakshmehta007 Can you please also submit a pull request to update the documentation? (https://github.com/apache/grails-doc/blob/7.0.x/src/en/ref/Controllers/redirect.adoc)

@@ -39,6 +39,7 @@ class ResponseRedirector {

public static final String ARGUMENT_PERMANENT = "permanent"
public static final String ARGUMENT_ABSOLUTE = "absolute"
public static final String ARGUMENT_TEMPORARY = "temporary" // Add new constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the comment to be consistent with the current code

@@ -104,13 +105,13 @@ class ResponseRedirector {
namedParameters.put(LinkGenerator.ATTRIBUTE_PARAMS, configuredParams + webRequest.originalParams)
}
}
redirectResponse(linkGenerator.getServerBaseURL(), linkGenerator.link(namedParameters), request, response, permanent, absolute)
redirectResponse(linkGenerator.getServerBaseURL(), linkGenerator.link(namedParameters), request, response, permanent, absolute, arguments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets match the existing style here and not pass the map?

int status = permanent ? HttpServletResponse.SC_MOVED_PERMANENTLY : HttpServletResponse.SC_MOVED_TEMPORARILY

// Update to use arguments instead of request attributes
boolean temporary = Boolean.TRUE == arguments.get(ARGUMENT_TEMPORARY)
Copy link
Contributor

@jdaugherty jdaugherty Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a string value (like permanent). Can you please parse this in the prior method to be consistent and handle the same edge cases as permanent.

boolean temporary = Boolean.TRUE == arguments.get(ARGUMENT_TEMPORARY)
int status
if (permanent) {
status = temporary ? HttpServletResponse.SC_PERMANENT_REDIRECT : HttpServletResponse.SC_MOVED_PERMANENTLY
Copy link
Contributor

@jdaugherty jdaugherty Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the name temporary, wouldn't 'moved' be a better name? Where permanent decides between temporary/ permanent and moved decides between the MOVED value or not.

@dakshmehta007
Copy link
Author

dakshmehta007 commented Mar 29, 2025

@jdaugherty, here is the pull request you requested (apache/grails-doc#966) in the grails-doc repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]Making it possible to redirect with 307 and 308 status
2 participants