Skip to content

Commit

Permalink
Merge pull request #253 from warrenc5/master
Browse files Browse the repository at this point in the history
Add support for 307 Temprorary & 308 Permanent redirect
  • Loading branch information
paultuckey committed Jul 1, 2023
2 parents a7f371b + f70c8a6 commit 6dad8dc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Expand Up @@ -60,6 +60,8 @@ public class NormalRewrittenUrl implements RewrittenUrl {
private boolean redirect = false;
private boolean permanentRedirect = false;
private boolean temporaryRedirect = false;
private boolean permanentRedirect308 = false;
private boolean temporaryRedirect307 = false;
private boolean preInclude = false;
private boolean postInclude = false;
private boolean proxy = false;
Expand All @@ -72,7 +74,7 @@ public class NormalRewrittenUrl implements RewrittenUrl {
private ServletContext targetContext = null;

/**
* Holds information about the rewirtten url.
* Holds information about the rewritten url.
*
* @param ruleExecutionOutput the url to rewrite to
*/
Expand Down Expand Up @@ -135,6 +137,22 @@ public boolean isTemporaryRedirect() {
return temporaryRedirect;
}

public void set308PermanentRedirect(boolean permanentRedirect308) {
this.permanentRedirect308 = permanentRedirect308;
}

public boolean is308PermanentRedirect() {
return permanentRedirect308;
}

public void set307TemporaryRedirect(boolean temporaryRedirect307) {
this.temporaryRedirect307 = temporaryRedirect307;
}

public boolean is307TemporaryRedirect() {
return temporaryRedirect307;
}

public void setEncode(boolean b) {
encode = b;
}
Expand Down Expand Up @@ -272,6 +290,34 @@ public boolean doRewrite(final HttpServletRequest hsRequest,
}
requestRewritten = true;

} else if (is307TemporaryRedirect()) {
if (hsResponse.isCommitted()) {
log.error("response is committed cannot temporary redirect (307) to " + target +
" (check you haven't done anything to the response (ie, written to it) before here)");
} else {
if (isEncode()) {
target = hsResponse.encodeRedirectURL(target);
}
hsResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
hsResponse.setHeader("Location", target);
if (log.isDebugEnabled()) log.debug("temporarily redirected (with response 307) to " + target);
}
requestRewritten = true;

} else if (is308PermanentRedirect()) {
if (hsResponse.isCommitted()) {
log.error("response is committed cannot permanent redirect (308) to " + target +
" (check you haven't done anything to the response (ie, written to it) before here)");
} else {
if (isEncode()) {
target = hsResponse.encodeRedirectURL(target);
}
hsResponse.setStatus(308);
hsResponse.setHeader("Location", target);
if (log.isDebugEnabled()) log.debug("permanently redirected (with response 308) to " + target);
}
requestRewritten = true;

} else if (isProxy()) {
if (hsResponse.isCommitted()) {
log.error("response is committed. cannot proxy " + target + ". Check that you havn't written to the response before.");
Expand Down
Expand Up @@ -65,6 +65,8 @@ public class NormalRule extends RuleBase implements Rule {
public static final short TO_TYPE_PRE_INCLUDE = 4;
public static final short TO_TYPE_POST_INCLUDE = 5;
public static final short TO_TYPE_PROXY = 6;
public static final short TO_TYPE_307_TEMPORARY_REDIRECT = 7;
public static final short TO_TYPE_308_PERMANENT_REDIRECT = 8;

private boolean dropCookies = true;
private boolean encodeToUrl = false;
Expand Down Expand Up @@ -164,6 +166,10 @@ public void setToType(final String toTypeStr) {
toType = TO_TYPE_PERMANENT_REDIRECT;
} else if ("temporary-redirect".equals(toTypeStr)) {
toType = TO_TYPE_TEMPORARY_REDIRECT;
} else if ("308-permanent-redirect".equals(toTypeStr)) {
toType = TO_TYPE_308_PERMANENT_REDIRECT;
} else if ("307-temporary-redirect".equals(toTypeStr)) {
toType = TO_TYPE_307_TEMPORARY_REDIRECT;
} else if ("pre-include".equals(toTypeStr)) {
toType = TO_TYPE_PRE_INCLUDE;
} else if ("post-include".equals(toTypeStr)) {
Expand All @@ -187,6 +193,8 @@ public String getToType() {
if (toType == TO_TYPE_REDIRECT) return "redirect";
if (toType == TO_TYPE_PERMANENT_REDIRECT) return "permanent-redirect";
if (toType == TO_TYPE_TEMPORARY_REDIRECT) return "temporary-redirect";
if (toType == TO_TYPE_308_PERMANENT_REDIRECT) return "permanent-redirect-308";
if (toType == TO_TYPE_307_TEMPORARY_REDIRECT) return "temporary-redirect-307";
if (toType == TO_TYPE_PRE_INCLUDE) return "pre-include";
if (toType == TO_TYPE_POST_INCLUDE) return "post-include";
if (toType == TO_TYPE_PROXY) return "proxy";
Expand Down
Expand Up @@ -84,6 +84,18 @@ public static RewrittenUrl getRewritenUrl(short toType, boolean encodeToUrl, Rul
}
rewrittenRequest.setTemporaryRedirect(true);

} else if (toType == NormalRule.TO_TYPE_308_PERMANENT_REDIRECT) {
if (log.isDebugEnabled()) {
log.debug("needs to be permanently redirected (with response code 308) to " + toUrl);
}
rewrittenRequest.set308PermanentRedirect(true);

} else if (toType == NormalRule.TO_TYPE_307_TEMPORARY_REDIRECT) {
if (log.isDebugEnabled()) {
log.debug("needs to be temporarily redirected (with response code 307) to " + toUrl);
}
rewrittenRequest.set307TemporaryRedirect(true);

} else if (toType == NormalRule.TO_TYPE_PRE_INCLUDE) {
if (log.isDebugEnabled()) {
log.debug(toUrl + " needs to be pre included");
Expand Down

0 comments on commit 6dad8dc

Please sign in to comment.