KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > utils > HarmoniseNameResolver


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.dav.server.utils;
21
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25 import java.util.logging.*;
26
27 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
28 import org.openharmonise.commons.xml.namespace.NamespaceType;
29 import org.openharmonise.rm.*;
30 import org.openharmonise.rm.config.*;
31 import org.openharmonise.rm.factory.*;
32 import org.openharmonise.rm.metadata.*;
33 import org.openharmonise.rm.resources.*;
34 import org.openharmonise.rm.resources.audit.*;
35 import org.openharmonise.rm.resources.content.*;
36 import org.openharmonise.rm.resources.metadata.properties.*;
37 import org.openharmonise.rm.resources.metadata.values.*;
38 import org.openharmonise.rm.resources.publishing.*;
39 import org.openharmonise.rm.resources.users.*;
40 import org.openharmonise.rm.resources.workflow.properties.*;
41 import org.openharmonise.rm.resources.workflow.values.*;
42 import org.openharmonise.rm.resources.xml.*;
43 import org.openharmonise.rm.security.authorization.*;
44 import org.openharmonise.rm.workflow.WorkflowProfile;
45
46 import com.ibm.webdav.*;
47
48 /**
49  * This class deals with the resolving of resource names and paths from DAV to
50  * Harmonise and vice versa.
51  *
52  * @author Michael Bell
53  * @version $Revision: 1.6 $
54  *
55  */

