Author: scottbw
Date: Wed Apr 14 20:09:31 2010
New Revision: 934168

URL: http://svn.apache.org/viewvc?rev=934168&view=rev
Log:
Added the capability for the parser to process widgets by providing a URL 
instead of a File; the parser downloads widgets using HttpClient and checks for 
the "application/widget" MIME type. (Added W3C conformance tests Z3,Z4,Z5 - 
which test this behaviour - to the test cases)

Modified:
    incubator/wookie/trunk/parser/java/ivy.xml
    
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
    
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java

Modified: incubator/wookie/trunk/parser/java/ivy.xml
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/ivy.xml?rev=934168&r1=934167&r2=934168&view=diff
==============================================================================
--- incubator/wookie/trunk/parser/java/ivy.xml (original)
+++ incubator/wookie/trunk/parser/java/ivy.xml Wed Apr 14 20:09:31 2010
@@ -22,6 +22,8 @@
         </dependency>
         <dependency org="commons-fileupload" name="commons-fileupload" 
rev="1.0" conf="deploy->default">
         </dependency>
+        <dependency org="commons-httpclient" name="commons-httpclient" 
rev="3.0.1" conf="deploy->default">
+        </dependency>
         <dependency org="org.jdom" name="jdom" rev="1.1" 
conf="deploy->default">
         </dependency>
         <dependency org="org.mortbay.jetty" name="servlet-api-2.5" rev="6.1.3" 
conf="dev->default">

Modified: 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java?rev=934168&r1=934167&r2=934168&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
 Wed Apr 14 20:09:31 2010
@@ -20,20 +20,12 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.List;
 
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.io.IOUtils;
-
 import org.apache.wookie.w3c.IContentEntity;
 import org.apache.wookie.w3c.IDescriptionEntity;
 import org.apache.wookie.w3c.IFeatureEntity;
@@ -46,6 +38,7 @@ import org.apache.wookie.w3c.W3CWidget;
 import org.apache.wookie.w3c.W3CWidgetFactory;
 import org.apache.wookie.w3c.exceptions.BadManifestException;
 import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
+import org.apache.wookie.w3c.exceptions.InvalidContentTypeException;
 import org.apache.wookie.w3c.util.LocalizationUtils;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -79,40 +72,28 @@ public class PackagingAndConfiguration{
                output.delete();
        }
        
