Adding more friendly result constructors and setters, changed

xwork dep to beta 1
WW-1463 WW-1453



git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@454455 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2006-10-09 18:49:38 +00:00
parent 6e730e7ca1
commit f925e95f05
19 changed files with 260 additions and 84 deletions
+1 -11
View File
@@ -36,20 +36,10 @@
<dependencies>
<!-- unreferenced dependencies:
* clover, mockobjects, xdoclet : only needed for build
* cewolf : only used in demos
* quickstart : because i'm lazy, and because it's unlikely to be needed to build a
project using ww
* w3c.dom : needed for the xslt : this is all included in jdk1.5
- classes missing in jdk1.4 are org.w3c.dom.TypeInfo,
org.w3c.dom.UserDataHandler, org.w3c.dom.DOMConfiguration
- this should be configurable with <profiles>
-->
<dependency>
<groupId>opensymphony</groupId>
<artifactId>xwork</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.0-beta-1</version>
</dependency>
<dependency>
@@ -75,8 +75,19 @@ public class HttpHeaderResult implements Result {
private boolean parse = true;
private Map headers;
private Map<String,String> headers;
private int status = -1;
public HttpHeaderResult() {
super();
headers = new HashMap<String,String>();
}
public HttpHeaderResult(int status) {
this();
this.status = status;
this.parse = false;
}
/**
@@ -85,10 +96,6 @@ public class HttpHeaderResult implements Result {
* @return a Map of all HTTP headers.
*/
public Map getHeaders() {
if (headers == null) {
headers = new HashMap();
}
return headers;
}
@@ -98,8 +105,9 @@ public class HttpHeaderResult implements Result {
* @param parse <tt>true</tt> if HTTP header values should be evaluated agains the ValueStack, <tt>false</tt>
* otherwise.
*/
public void setParse(boolean parse) {
public HttpHeaderResult setParse(boolean parse) {
this.parse = parse;
return this;
}
/**
@@ -108,8 +116,19 @@ public class HttpHeaderResult implements Result {
* @param status the Http status code
* @see javax.servlet.http.HttpServletResponse#setStatus(int)
*/
public void setStatus(int status) {
public HttpHeaderResult setStatus(int status) {
this.status = status;
return this;
}
/**
* Adds an HTTP header to the response
* @param name
* @param value
*/
public HttpHeaderResult addHeader(String name, String value) {
headers.put(name, value);
return this;
}
/**
@@ -79,6 +79,13 @@ public class PlainTextResult extends StrutsResultSupport {
private String charSet;
public PlainTextResult() {
super();
}
public PlainTextResult(String location) {
super(location);
}
/**
* Set the character set
@@ -94,8 +101,9 @@ public class PlainTextResult extends StrutsResultSupport {
*
* @param charSet The character set
*/
public void setCharSet(String charSet) {
public PlainTextResult setCharSet(String charSet) {
this.charSet = charSet;
return this;
}
/* (non-Javadoc)
@@ -132,13 +140,13 @@ public class PlainTextResult extends StrutsResultSupport {
InputStreamReader reader = null;
try {
if (charset != null) {
reader = new InputStreamReader(servletContext.getResourceAsStream(location), charset);
reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset);
}
else {
reader = new InputStreamReader(servletContext.getResourceAsStream(location));
reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation));
}
if (reader == null) {
_log.warn("resource at location ["+location+"] cannot be obtained (return null) from ServletContext !!! ");
_log.warn("resource at location ["+finalLocation+"] cannot be obtained (return null) from ServletContext !!! ");
}
else {
char[] buffer = new char[BUFFER_SIZE];
@@ -132,7 +132,28 @@ public class ServletActionRedirectResult extends ServletRedirectResult {
protected String actionName;
protected String namespace;
protected String method;
private Map<String, String> requestParameters = new HashMap<String, String>();
public ServletActionRedirectResult() {
super();
}
public ServletActionRedirectResult(String actionName) {
this(null, actionName, null);
}
public ServletActionRedirectResult(String actionName, String method) {
this(null, actionName, method);
}
public ServletActionRedirectResult(String namespace, String actionName, String method) {
super(null);
this.namespace = namespace;
this.actionName = actionName;
this.method = method;
}
protected List<String> prohibitedResultParam = Arrays.asList(new String[] {
DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location",
"prependServletContext" });
@@ -154,24 +175,26 @@ public class ServletActionRedirectResult extends ServletRedirectResult {
method = conditionalParse(method, invocation);
}
Map<String, String> requestParameters = new HashMap<String, String>();
ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(
invocation.getResultCode());
Map resultConfigParams = resultConfig.getParams();
for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
if (! prohibitedResultParam.contains(e.getKey())) {
requestParameters.put(e.getKey().toString(),
e.getValue() == null ? "":
conditionalParse(e.getValue().toString(), invocation));
}
String resultCode = invocation.getResultCode();
if (resultCode != null) {
ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(
resultCode);
Map resultConfigParams = resultConfig.getParams();
for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
if (! prohibitedResultParam.contains(e.getKey())) {
requestParameters.put(e.getKey().toString(),
e.getValue() == null ? "":
conditionalParse(e.getValue().toString(), invocation));
}
}
}
ActionMapper mapper = ActionMapperFactory.getMapper();
StringBuffer tmpLocation = new StringBuffer(mapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)));
UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");
location = tmpLocation.toString();
setLocation(tmpLocation.toString());
super.execute(invocation);
}
@@ -181,8 +204,9 @@ public class ServletActionRedirectResult extends ServletRedirectResult {
*
* @param actionName The name
*/
public void setActionName(String actionName) {
public ServletActionRedirectResult setActionName(String actionName) {
this.actionName = actionName;
return this;
}
/**
@@ -190,8 +214,9 @@ public class ServletActionRedirectResult extends ServletRedirectResult {
*
* @param namespace The namespace
*/
public void setNamespace(String namespace) {
public ServletActionRedirectResult setNamespace(String namespace) {
this.namespace = namespace;
return this;
}
/**
@@ -199,7 +224,20 @@ public class ServletActionRedirectResult extends ServletRedirectResult {
*
* @param method The method
*/
public void setMethod(String method) {
public ServletActionRedirectResult setMethod(String method) {
this.method = method;
return this;
}
/**
* Adds a request parameter to be added to the redirect url
*
* @param key The parameter name
* @param value The parameter value
*/
public ServletActionRedirectResult addParameter(String key, Object value) {
requestParameters.put(key, String.valueOf(value));
return this;
}
}
@@ -88,7 +88,14 @@ public class ServletDispatcherResult extends StrutsResultSupport {
private static final Log log = LogFactory.getLog(ServletDispatcherResult.class);
public ServletDispatcherResult() {
super();
}
public ServletDispatcherResult(String location) {
super(location);
}
/**
* Dispatches to the given location. Does its forward via a RequestDispatcher. If the
* dispatch fails a 404 error will be sent back in the http response.
@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapperFactory;
import org.springframework.beans.factory.config.SetFactoryBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
@@ -80,14 +81,23 @@ public class ServletRedirectResult extends StrutsResultSupport {
protected boolean prependServletContext = true;
public ServletRedirectResult() {
super();
}
public ServletRedirectResult(String location) {
super(location);
}
/**
* Sets whether or not to prepend the servlet context path to the redirected URL.
*
* @param prependServletContext <tt>true</tt> to prepend the location with the servlet context path,
* <tt>false</tt> otherwise.
*/
public void setPrependServletContext(boolean prependServletContext) {
public ServletRedirectResult setPrependServletContext(boolean prependServletContext) {
this.prependServletContext = prependServletContext;
return this;
}
/**
@@ -83,8 +83,17 @@ public class StreamResult extends StrutsResultSupport {
protected String contentLength;
protected String contentDisposition = "inline";
protected String inputName = "inputStream";
protected InputStream inputStream;
protected int bufferSize = 1024;
public StreamResult() {
super();
}
public StreamResult(InputStream in) {
this.inputStream = in;
}
/**
* @return Returns the bufferSize.
*/
@@ -95,8 +104,9 @@ public class StreamResult extends StrutsResultSupport {
/**
* @param bufferSize The bufferSize to set.
*/
public void setBufferSize(int bufferSize) {
public StreamResult setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
return this;
}
/**
@@ -109,8 +119,9 @@ public class StreamResult extends StrutsResultSupport {
/**
* @param contentType The contentType to set.
*/
public void setContentType(String contentType) {
public StreamResult setContentType(String contentType) {
this.contentType = contentType;
return this;
}
/**
@@ -123,8 +134,9 @@ public class StreamResult extends StrutsResultSupport {
/**
* @param contentLength The contentLength to set.
*/
public void setContentLength(String contentLength) {
public StreamResult setContentLength(String contentLength) {
this.contentLength = contentLength;
return this;
}
/**
@@ -137,8 +149,9 @@ public class StreamResult extends StrutsResultSupport {
/**
* @param contentDisposition the Content-disposition header value to use.
*/
public void setContentDisposition(String contentDisposition) {
public StreamResult setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
return this;
}
/**
@@ -151,8 +164,9 @@ public class StreamResult extends StrutsResultSupport {
/**
* @param inputName The inputName to set.
*/
public void setInputName(String inputName) {
public StreamResult setInputName(String inputName) {
this.inputName = inputName;
return this;
}
/**
@@ -160,14 +174,15 @@ public class StreamResult extends StrutsResultSupport {
*/
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
InputStream oInput = null;
OutputStream oOutput = null;
try {
// Find the inputstream from the invocation variable stack
oInput = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
if (oInput == null) {
if (inputStream == null) {
// Find the inputstream from the invocation variable stack
inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
}
if (inputStream == null) {
String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
"Check the <param name=\"inputName\"> tag specified for this action.");
log.error(msg);
@@ -212,7 +227,7 @@ public class StreamResult extends StrutsResultSupport {
log.debug("Streaming to output buffer +++ START +++");
byte[] oBuff = new byte[bufferSize];
int iSize;
while (-1 != (iSize = oInput.read(oBuff))) {
while (-1 != (iSize = inputStream.read(oBuff))) {
oOutput.write(oBuff, 0, iSize);
}
log.debug("Streaming to output buffer +++ END +++");
@@ -221,7 +236,7 @@ public class StreamResult extends StrutsResultSupport {
oOutput.flush();
}
finally {
if (oInput != null) oInput.close();
if (inputStream != null) inputStream.close();
if (oOutput != null) oOutput.close();
}
}
@@ -104,11 +104,25 @@ public abstract class StrutsResultSupport implements Result, StrutsStatics {
/** The default parameter */
public static final String DEFAULT_PARAM = "location";
protected boolean parse = true;
protected boolean encode = false;
protected String location;
protected String lastFinalLocation;
private boolean parse;
private boolean encode;
private String location;
private String lastFinalLocation;
public StrutsResultSupport() {
this(null, true, false);
}
public StrutsResultSupport(String location) {
this(location, false, false);
}
public StrutsResultSupport(String location, boolean parse, boolean encode) {
this.location = location;
this.parse = parse;
this.encode = encode;
}
/**
* The location to go to after action execution. This could be a JSP page or another action.
* The location can contain OGNL expressions which will be evaulated if the <tt>parse</tt>
@@ -86,6 +86,13 @@ public class VelocityResult extends StrutsResultSupport {
private static final Log log = LogFactory.getLog(VelocityResult.class);
public VelocityResult() {
super();
}
public VelocityResult(String location) {
super(location);
}
/**
* Creates a Velocity context from the action, loads a Velocity template and executes the
@@ -57,6 +57,14 @@ public class PortletResult extends StrutsResultSupport {
private String contentType = "text/html";
private String title;
public PortletResult() {
super();
}
public PortletResult(String location) {
super(location);
}
/**
* Execute the result. Obtains the
@@ -95,6 +95,14 @@ public class PortletVelocityResult extends StrutsResultSupport {
private static final Log log = LogFactory
.getLog(PortletVelocityResult.class);
public PortletVelocityResult() {
super();
}
public PortletVelocityResult(String location) {
super(location);
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
*/
@@ -106,9 +106,17 @@ public class FreemarkerResult extends StrutsResultSupport {
protected String location;
private String pContentType = "text/html";
public FreemarkerResult() {
super();
}
public FreemarkerResult(String location) {
super(location);
}
public void setContentType(String aContentType) {
public FreemarkerResult setContentType(String aContentType) {
pContentType = aContentType;
return this;
}
/**
@@ -65,8 +65,17 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
private String pContentType = "text/html";
public void setContentType(String aContentType) {
public PortletFreemarkerResult() {
super();
}
public PortletFreemarkerResult(String location) {
super(location);
}
public PortletFreemarkerResult setContentType(String aContentType) {
pContentType = aContentType;
return this;
}
/**
@@ -201,6 +201,11 @@ public class XSLTResult implements Result {
templatesCache = new HashMap<String, Templates>();
noCache = Settings.get("struts.xslt.nocache").trim().equalsIgnoreCase("true");
}
public XSLTResult(String stylesheetLocation) {
this();
setStylesheetLocation(stylesheetLocation);
}
/**
* @deprecated Use #setStylesheetLocation(String)
@@ -209,11 +214,11 @@ public class XSLTResult implements Result {
setStylesheetLocation(location);
}
public void setStylesheetLocation(String location) {
public XSLTResult setStylesheetLocation(String location) {
if (location == null)
throw new IllegalArgumentException("Null location");
System.out.println("location = " + location);
this.stylesheetLocation = location;
return this;
}
public String getStylesheetLocation() {
@@ -225,8 +230,9 @@ public class XSLTResult implements Result {
*
* @param parse
*/
public void setParse(boolean parse) {
public XSLTResult setParse(boolean parse) {
this.parse = parse;
return this;
}
public void execute(ActionInvocation invocation) throws Exception {
@@ -128,33 +128,46 @@ public class JasperReportsResult extends StrutsResultSupport implements JasperRe
protected String delimiter;
protected String imageServletUrl = "/images/";
public JasperReportsResult() {
super();
}
public JasperReportsResult(String location) {
super(location);
}
public String getImageServletUrl() {
return imageServletUrl;
}
public void setImageServletUrl(final String imageServletUrl) {
public JasperReportsResult setImageServletUrl(final String imageServletUrl) {
this.imageServletUrl = imageServletUrl;
return this;
}
public void setDataSource(String dataSource) {
public JasperReportsResult setDataSource(String dataSource) {
this.dataSource = dataSource;
return this;
}
public void setFormat(String format) {
public JasperReportsResult setFormat(String format) {
this.format = format;
return this;
}
public void setDocumentName(String documentName) {
public JasperReportsResult setDocumentName(String documentName) {
this.documentName = documentName;
return this;
}
public void setContentDisposition(String contentDisposition) {
public JasperReportsResult setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
return this;
}
public void setDelimiter(String delimiter) {
public JasperReportsResult setDelimiter(String delimiter) {
this.delimiter = delimiter;
return this;
}
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
@@ -42,15 +42,25 @@ public class ChartResult implements Result {
private int height;
private int width;
public ChartResult() {
super();
}
public ChartResult(JFreeChart chart, int height, int width) {
setChart(chart);
this.height = height;
this.width = width;
}
/**
* Sets the JFreeChart to use.
*
* @param chart a JFreeChart object.
*/
public void setChart(JFreeChart chart) {
public ChartResult setChart(JFreeChart chart) {
this.chart = chart;
chartSet = true;
return this;
}
/**
@@ -58,8 +68,9 @@ public class ChartResult implements Result {
*
* @param height the height of the chart in pixels.
*/
public void setHeight(int height) {
public ChartResult setHeight(int height) {
this.height = height;
return this;
}
/**
@@ -67,8 +78,9 @@ public class ChartResult implements Result {
*
* @param width the width of the chart in pixels.
*/
public void setWidth(int width) {
public ChartResult setWidth(int width) {
this.width = width;
return this;
}
/**
@@ -33,6 +33,13 @@ public class FacesResult extends StrutsResultSupport implements Result {
private static final long serialVersionUID = -3548970638740937804L;
public FacesResult() {
super();
}
public FacesResult(String location) {
super(location);
}
/**
* Checks to see if we need to build a new JSF ViewId from the Struts Result
* config and then renders the result by delegating to the
@@ -84,6 +84,13 @@ public class TilesResult extends ServletDispatcherResult {
protected ActionInvocation invocation;
private DefinitionsFactory definitionsFactory;
public TilesResult() {
super();
}
public TilesResult(String location) {
super(location);
}
/**
* Dispatches to the given location. Does its forward via a RequestDispatcher. If the
* dispatch fails a 404 error will be sent back in the http response.
@@ -94,7 +101,7 @@ public class TilesResult extends ServletDispatcherResult {
* HTTP request.
*/
public void doExecute(String location, ActionInvocation invocation) throws Exception {
this.location = location;
setLocation(location);
this.invocation = invocation;
HttpServletRequest request = ServletActionContext.getRequest();
@@ -106,7 +113,7 @@ public class TilesResult extends ServletDispatcherResult {
(DefinitionsFactory) servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY);
// get component definition
ComponentDefinition definition = getComponentDefinition(this.definitionsFactory, request);
ComponentDefinition definition = getComponentDefinition(location, this.definitionsFactory, request);
if (definition == null) {
throw new ServletException("No Tiles definition found for name '" + location + "'");
}
@@ -150,7 +157,7 @@ public class TilesResult extends ServletDispatcherResult {
* @param request current HTTP request
* @return the component definition
*/
protected ComponentDefinition getComponentDefinition(DefinitionsFactory factory, HttpServletRequest request)
protected ComponentDefinition getComponentDefinition(String location, DefinitionsFactory factory, HttpServletRequest request)
throws Exception {
ComponentDefinitions definitions = factory.readDefinitions();
return definitions.getDefinition(location, deduceLocale(request));
+1 -11
View File
@@ -256,16 +256,6 @@
</plugins>
</reporting>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>xwork</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>snapshots-maven-codehaus</id>
@@ -297,7 +287,7 @@
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
<releases>
<enabled>false</enabled>
<enabled>true</enabled>
</releases>
<url>http://maven.opensymphony.com</url>
</repository>