Author: scottbw
Date: Thu Aug 30 10:45:14 2012
New Revision: 1378880

URL: http://svn.apache.org/viewvc?rev=1378880&view=rev
Log:
Refactored the code for creating a W3CWidgetFactory instance into a Utils class 
- this was duplicated across three classes.

Added:
    incubator/wookie/trunk/src/org/apache/wookie/util/W3CWidgetFactoryUtils.java
Modified:
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java
    incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java
    incubator/wookie/trunk/src/org/apache/wookie/updates/UpdatesController.java

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=1378880&r1=1378879&r2=1378880&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java 
(original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java 
Thu Aug 30 10:45:14 2012
@@ -16,11 +16,9 @@ package org.apache.wookie.controller;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.security.KeyStore;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -36,17 +34,15 @@ import org.apache.wookie.exceptions.Inva
 import org.apache.wookie.exceptions.ResourceDuplicationException;
 import org.apache.wookie.exceptions.ResourceNotFoundException;
 import org.apache.wookie.exceptions.UnauthorizedAccessException;
-import org.apache.wookie.feature.Features;
 import org.apache.wookie.helpers.WidgetFactory;
 import org.apache.wookie.helpers.WidgetAdvertHelper;
 import org.apache.wookie.helpers.WidgetImportHelper;
 import org.apache.wookie.server.LocaleHandler;
 import org.apache.wookie.util.NewWidgetBroadcaster;
+import org.apache.wookie.util.W3CWidgetFactoryUtils;
 import org.apache.wookie.util.WidgetFileUtils;
 import org.apache.wookie.util.WidgetJavascriptSyntaxAnalyzer;
-import org.apache.wookie.util.digitalsignature.DigitalSignatureProcessor;
 import org.apache.wookie.util.gadgets.GadgetUtils;
-import org.apache.wookie.util.html.StartPageProcessor;
 import org.apache.wookie.w3c.W3CWidget;
 import org.apache.wookie.w3c.W3CWidgetFactory;
 import org.apache.wookie.w3c.exceptions.BadManifestException;
@@ -228,14 +224,8 @@ public class WidgetsController extends C
     // Get the path to the upload folder, and the widget install folder
     //
     Configuration properties = (Configuration) 
getServletContext().getAttribute("properties"); //$NON-NLS-1$
-    final String WIDGETFOLDER = 
getServletContext().getRealPath(properties.getString("widget.widgetfolder"));//$NON-NLS-1$
     final String UPLOADFOLDER = 
getServletContext().getRealPath(properties.getString("widget.useruploadfolder"));//$NON-NLS-1$
-       // Digital signature settings
-    final boolean VERIFYSIGNATURE = 
properties.getBoolean("widget.deployment.verifysignature");//$NON-NLS-1$
-    final boolean REJECTINVALID= 
properties.getBoolean("widget.deployment.rejectinvalidsignatures");
-    final boolean REJECTUNTRUSTED= 
properties.getBoolean("widget.deployment.rejectuntrustedsignatures");
-    final String PASSWORD = 
properties.getString("widget.deployment.trustedkeystore.password");
-    final String KEYSTORE = 
properties.getString("widget.deployment.trustedkeystore");//$NON-NLS-1$
+    
     //
     // Get localized messages so we can return errors
     //
@@ -263,40 +253,10 @@ public class WidgetsController extends C
         //
         // Parse and validate the zip as a widget
         //
-        final String[] locales = properties.getStringArray("widget.locales");
-        W3CWidgetFactory fac = new W3CWidgetFactory();
-        fac.setLocales(locales);
-        fac.setLocalPath(getServletContext().getContextPath() + 
properties.getString("widget.widgetfolder"));
-        fac.setOutputDirectory(WIDGETFOLDER);
-        fac.setFeatures(Features.getFeatureNames());
-        fac.setStartPageProcessor(new StartPageProcessor());
-        if (VERIFYSIGNATURE) {
-            KeyStore keyStore = KeyStore.getInstance("JKS");
-            String digSigSchema = getServletContext()
-            
.getRealPath("/WEB-INF/classes/org/apache/wookie/util/digitalsignature/xmldsig-core-schema.xsd");
-
-            InputStream stream = 
getServletContext().getResourceAsStream("/WEB-INF/classes/" + KEYSTORE);
-            if (stream == null) {
-                stream = 
getServletContext().getResourceAsStream("/WEB-INF/classes/" + "generated-" + 
KEYSTORE);
-            }
-            if (stream == null) {
-                FileOutputStream fos = new FileOutputStream(getServletContext()
-                        .getRealPath("/WEB-INF/classes/") + "generated-" + 
KEYSTORE);
-                keyStore.load(null, PASSWORD.toCharArray());
-                keyStore.store(fos, PASSWORD.toCharArray());
-                fos.close();
-                fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
-                        digSigSchema, REJECTINVALID, REJECTUNTRUSTED));
-                _logger.info(localizedMessages.getString("WidgetHotDeploy.4"));
-            } else {
-                keyStore.load(stream, PASSWORD.toCharArray());
-                stream.close();
-                fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
-                        digSigSchema, REJECTINVALID, REJECTUNTRUSTED));
-            }
-        }
+        W3CWidgetFactory fac = 
W3CWidgetFactoryUtils.createW3CWidgetFactory(getServletContext(), properties, 
localizedMessages);        
         W3CWidget widgetModel = fac.parse(zipFile);
         new WidgetJavascriptSyntaxAnalyzer(fac.getUnzippedWidgetDirectory());
