This is an automated email from the ASF dual-hosted git repository.

jogep pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/master by this push:
     new 2908d74  Minor code optimization in jasper reports result
2908d74 is described below

commit 2908d746ce2ac2a6a5298ef0ecef7bff359fabb2
Author: Johannes Geppert <jo...@apache.org>
AuthorDate: Sun Feb 10 16:15:38 2019 +0100

    Minor code optimization in jasper reports result
---
 .../views/jasperreports/JasperReportsResult.java   | 129 ++++++++++++---------
 .../views/jasperreports/ValueStackShadowMap.java   |  23 ++--
 2 files changed, 81 insertions(+), 71 deletions(-)

diff --git 
a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java
 
b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java
index 4eeb52d..3fbdf98 100644
--- 
a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java
+++ 
b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java
@@ -20,11 +20,22 @@ package org.apache.struts2.views.jasperreports;
 
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.util.ValueStack;
-
-import net.sf.jasperreports.engine.*;
-import net.sf.jasperreports.engine.export.*;
+import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JRExporter;
+import net.sf.jasperreports.engine.JRExporterParameter;
+import net.sf.jasperreports.engine.JRParameter;
+import net.sf.jasperreports.engine.JasperFillManager;
+import net.sf.jasperreports.engine.JasperPrint;
+import net.sf.jasperreports.engine.JasperReport;
+import net.sf.jasperreports.engine.export.HtmlExporter;
+import net.sf.jasperreports.engine.export.JRCsvExporter;
+import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
+import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
+import net.sf.jasperreports.engine.export.JRPdfExporter;
+import net.sf.jasperreports.engine.export.JRRtfExporter;
+import net.sf.jasperreports.engine.export.JRXlsExporter;
+import net.sf.jasperreports.engine.export.JRXmlExporter;
 import net.sf.jasperreports.engine.util.JRLoader;
-
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -35,7 +46,6 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
@@ -112,7 +122,7 @@ import java.util.TimeZone;
  * &lt;/result&gt;
  * <!-- END SNIPPET: example1 -->
  * </pre>
- *
+ * <p>
  * or for pdf
  *
  * <pre>
@@ -128,7 +138,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
 
     private static final long serialVersionUID = -2523174799621182907L;
 
-    private final static Logger LOG = 
LogManager.getLogger(JasperReportsResult.class);
+    private static final Logger LOG = 
LogManager.getLogger(JasperReportsResult.class);
 
     protected String dataSource;
     protected String format;
@@ -202,9 +212,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
     }
 
     /**
-     * set time zone id
-     *
-     * @param timeZone
+     * @param timeZone set time zone id
      */
     public void setTimeZone(final String timeZone) {
         this.timeZone = timeZone;
@@ -244,8 +252,10 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
 
         LOG.debug("Creating JasperReport for dataSource = {}, format = {}", 
dataSource, format);
 
-        HttpServletRequest request = (HttpServletRequest) 
invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
-        HttpServletResponse response = (HttpServletResponse) 
invocation.getInvocationContext().get(ServletActionContext.HTTP_RESPONSE);
+        HttpServletRequest request = (HttpServletRequest) 
invocation.getInvocationContext()
+                .get(ServletActionContext.HTTP_REQUEST);
+        HttpServletResponse response = (HttpServletResponse) 
invocation.getInvocationContext()
+                .get(ServletActionContext.HTTP_RESPONSE);
 
         // Handle IE special case: it sends a "contype" request first.
         // TODO Set content type to config settings?
@@ -265,8 +275,9 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
         ValueStackDataSource stackDataSource = null;
 
         Connection conn = (Connection) stack.findValue(connection);
-        if (conn == null)
+        if (conn == null) {
             stackDataSource = new ValueStackDataSource(stack, dataSource, 
wrapField);
+        }
 
         if ("https".equalsIgnoreCase(request.getScheme())) {
             // set the the HTTP Header to work around IE SSL weirdness
@@ -279,9 +290,10 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
         // Determine the directory that the report file is in and set the 
reportDirectory parameter
         // For WW 2.1.7:
         //  ServletContext servletContext = ((ServletConfig) 
invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONFIG)).getServletContext();
-        ServletContext servletContext = (ServletContext) 
invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONTEXT);
+        ServletContext servletContext = (ServletContext) 
invocation.getInvocationContext()
+                .get(ServletActionContext.SERVLET_CONTEXT);
         String systemId = servletContext.getRealPath(finalLocation);
