Author: scottbw
Date: Thu Mar 24 13:28:05 2011
New Revision: 1084952

URL: http://svn.apache.org/viewvc?rev=1084952&view=rev
Log:
Added the ability to POST a .wgt file to /widgets/ to upload a new widget. By 
default using this API requires admin rights. See issue WOOKIE-98

Modified:
    incubator/wookie/trunk/WebContent/WEB-INF/web.xml
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java

Modified: incubator/wookie/trunk/WebContent/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/WebContent/WEB-INF/web.xml?rev=1084952&r1=1084951&r2=1084952&view=diff
==============================================================================
--- incubator/wookie/trunk/WebContent/WEB-INF/web.xml (original)
+++ incubator/wookie/trunk/WebContent/WEB-INF/web.xml Thu Mar 24 13:28:05 2011
@@ -352,6 +352,16 @@
                                <role-name>widgetadmin</role-name>
                        </auth-constraint>
                </security-constraint>
+               <security-constraint>           
+                       <web-resource-collection>
+                               
<web-resource-name>WidgetServlet</web-resource-name>
+                               <url-pattern>/widgets/*</url-pattern>
+                               <http-method>POST</http-method>
+                       </web-resource-collection>              
+                       <auth-constraint>
+                               <role-name>widgetadmin</role-name>
+                       </auth-constraint>
+               </security-constraint>
                
                <login-config>
                        <auth-method>BASIC</auth-method>

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java?rev=1084952&r1=1084951&r2=1084952&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java 
(original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java 
Thu Mar 24 13:28:05 2011
@@ -14,19 +14,30 @@
 
 package org.apache.wookie.controller;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.List;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.wookie.beans.IWidget;
 import org.apache.wookie.beans.IWidgetDefault;
 import org.apache.wookie.beans.IWidgetService;
 import org.apache.wookie.beans.util.IPersistenceManager;
 import org.apache.wookie.beans.util.PersistenceManagerFactory;
+import org.apache.wookie.exceptions.InvalidParametersException;
+import org.apache.wookie.exceptions.ResourceDuplicationException;
 import org.apache.wookie.exceptions.ResourceNotFoundException;
+import org.apache.wookie.exceptions.UnauthorizedAccessException;
 import org.apache.wookie.helpers.WidgetHelper;
 
 /**
@@ -53,12 +64,6 @@ public class WidgetsController extends C
        }
 
        @Override
-       protected void doPost(HttpServletRequest req, HttpServletResponse resp)
-                       throws ServletException, IOException {
-               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
-       }
-
-       @Override
        protected void doPut(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@@ -141,4 +146,37 @@ public class WidgetsController extends C
                }
                returnXml(WidgetHelper.createXMLWidgetsDocument(widgets, 
getLocalPath(request), getLocales(request)),response);
        }
+
+       /**
+        * Install a new Widget by saving it in the deploy folder
+        * Note: a Widget must have a .wgt extension!
+        */
+       @Override
+       protected boolean create(String resourceId, HttpServletRequest request)
+                       throws ResourceDuplicationException, 
InvalidParametersException,
+                       UnauthorizedAccessException {
+                               
+               Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
+               final String DEPLOY_FOLDER = 
getServletContext().getRealPath(properties.getString("widget.deployfolder"));//$NON-NLS-1$
+               FileItemFactory factory = new DiskFileItemFactory();
+               // Create a new file upload handler
+               ServletFileUpload upload = new ServletFileUpload(factory);
+               
+               try {
+                       @SuppressWarnings("unchecked")
+                       List <FileItem> items = upload.parseRequest(request);
+                       
+                       for (FileItem item: items){
+                               File saveFile = new File(DEPLOY_FOLDER + "/" + 
item.getName());
+                               item.write(saveFile);
+                       }
+                       
+               } catch (FileUploadException e) {
+                       throw new InvalidParametersException();
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       return false;
+               }
+               return true;
+       }
 }


Reply via email to