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

Allow the redirect type to be specified in the URL Rewrite Controller #5038

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.annotation.Nullable;
import java.io.IOException;

public class Redirect extends URLRewrite {

private @Nullable RedirectType redirectType;

public Redirect(final Element config, final String uri) throws ServletException {
super(config, uri);
this.redirectType = parseRedirectType(config.getAttribute("type"));
final String redirectTo = config.getAttribute("url");
if (redirectTo.length() == 0) {
if (redirectTo.isEmpty()) {
throw new ServletException("<exist:redirect> needs an attribute 'url'.");
}
if (redirectTo.matches("^\\w+://.*")) {
Expand All @@ -47,16 +52,66 @@ public Redirect(final Element config, final String uri) throws ServletException

public Redirect(final Redirect other) {
super(other);
this.redirectType = other.redirectType;
}

@Override
public void doRewrite(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
setHeaders(new HttpResponseWrapper(response));
response.sendRedirect(target);
if (redirectType == null) {
redirectType = "GET".equals(request.getMethod()) ? RedirectType.Found : RedirectType.SeeOther;
}

final HttpResponseWrapper responseWrapper = new HttpResponseWrapper(response);
setHeaders(responseWrapper);
responseWrapper.setStatusCode(redirectType.httpStatusCode);
responseWrapper.setHeader("Location", target);

// commit the response
responseWrapper.flushBuffer();
}

@Override
protected URLRewrite copy() {
return new Redirect(this);
}

private static @Nullable RedirectType parseRedirectType(@Nullable final String strRedirectType) throws ServletException {
// first, if no value use the default
if (strRedirectType == null || strRedirectType.isEmpty()) {
return null;
}

// second, try to parse by number
try {
final int intRedirectType = Integer.valueOf(strRedirectType);
for (final RedirectType redirectType : RedirectType.values()) {
if (redirectType.httpStatusCode == intRedirectType) {
return redirectType;
}
}
} catch (final NumberFormatException e) {
// ignore - no op
}

// third, try to parse by name
try {
return RedirectType.valueOf(strRedirectType);
} catch (final IllegalArgumentException e) {
throw new ServletException("<exist:redirect type=\"" + strRedirectType + "\" is unsupported.");
}
}

private enum RedirectType {
MovedPermanently(301),
Found(302),
SeeOther(303),
TemporaryRedirect(307),
PermanentRedirect(308);

public final int httpStatusCode;

RedirectType(final int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
}
}
Loading