001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.webdav;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
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.dynamicdatamapping.NoSuchStructureException;
027    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
028    import com.liferay.portlet.dynamicdatamapping.io.DDMFormXSDDeserializerUtil;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
032    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
033    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
037    
038    import java.util.HashMap;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    /**
046     * @author Juan Fern??ndez
047     */
048    public class DDMWebDavUtil {
049    
050            public static final String TYPE_STRUCTURES = "Structures";
051    
052            public static final String TYPE_TEMPLATES = "Templates";
053    
054            public static int addResource(WebDAVRequest webDavRequest, long classNameId)
055                    throws Exception {
056    
057                    String[] pathArray = webDavRequest.getPathArray();
058    
059                    if (pathArray.length != 4) {
060                            return HttpServletResponse.SC_FORBIDDEN;
061                    }
062    
063                    String type = pathArray[2];
064                    String typeId = pathArray[3];
065    
066                    if (type.equals(TYPE_STRUCTURES)) {
067                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
068    
069                            String definition = StringUtil.read(request.getInputStream());
070    
071                            DDMForm ddmForm = getDDMForm(definition);
072    
073                            Map<Locale, String> nameMap = new HashMap<Locale, String>();
074    
075                            Locale defaultLocale = ddmForm.getDefaultLocale();
076    
077                            nameMap.put(defaultLocale, typeId);
078    
079                            ServiceContext serviceContext = new ServiceContext();
080    
081                            serviceContext.setAddGroupPermissions(true);
082                            serviceContext.setAddGuestPermissions(true);
083    
084                            DDMStructureLocalServiceUtil.addStructure(
085                                    webDavRequest.getUserId(), webDavRequest.getGroupId(),
086                                    classNameId, nameMap, null, ddmForm, serviceContext);
087    
088                            return HttpServletResponse.SC_CREATED;
089                    }
090                    else if (type.equals(TYPE_TEMPLATES)) {
091    
092                            // DDM templates can not be added via WebDAV because there is no way
093                            // to know the associated class name or class PK
094    
095                            return HttpServletResponse.SC_FORBIDDEN;
096                    }
097    
098                    return HttpServletResponse.SC_FORBIDDEN;
099            }
100    
101            public static int deleteResource(
102                            WebDAVRequest webDAVRequest, String rootPath, String token,
103                            long classNameId)
104                    throws WebDAVException {
105    
106                    try {
107                            Resource resource = getResource(
108                                    webDAVRequest, rootPath, token, classNameId);
109    
110                            if (resource == null) {
111                                    return HttpServletResponse.SC_NOT_FOUND;
112                            }
113    
114                            Object model = resource.getModel();
115    
116                            if (model instanceof DDMStructure) {
117                                    DDMStructure structure = (DDMStructure)model;
118    
119                                    DDMStructureServiceUtil.deleteStructure(
120                                            structure.getStructureId());
121    
122                                    return HttpServletResponse.SC_NO_CONTENT;
123                            }
124                            else if (model instanceof DDMTemplate) {
125                                    DDMTemplate template = (DDMTemplate)model;
126    
127                                    DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId());
128    
129                                    return HttpServletResponse.SC_NO_CONTENT;
130                            }
131                            else {
132                                    return HttpServletResponse.SC_FORBIDDEN;
133                            }
134                    }
135                    catch (PortalException pe) {
136                            return HttpServletResponse.SC_FORBIDDEN;
137                    }
138                    catch (Exception e) {
139                            throw new WebDAVException(e);
140                    }
141            }
142    
143            public static Resource getResource(
144                            WebDAVRequest webDAVRequest, String rootPath, String token,
145                            long classNameId)
146                    throws WebDAVException {
147    
148                    try {
149                            String[] pathArray = webDAVRequest.getPathArray();
150    
151                            if (pathArray.length == 2) {
152                                    String path = rootPath + webDAVRequest.getPath();
153    
154                                    return new BaseResourceImpl(path, StringPool.BLANK, token);
155                            }
156                            else if (pathArray.length == 3) {
157                                    String type = pathArray[2];
158    
159                                    return toResource(webDAVRequest, type, rootPath, false);
160                            }
161                            else if (pathArray.length == 4) {
162                                    String type = pathArray[2];
163                                    String typeId = pathArray[3];
164    
165                                    if (type.equals(TYPE_STRUCTURES)) {
166                                            try {
167                                                    DDMStructure structure = null;
168    
169                                                    try {
170                                                            structure =
171                                                                    DDMStructureLocalServiceUtil.getStructure(
172                                                                            GetterUtil.getLong(typeId));
173                                                    }
174                                                    catch (NumberFormatException nfe) {
175                                                            structure =
176                                                                    DDMStructureLocalServiceUtil.getStructure(
177                                                                            webDAVRequest.getGroupId(), classNameId,
178                                                                            typeId);
179                                                    }
180    
181                                                    return DDMWebDavUtil.toResource(
182                                                            webDAVRequest, structure, rootPath, false);
183                                            }
184                                            catch (NoSuchStructureException nsse) {
185                                                    return null;
186                                            }
187                                    }
188                                    else if (type.equals(TYPE_TEMPLATES)) {
189                                            try {
190                                                    DDMTemplate template = null;
191    
192                                                    try {
193                                                            template = DDMTemplateLocalServiceUtil.getTemplate(
194                                                                    GetterUtil.getLong(typeId));
195                                                    }
196                                                    catch (NumberFormatException nfe) {
197                                                            template = DDMTemplateLocalServiceUtil.getTemplate(
198                                                                    webDAVRequest.getGroupId(), classNameId,
199                                                                    typeId);
200                                                    }
201    
202                                                    return DDMWebDavUtil.toResource(
203                                                            webDAVRequest, template, rootPath, false);
204                                            }
205                                            catch (NoSuchTemplateException nste) {
206                                                    return null;
207                                            }
208                                    }
209                            }
210    
211                            return null;
212                    }
213                    catch (Exception e) {
214                            throw new WebDAVException(e);
215                    }
216            }
217    
218            public static int putResource(
219                            WebDAVRequest webDAVRequest, String rootPath, String token,
220                            long classNameId)
221                    throws WebDAVException {
222    
223                    try {
224                            Resource resource = getResource(
225                                    webDAVRequest, rootPath, token, classNameId);
226    
227                            if (resource == null) {
228                                    return addResource(webDAVRequest, classNameId);
229                            }
230    
231                            Object model = resource.getModel();
232    
233                            if (model instanceof DDMStructure) {
234                                    DDMStructure structure = (DDMStructure)model;
235    
236                                    HttpServletRequest request =
237                                            webDAVRequest.getHttpServletRequest();
238    
239                                    String definition = StringUtil.read(request.getInputStream());
240    
241                                    DDMForm ddmForm = getDDMForm(definition);
242    
243                                    DDMStructureServiceUtil.updateStructure(
244                                            structure.getGroupId(), structure.getParentStructureId(),
245                                            structure.getClassNameId(), structure.getStructureKey(),
246                                            structure.getNameMap(), structure.getDescriptionMap(),
247                                            ddmForm, new ServiceContext());
248    
249                                    return HttpServletResponse.SC_CREATED;
250                            }
251                            else if (model instanceof DDMTemplate) {
252                                    DDMTemplate template = (DDMTemplate)model;
253    
254                                    HttpServletRequest request =
255                                            webDAVRequest.getHttpServletRequest();
256    
257                                    String script = StringUtil.read(request.getInputStream());
258    
259                                    DDMTemplateServiceUtil.updateTemplate(
260                                            template.getTemplateId(), template.getClassPK(),
261                                            template.getNameMap(), template.getDescriptionMap(),
262                                            template.getType(), template.getMode(),
263                                            template.getLanguage(), script, template.isCacheable(),
264                                            template.isSmallImage(), template.getSmallImageURL(), null,
265                                            new ServiceContext());
266    
267                                    return HttpServletResponse.SC_CREATED;
268                            }
269                            else {
270                                    return HttpServletResponse.SC_FORBIDDEN;
271                            }
272                    }
273                    catch (PortalException pe) {
274                            return HttpServletResponse.SC_FORBIDDEN;
275                    }
276                    catch (Exception e) {
277                            throw new WebDAVException(e);
278                    }
279            }
280    
281            public static Resource toResource(
282                    WebDAVRequest webDAVRequest, DDMStructure structure, String rootPath,
283                    boolean appendPath) {
284    
285                    String parentPath = rootPath + webDAVRequest.getPath();
286    
287                    String name = StringPool.BLANK;
288    
289                    if (appendPath) {
290                            name = String.valueOf(structure.getStructureId());
291                    }
292    
293                    return new DDMStructureResourceImpl(structure, parentPath, name);
294            }
295    
296            public static Resource toResource(
297                    WebDAVRequest webDAVRequest, DDMTemplate template, String rootPath,
298                    boolean appendPath) {
299    
300                    String parentPath = rootPath + webDAVRequest.getPath();
301    
302                    String name = StringPool.BLANK;
303    
304                    if (appendPath) {
305                            name = String.valueOf(template.getTemplateId());
306                    }
307    
308                    return new DDMTemplateResourceImpl(template, parentPath, name);
309            }
310    
311            public static Resource toResource(
312                    WebDAVRequest webDAVRequest, String type, String rootPath,
313                    boolean appendPath) {
314    
315                    String parentPath = rootPath + webDAVRequest.getPath();
316    
317                    String name = StringPool.BLANK;
318    
319                    if (appendPath) {
320                            name = type;
321                    }
322    
323                    Resource resource = new BaseResourceImpl(parentPath, name, type);
324    
325                    resource.setModel(type);
326    
327                    return resource;
328            }
329    
330            protected static DDMForm getDDMForm(String definition)
331                    throws PortalException {
332    
333                    DDMXMLUtil.validateXML(definition);
334    
335                    return DDMFormXSDDeserializerUtil.deserialize(definition);
336            }
337    
338    }