KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > portlet > cms > WebDAVUtil


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;
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 JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 /**
31  * Helper class for dealing with the webdav server. This class handles file path, connection, and all issues related to
32  * the webdav resource obj.
33  *
34  * @author Roy Russo : roy at jboss dot org Date: Nov 29, 2004 Time: 3:41:27 PM
35  */

36 public class WebDAVUtil extends WebdavResource
37 {
38
39    /**
40     * local webdav resource *
41     */

42    private WebdavResource wdResource = null;
43
44    /**
45     * Connects to webdav server from the coreservlet on binary content-type calls.
46     *
47     * @param sURI
48     * @param sUname
49     * @param sPW
50     * @throws IOException
51     * @throws HttpException
52     * @throws MalformedURLException
53     */

54    public void connectInit(String JavaDoc sURI, String JavaDoc sUname, String JavaDoc sPW) throws IOException JavaDoc, HttpException, MalformedURLException JavaDoc
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    /**
65     * Open a connection to the webdav server.
66     *
67     * @param sURI
68     * @param sUname
69     * @param sPW
70     * @throws HttpException
71     * @throws IOException
72     */

73    private void connect(String JavaDoc sURI, String JavaDoc sUname, String JavaDoc sPW)
74          throws HttpException, IOException JavaDoc
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    /**
91     * Sets the path to the collection in the webdavresource obj.
92     *
93     * @param wdResource
94     * @param sPath
95     * @throws IOException
96     * @throws HttpException
97     */

98    public void setCurrentPath(WebdavResource wdResource, String JavaDoc sPath) throws IOException JavaDoc, HttpException
99    {
100       String JavaDoc sCollectionPath = cleanDoubleSlashes(sPath);
101       wdResource.setPath(sCollectionPath);
102    }
103
104    /**
105     * Helper method, returns the path to the parent from the given path.
106     *
107     * @param sPath
108     * @return
109     */

110    public String JavaDoc fetchParentPath(String JavaDoc 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 JavaDoc sNewpath = sPath.substring(0, nfirstSlashIndex);
120       int nLastSlashIndex = sNewpath.lastIndexOf('/');
121       String JavaDoc sParentPath = sNewpath.substring(0, nLastSlashIndex + 1);
122
123       return sParentPath;
124    }
125
126    public String JavaDoc killTrailingSlash(String JavaDoc 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    /**
142     * Helper method, cleans up double slashes if they exist.
143     *
144     * @param sPath
145     * @return
146     */

147    public String JavaDoc cleanDoubleSlashes(String JavaDoc 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 JavaDoc 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 JavaDoc RELATIVE_URI_PATTERN = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
169
170    /**
171     * Instantiates a connection to the webdav server if one does not already exist.
172     *
173     * @param cmsPortlet
174     * @throws PortletException
175     * @throws IOException
176     * @throws UnavailableException
177     * @deprecated Init() in cmsportlet has made this obsolete.
178     */

179    public void connectInit(WebdavResource wdResource, CMSPortlet cmsPortlet) throws PortletException, IOException JavaDoc, UnavailableException
180    {
181       try
182       {
183          if(this.getResource() == null)
184          {
185             //this.connectInit(cmsPortlet);
186
System.out.println("Connecting to WebDAV server");
187             PortletConfig pConfig = cmsPortlet.getPortletConfig();
188             String JavaDoc sURI = pConfig.getInitParameter("URL");
189             String JavaDoc sUname = pConfig.getInitParameter("username");
190             String JavaDoc sPW = pConfig.getInitParameter("password");
191             this.connect(sURI, sUname, sPW);
192             System.out.println("SUCCESSFULLY connected to WebDAV server");
193          }
194       }
195       catch(MalformedURLException JavaDoc 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 JavaDoc ioe)
208       {
209          System.out.println(ioe.getMessage());
210          ioe.printStackTrace();
211          throw new UnavailableException(ioe.getMessage());
212       }
213    }
214
215    /**
216     * Creates default data (homepage, support page, and images dir) if doesnt exist.
217     */

218    public void createDefaultData(String JavaDoc sSlideRoot)
219    {
220       try
221       {
222          // make colls
223
wdResource.mkcolMethod("/slide" + sSlideRoot + "/images");
224          wdResource.mkcolMethod("/slide" + sSlideRoot + "/support");
225          wdResource.mkcolMethod("/slide" + sSlideRoot + "/errorpages");
226
227          // move index pages.
228
URL urlIndex = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/index.html");
229          wdResource.putMethod("/slide" + sSlideRoot + "/index.html", urlIndex); // home page
230
URL urlSupport = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/support/index.html");
231          wdResource.putMethod("/slide" + sSlideRoot + "/support/index.html", urlSupport); // support page
232
URL urlError404 = getClass().getResource(CMSConstants.DEFAULT_PAGES_PATH + "/errorpages/404.html");
233          wdResource.putMethod("/slide" + sSlideRoot + "/errorpages/404.html", urlError404); // support page
234

235          // insert images
236
URL urlLogo = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/jbportal_logo.gif");
237          wdResource.putMethod("/slide" + sSlideRoot + "/images/jbportal_logo.gif", urlLogo); // logo
238
URL urlBookmark = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/bookmark.gif");
239          wdResource.putMethod("/slide" + sSlideRoot + "/images/bookmark.gif", urlBookmark); // star
240
URL urlBack = getClass().getResource(CMSConstants.DEFAULT_IMAGES_PATH + "/back.gif");
241          wdResource.putMethod("/slide" + sSlideRoot + "/images/back.gif", urlBack); // back
242
}
243       catch(HttpException httpe)
244       {
245          httpe.printStackTrace();
246       }
247       catch(IOException JavaDoc ioe)
248       {
249          ioe.printStackTrace();
250       }
251    }
252
253    /**
254     * Returns the initialized webdav resource.
255     *
256     * @return
257     */

258    public WebdavResource getResource()
259    {
260       return wdResource;
261    }
262
263    /**
264     * Gets creation date, as the slide getCreationDate() method is broken.
265     *
266     * @return
267     */

268    public String JavaDoc getCreateDate(WebdavResource wdResource)
269    {
270       try
271       {
272          Vector JavaDoc properties = new Vector JavaDoc();
273          properties.add(new PropertyName("DAV:", "creationdate"));
274
275          Enumeration JavaDoc e = wdResource.propfindMethod(0, properties);
276          if(e.hasMoreElements())
277          {
278             ResponseEntity response = (ResponseEntity) e.nextElement();
279             Enumeration JavaDoc props = response.getProperties();
280             if(props.hasMoreElements())
281             {
282                Property property = (Property) props.nextElement();
283                Date JavaDoc dateDate = ((DateProperty) property).getDate();
284                return dateDate.toString();
285             }
286
287          }
288       }
289       catch(Exception JavaDoc e)
290       {
291          e.printStackTrace();
292       }
293       return "";
294    }
295
296    /**
297     * Used in permissions function to obtain the root directory an admin *may* be allowed to edit in.
298     *
299     * @param sPath
300     * @return
301     */

302    public String JavaDoc getSecuredRoot(String JavaDoc sPath, String JavaDoc sRootPath)
303    {
304       if(sPath == null)
305       {
306          return "";
307       }
308
309       String JavaDoc[] 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