struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1311895 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/dispatcher/PlainTextResult.java test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java
Date Tue, 10 Apr 2012 18:07:16 GMT
Author: lukaszlenart
Date: Tue Apr 10 18:07:16 2012
New Revision: 1311895

URL: http://svn.apache.org/viewvc?rev=1311895&view=rev
Log:
WW-3795 allows specify location as a relative for result type plainText and add possibility
to extend class by overriding protected methods

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/PlainTextResult.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/PlainTextResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/PlainTextResult.java?rev=1311895&r1=1311894&r2=1311895&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/PlainTextResult.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/PlainTextResult.java
Tue Apr 10 18:07:16 2012
@@ -27,6 +27,7 @@ import com.opensymphony.xwork2.util.logg
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
@@ -112,53 +113,27 @@ public class PlainTextResult extends Str
      * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String,
com.opensymphony.xwork2.ActionInvocation)
      */
     protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception
{
-
         // verify charset
-        Charset charset = null;
-        if (charSet != null) {
-            if (Charset.isSupported(charSet)) {
-                charset = Charset.forName(charSet);
-            }
-            else {
-                if (LOG.isWarnEnabled()) {
-                    LOG.warn("charset ["+charSet+"] is not recognized ");
-                }
-                charset = null;
-            }
-        }
+        Charset charset = readCharset();
 
         HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
-        ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);
-
-
-        if (charset != null) {
-            response.setContentType("text/plain; charset="+charSet);
-        }
-        else {
-            response.setContentType("text/plain");
-        }
-        response.setHeader("Content-Disposition", "inline");
 
+        applyCharset(charset, response);
+        applyAdditionalHeaders(response);
+        String location = adjustLocation(finalLocation);
 
         PrintWriter writer = response.getWriter();
         InputStreamReader reader = null;
         try {
-        	InputStream resourceAsStream = servletContext.getResourceAsStream(finalLocation);
+            InputStream resourceAsStream = readStream(invocation, location);
+            logWrongStream(finalLocation, resourceAsStream);
             if (charset != null) {
                 reader = new InputStreamReader(resourceAsStream, charset);
             } else {
                 reader = new InputStreamReader(resourceAsStream);
             }
-            if (resourceAsStream == null) {
-                if (LOG.isWarnEnabled()) {
-            		LOG.warn("resource at location ["+finalLocation+"] cannot be obtained (return
null) from ServletContext !!! ");
-                }
-            } else {
-                char[] buffer = new char[BUFFER_SIZE];
-                int charRead;
-                while((charRead = reader.read(buffer)) != -1) {
-                    writer.write(buffer, 0, charRead);
-                }
+            if (resourceAsStream != null) {
+                sendStream(writer, reader);
             }
         } finally {
             if (reader != null)
@@ -169,4 +144,60 @@ public class PlainTextResult extends Str
             }
         }
     }
+
+    protected InputStream readStream(ActionInvocation invocation, String location) {
+        ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);
+        return servletContext.getResourceAsStream(location);
+    }
+
+    protected void logWrongStream(String finalLocation, InputStream resourceAsStream) {
+        if (resourceAsStream == null) {
+            if (LOG.isWarnEnabled()) {
+                LOG.warn("Resource at location [" + finalLocation + "] cannot be obtained
(return null) from ServletContext !!! ");
+            }
+        }
+    }
+
+    protected void sendStream(PrintWriter writer, InputStreamReader reader) throws IOException
{
+        char[] buffer = new char[BUFFER_SIZE];
+        int charRead;
+        while((charRead = reader.read(buffer)) != -1) {
+            writer.write(buffer, 0, charRead);
+        }
+    }
+
+    protected String adjustLocation(String location) {
+        if (location.charAt(0) != '/') {
+            return "/" + location;
+        }
+        return location;
+    }
+
+    protected void applyAdditionalHeaders(HttpServletResponse response) {
+        response.setHeader("Content-Disposition", "inline");
+    }
+
+    protected void applyCharset(Charset charset, HttpServletResponse response) {
+        if (charset != null) {
+            response.setContentType("text/plain; charset=" + charSet);
+        } else {
+            response.setContentType("text/plain");
+        }
+    }
+
+    protected Charset readCharset() {
+        Charset charset = null;
+        if (charSet != null) {
+            if (Charset.isSupported(charSet)) {
+                charset = Charset.forName(charSet);
+            } else {
+                if (LOG.isWarnEnabled()) {
+                    LOG.warn("charset [" + charSet + "] is not recognized ");
+                }
+                charset = null;
+            }
+        }
+        return charset;
+    }
+
 }

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java?rev=1311895&r1=1311894&r2=1311895&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java
(original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/PlainTextResultTest.java
Tue Apr 10 18:07:16 2012
@@ -21,23 +21,19 @@
 
 package org.apache.struts2.dispatcher;
 
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-import junit.framework.TestCase;
-
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.mock.MockActionInvocation;
+import com.opensymphony.xwork2.util.ClassLoaderUtil;
+import com.opensymphony.xwork2.util.ValueStack;
 import org.apache.struts2.StrutsStatics;
 import org.apache.struts2.StrutsTestCase;
 import org.apache.struts2.views.jsp.AbstractUITagTest;
 import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
 import org.apache.struts2.views.jsp.StrutsMockServletContext;
 
-import com.opensymphony.xwork2.util.ClassLoaderUtil;
-import com.opensymphony.xwork2.util.ValueStackFactory;
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.mock.MockActionInvocation;
-import com.opensymphony.xwork2.util.ValueStack;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 
 /**
  * Test case for PlainTextResult.
@@ -80,6 +76,29 @@ public class PlainTextResultTest extends
         }
     }
 
+    public void testPlainTextWithoutSlash() throws Exception {
+        PlainTextResult result = new PlainTextResult();
+        result.setLocation("someJspFile.jsp");
+
+        response.setExpectedContentType("text/plain");
+        response.setExpectedHeader("Content-Disposition", "inline");
+        InputStream jspResourceInputStream =
+            ClassLoaderUtil.getResourceAsStream("org/apache/struts2/dispatcher/someJspFile.jsp",
PlainTextResultTest.class);
+
+
+        try {
+            servletContext.setResourceAsStream(jspResourceInputStream);
+            result.execute(invocation);
+
+            String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
+            String e = AbstractUITagTest.normalize(readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"),
true);
+            assertEquals(r, e);
+        }
+        finally {
+            jspResourceInputStream.close();
+        }
+    }
+
     public void testPlainTextWithEncoding() throws Exception {
         PlainTextResult result = new PlainTextResult();
         result.setLocation("/someJspFile.jsp");



Mime
View raw message