Author: scottbw
Date: Sun Nov 22 11:08:47 2009
New Revision: 883058

URL: http://svn.apache.org/viewvc?rev=883058&view=rev
Log:
Refactored the WidgetInstancesController to better separate the concerns of 
processing REST requests, representing  WidgetInstance resources, and minting 
new instances

Added:
    incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java
      - copied, changed from r835529, 
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java
    
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java
Modified:
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java

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=883058&r1=883057&r2=883058&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
 Sun Nov 22 11:08:47 2009
@@ -16,6 +16,7 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.net.MalformedURLException;
 import java.net.URL;
 
 import javax.servlet.ServletException;
@@ -27,16 +28,13 @@
 import org.apache.log4j.Logger;
 import org.apache.wookie.Messages;
 import org.apache.wookie.beans.SharedData;
-import org.apache.wookie.beans.Widget;
 import org.apache.wookie.beans.WidgetInstance;
 import org.apache.wookie.exceptions.InvalidWidgetCallException;
 import org.apache.wookie.helpers.Notifier;
 import org.apache.wookie.helpers.WidgetFactory;
+import org.apache.wookie.helpers.WidgetInstanceHelper;
 import org.apache.wookie.helpers.WidgetKeyManager;
-import org.apache.wookie.manifestmodel.IW3CXMLConfiguration;
 import org.apache.wookie.server.LocaleHandler;
-import org.apache.wookie.util.HashGenerator;
-import org.apache.wookie.util.RandomGUID;
 
 /**
  * REST implementation for widgetInstance
@@ -50,9 +48,26 @@
 
        private static final long serialVersionUID = 308590474406800659L;       
        
        static Logger _logger = 
Logger.getLogger(WidgetInstancesController.class.getName());    
-       private static final String XMLDECLARATION = "<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>"; //$NON-NLS-1$
-       private static final String CONTENT_TYPE = 
"text/xml;charset=\"UTF-8\"";         //$NON-NLS-1$
-       private static URL urlWidgetProxyServer = null; 
+       protected static final String CONTENT_TYPE = 
"text/xml;charset=\"UTF-8\"";       //$NON-NLS-1$
+       protected static URL urlWidgetProxyServer = null;       
+       
+       protected static void checkProxy(HttpServletRequest request){
+               // set the proxy url.
+               if(urlWidgetProxyServer==null){
+                       Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
+                       String scheme = request.getScheme();
+                       String serverName = request.getServerName();
+                       int serverPort = request.getServerPort();
+                       if 
(!properties.getString("widget.proxy.scheme").trim().equals("")) scheme = 
properties.getString("widget.proxy.scheme"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
+                       if 
(!properties.getString("widget.proxy.hostname").trim().equals("")) serverName = 
properties.getString("widget.proxy.hostname"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
+                       if 
(!properties.getString("widget.proxy.port").trim().equals("")) serverPort = 
Integer.parseInt(properties.getString("widget.proxy.port")); //$NON-NLS-1$ 
//$NON-NLS-2$ //$NON-NLS-3$
+                       try {
+                               urlWidgetProxyServer = new 
URL(scheme,serverName,serverPort,properties.getString("widget.proxy.path"));
+                       } catch (MalformedURLException e) {
+                               // ignore errors
+                       } 
+               }
+       }
 
        /* (non-Javadoc)
         * @see 
javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, 
javax.servlet.http.HttpServletResponse)
@@ -142,7 +157,7 @@
                        Notifier.notifyWidgets(request.getSession(), instance, 
Notifier.STATE_UPDATED);
                        response.setStatus(HttpServletResponse.SC_OK);
                }else{
-                       
response.sendError(HttpServletResponse.SC_BAD_REQUEST,localizedMessages.getString("WidgetServiceServlet.3"));
+                       
response.sendError(HttpServletResponse.SC_BAD_REQUEST,localizedMessages.getString("WidgetServiceServlet.3"));//$NON-NLS-1$
                }
        }
 
@@ -154,7 +169,7 @@
                        Notifier.notifyWidgets(request.getSession(), instance, 
Notifier.STATE_UPDATED);
                        response.setStatus(HttpServletResponse.SC_OK);
                }else{
-                       
response.sendError(HttpServletResponse.SC_BAD_REQUEST,localizedMessages.getString("WidgetServiceServlet.3"));
+                       
response.sendError(HttpServletResponse.SC_BAD_REQUEST,localizedMessages.getString("WidgetServiceServlet.3"));//$NON-NLS-1$
                }
        }
        
@@ -163,6 +178,8 @@
                String sharedDataKey = getSharedDataKey(request);       
                String serviceType = request.getParameter("servicetype"); 
//$NON-NLS-1$
                String widgetId = request.getParameter("widgetid"); 
//$NON-NLS-1$
+               HttpSession session = request.getSession(true);                 
                        
+               Messages localizedMessages = 
LocaleHandler.localizeMessages(request);
 
                try {                                           
                        if(userId==null || sharedDataKey==null || 
(serviceType==null && widgetId==null)){
@@ -173,37 +190,30 @@
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        return;
                }
-
-               // set the proxy url.
-               if(urlWidgetProxyServer==null){
-                       Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("properties"); 
//$NON-NLS-1$
-                       String scheme = request.getScheme();
-                       String serverName = request.getServerName();
-                       int serverPort = request.getServerPort();
-                       if 
(!properties.getString("widget.proxy.scheme").trim().equals("")) scheme = 
properties.getString("widget.proxy.scheme"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-                       if 
(!properties.getString("widget.proxy.hostname").trim().equals("")) serverName = 
properties.getString("widget.proxy.hostname"); //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$
-                       if 
(!properties.getString("widget.proxy.port").trim().equals("")) serverPort = 
Integer.parseInt(properties.getString("widget.proxy.port")); //$NON-NLS-1$ 
//$NON-NLS-2$ //$NON-NLS-3$
-                       urlWidgetProxyServer = new 
URL(scheme,serverName,serverPort,properties.getString("widget.proxy.path")); 
//$NON-NLS-1$
-               }
-
+               
+               checkProxy(request);
+               
                WidgetInstance instance = 
WidgetInstancesController.findWidgetInstance(request);
 
                // Widget exists
                if(instance==null){
-                       instance = newInstance(request);
+                       String apiKey = request.getParameter("api_key"); 
//$NON-NLS-1$
+                       instance = WidgetFactory.getWidgetFactory(session, 
localizedMessages).newInstance(apiKey, userId, sharedDataKey, serviceType, 
widgetId);
                        response.setStatus(HttpServletResponse.SC_CREATED);
                } else {
                        response.setStatus(HttpServletResponse.SC_OK);          
        
                }
                
-               // Return default widget if not created by now
+               // Return the default widget if not created by now
                if(instance==null){
-                       Widget unsupportedWidget = 
Widget.findDefaultByType("unsupported"); //$NON-NLS-1$
+                       instance = WidgetFactory.getWidgetFactory(session, 
localizedMessages).defaultInstance();
                        response.setStatus(HttpServletResponse.SC_NOT_FOUND);   
-                       formatReturnDoc(request, response, unsupportedWidget, 
"0000", ""); //$NON-NLS-1$ //$NON-NLS-2$
-               } else {
-                       formatReturnDoc(request, response, 
instance.getWidget(), instance.getIdKey(), instance.getOpensocialToken());
                }
+               
+               String url = getUrl(request, instance);
+               response.setContentType(CONTENT_TYPE);
+               PrintWriter out = response.getWriter();
+               
out.println(WidgetInstanceHelper.createXMLWidgetInstanceDocument(instance, 
url));
        }  
        
        public static void cloneSharedData(HttpServletRequest request, 
HttpServletResponse response){
@@ -214,11 +224,11 @@
                }
                String sharedDataKey = getSharedDataKey(request);       
                String cloneSharedDataKey = 
request.getParameter("cloneshareddatakey");
-               if (sharedDataKey == null || sharedDataKey.trim().equals("") || 
cloneSharedDataKey == null || cloneSharedDataKey.trim().equals("")){
+               if (sharedDataKey == null || sharedDataKey.trim().equals("") || 
cloneSharedDataKey == null || 
cloneSharedDataKey.trim().equals("")){//$NON-NLS-1$ //$NON-NLS-2$
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        return;
                }
-               String cloneKey = 
String.valueOf((request.getParameter("apikey")+":"+cloneSharedDataKey).hashCode());
+               String cloneKey = 
String.valueOf((request.getParameter("apikey")+":"+cloneSharedDataKey).hashCode());//$NON-NLS-1$
 
                boolean ok = SharedData.clone(sharedDataKey, 
instance.getWidget().getGuid(), cloneKey);
                if (ok){
                        response.setStatus(HttpServletResponse.SC_OK);
@@ -227,111 +237,60 @@
                }
        }
        
-       // Utility methods
+       public synchronized static void lockWidgetInstance(WidgetInstance 
instance){
+               //doLock(instance, true);
+               PropertiesController.updateSharedDataEntry(instance, 
"isLocked", "true", false);//$NON-NLS-1$ //$NON-NLS-2$
+               instance.setLocked(true);
+               instance.save();
+       }
 
-       private static WidgetInstance newInstance(HttpServletRequest request){
-               String apiKey = request.getParameter("api_key"); //$NON-NLS-1$
-               String userId = request.getParameter("userid"); //$NON-NLS-1$
-               String sharedDataKey = getSharedDataKey(request);       
-               String serviceType = request.getParameter("servicetype"); 
//$NON-NLS-1$
-               String widgetId = request.getParameter("widgetid"); 
//$NON-NLS-1$
-               HttpSession session = request.getSession(true);                 
                        
-               Messages localizedMessages = 
LocaleHandler.localizeMessages(request);
-               WidgetFactory factory = getWidgetFactory(session, 
localizedMessages); 
-               try {
-                       Widget widget;
-                       WidgetInstance widgetInstance;
-                       // Widget ID or Widget Type?
-                       if (widgetId != null){
-                               widget = Widget.findByGuid(widgetId);
-                       } 
-                       else {
-                               // does this type of widget exist?
-                               widget = Widget.findDefaultByType(serviceType); 
                        
-                       }
-                       // Unsupported
-                       if (widget == null) return null;
+       public synchronized static void unlockWidgetInstance(WidgetInstance 
instance){
+               //doLock(instance, false);
+               PropertiesController.updateSharedDataEntry(instance, 
"isLocked", "false", false);//$NON-NLS-1$ //$NON-NLS-2$
+               instance.setLocked(false);
+               instance.save();
+       }
+       
+       // Utility methods
 
-                       // generate a nonce
-                       RandomGUID r = new RandomGUID();
-                       String nonce = "nonce-" + r.toString();                 
         //$NON-NLS-1$
-
-                       // now use SHA hash on the nonce                        
        
-                       String hashKey = 
HashGenerator.getInstance().encrypt(nonce);    
-
-                       // get rid of any chars that might upset a url...
-                       hashKey = hashKey.replaceAll("=", ".eq."); 
//$NON-NLS-1$ //$NON-NLS-2$
-                       hashKey = hashKey.replaceAll("\\?", ".qu."); 
//$NON-NLS-1$ //$NON-NLS-2$
-                       hashKey = hashKey.replaceAll("&", ".am."); 
//$NON-NLS-1$ //$NON-NLS-2$
-                       hashKey = hashKey.replaceAll("\\+", ".pl."); 
//$NON-NLS-1$ //$NON-NLS-2$
-            hashKey = hashKey.replaceAll("/", ".sl."); //$NON-NLS-1$ 
//$NON-NLS-2$
-                       
-                       Configuration properties = (Configuration) 
request.getSession().getServletContext().getAttribute("opensocial"); 
//$NON-NLS-1$
-                       widgetInstance = factory.addNewWidgetInstance(apiKey, 
userId, sharedDataKey, widget, nonce, hashKey, properties);
-                       return widgetInstance;
-               } catch (Exception ex) {
-                       return null;
-               }
+       /**
+        * Returns the internal form of shared data key, which is hashed along 
with the API key. This
+        * prevents shared data keys from clashing between different 
applications
+        * @param request the HTTP request to retrieve the shared data key from 
+        * @return the shared data key
+        */
+       public static String getSharedDataKey(HttpServletRequest request){
+               return 
String.valueOf((request.getParameter("apikey")+":"+request.getParameter("shareddatakey")).hashCode());
    //$NON-NLS-1$ //$NON-NLS-2$
        }
        