+        
         //
         // Check if the widget model corresponds to an existing installed 
widget
         //

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java?rev=1378880&r1=1378879&r2=1378880&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/server/ContextListener.java 
Thu Aug 30 10:45:14 2012
@@ -15,10 +15,7 @@
 package org.apache.wookie.server;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.security.KeyStore;
 import java.util.Iterator;
 import java.util.Locale;
 
@@ -38,11 +35,10 @@ import org.apache.wookie.feature.Feature
 import org.apache.wookie.helpers.WidgetFactory;
 import org.apache.wookie.helpers.WidgetRuntimeHelper;
 import org.apache.wookie.util.NewWidgetBroadcaster;
+import org.apache.wookie.util.W3CWidgetFactoryUtils;
 import org.apache.wookie.util.WgtWatcher;
 import org.apache.wookie.util.WidgetFileUtils;
 import org.apache.wookie.util.WidgetJavascriptSyntaxAnalyzer;
-import org.apache.wookie.util.digitalsignature.DigitalSignatureProcessor;
-import org.apache.wookie.util.html.StartPageProcessor;
 import org.apache.wookie.w3c.W3CWidget;
 import org.apache.wookie.w3c.W3CWidgetFactory;
 import org.apache.wookie.w3c.exceptions.BadManifestException;
@@ -167,17 +163,6 @@ public class ContextListener implements 
          */
         final File deploy = new 
File(WidgetPackageUtils.convertPathToPlatform(context.getRealPath(configuration.getString("widget.deployfolder"))));
         final String UPLOADFOLDER = 
