View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout.impl;
18  
19  import java.util.Map;
20  import java.util.StringTokenizer;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.jetspeed.JetspeedActions;
25  import org.apache.jetspeed.ajax.AjaxAction;
26  import org.apache.jetspeed.ajax.AjaxBuilder;
27  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
28  import org.apache.jetspeed.om.folder.Folder;
29  import org.apache.jetspeed.page.PageManager;
30  import org.apache.jetspeed.request.RequestContext;
31  
32  /***
33   * Get the immediate contents of a folder in JSON format 
34   *
35   * AJAX Parameters: 
36   *    folder: full path to the folder 
37   *    
38   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39   * @version $Id: $
40   */
41  public class GetFolderListAction 
42      extends BaseGetResourceAction 
43      implements AjaxAction, AjaxBuilder, Constants
44  {
45      protected Log log = LogFactory.getLog(GetThemesAction.class);
46      
47      public GetFolderListAction(String template, 
48              String errorTemplate,
49              PageManager pageManager,
50              PortletActionSecurityBehavior securityBehavior)
51      {
52          super(template, errorTemplate, pageManager, securityBehavior);        
53      }
54  
55      public boolean run(RequestContext requestContext, Map resultMap)
56      {
57          boolean success = true;
58          String status = "success";
59          try
60          {
61              resultMap.put(ACTION, "getfolderlist");
62              if (false == checkAccess(requestContext, JetspeedActions.VIEW))
63              {
64                      success = false;
65                      resultMap.put(REASON, "Insufficient access to get folderlist");
66                      return success;
67              }                     
68              String data = getActionParameter(requestContext, "data");
69              StringTokenizer tokenizer = new StringTokenizer(data, "[{:\"");
70              String folderName = null;            
71              while (tokenizer.hasMoreTokens())
72              {
73                  String token = tokenizer.nextToken();
74                  if (token.equals("widgetId"))
75                  {
76                      folderName = tokenizer.nextToken();
77                      break;
78                  }
79              }
80              String format = getActionParameter(requestContext, FORMAT);
81              if (format == null)
82                  format = "json";
83              if (folderName == null)
84              {
85                  success = false;
86                  resultMap.put(REASON, "Folder name not found.");
87                  return success;                
88              }
89              resultMap.put(FORMAT, format);
90              Folder folder = pageManager.getFolder(folderName);
91              resultMap.put(FOLDER, folder);
92              resultMap.put("folders", folder.getFolders().iterator());
93              resultMap.put("pages", folder.getPages().iterator());
94              resultMap.put("links", folder.getLinks().iterator());
95              resultMap.put(STATUS, status);            
96          } 
97          catch (Exception e)
98          {
99              // Log the exception
100             log.error("exception while getting theme info", e);
101             // Return a failure indicator
102             success = false;
103         }
104 
105         return success;
106     }
107     
108     
109 }