-       private static void formatReturnDoc(HttpServletRequest request, 
HttpServletResponse response, Widget widget, String key, String token) throws 
IOException{
+       /**
+        * Returns the absolute URL of the widget instance including id key, 
proxy url and opensocial token 
+        * @param request the current request
+        * @param instance the widget instance
+        * @return the absolute URL
+        * @throws IOException
+        */
+       protected static String getUrl(HttpServletRequest request, 
WidgetInstance instance) throws IOException{
+               String url = "";
                URL urlWidget =  new URL(request.getScheme() ,
                                request.getServerName() ,
-                               request.getServerPort() , widget.getUrl());
-               
-               // Return a default width and height where the original value 
is either not provided
-               // or of an invalid range (<0)
-               String width = 
String.valueOf(IW3CXMLConfiguration.DEFAULT_WIDTH_LARGE);
-               String height = 
String.valueOf(IW3CXMLConfiguration.DEFAULT_HEIGHT_LARGE);
-               if (widget.getWidth()!=null && widget.getWidth()>0) width = 
widget.getWidth().toString();
-               if (widget.getHeight()!=null && widget.getHeight()>0) height = 
widget.getHeight().toString();
+                               request.getServerPort() , 
instance.getWidget().getUrl());
                
-               response.setContentType(CONTENT_TYPE);
-               PrintWriter out = response.getWriter();
-               out.println(XMLDECLARATION);                    
-               out.println("<widgetdata>");                                    
 //$NON-NLS-1$
-               out.print("<url>"); //$NON-NLS-1$
                if (urlWidget.getQuery() != null){
-                       out.print(urlWidget + "&amp;idkey=" + key  //$NON-NLS-1$
+                       url+= urlWidget + "&amp;idkey=" + instance.getIdKey()  
//$NON-NLS-1$
                                        + "&amp;proxy=" + 
urlWidgetProxyServer.toExternalForm()  //$NON-NLS-1$
-                                       + "&amp;st=" + token //$NON-NLS-1$
-                       );      
+                                       + "&amp;st=" + 
instance.getOpensocialToken(); //$NON-NLS-1$
                } else {
-                       out.print(urlWidget + "?idkey=" + key  //$NON-NLS-1$
+                       url+= urlWidget + "?idkey=" + instance.getIdKey()  
//$NON-NLS-1$
                                        + "&amp;proxy=" + 
urlWidgetProxyServer.toExternalForm()  //$NON-NLS-1$
-                                       + "&amp;st=" + token //$NON-NLS-1$
-                       );
-               }
-               out.println("</url>"); //$NON-NLS-1$
-               out.println("<identifier>"+key+"</identifier>"); //$NON-NLS-1$ 
//$NON-NLS-2$
-               out.println("<title>"+widget.getWidgetTitle()+"</title>"); 
//$NON-NLS-1$ //$NON-NLS-2$
-               out.println("<height>"+height+"</height>"); //$NON-NLS-1$ 
//$NON-NLS-2$
-               out.println("<width>"+width+"</width>"); //$NON-NLS-1$ 
//$NON-NLS-2$
-               out.println("<maximize>"+widget.isMaximize()+"</maximize>"); 
//$NON-NLS-1$ //$NON-NLS-2$
-               out.println("</widgetdata>"); //$NON-NLS-1$
-       }
-
-       public static String getSharedDataKey(HttpServletRequest request){
-               return 
String.valueOf((request.getParameter("apikey")+":"+request.getParameter("shareddatakey")).hashCode());
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-       }
-
-       /**
-        * Return a handle on a service manager localized for the session
-        * @param session
-        * @param localizedMessages
-        * @return
-        */
-       private static WidgetFactory getWidgetFactory(HttpSession session, 
Messages localizedMessages){
-               WidgetFactory manager = 
(WidgetFactory)session.getAttribute(WidgetFactory.class.getName());             
                
-               if(manager == null){
-                       manager = new WidgetFactory(localizedMessages);
-                       session.setAttribute(WidgetFactory.class.getName(), 
manager);
+                                       + "&amp;st=" + 
instance.getOpensocialToken(); //$NON-NLS-1$
                }
-               return manager;
+               return url;
        }
        
        /**
-        * Utility method for locating an instance based on various parameters
+        * Utility method for locating an instance based on various parameters. 
Consider moving to a utils class, or
+        * even to the WidgetInstance ActiveRecord class.
         * @param request
         * @return
         */
