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