Author: scottbw
Date: Mon Mar 21 14:05:18 2011
New Revision: 1083790

URL: http://svn.apache.org/viewvc?rev=1083790&view=rev
Log:
Added a client API for the Whitelist admin functions. See 
https://cwiki.apache.org/confluence/display/WOOKIE/Wookie+Admin+REST+API for 
information on how to use this with an admin client. Combined with the WARP API 
this starts to decouple the administration interface of Wookie from the 
built-in JSP/servlets so we can use Widgets to manage a Wookie server instance 
using any kind of container. 

Added:
    
incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java
    incubator/wookie/trunk/src/org/apache/wookie/helpers/WhitelistHelper.java
Modified:
    incubator/wookie/trunk/WebContent/WEB-INF/web.xml

Modified: incubator/wookie/trunk/WebContent/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/WebContent/WEB-INF/web.xml?rev=1083790&r1=1083789&r2=1083790&view=diff
==============================================================================
--- incubator/wookie/trunk/WebContent/WEB-INF/web.xml (original)
+++ incubator/wookie/trunk/WebContent/WEB-INF/web.xml Mon Mar 21 14:05:18 2011
@@ -183,6 +183,20 @@
                <servlet-name>WidgetAccessRequestPolicyController</servlet-name>
                <url-pattern>/warp/*</url-pattern>
        </servlet-mapping>
+       
+       <servlet>
+               <description></description>
+               <display-name>Whitelist</display-name>
+               <servlet-name>WhitelistController</servlet-name>
+               <servlet-class>
+                       org.apache.wookie.controller.WhiteListController
+               </servlet-class>
+               <load-on-startup>2</load-on-startup>
+       </servlet>      
+       <servlet-mapping>
+               <servlet-name>WhitelistController</servlet-name>
+               <url-pattern>/whitelist/*</url-pattern>
+       </servlet-mapping>
 
        <servlet>
                <description></description>
@@ -325,6 +339,19 @@
                                <role-name>widgetadmin</role-name>
                        </auth-constraint>
                </security-constraint>
+               <security-constraint>           
+                       <web-resource-collection>
+                               
<web-resource-name>WhitelistController</web-resource-name>
+                               <url-pattern>/whitelist/*</url-pattern>
+                               <http-method>GET</http-method>
+                               <http-method>DELETE</http-method>
+                               <http-method>PUT</http-method>
+                               <http-method>POST</http-method>
+                       </web-resource-collection>              
+                       <auth-constraint>
+                               <role-name>widgetadmin</role-name>
+                       </auth-constraint>
+               </security-constraint>
                
                <login-config>
                        <auth-method>BASIC</auth-method>

Added: 
incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java?rev=1083790&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java
 Mon Mar 21 14:05:18 2011
@@ -0,0 +1,77 @@
+/*
+ *  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.controller;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.wookie.beans.IWhitelist;
+import org.apache.wookie.beans.util.IPersistenceManager;
+import org.apache.wookie.beans.util.PersistenceManagerFactory;
+import org.apache.wookie.exceptions.InvalidParametersException;
+import org.apache.wookie.exceptions.ResourceDuplicationException;
+import org.apache.wookie.exceptions.ResourceNotFoundException;
+import org.apache.wookie.exceptions.UnauthorizedAccessException;
+import org.apache.wookie.helpers.WhitelistHelper;
+
+/**
+ * Controller for Whitelist entries
+ *
+ */
+public class WhiteListController extends Controller {
+
+       private static final long serialVersionUID = 5024023589608473984L;
+
+       @Override
+       protected void index(HttpServletRequest request,
+                       HttpServletResponse response) throws 
UnauthorizedAccessException,
+                       IOException {
+        IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+        IWhitelist[] entries = persistenceManager.findAll(IWhitelist.class);
+        
+               switch (format(request)) {
+                       case XML: 
returnXml(WhitelistHelper.createXMLDocument(entries),response);break;
+                       case JSON: 
returnHtml(WhitelistHelper.createJSON(entries),response);break;
+                       case HTML: 
returnHtml(WhitelistHelper.createHTML(entries),response);break;
+               }
+       }
+
+       @Override
+       protected boolean create(String resourceId, HttpServletRequest request)
+                       throws ResourceDuplicationException, 
InvalidParametersException,
+                       UnauthorizedAccessException {
+               
+               String url = request.getParameter("url");
+               if (url == null || url.trim().length() == 0) throw new 
InvalidParametersException();
+               IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+               IWhitelist entry = 
persistenceManager.newInstance(IWhitelist.class);
+               entry.setfUrl(url);
+               return persistenceManager.save(entry);
+       }
+
+       @Override
+       protected boolean remove(String resourceId, HttpServletRequest request)
+                       throws ResourceNotFoundException, 
UnauthorizedAccessException,
+                       InvalidParametersException {
+        IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+        IWhitelist entry = persistenceManager.findById(IWhitelist.class, 
resourceId);
+        if (entry == null)throw new ResourceNotFoundException();
+        return persistenceManager.delete(entry);       
+       }
+       
+       
+
+}

Added: incubator/wookie/trunk/src/org/apache/wookie/helpers/WhitelistHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/WhitelistHelper.java?rev=1083790&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/WhitelistHelper.java 
(added)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/WhitelistHelper.java 
Mon Mar 21 14:05:18 2011
@@ -0,0 +1,80 @@
+/*
+ *  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.IWhitelist;
+
+public class WhitelistHelper {
+
+       private static final String XMLDECLARATION = "<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>";
+
+       /**
+        * Creates an XML return document 
+        * @param accessRequests
+        * @return
+        */
+       public static String createXMLDocument(IWhitelist[] entries){
+               String document = XMLDECLARATION;
+               document += "\n<entries>\n";
+               if (entries != null){
+                       for (IWhitelist entry:entries){
+                               document += toXml(entry);
+                       }
+               }
+               document += "</entries>\n";
+               return document;
+       }
+       
+       private static String toXml(IWhitelist entry){
+               String xml = "\t<entry ";
+               xml += "id=\""+entry.getId()+"\" ";
+               xml += "url=\""+entry.getfUrl()+"\" ";
+               xml += "/>\n";
+               return xml;
+       }
+       
+       public static String createJSON(IWhitelist[] entries){
+               String json = "{";
+               json +="\"entries\": [";
+               if (entries != null){
+                       for (IWhitelist entry:entries){
+                               json += toJSON(entry);
+                       }
+               }               
+               // remove last comma
+               json = json.substring(0, json.length()-1);
+               json += "]}";
+               return json;
+       }
+       
+       public static String toJSON(IWhitelist entry){
+               String json = "{\"id\": \""+ entry.getId() +"\"";
+               json += ", \"url\":\"" + entry.getfUrl() + "\"},";
+               return  json;
+       }
+       
+       public static String createHTML(IWhitelist[] entries){
+               String html = "<ul>\n";
+               if (entries != null){
+                       for (IWhitelist entry:entries){
+                               html += toHTML(entry);                          
}
+               }               
+               html += "</ul>\n";
+               return html;
+       }
+       
+       public static String toHTML(IWhitelist entry){
+               return "\t<li>"+entry.getfUrl()+"</li>\n";
+       }
+}


Reply via email to