Author: jdonnerstag
Date: Sat Apr 23 09:19:45 2011
New Revision: 1096125

URL: http://svn.apache.org/viewvc?rev=1096125&view=rev
Log:
applied: merge upload progress monitoring functionality into wicket-core
Issue: WICKET-3524

Added:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
Removed:
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/MultipartRequest.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadInfo.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadWebRequest.java
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IApplicationSettings.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ApplicationSettings.java
    
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
 Sat Apr 23 09:19:45 2011
@@ -28,6 +28,8 @@ import javax.servlet.http.HttpServletReq
 
 import org.apache.wicket.Application;
 import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.settings.IApplicationSettings;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.string.StringValue;
 import org.apache.wicket.util.upload.DiskFileItemFactory;
@@ -280,13 +282,14 @@ public class MultipartServletWebRequestI
        }
 
        /**
-        * Subclasses that want to receive upload notifications should return 
true
+        * Subclasses that want to receive upload notifications should return 
true. By default it takes
+        * the value from {@link 
IApplicationSettings#isUploadProgressUpdatesEnabled()}.
         * 
         * @return true if upload status update event should be invoked
         */
        protected boolean wantUploadProgressUpdates()
        {
-               return false;
+               return 
Application.get().getApplicationSettings().isUploadProgressUpdatesEnabled();
        }
 
        /**
@@ -296,7 +299,9 @@ public class MultipartServletWebRequestI
         */
        protected void onUploadStarted(int totalBytes)
        {
+               UploadInfo info = new UploadInfo(totalBytes);
 
+               setUploadInfo(getContainerRequest(), info);
        }
 
        /**
@@ -307,7 +312,16 @@ public class MultipartServletWebRequestI
         */
        protected void onUploadUpdate(int bytesUploaded, int total)
        {
+               HttpServletRequest request = getContainerRequest();
+               UploadInfo info = getUploadInfo(request);
+               if (info == null)
+               {
+                       throw new IllegalStateException(
+                               "could not find UploadInfo object in session 
which should have been set when uploaded started");
+               }
+               info.setBytesUploaded(bytesUploaded);
 
+               setUploadInfo(request, info);
        }
 
        /**
@@ -315,7 +329,7 @@ public class MultipartServletWebRequestI
         */
        protected void onUploadCompleted()
        {
-
+               clearUploadInfo(getContainerRequest());
        }
 
        /**
@@ -390,4 +404,46 @@ public class MultipartServletWebRequestI
        {
                return this;
        }
+
+       private static final String SESSION_KEY = 
MultipartServletWebRequestImpl.class.getName();
+
+       /**
+        * Retrieves {@link UploadInfo} from session, null if not found.
+        * 
+        * @param req
+        *            http servlet request, not null
+        * @return {@link UploadInfo} object from session, or null if not found
+        */
+       public static UploadInfo getUploadInfo(final HttpServletRequest req)
+       {
+               Args.notNull(req, "req");
+               return (UploadInfo)req.getSession().getAttribute(SESSION_KEY);
+       }
+
+       /**
+        * Sets the {@link UploadInfo} object into session.
+        * 
+        * @param req
+        *            http servlet request, not null
+        * @param uploadInfo
+        *            {@link UploadInfo} object to be put into session, not null
+        */
+       public static void setUploadInfo(final HttpServletRequest req, final 
UploadInfo uploadInfo)
+       {
+               Args.notNull(req, "req");
+               Args.notNull(uploadInfo, "uploadInfo");
+               req.getSession().setAttribute(SESSION_KEY, uploadInfo);
+       }
+
+       /**
+        * Clears the {@link UploadInfo} object from session if one exists.
+        * 
+        * @param req
+        *            http servlet request, not null
+        */
+       public static void clearUploadInfo(final HttpServletRequest req)
+       {
+               Args.notNull(req, "req");
+               req.getSession().removeAttribute(SESSION_KEY);
+       }
 }
\ No newline at end of file

Added: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java?rev=1096125&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
 (added)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
 Sat Apr 23 09:19:45 2011
