1 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 ; 20 import java.util.*; 21 22 25 26 public class AdminCMSModelContentProvider implements ModelContentProvider 27 { 28 private Map map; 29 private Item rootItem; 30 private ResourceBundle bundle; 31 private String wdURL; 32 private String sRootPath; 33 34 public void init(ServletContext servletContext) 35 { 36 rootItem = new RootItemImpl(); 37 wdURL = servletContext.getInitParameter("URL"); 38 sRootPath = "/webdav" + servletContext.getInitParameter("rootdir"); 39 } 40 41 public Item getItem(String [] 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 [] 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 } 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 [] sAItem = (String []) 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 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 148 private void perform(final Job job) 149 { 150 String userName = "admin"; 151 IdentityPropagationLoginModule.propagate(userName, new IdentityPropagationLoginModule.Runnable() 152 { 153 public void run(String userName, String 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 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 190 public Item getDirectoryItem() 191 { 192 return directoryItem; 193 } 194 195 198 public Map getDirectoryMap() 199 { 200 return directoryMap; 201 } 202 } 203 204 } 205 | Popular Tags |