Author: woonsan
Date: Fri Nov 27 18:09:57 2009
New Revision: 884933

URL: http://svn.apache.org/viewvc?rev=884933&view=rev
Log:
JS2-1087: Adding query parameter and pagination params.
The following query parameters can be used: 
"query": query string parameter (optional)
"begin": begin index for pagination (optional)
"max": max item count for pagination (optional)

So, for example,

(1) GET all PA infos with query: 
      
http://localhost:8080/jetspeed/services/portletregistry/application/?_type=json&query=demo&begin=0&max=2
(2) GET demo PA info:
      
http://localhost:8080/jetspeed/services/portletregistry/application/demo/?_type=json&query=demo&begin=0&max=2
(3) GET all PD infos:
      
http://localhost:8080/jetspeed/services/portletregistry/definition/?_type=json&query=demo&begin=0&max=2
(4) GET all PD infos of demo PA:
      
http://localhost:8080/jetspeed/services/portletregistry/definition/demo/?_type=json&query=demo&begin=0&max=2
(5) GET demo::PickANumberPortlet PD info:
      
http://localhost:8080/jetspeed/services/portletregistry/definition/demo/PickANumberPortlet/?_type=json&query=demo&begin=0&max=2

TODO: 
 - I think that a new search index field for portlet application name needs to 
be added.
 - Also, "type" search index field can be either "portlet" or 
"portlet_application". However, the former should be renamed to something like 
"portlet_definition" because search by "type: portlet" index field will search 
portlet applications too.

Added:
    
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
   (with props)
    
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
   (with props)
Modified:
    
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PortletRegistryService.java

Modified: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PortletRegistryService.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PortletRegistryService.java?rev=884933&r1=884932&r2=884933&view=diff
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PortletRegistryService.java
 (original)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PortletRegistryService.java
 Fri Nov 27 18:09:57 2009
@@ -17,6 +17,7 @@
 package org.apache.jetspeed.services.rest;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 import javax.servlet.ServletConfig;
@@ -25,19 +26,26 @@
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.math.NumberUtils;
 import org.apache.jetspeed.Jetspeed;
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 import org.apache.jetspeed.om.portlet.PortletApplication;
 import org.apache.jetspeed.om.portlet.PortletDefinition;
+import org.apache.jetspeed.search.ParsedObject;
+import org.apache.jetspeed.search.SearchEngine;
+import org.apache.jetspeed.search.SearchResults;
 import org.apache.jetspeed.services.beans.PortletApplicationBean;
 import org.apache.jetspeed.services.beans.PortletApplicationBeans;
 import org.apache.jetspeed.services.beans.PortletDefinitionBean;
 import org.apache.jetspeed.services.beans.PortletDefinitionBeans;
+import org.apache.jetspeed.services.rest.util.PaginationUtils;
+import org.apache.jetspeed.services.rest.util.SearchEngineUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -67,14 +75,20 @@
     
     private PortletRegistry portletRegistry;
     
+    private SearchEngine searchEngine;
+    
     public PortletRegistryService()
     {
         portletRegistry = (PortletRegistry) 
Jetspeed.getComponentManager().getComponent(PortletRegistry.class);
+        searchEngine = (SearchEngine) 
Jetspeed.getComponentManager().getComponent(SearchEngine.class);
     }
     
     @GET
     @Path("/application/{path:.*}")
-    public PortletApplicationBeans getPortletApplication(@PathParam("path") 
List<PathSegment> pathSegments)
+    public PortletApplicationBeans getPortletApplication(@PathParam("path") 
List<PathSegment> pathSegments,
+                                                         @QueryParam("query") 
String queryParam, 
+                                                         @QueryParam("begin") 
String beginIndexParam,
+                                                         @QueryParam("max") 
String maxResultsParam)
     {
         String applicationName = null;
         
@@ -83,23 +97,52 @@
             applicationName = pathSegments.get(0).getPath();
         }
         
+        int beginIndex = NumberUtils.toInt(beginIndexParam, -1);
+        int maxResults = NumberUtils.toInt(maxResultsParam, -1);
+        
         PortletApplicationBeans paBeans = new PortletApplicationBeans();
         List<PortletApplicationBean> paBeanList = new 
ArrayList<PortletApplicationBean>();
         