@@ -0,0 +1,158 @@
+/*
+ * 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.wicket.protocol.http.servlet;
+
+import org.apache.wicket.IClusterable;
+import org.apache.wicket.util.lang.Bytes;
+import org.apache.wicket.util.time.Duration;
+
+
+/**
+ * Holds information about an upload, also has useful querying methods.
+ * 
+ * @author Igor Vaynberg (ivaynberg)
+ * 
+ */
+public class UploadInfo implements IClusterable
+{
+       private static final long serialVersionUID = 1L;
+
+       private transient long timeStarted;
+       private transient long totalBytes;
+       private transient long bytesUploaded;
+
+       /**
+        * @param totalBytes
+        */
+       public UploadInfo(final int totalBytes)
+       {
+               timeStarted = System.currentTimeMillis();
+               this.totalBytes = totalBytes;
+       }
+
+       /**
+        * @return bytes uploaded so far
+        */
+       public long getBytesUploaded()
+       {
+               return bytesUploaded;
+       }
+
+       /**
+        * Sets bytes uploaded so far
+        * 
+        * @param bytesUploaded
+        */
+       public void setBytesUploaded(final long bytesUploaded)
+       {
+               this.bytesUploaded = bytesUploaded;
+       }
+
+       /**
+        * @return human readable string of bytes uploaded so far
+        */
+       public String getBytesUploadedString()
+       {
+               return Bytes.bytes(bytesUploaded).toString();
+       }
+
+       /**
+        * @return human readable string of total number of bytes
+        */
+       public String getTotalBytesString()
+       {
+               return Bytes.bytes(totalBytes).toString();
+       }
+
+       /**
+        * @return total bytes in the upload
+        */
+       public long getTotalBytes()
+       {
+               return totalBytes;
+       }
+
+       /**
+        * @return milliseconds elapsed since upload started
+        */
+       public long getElapsedMilliseconds()
+       {
+               return System.currentTimeMillis() - timeStarted;
+       }
+
+       /**
+        * @return seconds elapsed since upload started
+        */
+       public long getElapsedSeconds()
+       {
+               return getElapsedMilliseconds() / 1000L;
+       }
+
+
+       /**
+        * @return transfer rate in bits per second
+        */
+       public long getTransferRateBPS()
+       {
+               return bytesUploaded / Math.max(getElapsedSeconds(), 1);
+       }
+
+       /**
+        * @return transfer rate in a human readable string
+        */
+       public String getTransferRateString()
+       {
+               return Bytes.bytes(getTransferRateBPS()).toString() + "/s";
+       }
+
+       /**
+        * @return percent of the upload completed
+        */
+       public int getPercentageComplete()
+       {
+               if (totalBytes == 0)
+               {
+                       return 100;
+               }
+               return (int)(((double)bytesUploaded / (double)totalBytes) * 
100);
+
+       }
+
+       /**
+        * @return estimate of the remaining number of milliseconds
+        */
+       public long getRemainingMilliseconds()
+       {
+               int percentageComplete = getPercentageComplete();
+
+
+               long totalTime = ((getElapsedSeconds() * 100) / 
Math.max(percentageComplete, 1));
+               long remainingTime = (totalTime - getElapsedSeconds());
+
+               return remainingTime * 1000; // convert seconds to milliseconds 
and return
+       }
+
+       /**
+        * @return estimate of the remaining time in a human readable string
+        */
+       public String getRemainingTimeString()
+       {
+               return 
Duration.milliseconds(getRemainingMilliseconds()).toString();
+       }
+
+
+}

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IApplicationSettings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IApplicationSettings.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IApplicationSettings.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IApplicationSettings.java
 Sat Apr 23 09:19:45 2011
@@ -47,7 +47,7 @@ public interface IApplicationSettings
        Class<? extends Page> getAccessDeniedPage();
 
        /**
-        * Gets the default resolver to use when finding classes and resources
+        * Gets the default resolver to use when finding classes and resources.
         * 
         * @return Default class resolver
         */
@@ -78,6 +78,13 @@ public interface IApplicationSettings
        Class<? extends Page> getPageExpiredErrorPage();
 
        /**
+        * Gets whether wicket is providing updates about the upload progress 
or not.
+        * 
+        * @return if true upload progress monitoring is enabled
+        */
+       boolean isUploadProgressUpdatesEnabled();
+
+       /**
         * Sets the access denied page class. The class must be bookmarkable 
and must extend Page.
         * 
         * @param accessDeniedPage
@@ -86,7 +93,7 @@ public interface IApplicationSettings
        void setAccessDeniedPage(final Class<? extends Page> accessDeniedPage);
 
        /**
-        * Sets the default class resolver to use when finding classes and 
resources
+        * Sets the default class resolver to use when finding classes and 
resources.
         * 
         * @param defaultClassResolver
         *            The default class resolver
@@ -117,4 +124,12 @@ public interface IApplicationSettings
         *            The pageExpiredErrorPage to set.
         */
        void setPageExpiredErrorPage(final Class<? extends Page> 
pageExpiredErrorPage);
+
+       /**
+        * Sets whether wicket should provide updates about the upload progress 
or not.
+        * 
+        * @param uploadProgressUpdatesEnabled
+        *            if true upload progress monitoring is enabled
+        */
+       void setUploadProgressUpdatesEnabled(boolean 
uploadProgressUpdatesEnabled);
 }

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ApplicationSettings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ApplicationSettings.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ApplicationSettings.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ApplicationSettings.java
 Sat Apr 23 09:19:45 2011
@@ -47,6 +47,8 @@ public class ApplicationSettings impleme
 
        private Bytes defaultMaximumUploadSize = Bytes.MAX;
 
+       private boolean uploadProgressUpdatesEnabled = false;
+
        /**
         * @see 
org.apache.wicket.settings.IApplicationSettings#getAccessDeniedPage()
         */
