Return-Path: X-Original-To: apmail-incubator-wookie-dev-archive@minotaur.apache.org Delivered-To: apmail-incubator-wookie-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B21C08DB9 for ; Mon, 22 Aug 2011 12:02:41 +0000 (UTC) Received: (qmail 70430 invoked by uid 500); 22 Aug 2011 12:02:37 -0000 Delivered-To: apmail-incubator-wookie-dev-archive@incubator.apache.org Received: (qmail 67641 invoked by uid 500); 22 Aug 2011 12:02:26 -0000 Mailing-List: contact wookie-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: wookie-dev@incubator.apache.org Delivered-To: mailing list wookie-dev@incubator.apache.org Received: (qmail 54427 invoked by uid 99); 22 Aug 2011 12:00:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Aug 2011 12:00:37 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Aug 2011 12:00:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 896542388AA7; Mon, 22 Aug 2011 12:00:12 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1160225 - in /incubator/wookie/trunk: src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java src-tests/testdata/not_a_widget.zip src-tests/testdata/notsupported.wgt src/org/apache/wookie/controller/WidgetsController.java Date: Mon, 22 Aug 2011 12:00:12 -0000 To: wookie-dev@incubator.apache.org From: scottbw@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110822120012.896542388AA7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: scottbw Date: Mon Aug 22 12:00:11 2011 New Revision: 1160225 URL: http://svn.apache.org/viewvc?rev=1160225&view=rev Log: Improved code comments for widget upload, and conduct checks for valid .wgt files in the upload. Also added some basic functional tests (unauthorized upload, valid upload, upload with wrong type of file). Note that POSTing URLs rather than files (e.g. for Shindig gadgets) is not yet implemented. Added: incubator/wookie/trunk/src-tests/testdata/not_a_widget.zip (with props) incubator/wookie/trunk/src-tests/testdata/notsupported.wgt (with props) Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java?rev=1160225&r1=1160224&r2=1160225&view=diff ============================================================================== --- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java (original) +++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java Mon Aug 22 12:00:11 2011 @@ -16,11 +16,18 @@ package org.apache.wookie.tests.function import static org.junit.Assert.*; +import java.io.File; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; import org.junit.Test; /** @@ -129,4 +136,82 @@ public class WidgetsControllerTest exten get.releaseConnection(); } + @Test + public void importWidget_unauthorized() throws HttpException, IOException{ + HttpClient client = new HttpClient(); + PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID); + client.executeMethod(post); + int code = post.getStatusCode(); + assertEquals(401,code); + post.releaseConnection(); + } + + @Test + public void importWidget() throws HttpException, IOException{ + HttpClient client = new HttpClient(); + // + // Use admin credentials + // + client.getState().setCredentials( + new AuthScope("localhost", 8080, "wookie"), + new UsernamePasswordCredentials("java", "java") + ); + PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID); + + // + // We'll use a copy of the unsupported widget widget for testing + // + File file = new File("src-tests/testdata/notsupported.wgt"); + assertTrue(file.exists()); + + // + // Add test wgt file to POST + // + Part[] parts = { new FilePart(file.getName(), file) }; + post.setRequestEntity(new MultipartRequestEntity(parts, post + .getParams())); + + // + // POST the file to /widgets and check we get 201 (Created) + // + client.executeMethod(post); + int code = post.getStatusCode(); + assertEquals(201,code); + post.releaseConnection(); + } + + @Test + public void importWrongFileType() throws HttpException, IOException{ + HttpClient client = new HttpClient(); + // + // Use admin credentials + // + client.getState().setCredentials( + new AuthScope("localhost", 8080, "wookie"), + new UsernamePasswordCredentials("java", "java") + ); + PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID); + + // + // We'll use a copy of the unsupported widget widget for testing + // + File file = new File("src-tests/testdata/not_a_widget.zip"); + assertTrue(file.exists()); + + // + // Add test wgt file to POST + // + Part[] parts = { new FilePart(file.getName(), file) }; + post.setRequestEntity(new MultipartRequestEntity(parts, post + .getParams())); + + // + // POST the file to /widgets and check we get a 400 + // + client.executeMethod(post); + int code = post.getStatusCode(); + assertEquals(400,code); + post.releaseConnection(); + } + } Added: incubator/wookie/trunk/src-tests/testdata/not_a_widget.zip URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/testdata/not_a_widget.zip?rev=1160225&view=auto ============================================================================== Binary file - no diff available. Propchange: incubator/wookie/trunk/src-tests/testdata/not_a_widget.zip ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: incubator/wookie/trunk/src-tests/testdata/notsupported.wgt URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/testdata/notsupported.wgt?rev=1160225&view=auto ============================================================================== Binary file - no diff available. Propchange: incubator/wookie/trunk/src-tests/testdata/notsupported.wgt ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream 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=1160225&r1=1160224&r2=1160225&view=diff ============================================================================== --- incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java (original) +++ incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java Mon Aug 22 12:00:11 2011 @@ -49,6 +49,7 @@ import org.apache.wookie.helpers.WidgetH *
  • GET widgets - index
  • *
  • GET widgets/{id} - show widget
  • *
  • GET widgets/{category} - index widgets for category
  • + *
  • POST widgets {.wgt file} - import a widget
  • * * @author scott * @@ -150,33 +151,83 @@ public class WidgetsController extends C /** * Install a new Widget by saving it in the deploy folder * Note: a Widget must have a .wgt extension! + * FIXME: Support POSTing references to OpenSocial gadgets, remote widget files */ @Override protected boolean create(String resourceId, HttpServletRequest request) throws ResourceDuplicationException, InvalidParametersException, UnauthorizedAccessException { - + + // + // Check for a "url" parameter in the request, indicating this is a remote widget or opensocial gadget xml file + // FIXME implement this + // + String url = request.getParameter("url"); + if (url != null && url.trim().length() != 0){ + _logger.info("POSTing URLs instead of files it not yet supported"); + throw new InvalidParametersException(); + } + + // + // Get the path for the deploy folder + // Configuration properties = (Configuration) request.getSession().getServletContext().getAttribute("properties"); //$NON-NLS-1$ final String DEPLOY_FOLDER = getServletContext().getRealPath(properties.getString("widget.deployfolder"));//$NON-NLS-1$ + + // + // Create factory for processing POSTed files + // FileItemFactory factory = new DiskFileItemFactory(); + + // // Create a new file upload handler + // ServletFileUpload upload = new ServletFileUpload(factory); + // + // Create a flag we'll use to check if we got any .wgt files in the POST + // + boolean requestContainedWgtFile = false; + + // + // Save file in the deploy folder + // try { @SuppressWarnings("unchecked") List items = upload.parseRequest(request); + // + // Only save .wgt files and ignore any others in the POST + // for (FileItem item: items){ - File saveFile = new File(DEPLOY_FOLDER + "/" + item.getName()); - item.write(saveFile); + if (item.getName().endsWith(".wgt")){ + File saveFile = new File(DEPLOY_FOLDER + "/" + item.getName()); + item.write(saveFile); + requestContainedWgtFile = true; + } } } catch (FileUploadException e) { throw new InvalidParametersException(); } catch (Exception e) { - e.printStackTrace(); - return false; + // + // Catch any other exceptions thrown by the save file operation + // and throw a basic 400 response to the client, though really + // this is more like a 500. At least we can log the details. + // + _logger.error(e.getMessage(), e); + throw new InvalidParametersException(); } + + // + // If there are no .wgt files in the POST, throw an exception + // We have to check for this here as other exceptions are caught in + // the code above, e.g. generic file system errors + // + if (requestContainedWgtFile == false){ + throw new InvalidParametersException(); + } + return true; } }