Author: scottbw
Date: Thu Sep  1 09:17:52 2011
New Revision: 1163957

URL: http://svn.apache.org/viewvc?rev=1163957&view=rev
Log:
Refactored generation of Wookie server URLs - pushed up to Controller from 
duplicate code in WidgetInstancesController and FlatpackController.

Modified:
    incubator/wookie/trunk/src/org/apache/wookie/controller/Controller.java
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
    
incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackController.java

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/controller/Controller.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/Controller.java?rev=1163957&r1=1163956&r2=1163957&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/controller/Controller.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/controller/Controller.java Thu 
Sep  1 09:17:52 2011
@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServlet;
 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.exceptions.InvalidParametersException;
 import org.apache.wookie.exceptions.ResourceDuplicationException;
@@ -350,5 +351,35 @@ public abstract class Controller extends
                return HTML;
        }
 
+       /**
+        * Get a URL for a resource on the current Wookie server; this is 
either determined from the request or overridden by properties
+        * defined in widgetserver.properties to support virtual hosts
+        * @param request the originating request
+        * @param path an optional path to the resource
+        * @return a URL pointing to the resource on the Wookie server
+        * @throws MalformedURLException
+        */
+       protected static URL getWookieServerURL(HttpServletRequest request, 
String path) throws MalformedURLException{
+
+    //
+    // Use the request to generate the initial URL components
+    //
+    String scheme = request.getScheme();
+    String serverName = request.getServerName();
+    int serverPort = request.getServerPort();
+    
+    //
+    // Override with configuration properties where present
+    //
+    Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
+    if (properties.getString("widget.server.scheme")!=null && 
!properties.getString("widget.server.scheme").trim().equals("")) scheme = 
properties.getString("widget.server.scheme"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
+    if (properties.getString("widget.server.hostname")!=null && 
!properties.getString("widget.server.hostname").trim().equals("")) serverName = 
properties.getString("widget.server.hostname"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
+    if (properties.getString("widget.server.port")!=null && 
!properties.getString("widget.server.port").trim().equals("")) serverPort = 
Integer.parseInt(properties.getString("widget.server.port")); //$NON-NLS-1$ 
//$NON-NLS-2$ //$NON-NLS-3$
+
+    //
+    // Construct and return URL
+    //
+    return new URL(scheme, serverName, serverPort, path);
+       }
 
 }

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java?rev=1163957&r1=1163956&r2=1163957&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 Thu Sep  1 09:17:52 2011
@@ -323,25 +323,31 @@ public class WidgetInstancesController e
        protected static String getUrl(HttpServletRequest request, 
IWidgetInstance instance) throws IOException{
                String url = "";
 
+               //
+               // Locate the startfile for the Widget Instance
+               //
                IStartFile[] startFiles = 
instance.getWidget().getStartFiles().toArray(new 
IStartFile[instance.getWidget().getStartFiles().size()]);
-    IStartFile sf = (IStartFile) 
LocalizationUtils.getLocalizedElement(startFiles, new 
String[]{instance.getLang()}, instance.getWidget().getDefaultLocale());
-               // Try default locale if no appropriate localization found
+        IStartFile sf = (IStartFile) 
LocalizationUtils.getLocalizedElement(startFiles, new 
String[]{instance.getLang()}, instance.getWidget().getDefaultLocale());
+    
+        //
+        // Try default locale if no appropriate localization found
+        //
                if (sf == null) sf = (IStartFile) 
LocalizationUtils.getLocalizedElement(startFiles, null, 
instance.getWidget().getDefaultLocale());
+               
+               //
                // No start file found, so throw an exception
+               //
                if (sf == null) throw new IOException("No start file located 
for widget "+instance.getWidget().getGuid());
                
-               // Use settings defined in properties if available, otherwise 
use the request context
-        Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
-        String scheme = request.getScheme();
-        String serverName = request.getServerName();
-        int serverPort = request.getServerPort();
+               //
+               // Get a URL for the start file on this Wookie server
+               //
         String path = sf.getUrl();
-        if (properties.getString("widget.server.scheme")!=null && 
!properties.getString("widget.server.scheme").trim().equals("")) scheme = 
properties.getString("widget.server.scheme"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-        if (properties.getString("widget.server.hostname")!=null && 
!properties.getString("widget.server.hostname").trim().equals("")) serverName = 
properties.getString("widget.server.hostname"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-        if (properties.getString("widget.server.port")!=null && 
!properties.getString("widget.server.port").trim().equals("")) serverPort = 
Integer.parseInt(properties.getString("widget.server.port")); //$NON-NLS-1$ 
//$NON-NLS-2$ //$NON-NLS-3$
-
-               URL urlWidget =  new URL(scheme, serverName, serverPort, path);
+               URL urlWidget =  getWookieServerURL(request, path);
                
+               //
+               // Append querystring parameters for the URL: id key, proxy 
URL, and social token
+               //
                if (urlWidget.getQuery() != null){
                        url+= urlWidget + "&idkey=" + instance.getIdKey()  
//$NON-NLS-1$
                                        + "&proxy=" + 
urlWidgetProxyServer.toExternalForm()  //$NON-NLS-1$

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackController.java?rev=1163957&r1=1163956&r2=1163957&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackController.java 
(original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackController.java 
Thu Sep  1 09:17:52 2011
@@ -202,10 +202,10 @@ public class FlatpackController extends 
                if (!WidgetKeyManager.isValidRequest(request)) throw new 
UnauthorizedAccessException();
                String path;
                try {
-                 //
-                 // Construct a FlatpackFactory for the instance identified in 
the request
-                 // If no instance can be found, throw an exception
-                 //
+                   //
+                   // Construct a FlatpackFactory for the instance identified 
in the request
+                   // If no instance can be found, throw an exception
+                   //
                        IWidgetInstance instance = 
WidgetInstancesController.findWidgetInstance(request);
                        if (instance == null) throw new 
InvalidParametersException();
                        FlatpackFactory fac = new FlatpackFactory(instance);
@@ -220,25 +220,16 @@ public class FlatpackController extends 
                        //
                        File flatpack = fac.pack();
                        
-                 //
-                       // Construct the URL pointing to the exported .wgt file
-                       // Use settings defined in properties if available, 
otherwise use the request context
-                       // to construct a URL. Note that the resource begins 
with the servlet path, typically
-                       // "/wookie"
+            //
+            // Construct the URL pointing to the exported .wgt file
+            //  Note that the resource begins with the servlet path, typically 
"/wookie"
                        //
-      Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
-      String scheme = request.getScheme();
-      String serverName = request.getServerName();
-      int serverPort = request.getServerPort();
-      String resource = 
request.getSession().getServletContext().getContextPath() + "/" + 
FlatpackFactory.DEFAULT_FLATPACK_FOLDER + "/" + flatpack.getName();
-      if (properties.getString("widget.server.scheme")!=null && 
!properties.getString("widget.server.scheme").trim().equals("")) scheme = 
properties.getString("widget.server.scheme"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-      if (properties.getString("widget.server.host")!=null && 
!properties.getString("widget.server.host").trim().equals("")) serverName = 
properties.getString("widget.server.host"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-      if (properties.getString("widget.server.port")!=null && 
!properties.getString("widget.server.port").trim().equals("")) serverPort = 
Integer.parseInt(properties.getString("widget.server.port")); //$NON-NLS-1$ 
//$NON-NLS-2$ //$NON-NLS-3$
-      URL url =  new URL(scheme, serverName, serverPort, resource);
+            String resource = 
request.getSession().getServletContext().getContextPath() + "/" + 
FlatpackFactory.DEFAULT_FLATPACK_FOLDER + "/" + flatpack.getName();
+            URL url =  getWookieServerURL(request, resource);
       
-      //
-      // Return the String version of the URL pointing to the exported .wgt 
file
-      //
+            //
+            // Return the String version of the URL pointing to the exported 
.wgt file
+            //
                        path = url.toString();
                        
                } catch (Exception e) {


Reply via email to