-        Map parameters = new ValueStackShadowMap(stack);
+        Map<String, Object> parameters = new ValueStackShadowMap(stack);
         File directory = new File(systemId.substring(0, 
systemId.lastIndexOf(File.separator)));
         parameters.put("reportDirectory", directory);
         parameters.put(JRParameter.REPORT_LOCALE, 
invocation.getInvocationContext().getLocale());
@@ -299,7 +311,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
         // Add any report parameters from action to param map.
         Map reportParams = (Map) stack.findValue(reportParameters);
         if (reportParams != null) {
-               LOG.debug("Found report parameters; adding to parameters...");
+            LOG.debug("Found report parameters; adding to parameters...");
             parameters.putAll(reportParams);
         }
 
@@ -311,8 +323,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
             JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new 
File(systemId));
             if (conn == null) {
                 jasperPrint = JasperFillManager.fillReport(jasperReport, 
parameters, stackDataSource);
-            }
-            else {
+            } else {
                 jasperPrint = JasperFillManager.fillReport(jasperReport, 
parameters, conn);
             }
         } catch (JRException e) {
@@ -323,7 +334,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
         // Export the print object to the desired output format
         try {
             if (contentDisposition != null || documentName != null) {
-                final StringBuffer tmp = new StringBuffer();
+                final StringBuilder tmp = new StringBuilder();
                 tmp.append((contentDisposition == null) ? "inline" : 
contentDisposition);
 
                 if (documentName != null) {
@@ -336,40 +347,48 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
                 response.setHeader("Content-disposition", tmp.toString());
             }
 
+            //TODO: replace deprecated logic
             JRExporter exporter;
 
-            if (format.equals(FORMAT_PDF)) {
-                response.setContentType("application/pdf");
-                exporter = new JRPdfExporter();
-            } else if (format.equals(FORMAT_CSV)) {
-                response.setContentType("text/csv");
-                exporter = new JRCsvExporter();
-            } else if (format.equals(FORMAT_HTML)) {
-                response.setContentType("text/html");
-
-                // IMAGES_MAPS seems to be only supported as "backward 
compatible" from JasperReports 1.1.0
-
-                Map imagesMap = new HashMap();
-                request.getSession(true).setAttribute("IMAGES_MAP", imagesMap);
-
-                exporter = new HtmlExporter();
-                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, 
imagesMap);
-                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, 
request.getContextPath() + imageServletUrl);
-
-                // Needed to support chart images:
-                exporter.setParameter(JRExporterParameter.JASPER_PRINT, 
jasperPrint);
-                
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", 
jasperPrint);
-            } else if (format.equals(FORMAT_XLS)) {
-                response.setContentType("application/vnd.ms-excel");
-                exporter = new JRXlsExporter();
-            } else if (format.equals(FORMAT_XML)) {
-                response.setContentType("text/xml");
-                exporter = new JRXmlExporter();
-            } else if (format.equals(FORMAT_RTF)) {
-                response.setContentType("application/rtf");
-                exporter = new JRRtfExporter();
-            } else {
-                throw new ServletException("Unknown report format: " + format);
+            switch (format) {
+                case FORMAT_PDF:
+                    response.setContentType("application/pdf");
+                    exporter = new JRPdfExporter();
+                    break;
+                case FORMAT_CSV:
+                    response.setContentType("text/csv");
+                    exporter = new JRCsvExporter();
+                    break;
+                case FORMAT_HTML:
+                    response.setContentType("text/html");
+
+                    // IMAGES_MAPS seems to be only supported as "backward 
compatible" from JasperReports 1.1.0
+
+                    Map imagesMap = new HashMap();
+                    request.getSession(true).setAttribute("IMAGES_MAP", 
imagesMap);
+
+                    exporter = new HtmlExporter();
+                    exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, 
imagesMap);
+                    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, 
request.getContextPath() + imageServletUrl);
+
+                    // Needed to support chart images:
+                    exporter.setParameter(JRExporterParameter.JASPER_PRINT, 
jasperPrint);
+                    
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", 
jasperPrint);
+                    break;
+                case FORMAT_XLS:
+                    response.setContentType("application/vnd.ms-excel");
+                    exporter = new JRXlsExporter();
+                    break;
+                case FORMAT_XML:
+                    response.setContentType("text/xml");
+                    exporter = new JRXmlExporter();
+                    break;
+                case FORMAT_RTF:
+                    response.setContentType("application/rtf");
+                    exporter = new JRRtfExporter();
+                    break;
+                default:
+                    throw new ServletException("Unknown report format: " + 
format);
             }
 
             Map exportParams = (Map) stack.findValue(exportParameters);