-        if (StringUtils.isBlank(applicationName))
+        if (!StringUtils.isBlank(queryParam))
         {
-            for (PortletApplication pa : 
portletRegistry.getPortletApplications())
+            String queryText = ParsedObject.FIELDNAME_TYPE + ":\"" + 
ParsedObject.OBJECT_TYPE_PORTLET_APPLICATION + "\" AND " + queryParam;
+            SearchResults searchResults = searchEngine.search(queryText);
+            List<ParsedObject> searchResultList = searchResults.getResults();
+            
+            for (ParsedObject parsedObject : (List<ParsedObject>) 
PaginationUtils.subList(searchResultList, beginIndex, maxResults))
             {
-                paBeanList.add(new PortletApplicationBean(pa));
+                String appName = 
SearchEngineUtils.getFieldAsString(parsedObject, "ID", null);
+                
+                if (StringUtils.isBlank(appName))
+                {
+                    continue;
+                }
+                
+                PortletApplication pa = 
portletRegistry.getPortletApplication(appName);
+                
+                if (pa != null)
+                {
+                    paBeanList.add(new PortletApplicationBean(pa));
+                }
             }
         }
         else
         {
-            PortletApplication pa = 
portletRegistry.getPortletApplication(applicationName, true);
-            
-            if (pa != null)
+            if (StringUtils.isBlank(applicationName))
             {
-                paBeanList.add(new PortletApplicationBean(pa));
+                for (PortletApplication pa : (Collection<PortletApplication>) 
PaginationUtils.subCollection(portletRegistry.getPortletApplications(), 
beginIndex, maxResults))
+                {
+                    paBeanList.add(new PortletApplicationBean(pa));
+                }
+            }
+            else
+            {
+                PortletApplication pa = 
portletRegistry.getPortletApplication(applicationName, true);
+                
+                if (pa != null)
+                {
+                    paBeanList.add(new PortletApplicationBean(pa));
+                }
             }
         }
         
@@ -109,7 +152,10 @@
     
     @GET
     @Path("/definition/{path:.*}")
-    public PortletDefinitionBeans getPortletDefinition(@PathParam("path") 
List<PathSegment> pathSegments)
+    public PortletDefinitionBeans getPortletDefinition(@PathParam("path") 
List<PathSegment> pathSegments, 
+                                                       @QueryParam("query") 
String queryParam, 
+                                                       @QueryParam("begin") 
String beginIndexParam,
+                                                       @QueryParam("max") 
String maxResultsParam)
     {
         String applicationName = null;
         String definitionName = null;
@@ -127,39 +173,71 @@
             }
         }
         
+        int beginIndex = NumberUtils.toInt(beginIndexParam, -1);
+        int maxResults = NumberUtils.toInt(maxResultsParam, -1);
+        
         PortletDefinitionBeans pdBeans = new PortletDefinitionBeans();
         List<PortletDefinitionBean> pdBeanList = new 
ArrayList<PortletDefinitionBean>();
         
