KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > portlet > cms > admin > security > AdminCMSModelContentProvider


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.portlet.cms.admin.security;
10
11 import org.apache.commons.httpclient.HttpURL;
12 import org.apache.webdav.lib.WebdavResource;
13 import org.jboss.portal.common.util.Tools;
14 import org.jboss.portal.core.plugins.security.ModelContentProvider;
15 import org.jboss.portal.core.portlet.cms.WebDAVUtil;
16 import org.jboss.portal.core.security.Item;
17 import org.jboss.portal.core.security.jaas.IdentityPropagationLoginModule;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import java.util.*;
21
22 /**
23  * @author Roy Russo : roy at jboss dot org
24  */

25
26 public class AdminCMSModelContentProvider implements ModelContentProvider
27 {
28    private Map map;
29    private Item rootItem;
30    private ResourceBundle bundle;
31    private String JavaDoc wdURL;
32    private String JavaDoc sRootPath;
33
34    public void init(ServletContext JavaDoc servletContext)
35    {
36       rootItem = new RootItemImpl();
37       wdURL = servletContext.getInitParameter("URL");
38       sRootPath = "/webdav" + servletContext.getInitParameter("rootdir");
39    }
40
41    public Item getItem(String JavaDoc[] path)
42    {
43       updateMap();
44       if((path == null) || (path.length == 0))
45       {
46          return rootItem;
47       }
48       else if(path.length == 1)
49       {
50          MapItem mapItem = (MapItem) map.get(path[0]);
51          return mapItem.directoryItem;
52       }
53       else if(path.length == 2)
54       {
55          MapItem mapItem = (MapItem) map.get(path[0]);
56          return (Item) mapItem.directoryMap.get(path[1]);
57       }
58       else
59       {
60          return null;
61       }
62    }
63
64    public Iterator getChildren(String JavaDoc[] path)
65    {
66       updateMap();
67       if((path == null) || (path.length == 0))
68       {
69          List list = new ArrayList();
70          Iterator it = map.values().iterator();
71          while(it.hasNext())
72          {
73             Item item = ((MapItem) it.next()).getDirectoryItem();
74             list.add(item);
75          }
76          return list.iterator();
77       }
78       else if(path.length == 1)
79       {
80          MapItem mapItem = (MapItem) map.get(path[0]);
81          if(mapItem != null)
82          {
83             return ((Map) mapItem.getDirectoryMap()).values().iterator();
84          }
85          else
86          {
87             return null;
88          }
89       }
90       else
91       {
92          return null;
93       }
94    }
95
96    public void destroy()
97    {
98       // TODO Auto-generated method stub
99
}
100
101    private Map directoryMap = null;
102
103    private void updateMap()
104    {
105       map = new HashMap();
106       directoryMap = new HashMap();
107
108       perform(new Job()
109       {
110
111          public void perform(WebdavResource wdResource)
112          {
113             try
114             {
115                if(!wdResource.isCollection())
116                {
117                   wdUtil.setCurrentPath(wdResource, sRootPath);
118                }
119                Vector collList = wdResource.listBasic();
120                for(int i = 0; i < collList.size(); i++)
121                {
122                   String JavaDoc[] sAItem = (String JavaDoc[]) collList.elementAt(i);
123                   DirectoryItemImpl directoryItemImpl = new DirectoryItemImpl(bundle, sAItem[0]);
124                   map.put(sAItem[0], new MapItem(directoryItemImpl, directoryMap));
125                }
126             }
127             catch(Exception JavaDoc e)
128             {
129                e.printStackTrace();
130             }
131          }
132       });
133    }
134
135    private WebDAVUtil wdUtil = new WebDAVUtil();
136
137
138    private interface Job
139    {
140       void perform(WebdavResource wdResource);
141    }
142
143    /**
144     * Wraps all admin actions with security and encapsulates webdavresource for concurrent operations.
145     *
146     * @param job
147     */

148    private void perform(final Job job)
149    {
150       String JavaDoc userName = "admin";
151       IdentityPropagationLoginModule.propagate(userName, new IdentityPropagationLoginModule.Runnable()
152       {
153          public void run(String JavaDoc userName, String JavaDoc password)
154          {
155             WebdavResource wdResource = null;
156             try
157             {
158                HttpURL rootUrl = new HttpURL(wdURL);
159                rootUrl.setUserinfo(userName, password);
160                wdResource = new WebdavResource(rootUrl, WebdavResource.NOACTION, 0);
161                job.perform(wdResource);
162             }
163             catch(Exception JavaDoc e)
164             {
165                e.printStackTrace();
166             }
167             finally
168             {
169                Tools.safeClose(wdResource);
170             }
171          }
172       });
173    }
174
175    public class MapItem
176    {
177
178       private Item directoryItem;
179       private Map directoryMap;
180
181       MapItem(Item directoryItem, Map directoryMap)
182       {
183          this.directoryItem = directoryItem;
184          this.directoryMap = directoryMap;
185       }
186
187       /**
188        * @return Returns the categoryItem.
189        */

190       public Item getDirectoryItem()
191       {
192          return directoryItem;
193       }
194
195       /**
196        * @return Returns the forumsMap.
197        */

198       public Map getDirectoryMap()
199       {
200          return directoryMap;
201       }
202    }
203
204 }
205
Popular Tags