56 public class HarmoniseNameResolver {
57
58     public static final String JavaDoc PNAME_AUTO_APPROVE_COLLECTIONS =
59         "AUTO_APPROVE_COLLECTIONS";
60
61     // TODO change hard coded root to a configurable mechanism
62
public static final String JavaDoc WEBDAV_ROOT_MARKER = "webdav";
63     public static final String JavaDoc URL_SEPARATOR = "/";
64     public static final String JavaDoc UTF_8 = "UTF-8";
65     private static final String JavaDoc VERSION_SEGMENT = "ver";
66
67     public static final String JavaDoc RESOURCE_TYPE_VALUE = "value";
68     public static final String JavaDoc RESOURCE_TYPE_RESOURCE = "resource";
69     public static final String JavaDoc RESOURCE_TYPE_PROPERTY = "property-resource";
70     public static final String JavaDoc RESOURCE_TYPE_COLLECTION = "collection";
71
72     public static final String JavaDoc CONTENT_NAME = "Content";
73     public static final String JavaDoc WEBSITE_NAME = "Website";
74     public static final String JavaDoc COMPOSITION_NAME = WEBSITE_NAME + "/Composition";
75     public static final String JavaDoc METADATA_NAME = "Metadata";
76     public static final String JavaDoc ARCHIVE_NAME = "Archive";
77     public static final String JavaDoc ASSETS_NAME = CONTENT_NAME + "/Assets";
78     public static final String JavaDoc ASSETS_PATH = "/root/assets";
79     public static final String JavaDoc REPORTS_NAME = "Reports";
80     public static final String JavaDoc REPORTS_PATH = "/root/reports";
81     public static final String JavaDoc DOCS_NAME = CONTENT_NAME + "/Documents";
82     public static final String JavaDoc DOCS_PATH = "/root/documents";
83     public static final String JavaDoc NEWSLETTER_NAME = "Newsletter";
84     public static final String JavaDoc NEWSLETTER_PATH = "/root/newsletter";
85     public static final String JavaDoc PROPERTY_NAME = METADATA_NAME + "/Properties";
86     public static final String JavaDoc PROPERTY_PATH = "/root/props";
87     public static final String JavaDoc VALUE_NAME = METADATA_NAME + "/Values";
88     public static final String JavaDoc VALUE_PATH = "/root/vals";
89     public static final String JavaDoc WEBPAGES_NAME =
90         WEBSITE_NAME + "/Page Definition";
91     public static final String JavaDoc WEBPAGES_PATH = "/root";
92     public static final String JavaDoc O_TEMPLATES_NAME =
93         COMPOSITION_NAME + "/Object Templates";
94     public static final String JavaDoc O_TEMPLATES_PATH = "/root";
95     public static final String JavaDoc P_TEMPLATES_NAME =
96         COMPOSITION_NAME + "/Page Templates";
97     public static final String JavaDoc P_TEMPLATES_PATH = "/root/pages";
98     public static final String JavaDoc INCLUDES_NAME = COMPOSITION_NAME + "/Includes";
99     public static final String JavaDoc INCLUDES_PATH = "/root/includes";
100     public static final String JavaDoc USERS_NAME = "Users";
101     public static final String JavaDoc USERS_PATH = "/root";
102     public static final String JavaDoc DISPLAY_NAME = WEBSITE_NAME + "/Display";
103     public static final String JavaDoc XSLT_NAME = DISPLAY_NAME + "/XSLT";
104     public static final String JavaDoc XSLT_PATH = "/root";
105     public static final String JavaDoc SITE_ASSETS_NAME = WEBSITE_NAME + "/Site Assets";
106     public static final String JavaDoc SITE_ASSETS_PATH = "/root/site assets";
107     public static final String JavaDoc RBS_PROPS_NAME = "RBS_PROPS";
108     public static final String JavaDoc RBS_PROPS_PATH = "/root/rbs";
109     public static final String JavaDoc RBS_VALS_NAME = "RBS_VALS";
110     public static final String JavaDoc RBS_VALS_PATH = "/root/rbs";
111     public static final String JavaDoc SYSTEM_PROPS_NAME = "SYSTEM_PROPS";
112     public static final String JavaDoc SYSTEM_PROPS_PATH = "/root/system";
113     public static final String JavaDoc WORKFLOW_PROPS_NAME = "WORKFLOW_PROPS";
114     public static final String JavaDoc WORKFLOW_PROPS_PATH = "/root/workflow";
115     public static final String JavaDoc WORKFLOW_DEFS_NAME = "WORKFLOW_DEFS";
116     public static final String JavaDoc WORKFLOW_DEFS_PATH = "/root/workflow_defs";
117     public static final String JavaDoc WORKFLOW_STAGES_NAME = "WORKFLOW_STAGES";
118     public static final String JavaDoc WORKFLOW_STAGES_PATH = "/root/workflow_stages";
119
120     public static final String JavaDoc ROOT_NAME = "root";
121     public static final String JavaDoc ROOT_PATH = "/root";
122
123     private static Map URI_2_CLASS_MAPPINGS = new Hashtable();
124     private static Map CONTENT_HARMONISE_PATH_MAPPINGS = new Hashtable();
125     private static Map TYPE_MAPPINGS = new Hashtable();
126
127     private static Map m_liveprop2XML_map = new Hashtable();
128     private static Map m_rootSections_map = new Hashtable();
129     private static Map m_histObjects_map = new Hashtable();
130     private static Map m_rootGroupMappings = new Hashtable();
131
132     private static Map CLASS_2_RESOURCE_TYPE_MAPPING = new Hashtable();
133     private static Document m_histDoc = null;
134     private static Section m_histSec = null;
135     private static Asset m_histAsset = null;
136     private static List ROOT_GROUPS = new Vector();
137     private static Map ROOT_COLLECTION_DESCS = new Hashtable();
138     private static Map CLASS_2_GROUPOBJECT = new Hashtable();
139
140     public static final String JavaDoc START_PROP_NAME = "START";
141     
142     /**
143      * Logger for this class
144      */

145     private static final Logger m_logger = Logger.getLogger(HarmoniseNameResolver.class.getName());
146
147     public static final String JavaDoc VIRTUAL_RESOURCE_NAME = ".resource";
148     public static final String JavaDoc VIRTUAL_COLLECTION_NAME = ".collection";
149
150     static {
151         //content desc
152
DAVCollectionDesc content = new DAVCollectionDesc(CONTENT_NAME, true);
153
154         List content_members = new Vector();
155         content_members.add(DOCS_NAME);
156         content_members.add(ASSETS_NAME);
157         content.addMembers(content_members);
158
159         ROOT_COLLECTION_DESCS.put(CONTENT_NAME, content);
160
161         //website desc
162
DAVCollectionDesc website = new DAVCollectionDesc(WEBSITE_NAME, true);
163
164         List website_members = new Vector();
165         website_members.add(COMPOSITION_NAME);
166         website_members.add(DISPLAY_NAME);
167         website_members.add(SITE_ASSETS_NAME);
168         website_members.add(WEBPAGES_NAME);
169         website.addMembers(website_members);
170
171         ROOT_COLLECTION_DESCS.put(WEBSITE_NAME, website);
172
173         //users desc
174
DAVCollectionDesc users =
175             new DAVCollectionDesc(
176                 USERS_NAME,
177                 USERS_PATH,
178                 UserGroup.class.getName());
179
180         ROOT_COLLECTION_DESCS.put(USERS_NAME, users);
181
182         //newsletter desc
183
DAVCollectionDesc newsletter =
184             new DAVCollectionDesc(
185                 NEWSLETTER_NAME,
186                 NEWSLETTER_PATH,
187                 Section.class.getName());
188         ROOT_COLLECTION_DESCS.put(NEWSLETTER_NAME, newsletter);
189
190         //reports desc
191
DAVCollectionDesc reports =
192             new DAVCollectionDesc(
193                 REPORTS_NAME,
194                 REPORTS_PATH,
195                 XMLAuditResourceGroup.class.getName());
196         ROOT_COLLECTION_DESCS.put(REPORTS_NAME, reports);
197
198         //metadata desc
199
DAVCollectionDesc metadata = new DAVCollectionDesc(METADATA_NAME, true);
200
201         List metamembers = new Vector();
202         metamembers.add(PROPERTY_NAME);
203         metamembers.add(VALUE_NAME);
204         metadata.addMembers(metamembers);
205         ROOT_COLLECTION_DESCS.put(METADATA_NAME, metadata);
206
207         //composition desc
208
DAVCollectionDesc composition =
209             new DAVCollectionDesc(COMPOSITION_NAME, true);
210
211         List comp_members = new Vector();
212
213         comp_members.add(O_TEMPLATES_NAME);
214         comp_members.add(P_TEMPLATES_NAME);
215         comp_members.add(INCLUDES_NAME);
216
217         composition.addMembers(comp_members);
218
219         ROOT_COLLECTION_DESCS.put(COMPOSITION_NAME, composition);
220
221         //docs desc
222
DAVCollectionDesc docs =
223             new DAVCollectionDesc(
224                 DOCS_NAME,
225                 DOCS_PATH,
226                 Section.class.getName());
227
228         ROOT_COLLECTION_DESCS.put(DOCS_NAME, docs);
229
230         //assets desc
231
DAVCollectionDesc assets =
232             new DAVCollectionDesc(
233                 ASSETS_NAME,
234                 ASSETS_PATH,
235                 Section.class.getName());
236
237         ROOT_COLLECTION_DESCS.put(ASSETS_NAME, assets);
238
239         //display desc
240
DAVCollectionDesc display = new DAVCollectionDesc(DISPLAY_NAME, true);
241
242         List disp_members = new Vector();
243
244         disp_members.add(XSLT_NAME);
245
246         display.addMembers(disp_members);
247
248         ROOT_COLLECTION_DESCS.put(DISPLAY_NAME, display);
249
250         //site assets desc
251
DAVCollectionDesc site_assets =
252             new DAVCollectionDesc(
253                 SITE_ASSETS_NAME,
254                 SITE_ASSETS_PATH,
255                 Section.class.getName());
256
257         ROOT_COLLECTION_DESCS.put(SITE_ASSETS_NAME, site_assets);
258
259         //web pages desc
260
DAVCollectionDesc pages =
261             new DAVCollectionDesc(
262                 WEBPAGES_NAME,
263                 WEBPAGES_PATH,
264                 WebPageGroup.class.getName());
265
266         ROOT_COLLECTION_DESCS.put(WEBPAGES_NAME, pages);
267
268         //property desc
269
DAVCollectionDesc prop =
270             new DAVCollectionDesc(
271                 PROPERTY_NAME,
272                 PROPERTY_PATH,
273                 PropertyGroup.class.getName());
274
275         ROOT_COLLECTION_DESCS.put(PROPERTY_NAME, prop);
276
277         //value desc
278
DAVCollectionDesc vals =
279             new DAVCollectionDesc(
280                 VALUE_NAME,
281                 VALUE_PATH,
282                 ValueGroup.class.getName());
283
284         ROOT_COLLECTION_DESCS.put(VALUE_NAME, vals);
285
286         //obj templates desc
287
DAVCollectionDesc obj_temps =
288             new DAVCollectionDesc(
289                 O_TEMPLATES_NAME,
290                 O_TEMPLATES_PATH,
291                 TemplateGroup.class.getName());
292
293         ROOT_COLLECTION_DESCS.put(O_TEMPLATES_NAME, obj_temps);
294
295         //page templates desc
296
DAVCollectionDesc page_temps =
297             new DAVCollectionDesc(
298                 P_TEMPLATES_NAME,
299                 P_TEMPLATES_PATH,
300                 XMLResourceGroup.class.getName());
301
302         ROOT_COLLECTION_DESCS.put(P_TEMPLATES_NAME, page_temps);
303
304         //includes desc
305
DAVCollectionDesc includes =
306             new DAVCollectionDesc(
307                 INCLUDES_NAME,
308                 INCLUDES_PATH,
309                 XMLResourceGroup.class.getName());
310
311         ROOT_COLLECTION_DESCS.put(INCLUDES_NAME, includes);
312
313         //xslt desc
314
DAVCollectionDesc xslts =
315             new DAVCollectionDesc(
316                 XSLT_NAME,
317                 XSLT_PATH,
318                 XSLResourceGroup.class.getName());
319
320         ROOT_COLLECTION_DESCS.put(XSLT_NAME, xslts);
321
322         //rbs props
323
DAVCollectionDesc rbs_props =
324             new DAVCollectionDesc(
325                 RBS_PROPS_NAME,
326                 RBS_PROPS_PATH,
327                 PropertyGroup.class.getName());
328         ROOT_COLLECTION_DESCS.put(RBS_PROPS_NAME, rbs_props);
329
330         //rbs_vals
331
DAVCollectionDesc rbs_vals =
332             new DAVCollectionDesc(
333                 RBS_VALS_NAME,
334                 RBS_VALS_PATH,
335                 ValueGroup.class.getName());
336         ROOT_COLLECTION_DESCS.put(RBS_VALS_NAME, rbs_vals);
337
338         //system props
339
DAVCollectionDesc sys_props =
340             new DAVCollectionDesc(
341                 SYSTEM_PROPS_NAME,
342                 SYSTEM_PROPS_PATH,
343                 PropertyGroup.class.getName());
344         ROOT_COLLECTION_DESCS.put(SYSTEM_PROPS_NAME, sys_props);
345
346         //workflow props
347
DAVCollectionDesc wrkflw_props =
348             new DAVCollectionDesc(
349                 WORKFLOW_PROPS_NAME,
350                 WORKFLOW_PROPS_PATH,
351                 WorkflowPropertyGroup.class.getName());
352         ROOT_COLLECTION_DESCS.put(WORKFLOW_PROPS_NAME, wrkflw_props);
353
354         //workflow defs
355
DAVCollectionDesc wrkflw_defs =
356             new DAVCollectionDesc(
357                 WORKFLOW_DEFS_NAME,
358                 WORKFLOW_DEFS_PATH,
359                 WorkflowStageValueGroup.class.getName());
360         ROOT_COLLECTION_DESCS.put(WORKFLOW_DEFS_NAME, wrkflw_defs);
361
362         //workflow stages
363
DAVCollectionDesc wrkflw_stages =
364             new DAVCollectionDesc(
365                 WORKFLOW_STAGES_NAME,
366                 WORKFLOW_STAGES_PATH,
367                 WorkflowStageValueGroup.class.getName());
368         ROOT_COLLECTION_DESCS.put(WORKFLOW_STAGES_NAME, wrkflw_stages);
369
370         TYPE_MAPPINGS.put("Document", Document.class.getName());
371         TYPE_MAPPINGS.put("Asset", Asset.class.getName());
372         TYPE_MAPPINGS.put("Section", Section.class.getName());
373         TYPE_MAPPINGS.put("User", User.class.getName());
374         TYPE_MAPPINGS.put("UserGroup", UserGroup.class.getName());
375         TYPE_MAPPINGS.put("Value", Value.class.getName());
376         TYPE_MAPPINGS.put("ValueGroup", ValueGroup.class.getName());
377         TYPE_MAPPINGS.put("Property", Property.class.getName());
378         TYPE_MAPPINGS.put("PropertyGroup", PropertyGroup.class.getName());
379         TYPE_MAPPINGS.put("Template", Template.class.getName());
380         TYPE_MAPPINGS.put("TemplateGroup", TemplateGroup.class.getName());
381         TYPE_MAPPINGS.put("WebPage", WebPage.class.getName());
382         TYPE_MAPPINGS.put("WebPageGroup", WebPageGroup.class.getName());
383         TYPE_MAPPINGS.put("XSLResourceGroup", XSLResourceGroup.class.getName());
384         TYPE_MAPPINGS.put("XSLResource", XSLResource.class.getName());
385         TYPE_MAPPINGS.put("XMLResourceGroup", XMLResourceGroup.class.getName());
386         TYPE_MAPPINGS.put("XMLResource", XMLResource.class.getName());
387         TYPE_MAPPINGS.put("XMLAuditResourceGroup", XMLAuditResourceGroup.class.getName());
388         TYPE_MAPPINGS.put("XMLAuditResource", XMLAuditResource.class.getName());
389         TYPE_MAPPINGS.put("WorkflowStageValueGroup", WorkflowStageValueGroup.class.getName());
390         TYPE_MAPPINGS.put("WorkflowStageValue", WorkflowStageValue.class.getName());
391         TYPE_MAPPINGS.put("WorkflowProperty", WorkflowProperty.class.getName());
392         TYPE_MAPPINGS.put("WorkflowPropertyGroup", WorkflowProperty.class.getName());
393
394         ROOT_GROUPS.add(CONTENT_NAME);
395         ROOT_GROUPS.add(WEBSITE_NAME);
396         ROOT_GROUPS.add(USERS_NAME);
397         ROOT_GROUPS.add(NEWSLETTER_NAME);
398         ROOT_GROUPS.add(REPORTS_NAME);
399         //ROOT_GROUPS.add(ARCHIVE_NAME);
400
ROOT_GROUPS.add(METADATA_NAME);
401         ROOT_GROUPS.add(WORKFLOW_DEFS_NAME);
402         ROOT_GROUPS.add(WORKFLOW_STAGES_NAME);
403         ROOT_GROUPS.add(WORKFLOW_PROPS_NAME);
404
405         CLASS_2_RESOURCE_TYPE_MAPPING.put(
406             Document.class.getName(),
407             RESOURCE_TYPE_RESOURCE);
408         CLASS_2_RESOURCE_TYPE_MAPPING.put(
409             Value.class.getName(),
410             RESOURCE_TYPE_VALUE);
411         CLASS_2_RESOURCE_TYPE_MAPPING.put(
412             Property.class.getName(),
413             RESOURCE_TYPE_RESOURCE);
414         CLASS_2_RESOURCE_TYPE_MAPPING.put(
415             Asset.class.getName(),
416             RESOURCE_TYPE_RESOURCE);
417         CLASS_2_RESOURCE_TYPE_MAPPING.put(
418             User.class.getName(),
419             RESOURCE_TYPE_RESOURCE);
420         CLASS_2_RESOURCE_TYPE_MAPPING.put(
421             Template.class.getName(),
422             RESOURCE_TYPE_RESOURCE);
423         CLASS_2_RESOURCE_TYPE_MAPPING.put(
424             WebPage.class.getName(),
425             RESOURCE_TYPE_RESOURCE);
426     }
427
428     public HarmoniseNameResolver() {
429     }
430
431     /** Translate a URL into a Harmonise path name.
432     *
433     * @param url the URL of the resource
434     * @return the translated pathname
435     */

436     public static String JavaDoc getRealPath(URL url) throws NameResolverException {
437
438         String JavaDoc sPath = null;
439         try {
440             sPath = URLDecoder.decode(url.getFile(), UTF_8);
441         } catch (UnsupportedEncodingException e) {
442             throw new NameResolverException(e);
443         }
444
445         return getRealPath(sPath);
446     }
447     
448     /**
449      * Returns the name of the resource referenced by the given
450      * <code>URL</code>. Effectively returns the last segment of the
451      * given URI.
452      *
453      * @param url the reosurce URL
454      * @return the name of the resource
455      * @throws UnsupportedEncodingException if an error occurs decoding
456      * the URI
457      */

458     public static String JavaDoc getResourceName(URL url) throws UnsupportedEncodingException {
459         String JavaDoc sResourceName = null;
460         
461         String JavaDoc sResourcePath =
462                             URLDecoder.decode(
463                                 url.getPath(),
464                                 HarmoniseNameResolver.UTF_8);
465
466         sResourceName = HarmoniseNameResolver.getLastSegment(sResourcePath);
467         
468         return sResourceName;
469     }
470
471     /** Translate a URL into a Harmonise path name.
472     *
473     * @param url the URL of the resource
474     * @return the translated pathname
475     */

476     public static String JavaDoc getRealPath(String JavaDoc sPath)
477         throws NameResolverException {
478
479         if (m_logger.isLoggable(Level.FINE)) {
480             m_logger.log(Level.FINE,"getRealPath: url file - " + sPath);
481         }
482
483         String JavaDoc pathName = null;
484         int nIndex = sPath.indexOf(WEBDAV_ROOT_MARKER);
485
486         if (nIndex >= 0) {
487             nIndex = sPath.indexOf('/', nIndex + 1);
488
489             if (nIndex >= 0) {
490                 pathName = sPath.substring(nIndex + 1);
491             } else {
492                 throw new NameResolverException("Invalid URI given - " + sPath);
493             }
494         } else {
495             throw new NameResolverException("Invalid URI given - " + sPath);
496         }
497
498         Iterator iter = ROOT_COLLECTION_DESCS.values().iterator();
499         boolean bFound = false;
500
501         while (iter.hasNext() == true && bFound == false) {
502             DAVCollectionDesc desc = (DAVCollectionDesc) iter.next();
503             if (desc.isVirtual() == false) {
504
505                 String JavaDoc sColURI = desc.getURI();
506
507                 if (pathName.startsWith(sColURI) == true) {
508                     bFound = true;
509                     if (pathName.equals(sColURI) == true) {
510                         pathName = desc.getHarmonisePath();
511                     } else {
512
513                         String JavaDoc sAdd = pathName.substring(sColURI.length() + 1);
514
515                         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
516
517                         strbuf.append(desc.getHarmonisePath());
518
519                         if (sAdd != null && sAdd.length() > 0) {
520                             strbuf.append(URL_SEPARATOR).append(sAdd);
521                         }
522
523                         pathName = strbuf.toString();
524
525                     }
526
527                 }
528             }
529         }
530
531         if (m_logger.isLoggable(Level.FINE)) {
532             m_logger.log(Level.FINE, "Returning path - " + pathName);
533         }
534
535         return pathName;
536     }
537
538     /**
539      * Returns the URI that the given object can be accessed by over WebDAV.
540      *
541      * @param cobj
542      * @return
543      * @throws Exception
544      */

545     public static String JavaDoc getDAVPath(AbstractChildObject cobj)
546         throws NameResolverException {
547         String JavaDoc sPath = null;
548
549         if(cobj != null) {
550             try {
551                 sPath = cobj.getFullPath();
552             } catch (DataAccessException e) {
553
554                 try {
555                     throw new NameResolverException(
556                         "Problem with " + cobj.getName(),
557                         e);
558                 } catch (DataAccessException e1) {
559                     throw new NameResolverException(e);
560                 }
561             }
562             
563             sPath = getDAVPath(cobj.getClass(), sPath);
564         }
565         
566
567         return sPath;
568     }
569     
570     /**
571      * Returns the DAV path equivalent of a Harmonise path.
572      *
573      * Note: it's more efficent to use the <code>getDAVPath</code>
574      * which takes both the class and the Harmonise path.
575      *
576      * @param dsi
577      * @param harmonisePath
578      * @return
579      * @throws NameResolverException
580      */

581     public static String JavaDoc getDAVPath(AbstractDataStoreInterface dsi, String JavaDoc harmonisePath) throws NameResolverException {
582         Iterator iter = ROOT_COLLECTION_DESCS.values().iterator();
583         String JavaDoc sDAVVirtDirName = null;
584         String JavaDoc sDavMapOHRoot = null;
585         boolean bFound = false;
586         
587         while (iter.hasNext() && bFound == false) {
588             DAVCollectionDesc desc = (DAVCollectionDesc) iter.next();
589             
590             if(desc.isVirtual() == false && harmonisePath.startsWith(desc.getHarmonisePath())) {
591                 String JavaDoc sClass = desc.getHarmoniseClass();
592             
593                 try {
594                     AbstractChildObject child = (AbstractChildObject) HarmoniseObjectFactory.instantiateHarmoniseObject(dsi,sClass,harmonisePath);
595                     
596                     if(child != null && child.exists() == true) {
597                         bFound = true;
598                         sDAVVirtDirName = desc.getURI();
599                         sDavMapOHRoot = desc.getHarmonisePath();
600                     } else {
601                         AbstractParentObject parent = (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject(dsi,sClass,getPathParent(harmonisePath));
602                         
603                         if(parent != null && parent.exists()) {
604                           child = parent.getChildByName(getLastSegment(harmonisePath));
605                           
606                           if(child != null) {
607                             bFound = true;
608                             sDAVVirtDirName = desc.getURI();
609                             sDavMapOHRoot = desc.getHarmonisePath();
610                           }
611                         }
612             
613                     }
614           
615                 } catch (HarmoniseFactoryException e) {
616                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
617                 } catch (DataAccessException e) {
618                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
619                 }
620             }
621         }
622     String JavaDoc sResult = null;
623     
624     if(bFound == true) {
625       StringBuffer JavaDoc sDAVpath = new StringBuffer JavaDoc();
626
627       sDAVpath.append(URL_SEPARATOR).append(WEBDAV_ROOT_MARKER).append(
628         URL_SEPARATOR);
629
630       if(sDavMapOHRoot == null || sDAVVirtDirName == null) {
631         throw new NameResolverException("Problem getting DAV path from " + sDavMapOHRoot + " and " + sDAVVirtDirName);
632       }
633       sDAVpath.append(harmonisePath.replaceFirst(sDavMapOHRoot, sDAVVirtDirName));
634
635       if (m_logger.isLoggable(Level.FINE)) {
636         m_logger.log(Level.FINE,
637           "Harmonise path - " + harmonisePath + ", dav path - " + sDAVpath);
638
639       }
640
641       sResult = sDAVpath.toString();
642   
643     }
644         
645         return sResult;
646     }
647
648     /**
649      * Returns the URI that the given object can be accessed by over WebDAV.
650      *
651      * @param clss
652      * @param sHarmonisePath
653      * @return
654      * @throws NameResolverException
655      */

656     public static String JavaDoc getDAVPath(Class JavaDoc clss, String JavaDoc sHarmonisePath) throws NameResolverException {
657
658         if (m_logger.isLoggable(Level.FINE)) {
659             m_logger.log(Level.FINE,
660                 "getDAVPath: class name - "
661                     + clss.getName()
662                     + " path - "
663                     + sHarmonisePath);
664         }
665
666         if (clss.isAssignableFrom(AbstractParentObject.class) == false) {
667             //is a child object class - need to find parent
668

669             String JavaDoc sGroupObjectClassName = (String JavaDoc) CLASS_2_GROUPOBJECT.get(clss.getName());
670             
671             if(sGroupObjectClassName == null) {
672             
673                 try {
674                     AbstractChildObject child =
675                         (AbstractChildObject) clss.newInstance();
676     
677                     sGroupObjectClassName = child.getParentObjectClassName();
678                 
679                     CLASS_2_GROUPOBJECT.put(clss.getName(), sGroupObjectClassName);
680                 } catch (InstantiationException JavaDoc e) {
681                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
682                 } catch (IllegalAccessException JavaDoc e) {
683                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
684                 }
685             }
686             
687             try {
688                 clss = Class.forName(sGroupObjectClassName);
689             } catch (ClassNotFoundException JavaDoc e) {
690                 
691                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
692             }
693         }
694
695         Iterator iter = ROOT_COLLECTION_DESCS.values().iterator();
696         String JavaDoc sDAVVirtDirName = null;
697         String JavaDoc sDavMapOHRoot = null;
698         boolean bFound = false;
699         while (iter.hasNext() && bFound == false) {
700             DAVCollectionDesc desc = (DAVCollectionDesc) iter.next();
701
702             if (clss.getName().equals(desc.getHarmoniseClass())
703                 && sHarmonisePath.startsWith(desc.getHarmonisePath())) {
704                 sDAVVirtDirName = desc.getURI();
705                 sDavMapOHRoot = desc.getHarmonisePath();
706                 bFound = true;
707             }
708         }
709
710         StringBuffer JavaDoc sDAVpath = new StringBuffer JavaDoc();
711
712         sDAVpath.append(URL_SEPARATOR).append(WEBDAV_ROOT_MARKER).append(
713             URL_SEPARATOR);
714             
715         if(sDavMapOHRoot == null || sDAVVirtDirName == null) {
716             throw new NameResolverException("Problem getting DAV path from " + sDavMapOHRoot + " and " + sDAVVirtDirName);
717         }
718         sDAVpath.append(sHarmonisePath.replaceFirst(sDavMapOHRoot, sDAVVirtDirName));
719
720         if (m_logger.isLoggable(Level.FINE)) {
721             m_logger.log(Level.FINE,
722                 "Harmonise path - " + sHarmonisePath + ", dav path - " + sDAVpath);
723
724         }
725
726         return sDAVpath.toString();
727     }
728
729     /**
730      * Returns the version path to a specific version of a resource
731      *
732      * @param child
733      * @return
734      * @throws NameResolverException
735      */

736     public static String JavaDoc getVersionPath(AbstractChildObject child)
737         throws NameResolverException {
738
739         int nId = -1;
740         int nKey = -1;
741         try {
742             AbstractChildObject live =
743                 (AbstractChildObject) child.getLiveVersion();
744             if (live != null) {
745                 nId = live.getId();
746             } else {
747                 nId = child.getId();
748             }
749
750             nKey = child.getKey();
751         } catch (DataAccessException e) {
752             throw new NameResolverException(e);
753         }
754
755         String JavaDoc sClass = child.getClass().getName();
756         String JavaDoc sType = sClass.substring(sClass.lastIndexOf(".") + 1);
757
758         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
759
760         strbuf.append(URL_SEPARATOR).append(WEBDAV_ROOT_MARKER).append(
761             URL_SEPARATOR);
762         strbuf.append(sType).append(URL_SEPARATOR);
763         strbuf
764             .append(String.valueOf(nId))
765             .append(URL_SEPARATOR)
766             .append(VERSION_SEGMENT)
767             .append(URL_SEPARATOR);
768         strbuf.append(String.valueOf(nKey));
769
770         return strbuf.toString();
771     }
772
773     /**
774      * Returns the full URL for the given child object.
775      *
776      * @param cobj
777      * @param resourceUrl
778      * @return
779      */

780     public static String JavaDoc getFullURL(AbstractChildObject cobj, URL resourceUrl)
781         throws NameResolverException {
782         StringBuffer JavaDoc sURL = new StringBuffer JavaDoc();
783
784         sURL.append(resourceUrl.getProtocol());
785         sURL.append("://");
786         sURL.append(resourceUrl.getHost());
787         sURL.append(":");
788         sURL.append(resourceUrl.getPort());
789         sURL.append(getDAVPath(cobj));
790
791         return sURL.toString();
792     }
793
794     /**
795      * Returns a Universal Unique Identifier for the combination of URI and user name.
796      *
797      * @param uri
798      * @param user_name
799      * @return
800      * @throws NameResolverException
801      */

802     public static String JavaDoc getUUID(String JavaDoc uri, String JavaDoc user_name)
803         throws NameResolverException {
804         String JavaDoc uuid_base;
805         try {
806             uuid_base = URLDecoder.decode(uri, UTF_8) + user_name;
807         } catch (UnsupportedEncodingException e) {
808             throw new NameResolverException(e);
809         }
810
811         if (m_logger.isLoggable(Level.FINE)) {
812             m_logger.log(Level.FINE,
813                 "getUUID: uri - " + uri + ", user - " + user_name);
814             m_logger.log(Level.FINE,"getUUID: UUID - " + uuid_base);
815         }
816
817         return String.valueOf(uuid_base.hashCode());
818     }
819
820     public static String JavaDoc getTagForLiveProperty(String JavaDoc livepropname) {
821         return (String JavaDoc) m_liveprop2XML_map.get(livepropname);
822     }
823
824     public static boolean isMappedLiveProperty(String JavaDoc livepropname) {
825         return m_liveprop2XML_map.containsKey(livepropname);
826     }
827
828     public static String JavaDoc getResourceType(String JavaDoc sResourceName) {
829         int nDotIndex = sResourceName.indexOf(".");
830         String JavaDoc sExt = "";
831
832         if (nDotIndex > 0) {
833             sExt = sResourceName.substring(nDotIndex);
834         }
835
836         return sExt;
837     }
838
839     /**
840      * Returns <code>true</code> if the given URL path is a version path of the form
841      * 'http://..../../4/ver/23' where 4 would be the live id of a versioned resource
842      * and 23 would be the key of the specific version.
843      *
844      * @param sPath
845      * @return
846      */

847     public static boolean isVersionPath(String JavaDoc sPath) {
848         boolean bIsVersion = false;
849
850         int nIndex =
851             sPath.lastIndexOf(URL_SEPARATOR + VERSION_SEGMENT + URL_SEPARATOR);
852
853         if (nIndex > 0) {
854             bIsVersion = true;
855             String JavaDoc sLastSeg = getLastSegment(sPath);
856
857             if (m_logger.isLoggable(Level.FINE)) {
858                 m_logger.log(Level.FINE,"Is a number? - " + sLastSeg);
859             }
860
861             try {
862                 int nKey = Integer.parseInt(sLastSeg);
863             } catch (NumberFormatException JavaDoc e) {
864                 bIsVersion = false;
865             }
866         }
867
868         if (m_logger.isLoggable(Level.FINE)) {
869             m_logger.log(Level.FINE,sPath + " is a version path:" + bIsVersion);
870         }
871
872         return bIsVersion;
873     }
874
875     /**
876      * Returns <code>true</code> if URL is a path to an archived version.
877      *
878      * @param sURL
879      * @return
880      */

881     public static boolean isArchiveURL(URL url) {
882         return isArchiveURL(url.getPath());
883     }
884
885     /**
886      * Returns <code>true</code> if URL is a path to an archived version.
887      *
888      * @param sURL
889      * @return
890      */

891     public static boolean isArchiveURL(String JavaDoc sURL) {
892
893         boolean bIsArchive =
894             (sURL
895                 .indexOf(
896                     URL_SEPARATOR
897                         + WEBDAV_ROOT_MARKER
898                         + URL_SEPARATOR
899                         + ARCHIVE_NAME)
900                 >= 0);
901
902         if (m_logger.isLoggable(Level.FINE)) {
903             m_logger.log(Level.FINE,
904                 "Checking for archive - " + sURL + " - " + bIsArchive);
905         }
906
907         return bIsArchive;
908     }
909
910     /**
911      * Returns the path to a file on the local file system associated to the
912      * given URL.
913      *
914      * @param url
915      * @return
916      * @throws Exception
917      */

918     public static String JavaDoc getFilePath(URL url) throws NameResolverException {
919         String JavaDoc sResult = null;
920
921         String JavaDoc sName = url.getFile();
922         sName = sName.substring(sName.lastIndexOf(URL_SEPARATOR) + 1);
923
924         try {
925             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
926
927             sbuf.append(ConfigSettings.getProperty("UPLOADED_TEMPFILE_DIR"));
928
929             if (sbuf.toString().endsWith(File.separator) == false) {
930                 sbuf.append(File.separator);
931             }
932
933             sbuf.append(sName);
934
935             sResult = sbuf.toString();
936         } catch (ConfigException e) {
937             throw new NameResolverException(e);
938         }
939         return sResult;
940     }
941
942     /**
943      * Returns a child object from the given URL.
944      *
945      * @param dsi
946      * @param url
947      * @return
948      * @throws NameResolverException
949      */

950     public static AbstractChildObject getObjectFromURL(
951         AbstractDataStoreInterface dsi,
952         URL url)
953         throws NameResolverException {
954         String JavaDoc sPath = null;
955         try {
956             sPath = URLDecoder.decode(url.getFile(), UTF_8);
957         } catch (UnsupportedEncodingException e) {
958             throw new NameResolverException(e);
959         }
960
961         return getObjectFromURL(dsi, sPath);
962     }
963
964     /**
965      * Returns a child object from the given URL.
966      *
967      * @param dsi
968      * @param sPath
969      * @return
970      * @throws NameResolverException
971      */

972     public static AbstractChildObject getObjectFromURL(
973         AbstractDataStoreInterface dsi,
974         String JavaDoc sPath)
975         throws NameResolverException {
976         AbstractChildObject child = null;
977         try {
978             sPath = URLDecoder.decode(sPath, UTF_8);
979         } catch (UnsupportedEncodingException e) {
980             throw new NameResolverException(e);
981         }
982         if (isVersionPath(sPath) == true) {
983             child = getObjectFromVersionURL(dsi, sPath);
984         } else if (isArchiveURL(sPath) == true) {
985             child = getArchivedObject(dsi, sPath);
986         } else {
987             child = getObjectFromNonVersionURL(dsi, sPath);
988         }
989
990         return child;
991     }
992
993     /**
994      * Returns a child object from the given non-version URL.
995      *
996      * @param dsi
997      * @param sPath
998      * @return
999      * @throws NameResolverException
1000     */

1001    public static AbstractChildObject getObjectFromNonVersionURL(
1002        AbstractDataStoreInterface dsi,
1003        String JavaDoc sPath)
1004        throws NameResolverException {
1005        AbstractChildObject child = null;
1006
1007        if (isVersionPath(sPath) == true) {
1008            throw new InvalidPathException("Invalid path");
1009        }
1010
1011        if (m_logger.isLoggable(Level.FINE)) {
1012            m_logger.log(Level.FINE,"dealing with path - " + sPath);
1013        }
1014
1015        String JavaDoc sParentType = getParentTypeFromURL(sPath);
1016
1017        if (m_logger.isLoggable(Level.FINE)) {
1018            m_logger.log(Level.FINE,"parent type - " + sParentType);
1019        }
1020
1021        if (sParentType != null) {
1022            String JavaDoc ohPath = getRealPath(sPath);
1023
1024            if (m_logger.isLoggable(Level.FINE)) {
1025                m_logger.log(Level.FINE,"real path - " + ohPath);
1026            }
1027
1028            try {
1029                if (ohPath.equals(ROOT_PATH) == true) {
1030                    child =
1031                        (
1032                            AbstractParentObject) HarmoniseObjectFactory
1033                                .instantiatePublishableObject(
1034                            dsi,
1035                            sParentType,
1036                            ohPath);
1037
1038                    if (child == null) {
1039                        throw new NameResolverException("Didn't find root!");
1040                    }
1041                } else {
1042                    String JavaDoc ohParentPath = getPathParent(ohPath);
1043
1044                    AbstractParentObject parent =
1045                        (
1046                            AbstractParentObject) HarmoniseObjectFactory
1047                                .instantiatePublishableObject(
1048                            dsi,
1049                            sParentType,
1050                            ohParentPath);
1051                    
1052                    if(parent != null) {
1053                        child = parent.getChildByName(getLastSegment(ohPath));
1054                    } else {
1055                        m_logger.logp(Level.INFO, HarmoniseNameResolver.class.getName(), "getObjectFromNonVersionURL", sParentType + " object not found for path " + ohParentPath);
1056                    }
1057                }
1058            } catch (HarmoniseFactoryException e) {
1059                throw new NameResolverException(e);
1060            } catch (DataAccessException e) {
1061                throw new NameResolverException(e);
1062            }
1063        }
1064
1065        return child;
1066    }
1067
1068    /**
1069     * Returns a child object from a version URL.
1070     *
1071     * @param dsi
1072     * @param sPath
1073     * @return
1074     * @throws NameResolverException
1075     */

1076    public static AbstractChildObject getObjectFromVersionURL(
1077        AbstractDataStoreInterface dsi,
1078        String JavaDoc sPath)
1079        throws NameResolverException {
1080        AbstractChildObject child = null;
1081
1082        if (isVersionPath(sPath) == false) {
1083            throw new InvalidPathException("Invalid path");
1084        }
1085
1086        int nLastSep = sPath.lastIndexOf(URL_SEPARATOR);
1087
1088        if (sPath.endsWith(URL_SEPARATOR)) {
1089            nLastSep =
1090                sPath.substring(0, nLastSep - 1).lastIndexOf(URL_SEPARATOR);
1091        }
1092
1093        int nKey = Integer.parseInt(getLastSegment(sPath));
1094
1095        int nPostIdSep =
1096            nLastSep - VERSION_SEGMENT.length() - URL_SEPARATOR.length();
1097
1098        int nPreIdSep =
1099            sPath.substring(0, nPostIdSep).lastIndexOf(URL_SEPARATOR);
1100
1101        String JavaDoc sLiveId = sPath.substring(nPreIdSep + 1, nPostIdSep);
1102
1103        if (m_logger.isLoggable(Level.FINE)) {
1104            m_logger.log(Level.FINE, "looking for live id - " + sLiveId);
1105        }
1106
1107        int nLiveId = Integer.parseInt(sLiveId);
1108
1109        String JavaDoc sType = getTypeFromVersionURL(sPath);
1110
1111        if (m_logger.isLoggable(Level.FINE)) {
1112            m_logger.log(Level.FINE,
1113                "Looking for "
1114                    + sType
1115                    + " live id = "
1116                    + nLiveId
1117                    + ", key = "
1118                    + nKey);
1119        }
1120
1121        try {
1122            AbstractChildObject liveChild =
1123                (AbstractChildObject) HarmoniseObjectFactory.instantiateHarmoniseObject(
1124                    dsi,
1125                    sType,
1126                    nLiveId);
1127            
1128            if(liveChild == null) {
1129                if(m_logger.isLoggable(Level.INFO)) {
1130                    m_logger.log(Level.INFO, "Live version in " + sPath + " not found!");
1131                }
1132                throw new InvalidPathException("Live version in " + sPath + " not found!");
1133            }
1134
1135            //check live child exists as live
1136
if (liveChild.exists() == true) {
1137                int nLiveKey = liveChild.getKey();
1138
1139                List versions = null;
1140
1141                if (nLiveKey == nKey) {
1142                    child = liveChild;
1143                } else {
1144
1145                    if (nLiveKey < nKey) {
1146                        //if version key is greater than live key we're looking for a pending version
1147
versions = liveChild.getPendingVersions();
1148                    } else if (nLiveKey > nKey) {
1149                        //if version key is less than live key we're looking for a historical version
1150
versions = liveChild.getHistoricalVersions();
1151                    }
1152
1153                    child = getChildByKeyFromList(versions, nKey);
1154
1155                    if (child == null) {
1156                        //initial assumptions about key ordering were wrong
1157
//try again reversing assumptions
1158

1159                        if (nLiveKey < nKey) {
1160                            //if version key is less than live key we're looking for a historical version
1161
versions = liveChild.getHistoricalVersions();
1162                        } else if (nLiveKey > nKey) {
1163                            //if version key is greater than live key we're looking for a pending version
1164
versions = liveChild.getPendingVersions();
1165                        }
1166
1167                        child = getChildByKeyFromList(versions, nKey);
1168                    }
1169                }
1170            } else {
1171                //must be historical
1172
child =
1173                    (AbstractChildObject) Class.forName(sType).newInstance();
1174                child.setId(nLiveId);
1175                child.setHistorical(true);
1176                child.setKey(nKey);
1177                child.setDataStoreInterface(dsi);
1178            }
1179        } catch (HarmoniseFactoryException e) {
1180            throw new NameResolverException(e);
1181        } catch (DataAccessException e) {
1182            throw new NameResolverException(e);
1183        } catch (InstantiationException JavaDoc e) {
1184            throw new NameResolverException(e);
1185        } catch (IllegalAccessException JavaDoc e) {
1186            throw new NameResolverException(e);
1187        } catch (ClassNotFoundException JavaDoc e) {
1188            throw new NameResolverException(e);
1189        } catch (PopulateException e) {
1190            throw new NameResolverException(e);
1191        }
1192
1193        return child;
1194    }
1195
1196    /**
1197     * Returns the archived child object reference by the given path.
1198     *
1199     * @param dsi
1200     * @param sPath
1201     * @return
1202     * @throws NameResolverException
1203     */

1204    public static AbstractChildObject getArchivedObject(
1205        AbstractDataStoreInterface dsi,
1206        String JavaDoc sPath)
1207        throws NameResolverException {
1208        AbstractChildObject child = null;
1209
1210        if (isArchiveURL(sPath) == false) {
1211            throw new InvalidPathException("Invalid path");
1212        }
1213
1214        try {
1215            AbstractParentObject parent = null;
1216            String JavaDoc sLivePath =
1217                sPath.replaceAll(URL_SEPARATOR + ARCHIVE_NAME, "");
1218
1219            //first assume the parent will not be archived
1220
String JavaDoc sParentPath = getPathParent(sPath);
1221
1222            String JavaDoc sLiveParentPath =
1223                sParentPath.replaceAll(URL_SEPARATOR + ARCHIVE_NAME, "");
1224
1225            if (isVirtualCollection(sLiveParentPath) == true) {
1226                //if parent is virtual collection this is gonna be a live root collection
1227
if (m_logger.isLoggable(Level.FINE)) {
1228                    m_logger.log(Level.FINE,
1229                        "looking for virtual parent child - " + sLivePath);
1230                }
1231
1232                if (isVirtualCollection(sLivePath) == false) {
1233                    child = getObjectFromURL(dsi, sLivePath);
1234                }
1235            } else {
1236                if (m_logger.isLoggable(Level.FINE)) {
1237                    m_logger.log(Level.FINE,
1238                        "looking for live parent - " + sLiveParentPath);
1239                }
1240                //try and get live parent
1241
parent =
1242                    (AbstractParentObject) getObjectFromURL(dsi,
1243                        sLiveParentPath);
1244
1245                if (parent == null) {
1246                    //parent was archived too
1247
parent =
1248                        (AbstractParentObject) getObjectFromURL(dsi,
1249                            sParentPath);
1250                }
1251
1252                String JavaDoc sName = getLastSegment(sPath);
1253
1254                child = parent.getArchivedChildByName(sName);
1255
1256                if (child == null) {
1257                    child = parent.getChildByName(sName);
1258
1259                    //the live child must be a parent object to be part of the archive hiarchy
1260
if ((child instanceof AbstractParentObject) == false) {
1261                        child = null;
1262                    }
1263                }
1264            }
1265
1266        } catch (DataAccessException e) {
1267            throw new NameResolverException(e);
1268        } catch (NameResolverException e) {
1269            throw new NameResolverException(e);
1270        }
1271
1272        return child;
1273    }
1274
1275    /**
1276     * Returns the key associated to the given value in the given map.
1277     *
1278     * @param val
1279     * @param map
1280     * @return
1281     */

1282    private static Object JavaDoc getKeyForValue(Object JavaDoc val, Map map) {
1283        Object JavaDoc result = null;
1284        Iterator iter = map.keySet().iterator();
1285
1286        boolean bFound = false;
1287
1288        while (iter.hasNext() == true && bFound == false) {
1289            Object JavaDoc key = iter.next();
1290            if (map.get(key).equals(val) == true) {
1291                bFound = true;
1292                result = key;
1293            }
1294        }
1295
1296        return result;
1297    }
1298
1299    /**
1300     * Returns the class name associated to the given URL of the resource, if the URL is
1301     * a version URL, or at least the resource type's group class name if the URL is a
1302     * standard DAV URL.
1303     *
1304     * @param sPath
1305     * @return
1306     * @throws NameResolverException
1307     */

1308    public static String JavaDoc getParentTypeFromURL(String JavaDoc sPath) throws NameResolverException {
1309        String JavaDoc sType = null;
1310
1311        try {
1312            sPath = URLDecoder.decode(sPath, UTF_8);
1313        } catch (UnsupportedEncodingException e) {
1314            throw new NameResolverException(e.getLocalizedMessage(), e);
1315        }
1316        
1317        DAVCollectionDesc desc = null;
1318
1319        String JavaDoc sSubPath =
1320            sPath.substring(
1321                sPath.indexOf(WEBDAV_ROOT_MARKER)
1322                    + WEBDAV_ROOT_MARKER.length()
1323                    + 1);
1324
1325        if (m_logger.isLoggable(Level.FINE)) {
1326            m_logger.log(Level.FINE,
1327                "getParentTypeFromURL: sPath - "
1328                    + sPath
1329                    + ", sSubPath = "
1330                    + sSubPath);
1331        }
1332
1333        Iterator iter = ROOT_COLLECTION_DESCS.keySet().iterator();
1334
1335        boolean bFound = false;
1336
1337        int nKeyLength = 0;
1338
1339        while (iter.hasNext()) {
1340            String JavaDoc sKey = (String JavaDoc) iter.next();
1341
1342            if (m_logger.isLoggable(Level.FINER)) {
1343                m_logger.log(Level.FINER,"comparing " + sSubPath + " with " + sKey);
1344            }
1345
1346            if (sSubPath.startsWith(sKey) && sKey.length() > nKeyLength) {
1347                bFound = true;
1348                desc = (DAVCollectionDesc) ROOT_COLLECTION_DESCS.get(sKey);
1349                nKeyLength = sKey.length();
1350            }
1351        }
1352
1353        if(desc != null){
1354            sType = desc.getHarmoniseClass();
1355        }
1356        
1357        return sType;
1358    }
1359
1360    /**
1361     * Returns the class name associated to the given URL of the resource, if the URL is
1362     * a version URL, or at least the resource type's group class name if the URL is a
1363     * standard DAV URL.
1364     *
1365     * @param sPath
1366     * @return
1367     */

1368    public static String JavaDoc getTypeFromVersionURL(String JavaDoc sPath) {
1369        String JavaDoc sType = null;
1370
1371        int nIndex = sPath.indexOf(WEBDAV_ROOT_MARKER);
1372
1373        String JavaDoc sSubPath =
1374            sPath.substring(nIndex + WEBDAV_ROOT_MARKER.length() + 1);
1375
1376        sSubPath = sSubPath.substring(0, sSubPath.indexOf(URL_SEPARATOR));
1377
1378        Iterator iter = TYPE_MAPPINGS.keySet().iterator();
1379
1380        boolean bFound = false;
1381
1382        while (iter.hasNext() == true && bFound == false) {
1383            String JavaDoc sKey = (String JavaDoc) iter.next();
1384
1385            if (sSubPath.equalsIgnoreCase(sKey) == true) {
1386                bFound = true;
1387                sType = (String JavaDoc) TYPE_MAPPINGS.get(sKey);
1388            }
1389        }
1390
1391        return sType;
1392    }
1393
1394    /**
1395     * Returns the class name associated to the given URL of the resource, if the URL is
1396     * a version URL, or at least the resource type's group class name if the URL is a
1397     * standard DAV URL.
1398     *
1399     * @param sPath
1400     * @return
1401     */

1402    public static String JavaDoc getChildTypeFromURL(String JavaDoc sPath)
1403        throws NameResolverException {
1404        String JavaDoc sReturnType = null;
1405
1406        try {
1407            sPath = URLDecoder.decode(sPath, UTF_8);
1408        } catch (UnsupportedEncodingException e) {
1409            throw new NameResolverException(e.getLocalizedMessage(), e);
1410        }
1411
1412        if (sPath.indexOf(ASSETS_NAME) > 0) {
1413            sReturnType = Asset.class.getName();
1414        } else if (
1415            sPath.indexOf(DOCS_NAME) > 0
1416                || sPath.indexOf(
1417                    WEBDAV_ROOT_MARKER + URL_SEPARATOR + NEWSLETTER_NAME)
1418                    > 0) {
1419            sReturnType = Document.class.getName();
1420        } else {
1421            //do the rest by finding the child class names from the parent class
1422

1423            String JavaDoc sType = getParentTypeFromURL(sPath);
1424
1425            try {
1426                AbstractParentObject parent =
1427                    (AbstractParentObject) Class.forName(sType).newInstance();
1428
1429                List childTypes = parent.getChildClassNames();
1430
1431                List childNonParentTypes = new Vector();
1432
1433                Iterator iter = childTypes.iterator();
1434
1435                while (iter.hasNext()) {
1436                    String JavaDoc className = (String JavaDoc) iter.next();
1437
1438                    if (className.equals(sType) == false) {
1439                        sReturnType = className;
1440                    }
1441                }
1442            } catch (InstantiationException JavaDoc e) {
1443                throw new NameResolverException(e);
1444            } catch (IllegalAccessException JavaDoc e) {
1445                throw new NameResolverException(e);
1446            } catch (ClassNotFoundException JavaDoc e) {
1447                throw new NameResolverException(e);
1448            }
1449        }
1450
1451        return sReturnType;
1452    }
1453
1454    /**
1455     * Returns the path of the parent of the object reference by the given path.
1456     *
1457     * @param sPath
1458     * @return
1459     */

1460    public static String JavaDoc getPathParent(String JavaDoc sPath) {
1461        String JavaDoc sParent = null;
1462
1463        if (sPath.endsWith(URL_SEPARATOR)) {
1464            sPath = sPath.substring(0, sPath.length() - 1);
1465        }
1466
1467        sParent = sPath.substring(0, sPath.lastIndexOf(URL_SEPARATOR));
1468
1469        return sParent;
1470    }
1471
1472    /**
1473     * Returns the path of the parent of the object reference by the given path.
1474     *
1475     * @param sPath
1476     * @return
1477     */

1478    public static String JavaDoc getLastSegment(String JavaDoc sPath) {
1479        String JavaDoc sParent = null;
1480
1481        if(sPath != null) {
1482            
1483            if (sPath.endsWith(URL_SEPARATOR)) {
1484                sPath = sPath.substring(0, sPath.length() - 1);
1485            }
1486    
1487            sParent =
1488                sPath.substring(
1489                    sPath.lastIndexOf(URL_SEPARATOR) + 1,
1490                    sPath.length());
1491        }
1492
1493        return sParent;
1494    }
1495
1496    /**
1497     * Returns the member collections of the DAV root.
1498     *
1499     * @return
1500     */

1501    public static List getRootCollections() {
1502        return ROOT_GROUPS;
1503    }
1504
1505    /**
1506     * Returns <code>true</code> if the given url references the root of the DAV server
1507     * i.e. http://.../webdav/
1508     *
1509     * @param url
1510     * @return
1511     */

1512    public static boolean isDAVRoot(URL url) {
1513        String JavaDoc sPath = url.getPath();
1514
1515        return isDAVRoot(sPath);
1516    }
1517
1518    /**
1519     * Returns <code>true</code> if the given url references the root of the DAV server
1520     * i.e. http://.../webdav/
1521     *
1522     * @param url
1523     * @return
1524     */

1525    public static boolean isDAVRoot(String JavaDoc sURL) {
1526        String JavaDoc sLastSegment = getLastSegment(sURL);
1527
1528        return sLastSegment.equals(WEBDAV_ROOT_MARKER);
1529    }
1530
1531    /**
1532     * Returns <code>true</code> if the given url references the archive root
1533     *
1534     * @param url
1535     * @return
1536     */

1537    public static boolean isArchiveRoot(URL url) {
1538        String JavaDoc sPath = url.getPath();
1539
1540        return isArchiveRoot(sPath);
1541    }
1542
1543    /**
1544     * Returns <code>true</code> if the given url references the archive root
1545     *
1546     * @param url
1547     * @return
1548     */

1549    public static boolean isArchiveRoot(String JavaDoc sPath) {
1550
1551        if (sPath.endsWith(URL_SEPARATOR) == true) {
1552            sPath = sPath.substring(0, sPath.length() - 1);
1553        }
1554
1555        return sPath.endsWith(
1556            WEBDAV_ROOT_MARKER + URL_SEPARATOR + ARCHIVE_NAME);
1557    }
1558
1559    /**
1560     * Returns <code>true</code> if the given url references one of the virtual collections
1561     * i.e. a collection represented in the DAV hierarchy that doesn't have an equivalent
1562     * Harmonise parent object
1563     *
1564     * @param url
1565     * @return
1566     */

1567    public static boolean isVirtualCollection(URL url) {
1568
1569        String JavaDoc sPath = url.getPath();
1570
1571        try {
1572            sPath = URLDecoder.decode(sPath, UTF_8);
1573        } catch (UnsupportedEncodingException e) {
1574            m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1575        }
1576
1577        return isVirtualCollection(sPath);
1578    }
1579    
1580    /**
1581     * Returns <code>true</code> if the given url references a virtual resource
1582     * i.e. a non-existent resource representing collection resources.
1583     *
1584     * @param url
1585     * @return
1586     */

1587    public static boolean isVirtualResource(URL url) {
1588
1589        String JavaDoc sPath = url.getPath();
1590
1591        try {
1592            sPath = URLDecoder.decode(sPath, UTF_8);
1593        } catch (UnsupportedEncodingException e) {
1594            m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1595        }
1596
1597        return isVirtualResource(sPath);
1598    }
1599
1600    /**
1601     * Returns <code>true</code> if the given path references a virtual resource
1602     * i.e. a non-existent resource representing collection resources.
1603     *
1604     * @param path
1605     * @return
1606     */

1607    private static boolean isVirtualResource(String JavaDoc path) {
1608        boolean bIsVirtual = false;
1609        
1610        
1611        String JavaDoc sLastSeg = getLastSegment(path);
1612        
1613        if(sLastSeg != null) {
1614            bIsVirtual = sLastSeg.equals(VIRTUAL_RESOURCE_NAME);
1615        }
1616        
1617        return bIsVirtual;
1618    }
1619
1620    /**
1621     * Returns <code>true</code> if the given url references one of the virtual collections
1622     * i.e. a collection represented in the DAV hierarchy that doesn't have an equivalent
1623     * Harmonise parent object.
1624     *
1625     * @param url
1626     * @return
1627     */

1628    public static boolean isVirtualCollection(String JavaDoc sPath) {
1629        boolean bIsVirtual = false;
1630
1631        bIsVirtual = isDAVRoot(sPath);
1632
1633        if (bIsVirtual == false) {
1634            bIsVirtual = isArchiveRoot(sPath);
1635        }
1636        
1637        if (bIsVirtual == false) {
1638            String JavaDoc sLastSegment = getLastSegment(sPath);
1639
1640            if (m_logger.isLoggable(Level.FINE)) {
1641                m_logger.log(Level.FINE,
1642                    "Checking virtual collection - " + sLastSegment);
1643            }
1644
1645            bIsVirtual = sLastSegment.equals(WEBDAV_ROOT_MARKER)
1646                || sLastSegment.equals(VIRTUAL_COLLECTION_NAME);
1647
1648            //check against all descs if necessary
1649
if (bIsVirtual == false) {
1650                DAVCollectionDesc desc = getCollectionDesc(sPath);
1651
1652                if (desc != null) {
1653                    bIsVirtual = desc.isVirtual();
1654                }
1655            }
1656
1657            if (m_logger.isLoggable(Level.FINE)) {
1658                m_logger.log(Level.FINE, "Is virtual collection - " + bIsVirtual);
1659            }
1660        }
1661
1662        return bIsVirtual;
1663    }
1664
1665    public static List getArchivedVirtualCollectionMemberNames(URL resourceURL) {
1666        List members = null;
1667
1668        String JavaDoc sPath = resourceURL.getPath();
1669
1670        if (isArchiveURL(sPath) == true) {
1671            sPath = sPath.replaceFirst(URL_SEPARATOR + ARCHIVE_NAME, "");
1672
1673            members = getVirtualCollectionMemberNames(sPath);
1674        }
1675
1676        return members;
1677    }
1678
1679    public static List getVirtualCollectionMemberNames(URL url) {
1680        return getVirtualCollectionMemberNames(url.getPath());
1681    }
1682
1683    /**
1684     * Returns the list of members for the virtual collection found at the given URL.
1685     *
1686     * @param resourceURL
1687     * @return
1688     */

1689    public static List getVirtualCollectionMemberNames(String JavaDoc sPath) {
1690
1691        try {
1692            sPath = URLDecoder.decode(sPath, UTF_8);
1693        } catch (UnsupportedEncodingException e) {
1694            m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1695        }
1696
1697        DAVCollectionDesc desc = getCollectionDesc(sPath);
1698
1699        List members = new ArrayList();
1700
1701        if (desc != null) {
1702            List fullpaths = desc.getMembers();
1703
1704            if (fullpaths != null) {
1705                for (Iterator iter = fullpaths.iterator(); iter.hasNext();) {
1706                    String JavaDoc sMemberPath = (String JavaDoc) iter.next();
1707                    members.add(getLastSegment(sMemberPath));
1708                }
1709            }
1710        }
1711
1712        return members;
1713    }
1714
1715    /**
1716     * Returns a <code>String</code> stripped of anything after and including a '.',
1717     * if there is one.
1718     *
1719     *
1720     * @param sName
1721     * @return
1722     */

1723    public static String JavaDoc stripExtension(String JavaDoc sName) {
1724        int nDotIndex = sName.indexOf(".");
1725        if (nDotIndex > 0) {
1726            sName = sName.substring(0, nDotIndex);
1727        }
1728
1729        return sName;
1730    }
1731
1732    /**
1733     * Returns the resource type for a given class name.
1734     *
1735     * @param sClassName
1736     * @return
1737     */

1738    public static String JavaDoc getResourceTypeFromClass(String JavaDoc sClassName)
1739        throws NameResolverException {
1740        String JavaDoc sResult = RESOURCE_TYPE_RESOURCE;
1741
1742        Class JavaDoc clss = null;
1743        try {
1744            clss = Class.forName(sClassName);
1745        } catch (ClassNotFoundException JavaDoc e) {
1746            throw new NameResolverException(e.getLocalizedMessage(), e);
1747        }
1748
1749        if (Profile.class.isAssignableFrom(clss) == true) {
1750            sResult = "property";
1751        } else if (AbstractParentObject.class.isAssignableFrom(clss) == true) {
1752            sResult = RESOURCE_TYPE_COLLECTION;
1753        } else if (CLASS_2_RESOURCE_TYPE_MAPPING.containsKey(sClassName)) {
1754            sResult = (String JavaDoc) CLASS_2_RESOURCE_TYPE_MAPPING.get(sClassName);
1755        }
1756
1757        return sResult;
1758    }
1759
1760    /**
1761     * Returns the Harmonise classname appropriate for the given combination of
1762     * resource type and content type.
1763     *
1764     * @param sResourceType
1765     * @param sContentType
1766     * @return
1767     */

1768    public static String JavaDoc getClassFromResourceTypeDesc(
1769        String JavaDoc sResourceType,
1770        String JavaDoc sContentType) {
1771        String JavaDoc sClassName = null;
1772        if (sContentType != null && sContentType.length() > 0) {
1773            if (sContentType.startsWith("text") == true) {
1774                sClassName = Document.class.getName();
1775            } else {
1776                sClassName = Asset.class.getName();
1777            }
1778        } else {
1779            Iterator iter = CLASS_2_RESOURCE_TYPE_MAPPING.keySet().iterator();
1780            boolean bFound = false;
1781            while (iter.hasNext() && bFound == false) {
1782                String JavaDoc sTmpName = (String JavaDoc) iter.next();
1783
1784                if (CLASS_2_RESOURCE_TYPE_MAPPING
1785                    .get(sTmpName)
1786                    .equals(sResourceType)
1787                    == true) {
1788                    sClassName = sTmpName;
1789                    bFound = true;
1790                }
1791            }
1792        }
1793
1794        return sClassName;
1795    }
1796
1797    /**
1798     * @param sDomainObj
1799     */

1800    public static String JavaDoc getRootForClass(String JavaDoc sClassname)
1801        throws NameResolverException {
1802
1803        StringBuffer JavaDoc sResult = new StringBuffer JavaDoc();
1804
1805        sResult.append(URL_SEPARATOR).append(WEBDAV_ROOT_MARKER).append(
1806            URL_SEPARATOR);
1807
1808        try {
1809            AbstractChildObject child =
1810                (AbstractChildObject) Class.forName(sClassname).newInstance();
1811
1812            String JavaDoc sParentClass = child.getParentObjectClassName();
1813
1814            boolean bFound = false;
1815
1816            Iterator iter = ROOT_COLLECTION_DESCS.values().iterator();
1817
1818            while (iter.hasNext() && bFound == false) {
1819                DAVCollectionDesc desc = (DAVCollectionDesc) iter.next();
1820
1821                if (desc.getHarmoniseClass().equals(sParentClass) == true) {
1822                    sResult.append(desc.getURI()).append(URL_SEPARATOR);
1823                    bFound = true;
1824                }
1825            }
1826        } catch (InstantiationException JavaDoc e) {
1827            throw new NameResolverException(e.getLocalizedMessage(), e);
1828        } catch (IllegalAccessException JavaDoc e) {
1829            throw new NameResolverException(e.getLocalizedMessage(), e);
1830        } catch (ClassNotFoundException JavaDoc e) {
1831            throw new NameResolverException(e.getLocalizedMessage(), e);
1832        }
1833
1834        return sResult.toString();
1835    }
1836
1837    /**
1838     * Retutrns the collection description for given path if it is stored in
1839     * <code>ROOT_COLLECTION_DESCS</code>.
1840     *
1841     * @param sPath
1842     * @return
1843     */

1844    private static DAVCollectionDesc getCollectionDesc(String JavaDoc sPath) {
1845        DAVCollectionDesc desc = null;
1846
1847        String JavaDoc sSubPath =
1848            sPath.substring(
1849                sPath.indexOf(WEBDAV_ROOT_MARKER)
1850                    + WEBDAV_ROOT_MARKER.length()
1851                    + 1);
1852
1853        if (sSubPath.endsWith(URL_SEPARATOR)) {
1854            sSubPath = sSubPath.substring(0, sSubPath.length() - 1);
1855        }
1856
1857        desc = (DAVCollectionDesc) ROOT_COLLECTION_DESCS.get(sSubPath);
1858
1859        return desc;
1860    }
1861
1862    /**
1863     * @param resourceURL
1864     * @return
1865     */

1866    public static boolean isArchivedVirtualCollection(URL resourceURL) {
1867        boolean bIsArchVirt = false;
1868        String JavaDoc sPath = resourceURL.getPath();
1869
1870        if (isArchiveURL(sPath) == true) {
1871            sPath = sPath.replaceFirst(URL_SEPARATOR + ARCHIVE_NAME, "");
1872
1873            bIsArchVirt = isVirtualCollection(sPath);
1874        }
1875
1876        return bIsArchVirt;
1877    }
1878
1879    public static boolean hasValidDAVPath(AbstractChildObject child)
1880        throws WebDAVException {
1881        boolean bHasValidDAVpath = false;
1882
1883        if (child != null) {
1884            try {
1885                String JavaDoc sOHPath = child.getPath();
1886                if(sOHPath != null) {
1887                    String JavaDoc sClassname = child.getParentObjectClassName();
1888                    Iterator iter = ROOT_COLLECTION_DESCS.values().iterator();
1889
1890                    while (iter.hasNext() && bHasValidDAVpath == false) {
1891                        DAVCollectionDesc desc = (DAVCollectionDesc) iter.next();
1892
1893                        if (desc.isVirtual() == false
1894                            && desc.getHarmoniseClass().equals(sClassname)
1895                            && sOHPath.startsWith(desc.getHarmonisePath())) {
1896                            bHasValidDAVpath = true;
1897                        }
1898                    }
1899                }
1900                
1901            } catch (DataAccessException e) {
1902                m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1903                throw new WebDAVException(
1904                    WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
1905                    e.getLocalizedMessage());
1906            }
1907
1908        }
1909
1910        return bHasValidDAVpath;
1911    }
1912
1913    /**
1914     * Returns child from list which matches the key given.
1915     *
1916     * @param children
1917     * @param nKey
1918     * @return
1919     * @throws DataAccessException
1920     */

1921    private static AbstractChildObject getChildByKeyFromList(
1922        List children,
1923        int nKey)
1924        throws DataAccessException {
1925        AbstractChildObject child = null;
1926        Iterator iter = children.iterator();
1927
1928        while (iter.hasNext() && child == null) {
1929            AbstractChildObject tmpChild = (AbstractChildObject) iter.next();
1930
1931            if (tmpChild.getKey() == nKey) {
1932                child = tmpChild;
1933            }
1934
1935        }
1936
1937        return child;
1938    }
1939    
1940    /**
1941     * Returns the correct namespace for the given property.
1942     *
1943     * @param prop
1944     * @return
1945     */

1946    public static NamespaceType getPropertyNamespace(Property prop) throws DataAccessException {
1947        NamespaceType ns = null;
1948        
1949        List rbs_props = AuthorizationValidator.getSecurityProperties(prop.getDataStoreInterface());
1950
1951        if(rbs_props.contains(prop)) {
1952            ns = NamespaceType.OHRM_RBS;
1953        } else if(prop instanceof WorkflowProperty) {
1954            ns = NamespaceType.OHRM_WORKFLOW;
1955        } else {
1956            ns = NamespaceType.OHRM;
1957        }
1958        
1959        return ns;
1960    }
1961    
1962    /**
1963     * Returns the correct profile for the <code>AbstarctProfiledObject</code>
1964     * for the given namespace URI. If the object doesn not have an associated
1965     * profile for a recognised namespace the profile is created, attached to
1966     * the object and returned.
1967     *
1968     * @param profObj
1969     * @param sURI
1970     * @return
1971     * @throws DataAccessException
1972     * @throws InvalidProfileException
1973     */

1974    public static Profile getProfileForNamespaceURI(AbstractProfiledObject profObj, String JavaDoc sURI) throws DataAccessException, InvalidProfileException {
1975        Profile nsProf = null;
1976        AbstractDataStoreInterface dsi = profObj.getDataStoreInterface();
1977        if(sURI.equals(NamespaceType.OHRM_RBS.getURI()) == true) {
1978            
1979            nsProf = profObj.getProfile(AuthorityProfile.SECURITY_PROFILE_NAME);
1980            if(nsProf == null) {
1981                nsProf = new AuthorityProfile(dsi);
1982                profObj.addProfile(nsProf);
1983            }
1984            
1985        } else if(sURI.equals(NamespaceType.OHRM.getURI()) == true) {
1986            nsProf = profObj.getProfile();
1987            if(nsProf == null) {
1988                nsProf = new Profile(dsi);
1989                profObj.setProfile(nsProf);
1990            }
1991        } else if(sURI.equals(NamespaceType.OHRM_WORKFLOW.getURI()) == true) {
1992            nsProf = profObj.getProfile(WorkflowProfile.WORKFLOW_PROFILE_NAME);
1993            if(nsProf == null) {
1994                nsProf = new WorkflowProfile(dsi);
1995                profObj.addProfile(nsProf);
1996            }
1997        }
1998        
1999        return nsProf;
2000    }
2001
2002    /**
2003     * Returns <code>true</code> if the specified path starts
2004     * with the <code>WEBDAV_ROOT_MARKER</code> segment.
2005     *
2006     * @param path
2007     * @return
2008     */

2009    public static boolean isDAVPath(String JavaDoc sPath) {
2010        boolean bIsDAVPath = false;
2011        
2012        if(sPath != null) {
2013            bIsDAVPath = sPath.startsWith(URL_SEPARATOR + WEBDAV_ROOT_MARKER);
2014        }
2015        
2016        return bIsDAVPath;
2017    }
2018
2019}
Popular Tags