context.getRealPath(configuration.getString("widget.useruploadfolder"));
-        final String WIDGETFOLDER = 
context.getRealPath(configuration.getString("widget.widgetfolder"));
-        final String localWidgetFolderPath = 
configuration.getString("widget.widgetfolder");
-        final String[] locales = 
configuration.getStringArray("widget.locales");
-        final String contextPath = context.getContextPath();
-        // Digital signature settings
-        final boolean VERIFYSIGNATURE = 
configuration.getBoolean("widget.deployment.verifysignature");//$NON-NLS-1$
-        final boolean REJECTINVALID= 
configuration.getBoolean("widget.deployment.rejectinvalidsignatures");
-        final boolean REJECTUNTRUSTED= 
configuration.getBoolean("widget.deployment.rejectuntrustedsignatures");
-        final String PASSWORD = 
configuration.getString("widget.deployment.trustedkeystore.password");
-        final String KEYSTORE = 
configuration.getString("widget.deployment.trustedkeystore");//$NON-NLS-1$
-
 
         Thread thr = new Thread(){
             public void run() {
@@ -191,36 +176,7 @@ public class ContextListener implements 
                         try{
                             persistenceManager.begin();
                             File upload = 
WidgetFileUtils.dealWithDroppedFile(UPLOADFOLDER, f);
-                            W3CWidgetFactory fac = new W3CWidgetFactory();
-                            fac.setLocales(locales);
-                            
fac.setLocalPath(contextPath+localWidgetFolderPath);
-                            fac.setOutputDirectory(WIDGETFOLDER);
-                            fac.setFeatures(Features.getFeatureNames());
-                            fac.setStartPageProcessor(new 
StartPageProcessor());
-                            if (VERIFYSIGNATURE) {
-                                KeyStore keyStore = 
KeyStore.getInstance("JKS");
-                                String digSigSchema = context
-                                        
.getRealPath("/WEB-INF/classes/org/apache/wookie/util/digitalsignature/xmldsig-core-schema.xsd");
-                                InputStream stream = 
context.getResourceAsStream("/WEB-INF/classes/" + KEYSTORE);
-                                if (stream == null) {
-                                    stream = 
context.getResourceAsStream("/WEB-INF/classes/" + "generated-" + KEYSTORE);
-                                }
-                                if (stream == null) {
-                                    FileOutputStream fos = new 
FileOutputStream(context
-                                            .getRealPath("/WEB-INF/classes/") 
+ "generated-" + KEYSTORE);
-                                    keyStore.load(null, 
PASSWORD.toCharArray());
-                                    keyStore.store(fos, 
PASSWORD.toCharArray());
-                                    fos.close();
-                                    fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
-                                            digSigSchema, REJECTINVALID, 
REJECTUNTRUSTED));
-                                    
_logger.info(localizedMessages.getString("WidgetHotDeploy.4"));
-                                } else {
-                                    keyStore.load(stream, 
PASSWORD.toCharArray());
-                                    stream.close();
-                                    fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
-                                            digSigSchema, REJECTINVALID, 
REJECTUNTRUSTED));
-                                }
-                            }
+                            W3CWidgetFactory fac = 
W3CWidgetFactoryUtils.createW3CWidgetFactory(context, configuration, 
localizedMessages);
 
                             W3CWidget model = fac.parse(upload);
                             WidgetJavascriptSyntaxAnalyzer jsa = new 
WidgetJavascriptSyntaxAnalyzer(fac.getUnzippedWidgetDirectory());

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/updates/UpdatesController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/updates/UpdatesController.java?rev=1378880&r1=1378879&r2=1378880&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/updates/UpdatesController.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/updates/UpdatesController.java 
Thu Aug 30 10:45:14 2012
@@ -17,11 +17,9 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.configuration.Configuration;
 import org.apache.log4j.Logger;
 import org.apache.wookie.beans.IWidget;
 import org.apache.wookie.beans.util.IPersistenceManager;
@@ -31,9 +29,8 @@ import org.apache.wookie.exceptions.Inva
 import org.apache.wookie.exceptions.ResourceDuplicationException;
 import org.apache.wookie.exceptions.ResourceNotFoundException;
 import org.apache.wookie.exceptions.UnauthorizedAccessException;
-import org.apache.wookie.feature.Features;
 import org.apache.wookie.helpers.WidgetFactory;
-import org.apache.wookie.util.html.StartPageProcessor;
+import org.apache.wookie.util.W3CWidgetFactoryUtils;
 import org.apache.wookie.w3c.W3CWidgetFactory;
 import org.apache.wookie.w3c.exceptions.BadManifestException;
 import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
