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 @Override
091 public Resource getResource(WebDAVRequest webDavRequest)
092 throws WebDAVException {
093
094 try {
095 String[] pathArray = webDavRequest.getPathArray();
096
097 if (pathArray.length == 2) {
098 String path = getRootPath() + webDavRequest.getPath();
099
100 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
101 }
102 else if (pathArray.length == 3) {
103 String type = pathArray[2];
104
105 return toResource(webDavRequest, type, false);
106 }
107 else if (pathArray.length == 4) {
108 String type = pathArray[2];
109 String journalTypeId = pathArray[3];
110
111 if (type.equals(_TYPE_STRUCTURES)) {
112 try {
113 JournalStructure journalStructure =
114 JournalStructureServiceUtil.getStructure(
115 webDavRequest.getGroupId(), journalTypeId,
116 true);
117
118 return toResource(
119 webDavRequest, journalStructure, false);
120 }
121 catch (NoSuchStructureException nsse) {
122 return null;
123 }
124 }
125 else if (type.equals(_TYPE_TEMPLATES)) {
126 try {
127 JournalTemplate journalTemplate =
128 JournalTemplateServiceUtil.getTemplate(
129 webDavRequest.getGroupId(), journalTypeId,
130 true);
131
132 return toResource(
133 webDavRequest, journalTemplate, false);
134 }
135 catch (NoSuchTemplateException nste) {
136 return null;
137 }
138 }
139 }
140
141 return null;
142 }
143 catch (Exception e) {
144 throw new WebDAVException(e);
145 }
146 }
147
148 @Override
149 public List<Resource> getResources(WebDAVRequest webDavRequest)
150 throws WebDAVException {
151
152 try {
153 String[] pathArray = webDavRequest.getPathArray();
154
155 if (pathArray.length == 2) {
156 return getFolders(webDavRequest);
157 }
158 else if (pathArray.length == 3) {
159 String type = pathArray[2];
160
161 if (type.equals(_TYPE_STRUCTURES)) {
162 return getStructures(webDavRequest);
163 }
164 else if (type.equals(_TYPE_TEMPLATES)) {
165 return getTemplates(webDavRequest);
166 }
167 }
168
169 return new ArrayList<Resource>();
170 }
171 catch (Exception e) {
172 throw new WebDAVException(e);
173 }
174 }
175
176 @Override
177 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
178 try {
179 Resource resource = getResource(webDavRequest);
180
181 if (resource == null) {
182 return HttpServletResponse.SC_NOT_FOUND;
183 }
184
185 ServiceContext serviceContext = new ServiceContext();
186
187 Object model = resource.getModel();
188
189 if (model instanceof JournalStructure) {
190 JournalStructure structure = (JournalStructure)model;
191
192 HttpServletRequest request =
193 webDavRequest.getHttpServletRequest();
194
195 String xsd = StringUtil.read(request.getInputStream());
196
197 JournalStructureServiceUtil.updateStructure(
198 structure.getGroupId(), structure.getStructureId(),
199 structure.getParentStructureId(), structure.getNameMap(),
200 structure.getDescriptionMap(), xsd, serviceContext);
201
202 return HttpServletResponse.SC_CREATED;
203 }
204 else if (model instanceof JournalTemplate) {
205 JournalTemplate template = (JournalTemplate)model;
206
207 HttpServletRequest request =
208 webDavRequest.getHttpServletRequest();
209
210 String xsl = StringUtil.read(request.getInputStream());
211 boolean formatXsl = true;
212 File smallFile = null;
213
214 JournalTemplateServiceUtil.updateTemplate(
215 template.getGroupId(), template.getTemplateId(),
216 template.getStructureId(), template.getNameMap(),
217 template.getDescriptionMap(), xsl, formatXsl,
218 template.getLangType(), template.isCacheable(),
219 template.isSmallImage(), template.getSmallImageURL(),
220 smallFile, serviceContext);
221
222 return HttpServletResponse.SC_CREATED;
223 }
224 else {
225 return HttpServletResponse.SC_FORBIDDEN;
226 }
227 }
228 catch (PortalException pe) {
229 return HttpServletResponse.SC_FORBIDDEN;
230 }
231 catch (Exception e) {
232 throw new WebDAVException(e);
233 }
234 }
235
236 protected List<Resource> getFolders(WebDAVRequest webDavRequest)
237 throws Exception {
238
239 List<Resource> folders = new ArrayList<Resource>();
240
241
242 folders.add(toResource(webDavRequest, _TYPE_STRUCTURES, true));
243 folders.add(toResource(webDavRequest, _TYPE_TEMPLATES, true));
244
245 return folders;
246 }
247
248 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
249 throws Exception {
250
251 List<Resource> resources = new ArrayList<Resource>();
252
253 long groupId = webDavRequest.getGroupId();
254
255 List<JournalStructure> structures =
256 JournalStructureLocalServiceUtil.getStructures(groupId);
257
258 for (JournalStructure structure : structures) {
259 Resource resource = toResource(webDavRequest, structure, true);
260
261 resources.add(resource);
262 }
263
264 return resources;
265 }
266
267 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
268 throws Exception {
269
270 List<Resource> resources = new ArrayList<Resource>();
271
272 long groupId = webDavRequest.getGroupId();
273
274 List<JournalTemplate> templates =
275 JournalTemplateLocalServiceUtil.getTemplates(groupId);
276
277 for (JournalTemplate template : templates) {
278 Resource resource = toResource(webDavRequest, template, true);
279
280 resources.add(resource);
281 }
282
283 return resources;
284 }
285
286 protected Resource toResource(
287 WebDAVRequest webDavRequest, JournalStructure structure,
288 boolean appendPath) {
289
290 String parentPath = getRootPath() + webDavRequest.getPath();
291
292 String name = StringPool.BLANK;
293
294 if (appendPath) {
295 name = structure.getStructureId();
296 }
297
298 return new JournalStructureResourceImpl(structure, parentPath, name);
299 }
300
301 protected Resource toResource(
302 WebDAVRequest webDavRequest, JournalTemplate template,
303 boolean appendPath) {
304
305 String parentPath = getRootPath() + webDavRequest.getPath();
306
307 String name = StringPool.BLANK;
308
309 if (appendPath) {
310 name = template.getTemplateId();
311 }
312
313 return new JournalTemplateResourceImpl(template, parentPath, name);
314 }
315
316 protected Resource toResource(
317 WebDAVRequest webDavRequest, String type, boolean appendPath) {
318
319 String parentPath = getRootPath() + webDavRequest.getPath();
320
321 String name = StringPool.BLANK;
322
323 if (appendPath) {
324 name = type;
325 }
326
327 Resource resource = new BaseResourceImpl(parentPath, name, type);
328
329 resource.setModel(type);
330
331 return resource;
332 }
333
334
335
336 private static final String _TYPE_STRUCTURES = "Structures";
337
338 private static final String _TYPE_TEMPLATES = "Templates";
339
340 }