-       public W3CWidget getWidget(String url) throws 
BadWidgetZipFileException, BadManifestException, Exception{
+       private W3CWidget downloadWidget(String url) throws 
InvalidContentTypeException, BadWidgetZipFileException, BadManifestException, 
Exception{
+               return downloadWidget(url, true);
+       }
+       
+       private W3CWidget downloadWidget(String url, boolean ignoreContentType) 
throws InvalidContentTypeException, BadWidgetZipFileException, 
BadManifestException, Exception{
                W3CWidgetFactory fac = new W3CWidgetFactory();
                fac.setLocalPath("http:localhost/widgets");
                fac.setFeatures(new String[]{"feature:a9bb79c1"});
-               
                if (download.exists()) download.delete();
                if (output.exists()) output.delete();
                output.mkdir();
                fac.setOutputDirectory(output.getAbsolutePath());
-               // Download widget to folder
-               HttpClient client = new HttpClient();
-               GetMethod get = new GetMethod(url);
-               client.executeMethod(get);
-               int code = get.getStatusCode();
-               assertEquals(200,code);
-               InputStream in = new 
BufferedInputStream(get.getResponseBodyAsStream());
-               BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(download));
-               IOUtils.copy(in, out);
-               out.flush();
-               out.close();
-               in.close();
-               get.releaseConnection();
-               W3CWidget widget = fac.parse(download);
-               return widget;
+               return fac.parse(new URL(url),ignoreContentType);
        }
        
        public String processWidgetWithErrors(String url){
                try {
-                       getWidget(url);
+                       downloadWidget(url);
                } catch (BadWidgetZipFileException e) {
                        if (e.getMessage()!=null) return e.getMessage();
                        return "Bad Widget Zip File";
                } catch (BadManifestException e) {
-                       // TODO Auto-generated catch block
                        if (e.getMessage()!=null) return e.getMessage();
                        return "Bad Manifest";
                } catch (Exception e) {
@@ -123,7 +104,7 @@ public class PackagingAndConfiguration{
        
        public W3CWidget processWidgetNoErrors(String url){
                try {
-                       return getWidget(url);
+                       return downloadWidget(url);
                } catch (Exception e) {
                        e.printStackTrace();
                        fail();
@@ -322,7 +303,6 @@ public class PackagingAndConfiguration{
        @Test
        public void ch(){
                W3CWidget widget = 
processWidgetNoErrors("http://dev.w3.org/2006/waf/widgets/test-suite/test-cases/ta-VerEfVGeTc/002/ch.wgt";);
-               System.out.println(widget.getVersion());
                assertEquals("PASS", widget.getVersion());
        }
 
@@ -1135,7 +1115,6 @@ public class PackagingAndConfiguration{
                W3CWidget widget = 
processWidgetNoErrors("http://dev.w3.org/2006/waf/widgets/test-suite/test-cases/ta-RGNHRBWNZV/000/b3.wgt";);
                String start = getSrc(widget);
                assertEquals("index.htm",start);
-               System.out.println(getStartFileContentType(widget));
                
assertTrue(getStartFileContentType(widget).startsWith("text/html"));
        }
        @Test
@@ -1257,6 +1236,27 @@ public class PackagingAndConfiguration{
                String start = getSrc(widget);
                assertEquals("pass.html",start);
        }
+       
+       @Test
+       public void z3(){
+               try {
+                       
downloadWidget("http://dev.w3.org/2006/waf/widgets/test-suite/test-cases/ta-GVVIvsdEUo/000/z3",false);
+               } catch (Exception e) {
+                       fail();
+               }
+       }
+       @Test
+       public void z4(){
+               try {
+                       
downloadWidget("http://dev.w3.org/2006/waf/widgets/test-suite/test-cases/ta-GVVIvsdEUo/001/z4.html",false);
+               } catch (Exception e) {
+                       fail();
+               }
+       }
+       @Test(expected=InvalidContentTypeException.class)
+       public void z5() throws InvalidContentTypeException, 
BadWidgetZipFileException, BadManifestException, Exception{
+               
downloadWidget("http://dev.w3.org/2006/waf/widgets/test-suite/test-cases/ta-GVVIvsdEUo/002/z5.wgt",false);
+       }
 
        // Utility methods
        

Modified: 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java?rev=934168&r1=934167&r2=934168&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
 Wed Apr 14 20:09:31 2010
@@ -16,9 +16,16 @@ package org.apache.wookie.w3c;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.net.URL;
 
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.wookie.w3c.exceptions.BadManifestException;
 import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
+import org.apache.wookie.w3c.exceptions.InvalidContentTypeException;
 import org.apache.wookie.w3c.util.WidgetPackageUtils;
 
 /**
@@ -134,4 +141,61 @@ public class W3CWidgetFactory {
                if (outputDirectory == null) throw new Exception("No output 
directory has been set; use setOutputDirectory(File) to set the location to 
output widget files");
                return WidgetPackageUtils.processWidgetPackage(zipFile, 
localPath, outputDirectory, locales, startPageProcessor, features);
        }
+       
+       /**
+        * Parse a widget at a given URL and return a W3CWidget object 
representing the processed information in the package.
+        * The widget will be saved in the outputFolder.
+        * @param url
+        * @return
+        * @throws BadWidgetZipFileException if there is a problem with the zip 
package
+        * @throws BadManifestException if there is a problem with the 
config.xml manifest file in the package
+        * @throws InvalidContentTypeException if the widget has an invalid 
content type
+        * @throws IOException if the widget cannot be downloaded
+        */
+       public W3CWidget parse(final URL url) throws BadWidgetZipFileException, 
BadManifestException, InvalidContentTypeException, IOException, Exception{
+               File file = download(url,false);
+               return parse(file);
+       }
+       
+       /**
+        * Parse a widget at a given URL and return a W3CWidget object 
representing the processed information in the package.
+        * The widget will be saved in the outputFolder.
+        * @param url
+        * @param ignoreContentType set to true to instruct the parser to 
ignore invalid content type exceptions
+        * @return
+        * @throws BadWidgetZipFileException if there is a problem with the zip 
package
+        * @throws BadManifestException if there is a problem with the 
config.xml manifest file in the package
+        * @throws InvalidContentTypeException if the widget has an invalid 
content type
+        * @throws IOException if the widget cannot be downloaded
+        */
+       public W3CWidget parse(final URL url, boolean ignoreContentType) throws 
BadWidgetZipFileException, BadManifestException, InvalidContentTypeException, 
IOException, Exception{
+               File file = download(url,ignoreContentType);
+               return parse(file);
+       }
+       
+       /**
+        * The standard MIME type for a W3C Widget
+        */
+       private static final String WIDGET_CONTENT_TYPE = "application/widget";
+       
+       /**
+        * Download widget from given URL
+        * @param url the URL to download
+        * @param ignoreContentType if set to true, will ignore invalid content 
types (not application/widget)
+        * @return the File downloaded
+        * @throws InvalidContentTypeException 
+        * @throws HttpException
+        * @throws IOException
+        */
+       private File download(URL url, boolean ignoreContentType) throws 
InvalidContentTypeException, HttpException, IOException {
+               HttpClient client = new HttpClient();
+               GetMethod method = new GetMethod(url.toString());
+               client.executeMethod(method);
+               String type = 
method.getResponseHeader("Content-Type").getValue();
+               if (!ignoreContentType && 
!type.startsWith(WIDGET_CONTENT_TYPE)) throw new 
InvalidContentTypeException("Problem downloading widget: expected a content 
type of "+WIDGET_CONTENT_TYPE+" but received:"+type);
+               File file = File.createTempFile("wookie", null);
+               FileUtils.writeByteArrayToFile(file, 
IOUtils.toByteArray(method.getResponseBodyAsStream()));
+               method.releaseConnection();
+               return file;
+       }
 }


Reply via email to