All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts2.result.PostbackResult Maven / Gradle / Ivy

/*
 * 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 com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 
 * 

* A result that renders the current request parameters as a form which * immediately submits a postback * to the specified destination. *

* *

* Parameters: *

* *
    *
  • location - http location to post the form
  • *
  • prependServletContext (true|false) - when location is relative, controls if to add Servlet Context, default "true"
  • *
  • actionName - action name to post the form (resolved as an expression)
  • *
  • namespace - action's namespace to use (resolved as an expression)
  • *
  • method - actions' method to use (resolved as an expression)
  • *
  • cache (true|false) - when set to true adds cache control headers, default "true"
  • *
  • parse (true|false) - when set to true actionName, namespace and method are parsed, default "true"
  • *
* *

* Examples: *

*
 * 
 * <action name="registerThirdParty" >
 *   <result type="postback">https://www.example.com/register</result>
 * </action>
 *
 * <action name="registerThirdParty" >
 *   <result type="postback">
 *     <param name="namespace">/secure</param>
 *     <param name="actionName">register2</param>
 *   </result>
 * </action>
 * 
 * 
*/ public class PostbackResult extends StrutsResultSupport { private static final long serialVersionUID = -2283504349296877429L; private String actionName; private String namespace; private String method; private boolean prependServletContext = true; private boolean cache = true; protected ActionMapper actionMapper; @Override 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); // Cache? if (!cache) { response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1 response.setHeader("Pragma", "no-cache"); // HTTP 1.0 response.setDateHeader("Expires", 0); // Proxies } //set contenttype @see ww-4564 response.setContentType("text/html"); // Render PrintWriter pw = new PrintWriter(response.getOutputStream()); pw.write("
"); writeFormElements(request, pw); writePrologueScript(pw); pw.write(""); pw.flush(); } @Override public void execute(ActionInvocation invocation) throws Exception { String postbackUri = makePostbackUri(invocation); setLocation(postbackUri); super.execute(invocation); } /** * Determines if the specified form input element should be included. * * @param name the input element name * @param values the input element values * @return {@code true} if included; otherwise {@code false} */ protected boolean isElementIncluded(String name, String[] values) { return !name.startsWith("action:"); } protected String makePostbackUri(ActionInvocation invocation) { ActionContext ctx = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); String postbackUri; if (actionName != null) { actionName = conditionalParse(actionName, invocation); parseLocation = false; if (namespace == null) { namespace = invocation.getProxy().getNamespace(); } else { namespace = conditionalParse(namespace, invocation); } if (method == null) { method = ""; } else { method = conditionalParse(method, invocation); } postbackUri = request.getContextPath() + actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)); } else { String location = getLocation(); // Do not prepend if the URL is a FQN if (!location.matches("^([a-zA-Z]+:)?//.*")) { // If the URL is relative to the servlet context, prepend the servlet context path if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) { location = request.getContextPath() + location; } } postbackUri = location; } return postbackUri; } @Inject public final void setActionMapper(ActionMapper mapper) { this.actionMapper = mapper; } /** * Sets the name of the destination action. * * @param actionName the action name */ public final void setActionName(String actionName) { this.actionName = actionName; } /** * Stores the option to cache the rendered intermediate page. The default * is {@code true}. * @param cache enable/disable cache */ public final void setCache(boolean cache) { this.cache = cache; } /** * Sets the method of the destination action. * * @param method the method */ public final void setMethod(String method) { this.method = method; } /** * Sets the namespace of the destination action. * * @param namespace the namespace */ public final void setNamespace(String namespace) { this.namespace = namespace; } public final void setPrependServletContext(boolean prependServletContext) { this.prependServletContext = prependServletContext; } protected void writeFormElement(PrintWriter pw, String name, String[] values) throws UnsupportedEncodingException { for (String value : values) { String encName = URLEncoder.encode(name, "UTF-8"); String encValue = URLEncoder.encode(value, "UTF-8"); pw.write(""); } } private void writeFormElements(HttpServletRequest request, PrintWriter pw) throws UnsupportedEncodingException { Map params = request.getParameterMap(); for (Map.Entry entry : params.entrySet()) { String name = entry.getKey(); String[] values = entry.getValue(); if (isElementIncluded(name, values)) { writeFormElement(pw, name, values); } } } /** * Outputs the script after the form has been emitted. The default script * is to submit the form using a JavaScript time out that immediately expires. * * @param pw the print writer */ protected void writePrologueScript(PrintWriter pw) { pw.write(""); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy