1 package net.sourceforge.blogentis.slide;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.util.Date;
28
29 import net.sourceforge.blogentis.om.Blog;
30 import net.sourceforge.blogentis.storage.AbstractFileResource;
31
32 import org.apache.slide.common.NamespaceAccessToken;
33 import org.apache.slide.common.SlideException;
34 import org.apache.slide.common.SlideToken;
35 import org.apache.slide.content.Content;
36 import org.apache.slide.content.NodeProperty;
37 import org.apache.slide.content.NodeRevisionContent;
38 import org.apache.slide.content.NodeRevisionDescriptor;
39 import org.apache.slide.content.NodeRevisionDescriptors;
40 import org.apache.slide.webdav.util.WebdavUtils;
41 import org.apache.turbine.util.RunData;
42
43 /***
44 * An AbstractFileResource that represents a file stored in Slide.
45 *
46 * @author abas
47 */
48 public class SlideFileResource
49 implements AbstractFileResource {
50
51 private String path;
52 private String slidePath;
53 private NamespaceAccessToken token;
54 private SlideToken slideToken;
55 private NodeRevisionDescriptor descriptor = null;
56
57 /* package */SlideFileResource(RunData data, Blog blog, String path) {/package-summary/html">class="comment"> package */SlideFileResource(RunData data, Blog blog, String path) {/package-summary.html">SlideFileResource(RunData data, Blog blog, String path) {
58 if (!path.startsWith("/"))
59 path = "/" + path;
60 this.path = path;
61 this.slidePath = SlideService.getInstance().getSlidePath(blog, path,
62 true);
63 this.slideToken = SlideService.getInstance().getSlideToken(data);
64 this.token = SlideService.getInstance().getAccessToken();
65 }
66
67 public String getPath() {
68 return path;
69 }
70
71 private NodeRevisionDescriptor getDescriptor() {
72 if (descriptor != null)
73 return descriptor;
74 try {
75 token.getStructureHelper().retrieve(slideToken, slidePath);
76 Content contentHelper = token.getContentHelper();
77 NodeRevisionDescriptors descriptors = contentHelper
78 .retrieve(slideToken, slidePath);
79 descriptor = contentHelper.retrieve(slideToken, descriptors);
80 } catch (SlideException e) {
81 return null;
82 }
83 return descriptor;
84 }
85
86 public String getName() {
87 return getDescriptor().getName();
88 }
89
90 public boolean exists() {
91 if (getDescriptor() == null)
92 return false;
93 return true;
94 }
95
96 public int getSize() {
97 NodeRevisionDescriptor d = getDescriptor();
98 if (d == null)
99 return 0;
100 return (int)d.getContentLength();
101 }
102
103 public Date getLastModified() {
104 NodeRevisionDescriptor d = getDescriptor();
105 if (d == null)
106 return null;
107 return d.getLastModifiedAsDate();
108 }
109
110 public String getMimeType() {
111 NodeRevisionDescriptor d = getDescriptor();
112 if (d == null)
113 return null;
114 return d.getContentType();
115 }
116
117 public InputStream getFileAsInputStream()
118 throws FileNotFoundException {
119 NodeRevisionDescriptor d = getDescriptor();
120 if (d == null)
121 return null;
122 try {
123 NodeRevisionContent content = token.getContentHelper()
124 .retrieve(slideToken, slidePath, d);
125 return content.streamContent();
126 } catch (Exception e) {
127 throw new FileNotFoundException(e.toString());
128 }
129 }
130
131 public String getFileAsString()
132 throws FileNotFoundException {
133 NodeRevisionDescriptor d = getDescriptor();
134 if (d == null)
135 return null;
136 try {
137 NodeRevisionContent content = token.getContentHelper()
138 .retrieve(slideToken, slidePath, d);
139 return new String(content.getContentBytes(), "UTF-8");
140 } catch (Exception e) {
141 throw new FileNotFoundException(e.toString());
142 }
143 }
144
145 public boolean isOriginal() {
146 String prop = getProperty("isOriginal");
147 return prop != null && prop.equals("true");
148 }
149
150 public String getProperty(String propertyName) {
151 NodeRevisionDescriptor d = getDescriptor();
152 if (d == null)
153 return null;
154 NodeProperty property = d.getProperty(propertyName,
155 SlideService.BLOG_NAMESPACE);
156 if (property == null)
157 return null;
158 return property.getValue().toString();
159 }
160
161 public void setProperty(String propertyName, String propertyContents) {
162 NodeRevisionDescriptor d = getDescriptor();
163 if (d == null)
164 return;
165 d.setProperty(propertyName, propertyContents,
166 SlideService.BLOG_NAMESPACE);
167 try {
168 token.getContentHelper().store(slideToken, getPath(), d, null);
169 } catch (SlideException ignored) {}
170 }
171
172 public boolean isDirectory() {
173 return WebdavUtils.isCollection(getDescriptor());
174 }
175 }