@@ -85,6 +87,14 @@ public class ApplicationSettings impleme
        }
 
        /**
+        * @see 
org.apache.wicket.settings.IApplicationSettings#isUploadProgressUpdatesEnabled()
+        */
+       public boolean isUploadProgressUpdatesEnabled()
+       {
+               return uploadProgressUpdatesEnabled;
+       }
+
+       /**
         * @see 
org.apache.wicket.settings.IApplicationSettings#setAccessDeniedPage(java.lang.Class)
         */
        public void setAccessDeniedPage(Class<? extends Page> accessDeniedPage)
@@ -143,6 +153,14 @@ public class ApplicationSettings impleme
        }
 
        /**
+        * @see 
org.apache.wicket.settings.IApplicationSettings#setUploadProgressUpdatesEnabled(boolean)
+        */
+       public void setUploadProgressUpdatesEnabled(boolean 
uploadProgressUpdatesEnabled)
+       {
+               this.uploadProgressUpdatesEnabled = 
uploadProgressUpdatesEnabled;
+       }
+
+       /**
         * Throws an IllegalArgumentException if the given class is not a 
subclass of Page.
         * 
         * @param <C>

Modified: 
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
 (original)
+++ 
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
 Sat Apr 23 09:19:45 2011
@@ -16,12 +16,8 @@
  */
 package org.apache.wicket.examples.upload;
 
-import javax.servlet.http.HttpServletRequest;
-
 import org.apache.wicket.Page;
 import org.apache.wicket.examples.WicketExampleApplication;
-import 
org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadWebRequest;
-import org.apache.wicket.request.http.WebRequest;
 import org.apache.wicket.request.mapper.MountedMapper;
 import org.apache.wicket.util.file.Folder;
 
@@ -77,15 +73,6 @@ public class UploadApplication extends W
                getRootRequestMapperAsCompound().add(new 
MountedMapper("/multi", MultiUploadPage.class));
                getRootRequestMapperAsCompound().add(new 
MountedMapper("/single", UploadPage.class));
 
-       }
-
-       /**
-        * @see 
org.apache.wicket.protocol.http.WebApplication#newWebRequest(javax.servlet.http.HttpServletRequest,
-        *      String)
-        */
-       @Override
-       protected WebRequest newWebRequest(HttpServletRequest servletRequest, 
String filterPath)
-       {
-               return new UploadWebRequest(servletRequest, filterPath);
+               getApplicationSettings().setUploadProgressUpdatesEnabled(true);
        }
 }

Modified: 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
 (original)
+++ 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
 Sat Apr 23 09:19:45 2011
@@ -30,7 +30,6 @@ import org.apache.wicket.markup.html.Wic
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.upload.FileUploadField;
 import org.apache.wicket.markup.html.panel.Panel;
-import org.apache.wicket.request.cycle.RequestCycle;
 import org.apache.wicket.request.resource.PackageResourceReference;
 import org.apache.wicket.request.resource.ResourceReference;
 import org.apache.wicket.request.resource.SharedResourceReference;
@@ -42,8 +41,22 @@ import org.slf4j.LoggerFactory;
 /**
  * A panel to show the progress of an HTTP upload.
  * <p>
- * NB: For this to work, you *must* use an {@link UploadWebRequest}. See the 
javadoc in that class
- * for details.
+ * Note: For this to work upload progress monitoring must be enabled in the 
wicket application.
+ * Example:
+ * 
+ * <pre>
+ * <code>
+ *  public class App extends WebApplication {
+ * 
+ *     &#64;Override
+ *     protected void init() {
+ *             super.init();
+ * 
+ *             
<b>getApplicationSettings().setUploadProgressUpdatesEnabled(true);</b> // <--
+ *     }
+ * }
+ * </code>
+ * </pre>
  * 
  * @author Andrew Lombardi
  */
@@ -144,12 +157,6 @@ public class UploadProgressBar extends P
                statusDiv = new WebMarkupContainer("status");
                statusDiv.setOutputMarkupId(true);
                add(statusDiv);
-
-               if (!(RequestCycle.get().getRequest() instanceof 
UploadWebRequest) &&
-                       !(RequestCycle.get().getRequest() instanceof 
MultipartRequest))
-               {
-                       log.warn("UploadProgressBar will not work without an 
UploadWebRequest. See the javadoc for details.");
-               }
        }
 
        /**

Modified: 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java?rev=1096125&r1=1096124&r2=1096125&view=diff
==============================================================================
--- 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java
 (original)
+++ 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java
 Sat Apr 23 09:19:45 2011
@@ -18,6 +18,8 @@ package org.apache.wicket.extensions.aja
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequestImpl;
+import org.apache.wicket.protocol.http.servlet.UploadInfo;
 import org.apache.wicket.request.resource.AbstractResource;
 
 
@@ -65,7 +67,7 @@ class UploadStatusResource extends Abstr
        private String getStatus(final Attributes attributes)
        {
                HttpServletRequest req = 
(HttpServletRequest)attributes.getRequest().getContainerRequest();
-               UploadInfo info = UploadWebRequest.getUploadInfo(req);
+               UploadInfo info = 
MultipartServletWebRequestImpl.getUploadInfo(req);
 
                String status = null;
                if ((info == null) || (info.getTotalBytes() < 1))


Reply via email to