portals-pluto-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msnickl...@apache.org
Subject portals-pluto git commit: Changed the temp directory used by the demo portlet.
Date Thu, 08 Feb 2018 08:35:22 GMT
Repository: portals-pluto
Updated Branches:
  refs/heads/master 13a67911e -> 89f6a59a7


Changed the temp directory used by the demo portlet.


Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/89f6a59a
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/89f6a59a
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/89f6a59a

Branch: refs/heads/master
Commit: 89f6a59a740d0a8318640ca6015e9a381c5c6b50
Parents: 13a6791
Author: Scott Nicklous <msnicklous@apache.org>
Authored: Thu Feb 8 09:32:29 2018 +0100
Committer: Scott Nicklous <msnicklous@apache.org>
Committed: Thu Feb 8 09:32:29 2018 +0100

----------------------------------------------------------------------
 .../portals/samples/MultipartPortlet.java       | 76 ++++++++++++++------
 1 file changed, 56 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/89f6a59a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/MultipartPortlet.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/MultipartPortlet.java
b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/MultipartPortlet.java
index 8b1b01c..b0a1042 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/MultipartPortlet.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/MultipartPortlet.java
@@ -26,6 +26,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
@@ -60,7 +62,7 @@ public class MultipartPortlet {
    private static final Logger LOGGER = Logger.getLogger(MultipartPortlet.class.getName());
 
    private static final String JSP    = "/WEB-INF/jsp/multipartDialog.jsp";
-   private static final String TMP    = "/temp/";
+   private static final String TMP    = "/MultipartPortlet/temp/";
 
    @ActionMethod(portletName = "MultipartPortlet")
    public void handleDialog(ActionRequest req, ActionResponse resp) throws IOException, PortletException
{
@@ -107,19 +109,10 @@ public class MultipartPortlet {
          // quick solution that doesn't require additional Tomcat configuration.
 
          try {
-            String path = req.getPortletContext().getRealPath(TMP);
-            File dir = new File(path);
-            lines.add("Temp path: " + dir.getCanonicalPath());
-            if (!dir.exists()) {
-               lines.add("Creating directory. Path: " + dir.getCanonicalPath());
-               Files.createDirectories(dir.toPath());
-            }
-            String fn = TMP + part.getSubmittedFileName();
-            lines.add("Temp file: " + fn);
-            path = req.getPortletContext().getRealPath(fn);
-            File img = new File(path);
+            String fn = part.getSubmittedFileName();
+            File img = getFile(fn);
             if (img.exists()) {
-               lines.add("deleting existing temp file.");
+               lines.add("deleting existing temp file: " + img.getCanonicalPath());
                img.delete();
             }
             InputStream is = part.getInputStream();
@@ -130,6 +123,15 @@ public class MultipartPortlet {
 
          } catch (Exception e) {
             lines.add("Exception doing I/O: " + e.toString());
+            
+            txt.setLength(0);
+            txt.append("Problem getting temp file: " + e.getMessage() + "\n");
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            e.printStackTrace(pw);
+            pw.flush();
+            txt.append(sw.toString());
+            LOGGER.warning(txt.toString());
          }
       } else {
          lines.add("file part was null");
@@ -172,8 +174,7 @@ public class MultipartPortlet {
          FileInputStream fis = null;
          BufferedReader rdr = null;
          try {
-            String path = req.getPortletContext().getRealPath(fn);
-            File file = new File(path);
+            File file = getFile(fn);
             fis = new FileInputStream(file);
 
             if (ct.equals("text/plain")) {
@@ -232,10 +233,45 @@ public class MultipartPortlet {
       
       resp.setContentType(ct);
 
-      String path = req.getPortletContext().getRealPath(fn);
-      File file = new File(path);
-      OutputStream os = resp.getPortletOutputStream();
-      Files.copy(file.toPath(), os);
-      os.flush();
+      try {
+         File file = getFile(fn);
+         OutputStream os = resp.getPortletOutputStream();
+         Files.copy(file.toPath(), os);
+         os.flush();
+      } catch (Exception e) {
+         StringBuilder txt = new StringBuilder(128);
+         txt.append("Problem retrieving temp file: " + e.getMessage() + "\n");
+         StringWriter sw = new StringWriter();
+         PrintWriter pw = new PrintWriter(sw);
+         e.printStackTrace(pw);
+         pw.flush();
+         txt.append(sw.toString());
+         LOGGER.warning(txt.toString());
+      }
+      
+   }
+
+   /**
+    * Returns a File object representing the uploaded temporary file location. Note that
the file may or may not exist.
+    * The temp directories are created as necessary.
+    * 
+    * @param fn
+    *           the file name
+    * @return the File object
+    * @throws IOException 
+    */
+   private File getFile(String fn) throws IOException {
+      File tmp = null;
+
+      String path = System.getProperty("java.io.tmpdir") + TMP;
+      File dir = new File(path);
+      if (!dir.exists()) {
+         LOGGER.fine("Creating directory. Path: " + dir.getCanonicalPath());
+         Files.createDirectories(dir.toPath());
+      }
+      tmp = new File(dir, fn);
+      LOGGER.fine("Temp file: " + tmp.getCanonicalPath());
+
+      return tmp;
    }
 }


Mime
View raw message