001
014
015 package com.liferay.portlet.journal.webdav;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.webdav.BaseResourceImpl;
021 import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
022 import com.liferay.portal.kernel.webdav.Resource;
023 import com.liferay.portal.kernel.webdav.WebDAVException;
024 import com.liferay.portal.kernel.webdav.WebDAVRequest;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portlet.journal.NoSuchStructureException;
027 import com.liferay.portlet.journal.NoSuchTemplateException;
028 import com.liferay.portlet.journal.model.JournalStructure;
029 import com.liferay.portlet.journal.model.JournalTemplate;
030 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
031 import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
032 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
033 import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
034
035 import java.io.File;
036
037 import java.util.ArrayList;
038 import java.util.List;
039
040 import javax.servlet.http.HttpServletRequest;
041 import javax.servlet.http.HttpServletResponse;
042
043
047 public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
048
049 @Override
050 public int deleteResource(WebDAVRequest webDavRequest)
051 throws WebDAVException {
052
053 try {
054 Resource resource = getResource(webDavRequest);
055
056 if (resource == null) {
057 return HttpServletResponse.SC_NOT_FOUND;
058 }
059
060 Object model = resource.getModel();
061
062 if (model instanceof JournalStructure) {
063 JournalStructure structure = (JournalStructure)model;
064
065 JournalStructureServiceUtil.deleteStructure(
066 structure.getGroupId(), structure.getStructureId());
067
068 return HttpServletResponse.SC_NO_CONTENT;
069 }
070 else if (model instanceof JournalTemplate) {
071 JournalTemplate template = (JournalTemplate)model;
072
073 JournalTemplateServiceUtil.deleteTemplate(
074 template.getGroupId(), template.getTemplateId());
075
076 return HttpServletResponse.SC_NO_CONTENT;
077 }
078 else {
079 return HttpServletResponse.SC_FORBIDDEN;
080 }
081 }
082 catch (PortalException pe) {
083 return HttpServletResponse.SC_FORBIDDEN;
084 }
085 catch (Exception e) {
086 throw new WebDAVException(e);
087 }
088 }
089
090 public Resource getResource(WebDAVRequest webDavRequest)
091 throws WebDAVException {
092
093 try {
094 String[] pathArray = webDavRequest.getPathArray();
095
096 if (pathArray.length == 2) {
097 String path = getRootPath() + webDavRequest.getPath();
098
099 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
100 }
101 else if (pathArray.length == 3) {
102 String type = pathArray[2];
103
104 return toResource(webDavRequest, type, false);
105 }
106 else if (pathArray.length == 4) {
107 String type = pathArray[2];
108 String journalTypeId = pathArray[3];
109
110 if (type.equals(_TYPE_STRUCTURES)) {
111 try {
112 JournalStructure journalStructure =
113 JournalStructureServiceUtil.getStructure(
114 webDavRequest.getGroupId(), journalTypeId,
115 true);
116
117 return toResource(
118 webDavRequest, journalStructure, false);
119 }
120 catch (NoSuchStructureException nsse) {
121 return null;
122 }
123 }
124 else if (type.equals(_TYPE_TEMPLATES)) {
125 try {
126 JournalTemplate journalTemplate =
127 JournalTemplateServiceUtil.getTemplate(
128 webDavRequest.getGroupId(), journalTypeId,
129 true);
130
131 return toResource(
132 webDavRequest, journalTemplate, false);
133 }
134 catch (NoSuchTemplateException nste) {
135 return null;
136 }
137 }
138 }
139
140 return null;
141 }
142 catch (Exception e) {
143 throw new WebDAVException(e);
144 }
145 }
146
147 public List<Resource> getResources(WebDAVRequest webDavRequest)
148 throws WebDAVException {
149
150 try {
151 String[] pathArray = webDavRequest.getPathArray();
152
153 if (pathArray.length == 2) {
154 return getFolders(webDavRequest);
155 }
156 else if (pathArray.length == 3) {
157 String type = pathArray[2];
158
159 if (type.equals(_TYPE_STRUCTURES)) {
160 return getStructures(webDavRequest);
161 }
162 else if (type.equals(_TYPE_TEMPLATES)) {
163 return getTemplates(webDavRequest);
164 }
165 }
166
167 return new ArrayList<Resource>();
168 }
169 catch (Exception e) {
170 throw new WebDAVException(e);
171 }
172 }
173
174 @Override
175 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
176 try {
177 Resource resource = getResource(webDavRequest);
178
179 if (resource == null) {
180 return HttpServletResponse.SC_NOT_FOUND;
181 }
182
183 ServiceContext serviceContext = new ServiceContext();
184
185 Object model = resource.getModel();
186
187 if (model instanceof JournalStructure) {
188 JournalStructure structure = (JournalStructure)model;
189
190 HttpServletRequest request =
191 webDavRequest.getHttpServletRequest();
192
193 String xsd = StringUtil.read(request.getInputStream());
194
195 JournalStructureServiceUtil.updateStructure(
196 structure.getGroupId(), structure.getStructureId(),
197 structure.getParentStructureId(), structure.getNameMap(),
198 structure.getDescriptionMap(), xsd, serviceContext);
199
200 return HttpServletResponse.SC_CREATED;
201 }
202 else if (model instanceof JournalTemplate) {
203 JournalTemplate template = (JournalTemplate)model;
204
205 HttpServletRequest request =
206 webDavRequest.getHttpServletRequest();
207
208 String xsl = StringUtil.read(request.getInputStream());
209 boolean formatXsl = true;
210 File smallFile = null;
211
212 JournalTemplateServiceUtil.updateTemplate(
213 template.getGroupId(), template.getTemplateId(),
214 template.getStructureId(), template.getNameMap(),
215 template.getDescriptionMap(), xsl, formatXsl,
216 template.getLangType(), template.isCacheable(),
217 template.isSmallImage(), template.getSmallImageURL(),
218 smallFile, serviceContext);
219
220 return HttpServletResponse.SC_CREATED;
221 }
222 else {
223 return HttpServletResponse.SC_FORBIDDEN;
224 }
225 }
226 catch (PortalException pe) {
227 return HttpServletResponse.SC_FORBIDDEN;
228 }
229 catch (Exception e) {
230 throw new WebDAVException(e);
231 }
232 }
233
234 protected List<Resource> getFolders(WebDAVRequest webDavRequest)
235 throws Exception {
236
237 List<Resource> folders = new ArrayList<Resource>();
238
239
240 folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
241 folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
242
243 return folders;
244 }
245
246 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
247 throws Exception {
248
249 List<Resource> resources = new ArrayList<Resource>();
250
251 long groupId = webDavRequest.getGroupId();
252
253 List<JournalStructure> structures =
254 JournalStructureLocalServiceUtil.getStructures(groupId);
255
256 for (JournalStructure structure : structures) {
257 Resource resource = toResource(webDavRequest, structure, true);
258
259 resources.add(resource);
260 }
261
262 return resources;
263 }
264
265 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
266 throws Exception {
267
268 List<Resource> resources = new ArrayList<Resource>();
269
270 long groupId = webDavRequest.getGroupId();
271
272 List<JournalTemplate> templates =
273 JournalTemplateLocalServiceUtil.getTemplates(groupId);
274
275 for (JournalTemplate template : templates) {
276 Resource resource = toResource(webDavRequest, template, true);
277
278 resources.add(resource);
279 }
280
281 return resources;
282 }
283
284 protected Resource toResource(
285 WebDAVRequest webDavRequest, JournalStructure structure,
286 boolean appendPath) {
287
288 String parentPath = getRootPath() + webDavRequest.getPath();
289
290 String name = StringPool.BLANK;
291
292 if (appendPath) {
293 name = structure.getStructureId();
294 }
295
296 return new JournalStructureResourceImpl(structure, parentPath, name);
297 }
298
299 protected Resource toResource(
300 WebDAVRequest webDavRequest, JournalTemplate template,
301 boolean appendPath) {
302
303 String parentPath = getRootPath() + webDavRequest.getPath();
304
305 String name = StringPool.BLANK;
306
307 if (appendPath) {
308 name = template.getTemplateId();
309 }
310
311 return new JournalTemplateResourceImpl(template, parentPath, name);
312 }
313
314 protected Resource toResource(
315 WebDAVRequest webDavRequest, String type, boolean appendPath) {
316
317 String parentPath = getRootPath() + webDavRequest.getPath();
318
319 String name = StringPool.BLANK;
320
321 if (appendPath) {
322 name = type;
323 }
324
325 Resource resource = new BaseResourceImpl(parentPath, name, type);
326
327 resource.setModel(type);
328
329 return resource;
330 }
331
332
333
334 private static final String _TYPE_STRUCTURES = "Structures";
335
336 private static final String _TYPE_TEMPLATES = "Templates";
337
338 }