Github user lukaszlenart commented on a diff in the pull request: https://github.com/apache/struts-extras/pull/3#discussion_r108500855 --- Diff: struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/SslOffloadAwareServletRedirectResult.java --- @@ -0,0 +1,188 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.result; + +import java.util.Collection; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.result.ServletRedirectResult; +import org.apache.struts2.views.util.UrlHelper; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.inject.Inject; + +public class SslOffloadAwareServletRedirectResult extends ServletRedirectResult { + private static final long serialVersionUID = -5384946213381645549L; + private static final Logger LOG = LogManager.getLogger(SslOffloadAwareServletRedirectResult.class); + private static final Pattern FORWARDED_PROTO_PARAM_HTTPS = Pattern.compile("[^;]proto=https[$;]"); + + private UrlHelper urlHelper; + + @Inject + public void setUrlHelper(UrlHelper urlHelper) { + this.urlHelper = urlHelper; + } + + public SslOffloadAwareServletRedirectResult() { + super(); + } + + public SslOffloadAwareServletRedirectResult(String location) { + this(location, null); + } + + public SslOffloadAwareServletRedirectResult(String location, String anchor) { + super(location, anchor); + } + + /** + * Redirects to the location specified by calling + * {@link HttpServletResponse#sendRedirect(String)}. + * + * @param finalLocation + * the location to redirect to. + * @param invocation + * an encapsulation of the action execution state. + * @throws Exception + * if an error occurs when redirecting. + */ + protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { + ActionContext ctx = invocation.getInvocationContext(); + HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); + HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); + + if (isPathUrl(finalLocation)) { + if (!finalLocation.startsWith("/")) { + ActionMapping mapping = actionMapper.getMapping(request, Dispatcher.getInstance().getConfigurationManager()); + String namespace = null; + if (mapping != null) { + namespace = mapping.getNamespace(); + } + + if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) { + finalLocation = namespace + "/" + finalLocation; + } else { + finalLocation = "/" + finalLocation; + } + } + + // if the URL's are relative to the servlet context, append the + // servlet context path + if (prependServletContext && (request.getContextPath() != null) + && (request.getContextPath().length() > 0)) { + finalLocation = request.getContextPath() + finalLocation; + } + + finalLocation = fixSchemeIfNeeded(finalLocation, request); + } + ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(invocation.getResultCode()); + if (resultConfig != null) { + Map<String, String> resultConfigParams = resultConfig.getParams(); + + List<String> prohibitedResultParams = getProhibitedResultParams(); + for (Map.Entry<String, String> e : resultConfigParams.entrySet()) { + if (!prohibitedResultParams.contains(e.getKey())) { + Collection<String> values = conditionalParseCollection(e.getValue(), invocation, + suppressEmptyParameters); + if (!suppressEmptyParameters || !values.isEmpty()) { + requestParameters.put(e.getKey(), values); + } + } + } + } + + StringBuilder tmpLocation = new StringBuilder(finalLocation); + urlHelper.buildParametersString(requestParameters, tmpLocation, "&"); + + // add the anchor + if (anchor != null) { + tmpLocation.append('#').append(anchor); + } + + finalLocation = response.encodeRedirectURL(tmpLocation.toString()); + + LOG.debug("Redirecting to finalLocation: {}", finalLocation); + + sendRedirect(response, finalLocation); + } + + protected String fixSchemeIfNeeded(String location, HttpServletRequest request) { + if (shouldFixScheme(request)) { + LOG.debug("https offloading happened, fixing redirectlocation"); + StringBuilder fixedLocation = new StringBuilder(); + fixedLocation.append("https"); + fixedLocation.append("://"); + fixedLocation.append(request.getServerName()); + if (request.getServerPort() != 80) { + fixedLocation.append(':'); + fixedLocation.append(request.getServerPort()); + } + fixedLocation.append(location); + + return fixedLocation.toString(); + } else { + return location; + } + } + + protected boolean shouldFixScheme(HttpServletRequest request) { + return "https".equals(request.getHeader("X-Forwarded-Proto")) || hasForwardedHeaderWithProtoParamHttps(request); --- End diff -- Could you extract `"https".equals(request.getHeader("X-Forwarded-Proto"))` into a method? the same as you have done for `hasForwardedHeaderWithProtoParamHttps` and make them `protected`?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@struts.apache.org For additional commands, e-mail: dev-h...@struts.apache.org