@@ -356,21 +315,4 @@
                }
                return instance;
        }
-       
-       /// Business Methods
-
-       public synchronized static void lockWidgetInstance(WidgetInstance 
instance){
-               //doLock(instance, true);
-               PropertiesController.updateSharedDataEntry(instance, 
"isLocked", "true", false);
-               instance.setLocked(true);
-               instance.save();
-       }
-
-       public synchronized static void unlockWidgetInstance(WidgetInstance 
instance){
-               //doLock(instance, false);
-               PropertiesController.updateSharedDataEntry(instance, 
"isLocked", "false", false);
-               instance.setLocked(false);
-               instance.save();
-       }
-
 }
\ No newline at end of file

Copied: incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java 
(from r835529, 
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java)
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java?p2=incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java&p1=incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java&r1=835529&r2=883058&rev=883058&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetFactory.java Sun 
Nov 22 11:08:47 2009
@@ -14,6 +14,8 @@
 
 package org.apache.wookie.helpers;
 
+import javax.servlet.http.HttpSession;
+
 import org.apache.commons.configuration.Configuration;
 import org.apache.log4j.Logger;
 import org.apache.wookie.Messages;
@@ -21,7 +23,8 @@
 import org.apache.wookie.beans.PreferenceDefault;
 import org.apache.wookie.beans.Widget;
 import org.apache.wookie.beans.WidgetInstance;
