001
014
015 package com.liferay.portlet.dynamicdatamapping.webdav;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
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.io.DDMFormXSDDeserializerUtil;
029 import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
030 import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayout;
031 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
033 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
034 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
035 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
036 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
037 import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
038 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
039
040 import java.util.HashMap;
041 import java.util.Locale;
042 import java.util.Map;
043
044 import javax.servlet.http.HttpServletRequest;
045 import javax.servlet.http.HttpServletResponse;
046
047
050 public class DDMWebDavUtil {
051
052 public static final String TYPE_STRUCTURES = "Structures";
053
054 public static final String TYPE_TEMPLATES = "Templates";
055
056 public static int addResource(WebDAVRequest webDavRequest, long classNameId)
057 throws Exception {
058
059 String[] pathArray = webDavRequest.getPathArray();
060
061 if (pathArray.length != 4) {
062 return HttpServletResponse.SC_FORBIDDEN;
063 }
064
065 String type = pathArray[2];
066 String typeId = pathArray[3];
067
068 if (type.equals(TYPE_STRUCTURES)) {
069 HttpServletRequest request = webDavRequest.getHttpServletRequest();
070
071 String definition = StringUtil.read(request.getInputStream());
072
073 DDMForm ddmForm = getDDMForm(definition);
074
075 DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(
076 ddmForm);
077
078 Map<Locale, String> nameMap = new HashMap<>();
079
080 Locale defaultLocale = ddmForm.getDefaultLocale();
081
082 nameMap.put(defaultLocale, typeId);
083
084 ServiceContext serviceContext = new ServiceContext();
085
086 serviceContext.setAddGroupPermissions(true);
087 serviceContext.setAddGuestPermissions(true);
088
089 DDMStructureLocalServiceUtil.addStructure(
090 webDavRequest.getUserId(), webDavRequest.getGroupId(),
091 classNameId, nameMap, null, ddmForm, ddmFormLayout,
092 serviceContext);
093
094 return HttpServletResponse.SC_CREATED;
095 }
096 else if (type.equals(TYPE_TEMPLATES)) {
097
098
099
100
101 return HttpServletResponse.SC_FORBIDDEN;
102 }
103
104 return HttpServletResponse.SC_FORBIDDEN;
105 }
106
107 public static int deleteResource(
108 WebDAVRequest webDAVRequest, String rootPath, String token,
109 long classNameId)
110 throws WebDAVException {
111
112 try {
113 Resource resource = getResource(
114 webDAVRequest, rootPath, token, classNameId);
115
116 if (resource == null) {
117 return HttpServletResponse.SC_NOT_FOUND;
118 }
119
120 Object model = resource.getModel();
121
122 if (model instanceof DDMStructure) {
123 DDMStructure structure = (DDMStructure)model;
124
125 DDMStructureServiceUtil.deleteStructure(
126 structure.getStructureId());
127
128 return HttpServletResponse.SC_NO_CONTENT;
129 }
130 else if (model instanceof DDMTemplate) {
131 DDMTemplate template = (DDMTemplate)model;
132
133 DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId());
134
135 return HttpServletResponse.SC_NO_CONTENT;
136 }
137 else {
138 return HttpServletResponse.SC_FORBIDDEN;
139 }
140 }
141 catch (PortalException pe) {
142 if (_log.isDebugEnabled()) {
143 _log.debug(pe, pe);
144 }
145
146 return HttpServletResponse.SC_FORBIDDEN;
147 }
148 catch (Exception e) {
149 throw new WebDAVException(e);
150 }
151 }
152
153 public static Resource getResource(
154 WebDAVRequest webDAVRequest, String rootPath, String token,
155 long classNameId)
156 throws WebDAVException {
157
158 try {
159 String[] pathArray = webDAVRequest.getPathArray();
160
161 if (pathArray.length == 2) {
162 String path = rootPath + webDAVRequest.getPath();
163
164 return new BaseResourceImpl(path, StringPool.BLANK, token);
165 }
166 else if (pathArray.length == 3) {
167 String type = pathArray[2];
168
169 return toResource(webDAVRequest, type, rootPath, false);
170 }
171 else if (pathArray.length == 4) {
172 String type = pathArray[2];
173 String typeId = pathArray[3];
174
175 if (type.equals(TYPE_STRUCTURES)) {
176 DDMStructure structure =
177 DDMStructureLocalServiceUtil.fetchStructure(
178 GetterUtil.getLong(typeId));
179
180 if (structure == null) {
181 structure = DDMStructureLocalServiceUtil.fetchStructure(
182 webDAVRequest.getGroupId(), classNameId, typeId);
183 }
184
185 if (structure == null) {
186 return null;
187 }
188
189 return DDMWebDavUtil.toResource(
190 webDAVRequest, structure, rootPath, false);
191 }
192 else if (type.equals(TYPE_TEMPLATES)) {
193 DDMTemplate template =
194 DDMTemplateLocalServiceUtil.fetchDDMTemplate(
195 GetterUtil.getLong(typeId));
196
197 if (template == null) {
198 template = DDMTemplateLocalServiceUtil.fetchTemplate(
199 webDAVRequest.getGroupId(), classNameId, typeId);
200 }
201
202 if (template == null) {
203 return null;
204 }
205
206 return DDMWebDavUtil.toResource(
207 webDAVRequest, template, rootPath, false);
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 DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(
244 ddmForm);
245
246 DDMStructureServiceUtil.updateStructure(
247 structure.getGroupId(), structure.getParentStructureId(),
248 structure.getClassNameId(), structure.getStructureKey(),
249 structure.getNameMap(), structure.getDescriptionMap(),
250 ddmForm, ddmFormLayout, new ServiceContext());
251
252 return HttpServletResponse.SC_CREATED;
253 }
254 else if (model instanceof DDMTemplate) {
255 DDMTemplate template = (DDMTemplate)model;
256
257 HttpServletRequest request =
258 webDAVRequest.getHttpServletRequest();
259
260 String script = StringUtil.read(request.getInputStream());
261
262 DDMTemplateServiceUtil.updateTemplate(
263 template.getTemplateId(), template.getClassPK(),
264 template.getNameMap(), template.getDescriptionMap(),
265 template.getType(), template.getMode(),
266 template.getLanguage(), script, template.isCacheable(),
267 template.isSmallImage(), template.getSmallImageURL(), null,
268 new ServiceContext());
269
270 return HttpServletResponse.SC_CREATED;
271 }
272 else {
273 return HttpServletResponse.SC_FORBIDDEN;
274 }
275 }
276 catch (PortalException pe) {
277 if (_log.isDebugEnabled()) {
278 _log.debug(pe, pe);
279 }
280
281 return HttpServletResponse.SC_FORBIDDEN;
282 }
283 catch (Exception e) {
284 throw new WebDAVException(e);
285 }
286 }
287
288 public static Resource toResource(
289 WebDAVRequest webDAVRequest, DDMStructure structure, String rootPath,
290 boolean appendPath) {
291
292 String parentPath = rootPath + webDAVRequest.getPath();
293
294 String name = StringPool.BLANK;
295
296 if (appendPath) {
297 name = String.valueOf(structure.getStructureId());
298 }
299
300 return new DDMStructureResourceImpl(structure, parentPath, name);
301 }
302
303 public static Resource toResource(
304 WebDAVRequest webDAVRequest, DDMTemplate template, String rootPath,
305 boolean appendPath) {
306
307 String parentPath = rootPath + webDAVRequest.getPath();
308
309 String name = StringPool.BLANK;
310
311 if (appendPath) {
312 name = String.valueOf(template.getTemplateId());
313 }
314
315 return new DDMTemplateResourceImpl(template, parentPath, name);
316 }
317
318 public static Resource toResource(
319 WebDAVRequest webDAVRequest, String type, String rootPath,
320 boolean appendPath) {
321
322 String parentPath = rootPath + webDAVRequest.getPath();
323
324 String name = StringPool.BLANK;
325
326 if (appendPath) {
327 name = type;
328 }
329
330 Resource resource = new BaseResourceImpl(parentPath, name, type);
331
332 resource.setModel(type);
333
334 return resource;
335 }
336
337 protected static DDMForm getDDMForm(String definition)
338 throws PortalException {
339
340 DDMXMLUtil.validateXML(definition);
341
342 return DDMFormXSDDeserializerUtil.deserialize(definition);
343 }
344
345 private static final Log _log = LogFactoryUtil.getLog(DDMWebDavUtil.class);
346
347 }