KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > cms > Node


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

11 package org.jboss.portal.cms;
12
13 import org.apache.log4j.Logger;
14 import org.apache.slide.authenticate.CredentialsToken;
15 import org.apache.slide.common.NamespaceAccessToken;
16 import org.apache.slide.common.SlideException;
17 import org.apache.slide.common.SlideToken;
18 import org.apache.slide.common.SlideTokenImpl;
19 import org.apache.slide.content.*;
20 import org.apache.slide.macro.DeleteMacroException;
21 import org.apache.slide.security.AccessDeniedException;
22 import org.apache.slide.structure.*;
23
24 import javax.transaction.SystemException JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Abstraction over the non API webdav.
32  *
33  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
34  * @version $Revision: 1.7 $
35  */

36 public class Node
37 {
38
39    public static final SlideToken ROOT_TOKEN = new SlideTokenImpl(new CredentialsToken("root"));
40
41    static
42    {
43       // Important to do, in order to force the node to participate into the transactions of slide
44
ROOT_TOKEN.setForceStoreEnlistment(true);
45    }
46
47    protected static final Logger log = Logger.getLogger(Node.class);
48
49    protected final NamespaceAccessToken nat;
50    protected final String JavaDoc uri;
51    protected final String JavaDoc name;
52    protected final String JavaDoc principal;
53    protected final String JavaDoc version;
54
55    // Slide state
56
protected final SlideToken slideToken;
57
58    protected State state;
59
60    Node(NamespaceAccessToken nat, String JavaDoc uri, String JavaDoc version, String JavaDoc principal) throws NodeException
61    {
62       this.nat = nat;
63       this.uri = uri;
64       this.name = uri.substring(uri.lastIndexOf('/') + 1);
65       this.principal = principal;
66       this.version = version;
67
68       // Initialize slide fields
69
slideToken = new SlideTokenImpl(new CredentialsToken(principal));
70       slideToken.setForceStoreEnlistment(true);
71       slideToken.setEnforceLockTokens(false);
72       refresh();
73    }
74
75    private void refresh() throws NodeException
76    {
77       state = null;
78       state = getState();
79    }
80
81    private State getState() throws NodeException
82    {
83       try
84       {
85          //
86
Structure structure = nat.getStructureHelper();
87          org.apache.slide.content.Content content = nat.getContentHelper();
88
89          //
90
ObjectNode node = structure.retrieve(slideToken, uri);
91          NodeRevisionDescriptors descs = content.retrieve(slideToken, uri);
92          NodeRevisionNumber version = null;
93          NodeRevisionDescriptor desc = null;
94          if(this.version != null)
95          {
96             // Get a specific version
97
version = new NodeRevisionNumber(this.version);
98             desc = content.retrieve(slideToken, descs, version);
99          }
100          else
101          {
102             // Get the latest one
103
desc = content.retrieve(slideToken, descs);
104             version = desc.getRevisionNumber();
105          }
106          State state = new State(node, version, descs, desc);
107          return state;
108       }
109       catch(ObjectNotFoundException e)
110       {
111          if(log.isDebugEnabled())
112          {
113             log.debug("Not found", e);
114          }
115          return null;
116       }
117       catch(RevisionDescriptorNotFoundException e)
118       {
119          if(log.isDebugEnabled())
120          {
121             log.debug("Could not retrieve descriptor", e);
122          }
123          return null;
124       }
125       catch(AccessDeniedException e)
126       {
127          throw new NodeException(uri, "Access denied", e);
128       }
129       catch(SlideException e)
130       {
131          throw new NodeException(uri, "Failure during source initialization", e);
132       }
133    }
134
135    public Content getContent() throws NodeException
136    {
137       if(state == null)
138       {
139          throw new NoSuchURIException(uri);
140       }
141       String JavaDoc mimeType = getMimeType();
142       byte[] bytes = getBytes();
143       return new Content(bytes, mimeType);
144    }
145
146    private String JavaDoc getMimeType()
147    {
148       return state.desc.getContentType();
149    }
150
151    private byte[] getBytes() throws NodeException
152    {
153       try
154       {
155          NodeRevisionContent content = nat.getContentHelper().retrieve(slideToken, state.descs, state.desc);
156          return content.getContentBytes();
157       }
158       catch(SlideException e)
159       {
160          throw new NodeException(uri, "Could not get source", e);
161       }
162    }
163
164    public void createContent(Content content, boolean recursive) throws NodeException
165    {
166       try
167       {
168          if(recursive)
169          {
170             _createContentRec(nat, state, slideToken, uri, content);
171          }
172          else
173          {
174             _createContent(nat, state, slideToken, uri, content);
175          }
176          refresh();
177       }
178       catch(ObjectNotFoundException e)
179       {
180          throw new NoSuchURIException(e.getObjectUri());
181       }
182       catch(NodeException e)
183       {
184          setRollbackOnly();
185          throw e;
186       }
187    }
188
189    private static void _createContentRec(NamespaceAccessToken nat, State state, SlideToken slideToken, String JavaDoc uri, Content content) throws NodeException
190    {
191       try
192       {
193          _createContent(nat, state, slideToken, uri, content);
194       }
195       catch(ObjectNotFoundException e)
196       {
197          String JavaDoc ancestorURI = e.getObjectUri();
198          try
199          {
200             _createDir(nat, slideToken, ancestorURI);
201             _createContentRec(nat, state, slideToken, uri, content);
202          }
203          catch(ObjectNotFoundException onfe)
204          {
205             throw new NodeException(ancestorURI, onfe);
206          }
207       }
208    }
209
210    private static void _createContent(NamespaceAccessToken nat, State state, SlideToken slideToken, String JavaDoc uri, Content content) throws NodeException, ObjectNotFoundException
211    {
212       try
213       {
214          NodeRevisionDescriptor desc = null;
215          if(state != null)
216          {
217             desc = state.desc;
218             desc.setContentLength(content.getBytes().length);
219             desc.setLastModified(new Date JavaDoc());
220             NodeRevisionContent nrc = new NodeRevisionContent();
221             nrc.setContent(content.getBytes());
222             nat.getContentHelper().store(slideToken, uri, desc, nrc);
223          }
224          else
225          {
226             SubjectNode subject = new SubjectNode();
227             nat.getStructureHelper().create(slideToken, subject, uri);
228             String JavaDoc contentType = content.getMimeType();
229             if(contentType == null)
230             {
231                contentType = "application/octet-stream";
232             }
233             desc = new NodeRevisionDescriptor(content.getBytes().length);
234             desc.setResourceType("");
235             desc.setSource("");
236             desc.setContentLanguage("en");
237             desc.setContentLength(content.getBytes().length);
238             desc.setContentType(content.getMimeType());
239             desc.setLastModified(new Date JavaDoc());
240             desc.setOwner(slideToken.getCredentialsToken().getPublicCredentials());
241             desc.setContentLength(content.getBytes().length);
242             desc.setLastModified(new Date JavaDoc());
243
244             //
245
NodeRevisionContent nrc = new NodeRevisionContent();
246             nrc.setContent(content.getBytes());
247             nat.getContentHelper().create(slideToken, uri, desc, nrc);
248             nat.getContentHelper().store(slideToken, uri, desc, nrc);
249          }
250       }
251       catch(ObjectNotFoundException e)
252       {
253          throw e;
254       }
255       catch(Exception JavaDoc e)
256       {
257          throw new NodeException(uri, "Could not create source", e);
258       }
259    }
260
261    private void setRollbackOnly()
262    {
263       try
264       {
265          nat.setRollbackOnly();
266       }
267       catch(SystemException JavaDoc e)
268       {
269          log.warn("Could not set transaction to rollback", e);
270       }
271    }
272
273    public void delete() throws NodeException
274    {
275       if(state == null)
276       {
277          throw new NoSuchURIException(uri);
278       }
279       try
280       {
281          nat.getMacroHelper().delete(slideToken, uri);
282          refresh();
283       }
284       catch(DeleteMacroException e)
285       {
286          setRollbackOnly();
287          throw new NodeException(uri, e);
288       }
289    }
290
291    public void createDir(boolean recursive) throws NodeException
292    {
293       try
294       {
295          if(recursive)
296          {
297             _createDirRec(nat, slideToken, uri);
298          }
299          else
300          {
301             _createDir(nat, slideToken, uri);
302          }
303          refresh();
304       }
305       catch(ObjectNotFoundException e)
306       {
307          throw new NoSuchURIException(e.getObjectUri(), e);
308       }
309       catch(NodeException e)
310       {
311          setRollbackOnly();
312          throw e;
313       }
314    }
315
316    private static void _createDirRec(NamespaceAccessToken nat, SlideToken slideToken, String JavaDoc uri) throws NodeException
317    {
318       try
319       {
320          _createDir(nat, slideToken, uri);
321       }
322       catch(ObjectNotFoundException e)
323       {
324          String JavaDoc ancestorURI = e.getObjectUri();
325          try
326          {
327             _createDir(nat, slideToken, ancestorURI);
328             _createDirRec(nat, slideToken, uri);
329          }
330          catch(ObjectNotFoundException onfe)
331          {
332             throw new NodeException(ancestorURI, onfe);
333          }
334       }
335    }
336
337    private static void _createDir(NamespaceAccessToken nat, SlideToken slideToken, String JavaDoc uri) throws NodeException, ObjectNotFoundException
338    {
339       try
340       {
341          SubjectNode collection = new SubjectNode();
342          nat.getStructureHelper().create(slideToken, collection, uri);
343       }
344       catch(ObjectAlreadyExistsException e)
345       {
346          //
347
}
348       catch(ObjectNotFoundException e)
349       {
350          throw e;
351       }
352       catch(Exception JavaDoc e)
353       {
354          throw new NodeException(uri, "Could not create dir.", e);
355       }
356
357       try
358       {
359          Date JavaDoc now = new Date JavaDoc();
360          NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor(0);
361          descriptor.setResourceType("<collection/>");
362          descriptor.setCreationDate(now);
363          descriptor.setLastModified(now);
364          descriptor.setContentLength(0);
365          descriptor.setSource("");
366          descriptor.setOwner(slideToken.getCredentialsToken().getPublicCredentials());
367          nat.getContentHelper().create(slideToken, uri, descriptor, null);
368       }
369       catch(Exception JavaDoc e)
370       {
371          throw new NodeException(uri, "Could not create dir.", e);
372       }
373    }
374
375    public Map JavaDoc getChildren() throws NodeException
376    {
377       if(state == null)
378       {
379          throw new NoSuchURIException(uri);
380       }
381       Map JavaDoc result = new HashMap JavaDoc();
382       final Enumeration JavaDoc children = state.node.enumerateChildren();
383       while(children.hasMoreElements())
384       {
385          String JavaDoc child = (String JavaDoc) children.nextElement();
386          int index = child.lastIndexOf('/');
387          String JavaDoc childName = child.substring(index + 1);
388          Node childNode = getChild(childName);
389          result.put(childName, childNode);
390       }
391       return result;
392    }
393
394    public Node getChild(String JavaDoc name) throws NodeException
395    {
396       if(state == null)
397       {
398          throw new NoSuchURIException(uri);
399       }
400       Node child = new Node(nat, uri + "/" + name, null, principal);
401       return child;
402    }
403
404    public Node getParent() throws NodeException
405    {
406       if(state == null)
407       {
408          throw new NoSuchURIException(uri);
409       }
410       if(uri.length() == 1)
411       {
412          return null;
413       }
414       int index = uri.lastIndexOf('/');
415       if(index == -1)
416       {
417          return null;
418       }
419       String JavaDoc parentURI;
420       if(index == 0)
421       {
422          parentURI = "/";
423       }
424       else if(index == uri.length() - 1)
425       {
426          // assert m_path.endsWith("/")
427
parentURI = uri.substring(0, uri.substring(0, uri.length() - 1).lastIndexOf('/'));
428       }
429       else
430       {
431          parentURI = uri.substring(0, index);
432       }
433       Node parent = new Node(nat, parentURI, null, principal);
434       return parent;
435
436    }
437
438    public boolean isDir() throws NodeException
439    {
440       if(state == null)
441       {
442          throw new NoSuchURIException(uri);
443       }
444       NodeProperty property = state.desc.getProperty("resourcetype");
445       if(property != null && ((String JavaDoc) property.getValue()).startsWith("<collection/>"))
446       {
447          return true;
448       }
449       return false;
450    }
451
452    public void moveTo(String JavaDoc targetURI) throws NodeException
453    {
454       if(state == null)
455       {
456          throw new NoSuchURIException(uri);
457       }
458       try
459       {
460          nat.getMacroHelper().move(slideToken, uri, targetURI);
461       }
462       catch(Exception JavaDoc se)
463       {
464          setRollbackOnly();
465          throw new NodeException(uri, "Could not move source", se);
466       }
467    }
468
469    public void copyTo(String JavaDoc targetURI) throws NodeException
470    {
471       if(state == null)
472       {
473          throw new NoSuchURIException(uri);
474       }
475       try
476       {
477          nat.getMacroHelper().copy(slideToken, uri, targetURI);
478       }
479       catch(Exception JavaDoc se)
480       {
481          setRollbackOnly();
482          throw new NodeException(uri, "Could not move source", se);
483       }
484    }
485
486    public boolean exists()
487    {
488       return state != null;
489    }
490
491    public String JavaDoc getName()
492    {
493       return name;
494    }
495
496    public String JavaDoc getURI()
497    {
498       return uri;
499    }
500
501    public String JavaDoc getProperty(String JavaDoc namespace, String JavaDoc name) throws NodeException
502    {
503       if(state == null)
504       {
505          throw new NoSuchURIException(uri);
506       }
507       NodeProperty property = state.desc.getProperty(name, namespace);
508       if(property == null)
509       {
510          return null;
511       }
512       String JavaDoc value = property.getValue().toString();
513       return value;
514    }
515
516    public void setProperty(String JavaDoc namespace, String JavaDoc name, String JavaDoc value) throws NodeException
517    {
518       if(state == null)
519       {
520          throw new NoSuchURIException(uri);
521       }
522       try
523       {
524          state.desc.setProperty(name, namespace, value);
525          state.desc.setLastModified(new Date JavaDoc());
526          nat.getContentHelper().store(slideToken, uri, state.desc, null);
527       }
528       catch(Exception JavaDoc e)
529       {
530          throw new NodeException(uri, e);
531       }
532    }
533
534    public String JavaDoc toString()
535    {
536       return "Node[" + uri + "]";
537    }
538
539    /**
540     * Contains the state from the slide repository.
541     */

542    private static class State
543    {
544       final ObjectNode node;
545       final NodeRevisionNumber version;
546       final NodeRevisionDescriptors descs;
547       final NodeRevisionDescriptor desc;
548
549       public State(ObjectNode node, NodeRevisionNumber version, NodeRevisionDescriptors desc, NodeRevisionDescriptor descs)
550       {
551          if(node == null)
552          {
553             throw new RuntimeException JavaDoc("Assert node is not null");
554          }
555          if(descs == null)
556          {
557             throw new RuntimeException JavaDoc("Assert descs is not null");
558          }
559          if(desc == null)
560          {
561             throw new RuntimeException JavaDoc("Assert desc is not null");
562          }
563          if(version == null)
564          {
565             throw new RuntimeException JavaDoc("Assert version is not null");
566          }
567          this.node = node;
568          this.version = version;
569          this.descs = desc;
570          this.desc = descs;
571       }
572    }
573
574 }
575
Popular Tags