001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.webdav;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.webdav.BaseResourceImpl;
027: import com.liferay.portal.webdav.BaseWebDAVStorageImpl;
028: import com.liferay.portal.webdav.Resource;
029: import com.liferay.portal.webdav.Status;
030: import com.liferay.portal.webdav.WebDAVException;
031: import com.liferay.portal.webdav.WebDAVRequest;
032: import com.liferay.portlet.journal.NoSuchStructureException;
033: import com.liferay.portlet.journal.NoSuchTemplateException;
034: import com.liferay.portlet.journal.model.JournalStructure;
035: import com.liferay.portlet.journal.model.JournalTemplate;
036: import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
037: import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
038: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
039: import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
040:
041: import java.io.File;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: /**
051: * <a href="JournalWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class JournalWebDAVStorageImpl extends BaseWebDAVStorageImpl {
057:
058: public Status addCollection(WebDAVRequest webDavReq)
059: throws WebDAVException {
060:
061: return new Status(HttpServletResponse.SC_FORBIDDEN);
062: }
063:
064: public int copyCollectionResource(WebDAVRequest webDavReq,
065: Resource resource, String destination, boolean overwrite,
066: long depth) throws WebDAVException {
067:
068: return HttpServletResponse.SC_FORBIDDEN;
069: }
070:
071: public int copySimpleResource(WebDAVRequest webDavReq,
072: Resource resource, String destination, boolean overwrite)
073: throws WebDAVException {
074:
075: return HttpServletResponse.SC_FORBIDDEN;
076: }
077:
078: public int deleteResource(WebDAVRequest webDavReq)
079: throws WebDAVException {
080: try {
081: Resource resource = getResource(webDavReq);
082:
083: if (resource == null) {
084: return HttpServletResponse.SC_NOT_FOUND;
085: }
086:
087: Object model = resource.getModel();
088:
089: if (model instanceof JournalStructure) {
090: JournalStructure structure = (JournalStructure) model;
091:
092: JournalStructureServiceUtil.deleteStructure(structure
093: .getGroupId(), structure.getStructureId());
094:
095: return HttpServletResponse.SC_NO_CONTENT;
096: } else if (model instanceof JournalTemplate) {
097: JournalTemplate template = (JournalTemplate) model;
098:
099: JournalTemplateServiceUtil.deleteTemplate(template
100: .getGroupId(), template.getTemplateId());
101:
102: return HttpServletResponse.SC_NO_CONTENT;
103: } else {
104: return HttpServletResponse.SC_FORBIDDEN;
105: }
106: } catch (PortalException pe) {
107: return HttpServletResponse.SC_FORBIDDEN;
108: } catch (Exception e) {
109: throw new WebDAVException(e);
110: }
111: }
112:
113: public Resource getResource(WebDAVRequest webDavReq)
114: throws WebDAVException {
115:
116: try {
117: String[] pathArray = webDavReq.getPathArray();
118:
119: if (pathArray.length == 3) {
120: //String companyId = pathArray[0];
121: //String groupId = pathArray[1];
122: String type = pathArray[2];
123:
124: return toResource(webDavReq, type, false);
125: } else if (pathArray.length == 4) {
126: //String companyId = pathArray[0];
127: //String groupId = pathArray[1];
128: String type = pathArray[2];
129: String journalTypeId = pathArray[3];
130:
131: if (type.equals(_TYPE_STRUCTURES)) {
132: try {
133: JournalStructure journalStructure = JournalStructureLocalServiceUtil
134: .getStructure(webDavReq.getGroupId(),
135: journalTypeId);
136:
137: return toResource(webDavReq, journalStructure,
138: false);
139: } catch (NoSuchStructureException nsse) {
140: return null;
141: }
142: } else if (type.equals(_TYPE_TEMPLATES)) {
143: try {
144: JournalTemplate journalTemplate = JournalTemplateLocalServiceUtil
145: .getTemplate(webDavReq.getGroupId(),
146: journalTypeId);
147:
148: return toResource(webDavReq, journalTemplate,
149: false);
150: } catch (NoSuchTemplateException nste) {
151: return null;
152: }
153: }
154: }
155:
156: return null;
157: } catch (Exception e) {
158: throw new WebDAVException(e);
159: }
160: }
161:
162: public List getResources(WebDAVRequest webDavReq)
163: throws WebDAVException {
164:
165: try {
166: String[] pathArray = webDavReq.getPathArray();
167:
168: if (pathArray.length == 2) {
169: return getFolders(webDavReq);
170: } else if (pathArray.length == 3) {
171: String type = pathArray[2];
172:
173: if (type.equals(_TYPE_STRUCTURES)) {
174: return getStructures(webDavReq);
175: } else if (type.equals(_TYPE_TEMPLATES)) {
176: return getTemplates(webDavReq);
177: }
178: }
179:
180: return new ArrayList();
181: } catch (Exception e) {
182: throw new WebDAVException(e);
183: }
184: }
185:
186: public int moveCollectionResource(WebDAVRequest webDavReq,
187: Resource resource, String destination, boolean overwrite)
188: throws WebDAVException {
189:
190: return HttpServletResponse.SC_FORBIDDEN;
191: }
192:
193: public int moveSimpleResource(WebDAVRequest webDavReq,
194: Resource resource, String destination, boolean overwrite)
195: throws WebDAVException {
196:
197: return HttpServletResponse.SC_FORBIDDEN;
198: }
199:
200: public int putResource(WebDAVRequest webDavReq)
201: throws WebDAVException {
202: try {
203: Resource resource = getResource(webDavReq);
204:
205: if (resource == null) {
206: return HttpServletResponse.SC_NOT_FOUND;
207: }
208:
209: Object model = resource.getModel();
210:
211: if (model instanceof JournalStructure) {
212: JournalStructure structure = (JournalStructure) model;
213:
214: HttpServletRequest req = webDavReq
215: .getHttpServletRequest();
216:
217: String xsd = StringUtil.read(req.getInputStream());
218:
219: JournalStructureServiceUtil.updateStructure(structure
220: .getGroupId(), structure.getStructureId(),
221: structure.getName(),
222: structure.getDescription(), xsd);
223:
224: return HttpServletResponse.SC_CREATED;
225: } else if (model instanceof JournalTemplate) {
226: JournalTemplate template = (JournalTemplate) model;
227:
228: HttpServletRequest req = webDavReq
229: .getHttpServletRequest();
230:
231: String xsl = StringUtil.read(req.getInputStream());
232: boolean formatXsl = true;
233: File smallFile = null;
234:
235: JournalTemplateServiceUtil.updateTemplate(template
236: .getGroupId(), template.getTemplateId(),
237: template.getStructureId(), template.getName(),
238: template.getDescription(), xsl, formatXsl,
239: template.getLangType(), template.isCacheable(),
240: template.isSmallImage(), template
241: .getSmallImageURL(), smallFile);
242:
243: return HttpServletResponse.SC_CREATED;
244: } else {
245: return HttpServletResponse.SC_FORBIDDEN;
246: }
247: } catch (PortalException pe) {
248: return HttpServletResponse.SC_FORBIDDEN;
249: } catch (Exception e) {
250: throw new WebDAVException(e);
251: }
252: }
253:
254: protected List getFolders(WebDAVRequest webDavReq) throws Exception {
255: List folders = new ArrayList();
256:
257: //folders.add(toResource(webDavReq, _TYPE_ARTICLES, true));
258: folders.add(toResource(webDavReq, _TYPE_STRUCTURES, true));
259: folders.add(toResource(webDavReq, _TYPE_TEMPLATES, true));
260:
261: return folders;
262: }
263:
264: protected List getStructures(WebDAVRequest webDavReq)
265: throws Exception {
266: List templates = new ArrayList();
267:
268: Iterator itr = JournalStructureLocalServiceUtil.getStructures(
269: webDavReq.getGroupId()).iterator();
270:
271: while (itr.hasNext()) {
272: JournalStructure structure = (JournalStructure) itr.next();
273:
274: Resource resource = toResource(webDavReq, structure, true);
275:
276: templates.add(resource);
277: }
278:
279: return templates;
280: }
281:
282: protected List getTemplates(WebDAVRequest webDavReq)
283: throws Exception {
284: List templates = new ArrayList();
285:
286: Iterator itr = JournalTemplateLocalServiceUtil.getTemplates(
287: webDavReq.getGroupId()).iterator();
288:
289: while (itr.hasNext()) {
290: JournalTemplate template = (JournalTemplate) itr.next();
291:
292: Resource resource = toResource(webDavReq, template, true);
293:
294: templates.add(resource);
295: }
296:
297: return templates;
298: }
299:
300: protected Resource toResource(WebDAVRequest webDavReq, String type,
301: boolean appendPath) {
302:
303: String parentPath = getRootPath() + webDavReq.getPath();
304: String name = StringPool.BLANK;
305:
306: if (appendPath) {
307: name = type;
308: }
309:
310: Resource resource = new BaseResourceImpl(parentPath, name, type);
311:
312: resource.setModel(type);
313:
314: return resource;
315: }
316:
317: protected Resource toResource(WebDAVRequest webDavReq,
318: JournalStructure structure, boolean appendPath) {
319:
320: String parentPath = getRootPath() + webDavReq.getPath();
321: String name = StringPool.BLANK;
322:
323: if (appendPath) {
324: name = structure.getStructureId();
325: }
326:
327: return new JournalStructureResourceImpl(structure, parentPath,
328: name);
329: }
330:
331: protected Resource toResource(WebDAVRequest webDavReq,
332: JournalTemplate template, boolean appendPath) {
333:
334: String parentPath = getRootPath() + webDavReq.getPath();
335: String name = StringPool.BLANK;
336:
337: if (appendPath) {
338: name = template.getTemplateId();
339: }
340:
341: return new JournalTemplateResourceImpl(template, parentPath,
342: name);
343: }
344:
345: //private static final String _TYPE_ARTICLES = "Articles";
346:
347: private static final String _TYPE_STRUCTURES = "Structures";
348:
349: private static final String _TYPE_TEMPLATES = "Templates";
350:
351: }
|