001    /**
002     * Copyright (c) 2000-2013 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.LocaleUtil;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
024    import com.liferay.portal.kernel.webdav.Resource;
025    import com.liferay.portal.kernel.webdav.WebDAVException;
026    import com.liferay.portal.kernel.webdav.WebDAVRequest;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
029    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
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    
037    import java.util.HashMap;
038    import java.util.Locale;
039    import java.util.Map;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    /**
045     * @author Juan Fern??ndez
046     */
047    public class DDMWebDavUtil {
048    
049            public static final String TYPE_STRUCTURES = "Structures";
050    
051            public static final String TYPE_TEMPLATES = "Templates";
052    
053            public static int addResource(WebDAVRequest webDavRequest, long classNameId)
054                    throws Exception {
055    
056                    String[] pathArray = webDavRequest.getPathArray();
057    
058                    if (pathArray.length != 4) {
059                            return HttpServletResponse.SC_FORBIDDEN;
060                    }
061    
062                    String type = pathArray[2];
063                    String typeId = pathArray[3];
064    
065                    if (type.equals(TYPE_STRUCTURES)) {
066                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
067    
068                            String xsd = StringUtil.read(request.getInputStream());
069    
070                            String defaultLanguageId = LocalizationUtil.getDefaultLanguageId(
071                                    xsd);
072    
073                            Map<Locale, String> nameMap = new HashMap<Locale, String>();
074    
075                            nameMap.put(LocaleUtil.fromLanguageId(defaultLanguageId), typeId);
076    
077                            ServiceContext serviceContext = new ServiceContext();
078    
079                            serviceContext.setAddGroupPermissions(true);
080                            serviceContext.setAddGuestPermissions(true);
081    
082                            DDMStructureLocalServiceUtil.addStructure(
083                                    webDavRequest.getUserId(), webDavRequest.getGroupId(),
084                                    classNameId, nameMap, null, xsd, serviceContext);
085    
086                            return HttpServletResponse.SC_CREATED;
087                    }
088                    else if (type.equals(TYPE_TEMPLATES)) {
089    
090                            // DDM templates can not be added via WebDAV because there is no way
091                            // to know the associated class name or class PK
092    
093                            return HttpServletResponse.SC_FORBIDDEN;
094                    }
095    
096                    return HttpServletResponse.SC_FORBIDDEN;
097            }
098    
099            public static int deleteResource(
100                            WebDAVRequest webDAVRequest, String rootPath, String token,
101                            long classNameId)
102                    throws WebDAVException {
103    
104                    try {
105                            Resource resource = getResource(
106                                    webDAVRequest, rootPath, token, classNameId);
107    
108                            if (resource == null) {
109                                    return HttpServletResponse.SC_NOT_FOUND;
110                            }
111    
112                            Object model = resource.getModel();
113    
114                            if (model instanceof DDMStructure) {
115                                    DDMStructure structure = (DDMStructure)model;
116    
117                                    DDMStructureServiceUtil.deleteStructure(
118                                            structure.getStructureId());
119    
120                                    return HttpServletResponse.SC_NO_CONTENT;
121                            }
122                            else if (model instanceof DDMTemplate) {
123                                    DDMTemplate template = (DDMTemplate)model;
124    
125                                    DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId());
126    
127                                    return HttpServletResponse.SC_NO_CONTENT;
128                            }
129                            else {
130                                    return HttpServletResponse.SC_FORBIDDEN;
131                            }
132                    }
133                    catch (PortalException pe) {
134                            return HttpServletResponse.SC_FORBIDDEN;
135                    }
136                    catch (Exception e) {
137                            throw new WebDAVException(e);
138                    }
139            }
140    
141            public static Resource getResource(
142                            WebDAVRequest webDAVRequest, String rootPath, String token,
143                            long classNameId)
144                    throws WebDAVException {
145    
146                    try {
147                            String[] pathArray = webDAVRequest.getPathArray();
148    
149                            if (pathArray.length == 2) {
150                                    String path = rootPath + webDAVRequest.getPath();
151    
152                                    return new BaseResourceImpl(path, StringPool.BLANK, token);
153                            }
154                            else if (pathArray.length == 3) {
155                                    String type = pathArray[2];
156    
157                                    return toResource(webDAVRequest, type, rootPath, false);
158                            }
159                            else if (pathArray.length == 4) {
160                                    String type = pathArray[2];
161                                    String typeId = pathArray[3];
162    
163                                    if (type.equals(TYPE_STRUCTURES)) {
164                                            try {
165                                                    DDMStructure structure = null;
166    
167                                                    try {
168                                                            structure =
169                                                                    DDMStructureLocalServiceUtil.getStructure(
170                                                                            GetterUtil.getLong(typeId));
171                                                    }
172                                                    catch (NumberFormatException nfe) {
173                                                            structure =
174                                                                    DDMStructureLocalServiceUtil.getStructure(
175                                                                            webDAVRequest.getGroupId(), classNameId,
176                                                                            typeId);
177                                                    }
178    
179                                                    return DDMWebDavUtil.toResource(
180                                                            webDAVRequest, structure, rootPath, false);
181                                            }
182                                            catch (NoSuchStructureException nsse) {
183                                                    return null;
184                                            }
185                                    }
186                                    else if (type.equals(TYPE_TEMPLATES)) {
187                                            try {
188                                                    DDMTemplate template = null;
189    
190                                                    try {
191                                                            template = DDMTemplateLocalServiceUtil.getTemplate(
192                                                                    GetterUtil.getLong(typeId));
193                                                    }
194                                                    catch (NumberFormatException nfe) {
195                                                            template = DDMTemplateLocalServiceUtil.getTemplate(
196                                                                    webDAVRequest.getGroupId(), classNameId,
197                                                                    typeId);
198                                                    }
199    
200                                                    return DDMWebDavUtil.toResource(
201                                                            webDAVRequest, template, rootPath, false);
202                                            }
203                                            catch (NoSuchTemplateException nste) {
204                                                    return null;
205                                            }
206                                    }
207                            }
208    
209                            return null;
210                    }
211                    catch (Exception e) {
212                            throw new WebDAVException(e);
213                    }
214            }
215    
216            public static int putResource(
217                            WebDAVRequest webDAVRequest, String rootPath, String token,
218                            long classNameId)
219                    throws WebDAVException {
220    
221                    try {
222                            Resource resource = getResource(
223                                    webDAVRequest, rootPath, token, classNameId);
224    
225                            if (resource == null) {
226                                    return addResource(webDAVRequest, classNameId);
227                            }
228    
229                            Object model = resource.getModel();
230    
231                            if (model instanceof DDMStructure) {
232                                    DDMStructure structure = (DDMStructure)model;
233    
234                                    HttpServletRequest request =
235                                            webDAVRequest.getHttpServletRequest();
236    
237                                    String xsd = StringUtil.read(request.getInputStream());
238    
239                                    DDMStructureServiceUtil.updateStructure(
240                                            structure.getGroupId(), structure.getParentStructureId(),
241                                            structure.getClassNameId(), structure.getStructureKey(),
242                                            structure.getNameMap(), structure.getDescriptionMap(), xsd,
243                                            new ServiceContext());
244    
245                                    return HttpServletResponse.SC_CREATED;
246                            }
247                            else if (model instanceof DDMTemplate) {
248                                    DDMTemplate template = (DDMTemplate)model;
249    
250                                    HttpServletRequest request =
251                                            webDAVRequest.getHttpServletRequest();
252    
253                                    String script = StringUtil.read(request.getInputStream());
254    
255                                    DDMTemplateServiceUtil.updateTemplate(
256                                            template.getTemplateId(), template.getClassPK(),
257                                            template.getNameMap(), template.getDescriptionMap(),
258                                            template.getType(), template.getMode(),
259                                            template.getLanguage(), script, template.isCacheable(),
260                                            template.isSmallImage(), template.getSmallImageURL(), null,
261                                            new ServiceContext());
262    
263                                    return HttpServletResponse.SC_CREATED;
264                            }
265                            else {
266                                    return HttpServletResponse.SC_FORBIDDEN;
267                            }
268                    }
269                    catch (PortalException pe) {
270                            return HttpServletResponse.SC_FORBIDDEN;
271                    }
272                    catch (Exception e) {
273                            throw new WebDAVException(e);
274                    }
275            }
276    
277            public static Resource toResource(
278                    WebDAVRequest webDAVRequest, DDMStructure structure, String rootPath,
279                    boolean appendPath) {
280    
281                    String parentPath = rootPath + webDAVRequest.getPath();
282    
283                    String name = StringPool.BLANK;
284    
285                    if (appendPath) {
286                            name = String.valueOf(structure.getStructureId());
287                    }
288    
289                    return new DDMStructureResourceImpl(structure, parentPath, name);
290            }
291    
292            public static Resource toResource(
293                    WebDAVRequest webDAVRequest, DDMTemplate template, String rootPath,
294                    boolean appendPath) {
295    
296                    String parentPath = rootPath + webDAVRequest.getPath();
297    
298                    String name = StringPool.BLANK;
299    
300                    if (appendPath) {
301                            name = String.valueOf(template.getTemplateId());
302                    }
303    
304                    return new DDMTemplateResourceImpl(template, parentPath, name);
305            }
306    
307            public static Resource toResource(
308                    WebDAVRequest webDAVRequest, String type, String rootPath,
309                    boolean appendPath) {
310    
311                    String parentPath = rootPath + webDAVRequest.getPath();
312    
313                    String name = StringPool.BLANK;
314    
315                    if (appendPath) {
316                            name = type;
317                    }
318    
319                    Resource resource = new BaseResourceImpl(parentPath, name, type);
320    
321                    resource.setModel(type);
322    
323                    return resource;
324            }
325    
326    }