@@ -106,16 +103,17 @@ public class UpdatesController extends C
                        // Get all installed widgets
                        IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
                        IWidget[] widgets = 
persistenceManager.findAll(IWidget.class);
-                       // Create a W3CWidget factory for the current context
-                       W3CWidgetFactory factory  = 
getFactory(request.getSession().getServletContext());
-                       // Iterate over the widgets and attempt to install 
updates
-                       for (IWidget widget: widgets){
-                               try {
+
+                       try {
+                               // Create a W3CWidget factory for the current 
context
+                               W3CWidgetFactory factory = 
W3CWidgetFactoryUtils.createW3CWidgetFactory(getServletContext());
+                               // Iterate over the widgets and attempt to 
install updates
+                               for (IWidget widget: widgets){
                                        installUpdate(factory, widget, 
onlyUseHttps);
-                               } catch (Exception e) {
-                                       _logger.warn(e.getMessage(), e);
                                }
-                       }       
+                       } catch (Exception e) {
+                               _logger.warn(e.getMessage(), e);
+                       }
                        return true;
        }
 
@@ -132,7 +130,7 @@ public class UpdatesController extends C
                        if (widget == null) throw new 
ResourceNotFoundException();
                        // FIXME localize error messages
                        try {
-                               W3CWidgetFactory factory  = 
getFactory(request.getSession().getServletContext());
+                               W3CWidgetFactory factory = 
W3CWidgetFactoryUtils.createW3CWidgetFactory(getServletContext());
                                installUpdate(factory, widget, false);
                        } catch (IOException e) {
                                _logger.warn("Problem updating "+resourceId+": 
widget couldn't be downloaded");
@@ -174,30 +172,6 @@ public class UpdatesController extends C
        }
        
        /**
-        * Obtain a W3CWidgetFactory configured for this servlet context
-        * @param context
-        * @return the factory
-        * @throws IOException
-        */
-       private W3CWidgetFactory getFactory(ServletContext context){
-               Configuration properties = (Configuration) 
context.getAttribute("properties"); //$NON-NLS-1$
-               W3CWidgetFactory factory = new W3CWidgetFactory();
-               final String[] locales = 
properties.getStringArray("widget.locales");
-               factory.setLocales(locales);
-               
factory.setLocalPath(context.getContextPath()+properties.getString("widget.widgetfolder"));
-               final String WIDGETFOLDER = 
context.getRealPath(properties.getString("widget.widgetfolder"));//$NON-NLS-1$
-               try {
-                       factory.setOutputDirectory(WIDGETFOLDER);
-               } catch (IOException e) {
-                       _logger.error(e);
-               }
-               // Configure the widget factory with the installed feature set
-               factory.setFeatures(Features.getFeatureNames());
-               factory.setStartPageProcessor(new StartPageProcessor());
-               return factory;
-       }
-       
-       /**
         * Get available updates for all installed widgets. Note that this 
method takes a long
         * time to return as it has to poll all the available update sites, so 
where possible
         * we ought to cache the returned updates

Added: 
incubator/wookie/trunk/src/org/apache/wookie/util/W3CWidgetFactoryUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/util/W3CWidgetFactoryUtils.java?rev=1378880&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/util/W3CWidgetFactoryUtils.java 
(added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/util/W3CWidgetFactoryUtils.java 
Thu Aug 30 10:45:14 2012
@@ -0,0 +1,109 @@
+/*
+ * 
+ * Licensed 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.wookie.util;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.KeyStore;
+
+import javax.servlet.ServletContext;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.log4j.Logger;
+import org.apache.wookie.Messages;
+import org.apache.wookie.feature.Features;
+import org.apache.wookie.util.digitalsignature.DigitalSignatureProcessor;
+import org.apache.wookie.util.html.StartPageProcessor;
+import org.apache.wookie.w3c.W3CWidgetFactory;
+
+/**
+ * Utility for creating a W3CWidgetFactory using current context and 
configuration settings
+ */
+public class W3CWidgetFactoryUtils {
+       
+       private static Logger logger = 
Logger.getLogger(W3CWidgetFactoryUtils.class.getName());
+       
+       /**
+        * Create a contextualized W3CWidgetFactory for parsing W3C Widget 
packages
+        * @param context the servlet context
+        * @param configuration the configuration properties
+        * @param localizedMessages the set of localized messages for 
errors/warnings
+        * @return a new W3CWidgetFactory instance
+        * @throws IOException
+        * @throws Exception
+        */
+       public static W3CWidgetFactory createW3CWidgetFactory(final 
ServletContext context, final Configuration configuration, final Messages 
localizedMessages) throws IOException, Exception {
+        final String WIDGETFOLDER = 
context.getRealPath(configuration.getString("widget.widgetfolder"));
+        final String localWidgetFolderPath = 
configuration.getString("widget.widgetfolder");
+        final String[] locales = 
configuration.getStringArray("widget.locales");
+        final String contextPath = context.getContextPath();
+        // Digital signature settings
+        final boolean VERIFYSIGNATURE = 
configuration.getBoolean("widget.deployment.verifysignature");//$NON-NLS-1$
+        final boolean REJECTINVALID= 
configuration.getBoolean("widget.deployment.rejectinvalidsignatures");
+        final boolean REJECTUNTRUSTED= 
configuration.getBoolean("widget.deployment.rejectuntrustedsignatures");
+        final String PASSWORD = 
configuration.getString("widget.deployment.trustedkeystore.password");
+        final String KEYSTORE = 
configuration.getString("widget.deployment.trustedkeystore");//$NON-NLS-1$
+        
+        W3CWidgetFactory fac = new W3CWidgetFactory();
+        fac.setLocales(locales);
+        fac.setLocalPath(contextPath+localWidgetFolderPath);
+        fac.setOutputDirectory(WIDGETFOLDER);
+        fac.setFeatures(Features.getFeatureNames());
+        fac.setStartPageProcessor(new StartPageProcessor());
+        if (VERIFYSIGNATURE) {
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            String digSigSchema = context
+                    
.getRealPath("/WEB-INF/classes/org/apache/wookie/util/digitalsignature/xmldsig-core-schema.xsd");
+            InputStream stream = 
context.getResourceAsStream("/WEB-INF/classes/" + KEYSTORE);
+            if (stream == null) {
+                stream = context.getResourceAsStream("/WEB-INF/classes/" + 
"generated-" + KEYSTORE);
+            }
+            if (stream == null) {
+                FileOutputStream fos = new FileOutputStream(context
+                        .getRealPath("/WEB-INF/classes/") + "generated-" + 
KEYSTORE);
+                keyStore.load(null, PASSWORD.toCharArray());
+                keyStore.store(fos, PASSWORD.toCharArray());
+                fos.close();
+                fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
+                        digSigSchema, REJECTINVALID, REJECTUNTRUSTED));
+                logger.info(localizedMessages.getString("WidgetHotDeploy.4"));
+            } else {
+                keyStore.load(stream, PASSWORD.toCharArray());
+                stream.close();
+                fac.setDigitalSignatureParser(new 
DigitalSignatureProcessor(keyStore,
+                        digSigSchema, REJECTINVALID, REJECTUNTRUSTED));
+            }
+        }
+        return fac;
+       }
+       
+       /**
+        * Create a W3CWidgetFactory instance for parsing W3C Widget packages
+        * @param context the servlet context
+        * @return a new W3CWidgetFactory instance
+        * @throws IOException
+        * @throws Exception
+        */
+       public static W3CWidgetFactory createW3CWidgetFactory(ServletContext 
context) throws IOException, Exception{
+               Configuration configuration = (Configuration) 
context.getAttribute("properties");
+               Messages messages = null;
+               return createW3CWidgetFactory(context, configuration, messages);
+       }
+
+}


Reply via email to