-import org.apache.wookie.server.LocaleHandler;
+import org.apache.wookie.util.HashGenerator;
+import org.apache.wookie.util.RandomGUID;
 import org.apache.wookie.util.opensocial.OpenSocialUtils;
 
 /**
@@ -35,21 +38,101 @@
 
        static Logger _logger = Logger.getLogger(WidgetFactory.class.getName());
        
+       private Messages localizedMessages;
+       private HttpSession session;
+       
+       boolean showProcess = false;
+       
        /**
-        * keep this here so that all subclasses can access it
+        * Return a handle on a Widget Factory localized for the session
+        * @param session
+        * @param localizedMessages
+        * @return
         */
-       protected Messages localizedMessages;
+       public static WidgetFactory getWidgetFactory(HttpSession session, 
Messages localizedMessages){
+               WidgetFactory factory = 
(WidgetFactory)session.getAttribute(WidgetFactory.class.getName());             
                
+               if(factory == null){
+                       factory = new WidgetFactory();
+                       factory.localizedMessages = localizedMessages;
+                       factory.session = session;
+                       session.setAttribute(WidgetFactory.class.getName(), 
factory);
+               }
+               return factory;
+       }
        
-       boolean showProcess = false;
+       /**
+        * Return the "default widget" instance
+        * @return
+        */
+       public WidgetInstance defaultInstance(){
+               WidgetInstance instance = new WidgetInstance();
+               instance.setWidget(Widget.findDefaultByType("unsupported")); 
//$NON-NLS-1$
+               instance.setIdKey("0000");
+               instance.setOpensocialToken("");
+               return instance;
+       }
+       
+       /**
+        * Create a new widget instance with the given parameters
+        * @param session
+        * @param apiKey
+        * @param userId
+        * @param sharedDataKey
+        * @param serviceType
+        * @param widgetId
+        * @param localizedMessages
+        * @return
+        */
+       public WidgetInstance newInstance(String apiKey, String userId, String 
sharedDataKey, String serviceType, String widgetId){
+               try {
+                       Widget widget;
+                       WidgetInstance widgetInstance;
+                       // Widget ID or Widget Type?
+                       if (widgetId != null){
+                               widget = Widget.findByGuid(widgetId);
+                       } 
+                       else {
+                               // does this type of widget exist?
+                               widget = Widget.findDefaultByType(serviceType); 
                        
+                       }
+                       // Unsupported
+                       if (widget == null) return null;
 
-       public WidgetFactory(Messages localizedMessages) {
-               this.localizedMessages = localizedMessages;     
+                       // generate a nonce
+                       RandomGUID r = new RandomGUID();
+                       String nonce = "nonce-" + r.toString();                 
         //$NON-NLS-1$
+
+                       // now use SHA hash on the nonce                        
        
+                       String hashKey = 
HashGenerator.getInstance().encrypt(nonce);    
+
+                       // get rid of any chars that might upset a url...
+                       hashKey = hashKey.replaceAll("=", ".eq."); 
//$NON-NLS-1$ //$NON-NLS-2$
+                       hashKey = hashKey.replaceAll("\\?", ".qu."); 
//$NON-NLS-1$ //$NON-NLS-2$
+                       hashKey = hashKey.replaceAll("&", ".am."); 
//$NON-NLS-1$ //$NON-NLS-2$
+                       hashKey = hashKey.replaceAll("\\+", ".pl."); 
//$NON-NLS-1$ //$NON-NLS-2$
+            hashKey = hashKey.replaceAll("/", ".sl."); //$NON-NLS-1$ 
//$NON-NLS-2$
+
+                       Configuration properties = (Configuration) 
session.getServletContext().getAttribute("opensocial"); //$NON-NLS-1$
+                       
+                       widgetInstance = addNewWidgetInstance(apiKey, userId, 
sharedDataKey, widget, nonce, hashKey, properties);
+                       return widgetInstance;
+               } catch (Exception ex) {
+                       return null;
+               }
        }
 
-       /* (non-Javadoc)
-        * @see 
org.apache.wookie.manager.IWidgetServiceManager#addNewWidgetInstance(java.lang.String,
 java.lang.String, java.lang.String, java.lang.String, 
org.apache.wookie.beans.Widget, java.lang.String, java.lang.String)
+       /**
+        * Create a new widget instance object, populate its default values, 
and save it.
+        * @param api_key
+        * @param userId
+        * @param sharedDataKey
+        * @param widget
+        * @param nonce
+        * @param idKey
+        * @param properties
+        * @return
         */
-       public WidgetInstance addNewWidgetInstance(String api_key, String 
userId, String sharedDataKey, Widget widget, String nonce, String idKey, 
Configuration properties) {          
+       private WidgetInstance addNewWidgetInstance(String api_key, String 
userId, String sharedDataKey, Widget widget, String nonce, String idKey, 
Configuration properties) {         
                WidgetInstance widgetInstance = new WidgetInstance();
                widgetInstance.setUserId(userId);
                widgetInstance.setSharedDataKey(sharedDataKey);

Added: 
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java?rev=883058&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java 
(added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java 
Sun Nov 22 11:08:47 2009
@@ -0,0 +1,59 @@
+/*
+ *  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.helpers;
+
+import org.apache.wookie.beans.Widget;
+import org.apache.wookie.beans.WidgetInstance;
+import org.apache.wookie.manifestmodel.IW3CXMLConfiguration;
+
+/**
+ * A helper to create representations of Widget Instance resources
+ */
+public class WidgetInstanceHelper {
+       
+       private static final String XMLDECLARATION = "<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>";
+
+       /**
+        * Generate a Widget Instance representation doc in XML for a single 
instance of the given widget
+        * @param widget the widget instantiated
+        * @param token the opensocial token of the instance
+        * @param key the id key of the instance
+        * @param urlWidget the local URL of the widget instance
+        * @param urlWidgetProxyServer the local URL of the proxy service
+        * @return
+        */
+       public static String createXMLWidgetInstanceDocument(WidgetInstance 
instance, String url){
+               String xml = XMLDECLARATION;
+               Widget widget = instance.getWidget();
+               
+               // Return a default width and height where the original value 
is either not provided
+               // or of an invalid range (<0)
+               String width = 
String.valueOf(IW3CXMLConfiguration.DEFAULT_WIDTH_LARGE);
+               String height = 
String.valueOf(IW3CXMLConfiguration.DEFAULT_HEIGHT_LARGE);
+               if (widget.getWidth()!=null && widget.getWidth()>0) width = 
widget.getWidth().toString();
+               if (widget.getHeight()!=null && widget.getHeight()>0) height = 
widget.getHeight().toString();
+                               
+               xml += "<widgetdata>"; //$NON-NLS-1$
+               xml += "\t<url>"+url+"</url>"; //$NON-NLS-1$ //$NON-NLS-2$
+               xml += "\t<identifier>"+instance.getIdKey()+"</identifier>\n"; 
//$NON-NLS-1$ //$NON-NLS-2$
+               xml += "\t<title>"+widget.getWidgetTitle()+"</title>\n"; 
//$NON-NLS-1$ //$NON-NLS-2$
+               xml += "\t<height>"+height+"</height>\n"; //$NON-NLS-1$ 
//$NON-NLS-2$
+               xml += "\t<width>"+width+"</width>\n"; //$NON-NLS-1$ 
//$NON-NLS-2$
+               xml += "\t<maximize>"+widget.isMaximize()+"</maximize>\n"; 
//$NON-NLS-1$ //$NON-NLS-2$
+               xml += "</widgetdata>"; //$NON-NLS-1$
+               
+               return xml;
+       }
+       
+}


Reply via email to