1 9 package org.jboss.portal.core.portlet.cms; 10 11 import org.apache.commons.httpclient.HttpException; 12 import org.apache.commons.httpclient.HttpURL; 13 import org.apache.webdav.lib.Property; 14 import org.apache.webdav.lib.PropertyName; 15 import org.apache.webdav.lib.ResponseEntity; 16 import org.apache.webdav.lib.WebdavResource; 17 import org.apache.webdav.lib.properties.DateProperty; 18 19 import javax.portlet.PortletConfig; 20 import javax.portlet.PortletException; 21 import javax.portlet.UnavailableException; 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Date ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 import java.util.regex.Pattern ; 29 30 36 public class WebDAVUtil extends WebdavResource 37 { 38 39 42 private WebdavResource wdResource = null; 43 44 54 public void connectInit(String sURI, String sUname, String sPW) throws IOException , HttpException, MalformedURLException 55 { 56 if(this.getResource() == null) 57 { 58 System.out.println("Connecting to WebDAV server"); 59 this.connect(sURI, sUname, sPW); 60 System.out.println("SUCCESSFULLY connected to WebDAV server"); 61 } 62 } 63 64 73 private void connect(String sURI, String sUname, String sPW) 74 throws HttpException, IOException 75 { 76 HttpURL httpUrl = new HttpURL(sURI); 77 78 if(wdResource == null) 79 { 80 httpUrl.setUserinfo(sUname, sPW); 81 wdResource = new WebdavResource(httpUrl); 82 } 83 else 84 { 85 wdResource.close(); 86 wdResource.setHttpURL(httpUrl); 87 } 88 } 89 90 98 public void setCurrentPath(WebdavResource wdResource, String sPath) throws IOException , HttpException 99 { 100 String sCollectionPath = cleanDoubleSlashes(sPath); 101 wdResource.setPath(sCollectionPath); 102 } 103 104 110 public String fetchParentPath(String sPath) 111 { 112 sPath = cleanDoubleSlashes(sPath); 113 if(sPath.lastIndexOf('/') == sPath.indexOf('/', 1)) 114 { 115 return sPath; 116 } 117 118 int nfirstSlashIndex = sPath.lastIndexOf('/'); 119 String sNewpath = sPath.substring(0, nfirstSlashIndex); 120 int nLastSlashIndex = sNewpath.lastIndexOf('/'); 121 String sParentPath = sNewpath.substring(0, nLastSlashIndex + 1); 122 123 return sParentPath; 124 } 125 126 public String killTrailingSlash(String sPath) 127 { 128 sPath = cleanDoubleSlashes(sPath); 129 if(sPath.lastIndexOf('/') == sPath.indexOf('/', 1)) 130 { 131 return sPath; 132 } 133 134 if(sPath.endsWith("/")) 135 { 136 sPath = sPath.substring(0, sPath.length() - 1); 137 } 138 return sPath; 139 } 140 141 147 public String cleanDoubleSlashes(String sPath) 148 { 149 if(!sPath.startsWith("/")) 150 { 151 sPath = wdResource.getPath() + "/" + sPath; 152 } 153 sPath = sPath.replaceAll("//", "/"); 154 return sPath; 155 } 156 157 private static final String regex = 158 "((?:href|src)\\s*=\\s*) # Capture preliminaries in $1. \n" + 159 "(?: # First look for URL in quotes. \n" + 160 " ([\"\']) # Capture open quote in $2. \n" + 161 " (?!http:) # If it isn't absolute... \n" + 162 " /?(.+?) # ...capture URL in $3 \n" + 163 " \\2 # Match the closing quote \n" + 164 " | # Look for non-quoted URL. \n" + 165 " (?![\"\']|http:) # If it isn't absolute... \n" + 166 " /?([^\\s>]+) # ...capture URL in $4 \n" + 167 ")"; 168 public static final Pattern RELATIVE_URI_PATTERN = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS); 169 170 179 public void connectInit(WebdavResource wdResource, CMSPortlet cmsPortlet) throws PortletException, IOException , UnavailableException 180 { 181 try 182 { 183 if(this.getResource() == null) 184 { 185 System.out.println("Connecting to WebDAV server"); 187 PortletConfig pConfig = cmsPortlet.getPortletConfig(); 188 String sURI = pConfig.getInitParameter("URL"); 189 String sUname = pConfig.getInitParameter("username"); 190 String sPW = pConfig.getInitParameter("password"); 191 this.connect(sURI, sUname, sPW); 192 System.out.println("SUCCESSFULLY connected to WebDAV server"); 193 } 194 } 195 catch(MalformedURLException me) 196 { 197 System.out.println(me.getMessage()); 198 me.printStackTrace(); 199 throw new UnavailableException(me.getMessage()); 200 } 201 catch(HttpException hte) 202 { 203 System.out.println(hte.getMessage()); 204 hte.printStackTrace(); 205 throw new UnavailableException(hte.getMessage()); 206 } 207 catch(IOException ioe) 208 { 209 System.out.println(ioe.getMessage()); 210 ioe.printStackTrace(); 211 throw new UnavailableException(ioe.getMessage()); 212 } 213 } 214 215 218 public void createDefaultData(String sSlideRoot) 219 { 220 try 221 { 222 wdResource.mkcolMethod("/slide" + sSlideRoot + "/images"); 224 wdResource.mkcolMethod("/slide" + sSlideRoot + "/support"); 225 wdResource.mkcolMethod("/slide" + sSlideRoot + "/errorpages"); 226 227 URL urlIndex = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/index.html"); 229 wdResource.putMethod("/slide" + sSlideRoot + "/index.html", urlIndex); URL urlSupport = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/support/index.html"); 231 wdResource.putMethod("/slide" + sSlideRoot + "/support/index.html", urlSupport); URL urlError404 = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/errorpages/404.html"); 233 wdResource.putMethod("/slide" + sSlideRoot + "/errorpages/404.html", urlError404); 235 URL urlLogo = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/jbportal_logo.gif"); 237 wdResource.putMethod("/slide" + sSlideRoot + "/images/jbportal_logo.gif", urlLogo); URL urlBookmark = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/bookmark.gif"); 239 wdResource.putMethod("/slide" + sSlideRoot + "/images/bookmark.gif", urlBookmark); URL urlBack = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/back.gif"); 241 wdResource.putMethod("/slide" + sSlideRoot + "/images/back.gif", urlBack); } 243 catch(HttpException httpe) 244 { 245 httpe.printStackTrace(); 246 } 247 catch(IOException ioe) 248 { 249 ioe.printStackTrace(); 250 } 251 } 252 253 258 public WebdavResource getResource() 259 { 260 return wdResource; 261 } 262 263 268 public String getCreateDate(WebdavResource wdResource) 269 { 270 try 271 { 272 Vector properties = new Vector (); 273 properties.add(new PropertyName("DAV:", "creationdate")); 274 275 Enumeration e = wdResource.propfindMethod(0, properties); 276 if(e.hasMoreElements()) 277 { 278 ResponseEntity response = (ResponseEntity) e.nextElement(); 279 Enumeration props = response.getProperties(); 280 if(props.hasMoreElements()) 281 { 282 Property property = (Property) props.nextElement(); 283 Date dateDate = ((DateProperty) property).getDate(); 284 return dateDate.toString(); 285 } 286 287 } 288 } 289 catch(Exception e) 290 { 291 e.printStackTrace(); 292 } 293 return ""; 294 } 295 296 302 public String getSecuredRoot(String sPath, String sRootPath) 303 { 304 if(sPath == null) 305 { 306 return ""; 307 } 308 309 String [] Args = sPath.split(sRootPath + "/"); 310 if(Args.length > 1) 311 { 312 int nfirstSlashIndex = Args[1].indexOf("/"); 313 if(nfirstSlashIndex == -1) 314 { 315 return Args[1]; 316 } 317 else 318 { 319 return Args[1].substring(0, nfirstSlashIndex); 320 } 321 } 322 else 323 { 324 return ""; 325 } 326 } 327 } | Popular Tags |