@@ -421,14 +440,15 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
      * @param invocation Current invocation.
      * @throws Exception on initialization error.
      */
-    private void initializeProperties(ActionInvocation invocation) throws 
Exception {
+    private void initializeProperties(ActionInvocation invocation) {
         if (dataSource == null && connection == null) {
             String message = "No dataSource specified...";
             LOG.error(message);
             throw new RuntimeException(message);
         }
-        if (dataSource != null)
+        if (dataSource != null) {
             dataSource = conditionalParse(dataSource, invocation);
+        }
 
         format = conditionalParse(format, invocation);
         if (StringUtils.isEmpty(format)) {
@@ -453,8 +473,7 @@ public class JasperReportsResult extends 
StrutsResultSupport implements JasperRe
      * @param jasperPrint The Print object to render as CSV
      * @param exporter    The exporter to use to export the report
      * @return A CSV formatted report
-     * @throws net.sf.jasperreports.engine.JRException
-     *          If there is a problem running the report
+     * @throws net.sf.jasperreports.engine.JRException If there is a problem 
running the report
      */
     private ByteArrayOutputStream exportReportToBytes(JasperPrint jasperPrint, 
JRExporter exporter) throws JRException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
diff --git 
a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/ValueStackShadowMap.java
 
b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/ValueStackShadowMap.java
index c6e3766..f91ecc1 100644
--- 
a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/ValueStackShadowMap.java
+++ 
b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/ValueStackShadowMap.java
@@ -21,26 +21,19 @@ package org.apache.struts2.views.jasperreports;
 import com.opensymphony.xwork2.util.ValueStack;
 
 import java.util.HashMap;
-import java.util.Set;
 
 
 /**
  * Ported to Struts:
- *
  */
-public class ValueStackShadowMap extends HashMap {
+public class ValueStackShadowMap extends HashMap<String, Object> {
 
     private static final long serialVersionUID = -167109778490907240L;
 
     /**
      * valueStack reference
      */
-    ValueStack valueStack;
-
-    /**
-     * entries reference
-     */
-    Set entries;
+    transient ValueStack valueStack;
 
 
     /**
@@ -60,13 +53,11 @@ public class ValueStackShadowMap extends HashMap {
      * @return <tt>true</tt>, if contains key, <tt>false</tt> otherwise.
      * @see java.util.HashMap#containsKey
      */
-    public boolean containsKey(Object key) {
+    public boolean containsKey(String key) {
         boolean hasKey = super.containsKey(key);
 
-        if (!hasKey) {
-            if (valueStack.findValue((String) key) != null) {
-                hasKey = true;
-            }
+        if (!hasKey && valueStack.findValue(key) != null) {
+            hasKey = true;
         }
 
         return hasKey;
@@ -79,10 +70,10 @@ public class ValueStackShadowMap extends HashMap {
      * @return value - The object from HashMap or if null, from the valueStack.
      * @see java.util.HashMap#get
      */
-    public Object get(Object key) {
+    public Object get(String key) {
         Object value = super.get(key);
 
-        if ((value == null) && key instanceof String) {
+        if ((value == null)) {
             value = valueStack.findValue((String) key);
         }
 

Reply via email to