-        if (StringUtils.isBlank(applicationName) && 
StringUtils.isBlank(definitionName))
+        if (!StringUtils.isBlank(queryParam))
         {
-            for (PortletDefinition pd : 
portletRegistry.getAllPortletDefinitions())
+            String queryText = 
+                ParsedObject.FIELDNAME_TYPE + ":\"" + 
ParsedObject.OBJECT_TYPE_PORTLET + "\" " +
+                "AND NOT " + ParsedObject.FIELDNAME_TYPE + ":\"" + 
ParsedObject.OBJECT_TYPE_PORTLET_APPLICATION + "\" " + 
+                "AND " + queryParam;
+            SearchResults searchResults = searchEngine.search(queryText);
+            List<ParsedObject> searchResultList = searchResults.getResults();
+            
+            for (ParsedObject parsedObject : (List<ParsedObject>) 
PaginationUtils.subList(searchResultList, beginIndex, maxResults))
             {
-                pdBeanList.add(new PortletDefinitionBean(pd));
+                String uniqueName = 
SearchEngineUtils.getPortletUniqueName(parsedObject);
+                
+                if (StringUtils.isBlank(uniqueName))
+                {
+                    continue;
+                }
+                
+                PortletDefinition pd = 
portletRegistry.getPortletDefinitionByUniqueName(uniqueName);
+                
+                if (pd != null)
+                {
+                    pdBeanList.add(new PortletDefinitionBean(pd));
+                }
             }
         }
         else
         {
-            PortletApplication pa = 
portletRegistry.getPortletApplication(applicationName, true);
-            
-            if (pa != null)
+            if (StringUtils.isBlank(applicationName) && 
StringUtils.isBlank(definitionName))
             {
-                if (StringUtils.isBlank(definitionName))
+                for (PortletDefinition pd : (Collection<PortletDefinition>) 
PaginationUtils.subCollection(portletRegistry.getAllPortletDefinitions(), 
beginIndex, maxResults))
                 {
-                    if (pa != null)
+                    pdBeanList.add(new PortletDefinitionBean(pd));
+                }
+            }
+            else
+            {
+                PortletApplication pa = 
portletRegistry.getPortletApplication(applicationName, true);
+                
+                if (pa != null)
+                {
+                    if (StringUtils.isBlank(definitionName))
                     {
-                        for (PortletDefinition pd : pa.getPortlets())
+                        if (pa != null)
                         {
-                            pdBeanList.add(new PortletDefinitionBean(pd));
+                            for (PortletDefinition pd : 
(List<PortletDefinition>) PaginationUtils.subList(pa.getPortlets(), beginIndex, 
maxResults))
+                            {
+                                pdBeanList.add(new PortletDefinitionBean(pd));
+                            }
                         }
                     }
-                }
-                else
-                {
-                    PortletDefinition pd = pa.getPortlet(definitionName);
-                    
-                    if (pd != null)
+                    else
                     {
-                        pdBeanList.add(new PortletDefinitionBean(pd));
+                        PortletDefinition pd = pa.getPortlet(definitionName);
+                        
+                        if (pd != null)
+                        {
+                            pdBeanList.add(new PortletDefinitionBean(pd));
+                        }
                     }
                 }
             }

Added: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java?rev=884933&view=auto
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
 (added)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
 Fri Nov 27 18:09:57 2009
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jetspeed.services.rest.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * PaginationUtils
+ * 
+ * @version $Id$
+ */
+public class PaginationUtils
+{
+    
+    private PaginationUtils()
+    {
+        
+    }
+    
+    public static List<? extends Object> subList(final List<? extends Object> 
list, int beginIndex, int maxResults)
+    {
+        if (beginIndex < 0 || (beginIndex == 0 && maxResults < 0))
+        {
+            return list;
+        }
+        else if (beginIndex >= list.size())
+        {
+            return Collections.emptyList();
+        }
+        else
+        {
+            if (maxResults < 0)
+            {
+                return list.subList(beginIndex, list.size());
+            }
+            else
+            {
+                return list.subList(beginIndex, Math.min(list.size(), 
beginIndex + maxResults));
+            }
+        }
+    }
+    
+    public static Collection<? extends Object> subCollection(final 
Collection<? extends Object> collection, int beginIndex, int maxResults)
+    {
+        if (beginIndex < 0 || (beginIndex == 0 && maxResults < 0))
+        {
+            return collection;
+        }
+        else if (beginIndex >= collection.size())
+        {
+            return Collections.emptyList();
+        }
+        else
+        {
+            List<Object> list = null;
+            
+            if (collection instanceof List)
+            {
+                list = (List<Object>) collection;
+            }
+            else
+            {
+                list = new ArrayList<Object>(collection);
+            }
+            
+            if (maxResults < 0)
+            {
+                return list.subList(beginIndex, list.size());
+            }
+            else
+            {
+                return list.subList(beginIndex, Math.min(list.size(), 
beginIndex + maxResults));
+            }
+        }
+    }
+    
+}

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/PaginationUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java?rev=884933&view=auto
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
 (added)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
 Fri Nov 27 18:09:57 2009
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jetspeed.services.rest.util;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.jetspeed.search.ParsedObject;
+
+/**
+ * SearchEngineUtils
+ * 
+ * @version $Id$
+ */
+public class SearchEngineUtils
+{
+    
+    private SearchEngineUtils()
+    {
+        
+    }
+    
+    public static Object getField(final ParsedObject parsedObject, final 
String fieldName, final Object defaultValue)
+    {
+        Map fields = parsedObject.getFields();
+        
+        if (fields == null)
+        {
+            return null;
+        }
+        
+        Object field = fields.get(fieldName);
+        
+        if (field == null)
+        {
+            return defaultValue;
+        }
+        
+        return field;
+    }
+    
+    public static String getFieldAsString(final ParsedObject parsedObject, 
final String fieldName, final String defaultValue)
+    {
+        Object field = getField(parsedObject, fieldName, defaultValue);
+        
+        if (field instanceof Collection)
+        {
+            return (String) ((Collection) field).iterator().next();
+        }
+        else
+        {
+            return (String) field;
+        }
+    }
+    
+    public static String getPortletUniqueName(final ParsedObject parsedObject)
+    {
+        if (!"portlet".equals(parsedObject.getType()))
+        {
+            return null;
+        }
+        
+        String portletName = getFieldAsString(parsedObject, "ID", null);
+        
+        if (portletName == null)
+        {
+            return null;
+        }
+        
+        String applicationName = getFieldAsString(parsedObject, 
"portlet_application", null);
+        
+        if (applicationName == null)
+        {
+            return null;
+        }
+        
+        return applicationName + "::" + portletName;
+    }
+}

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: 
portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/util/SearchEngineUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to