001
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
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 defaultLocale = LocalizationUtil.getDefaultLocale(xsd);
071
072 Map<Locale, String> nameMap = new HashMap<Locale, String>();
073
074 nameMap.put(LocaleUtil.fromLanguageId(defaultLocale), typeId);
075
076 ServiceContext serviceContext = new ServiceContext();
077
078 serviceContext.setAddGroupPermissions(true);
079 serviceContext.setAddGuestPermissions(true);
080
081 DDMStructureLocalServiceUtil.addStructure(
082 webDavRequest.getUserId(), webDavRequest.getGroupId(),
083 classNameId, nameMap, null, xsd, serviceContext);
084
085 return HttpServletResponse.SC_CREATED;
086 }
087 else if (type.equals(TYPE_TEMPLATES)) {
088
089
090
091
092 return HttpServletResponse.SC_FORBIDDEN;
093 }
094
095 return HttpServletResponse.SC_FORBIDDEN;
096 }
097
098 public static int deleteResource(
099 WebDAVRequest webDAVRequest, String rootPath, String token,
100 long classNameId)
101 throws WebDAVException {
102
103 try {
104 Resource resource = getResource(
105 webDAVRequest, rootPath, token, classNameId);
106
107 if (resource == null) {
108 return HttpServletResponse.SC_NOT_FOUND;
109 }
110
111 Object model = resource.getModel();
112
113 if (model instanceof DDMStructure) {
114 DDMStructure structure = (DDMStructure)model;
115
116 DDMStructureServiceUtil.deleteStructure(
117 structure.getStructureId());
118
119 return HttpServletResponse.SC_NO_CONTENT;
120 }
121 else if (model instanceof DDMTemplate) {
122 DDMTemplate template = (DDMTemplate)model;
123
124 DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId());
125
126 return HttpServletResponse.SC_NO_CONTENT;
127 }
128 else {
129 return HttpServletResponse.SC_FORBIDDEN;
130 }
131 }
132 catch (PortalException pe) {
133 return HttpServletResponse.SC_FORBIDDEN;
134 }
135 catch (Exception e) {
136 throw new WebDAVException(e);
137 }
138 }
139
140 public static Resource getResource(
141 WebDAVRequest webDAVRequest, String rootPath, String token,
142 long classNameId)
143 throws WebDAVException {
144
145 try {
146 String[] pathArray = webDAVRequest.getPathArray();
147
148 if (pathArray.length == 2) {
149 String path = rootPath + webDAVRequest.getPath();
150
151 return new BaseResourceImpl(path, StringPool.BLANK, token);
152 }
153 else if (pathArray.length == 3) {
154 String type = pathArray[2];
155
156 return toResource(webDAVRequest, type, rootPath, false);
157 }
158 else if (pathArray.length == 4) {
159 String type = pathArray[2];
160 String typeId = pathArray[3];
161
162 if (type.equals(TYPE_STRUCTURES)) {
163 try {
164 DDMStructure structure = null;
165
166 try {
167 structure =
168 DDMStructureLocalServiceUtil.getStructure(
169 GetterUtil.getLong(typeId));
170 }
171 catch (NumberFormatException nfe) {
172 structure =
173 DDMStructureLocalServiceUtil.getStructure(
174 webDAVRequest.getGroupId(), classNameId,
175 typeId);
176 }
177
178 return DDMWebDavUtil.toResource(
179 webDAVRequest, structure, rootPath, false);
180 }
181 catch (NoSuchStructureException nsse) {
182 return null;
183 }
184 }
185 else if (type.equals(TYPE_TEMPLATES)) {
186 try {
187 DDMTemplate template = null;
188
189 try {
190 template = DDMTemplateLocalServiceUtil.getTemplate(
191 GetterUtil.getLong(typeId));
192 }
193 catch (NumberFormatException nfe) {
194 template = DDMTemplateLocalServiceUtil.getTemplate(
195 webDAVRequest.getGroupId(), classNameId,
196 typeId);
197 }
198
199 return DDMWebDavUtil.toResource(
200 webDAVRequest, template, rootPath, false);
201 }
202 catch (NoSuchTemplateException nste) {
203 return null;
204 }
205 }
206 }
207
208 return null;
209 }
210 catch (Exception e) {
211 throw new WebDAVException(e);
212 }
213 }
214
215 public static int putResource(
216 WebDAVRequest webDAVRequest, String rootPath, String token,
217 long classNameId)
218 throws WebDAVException {
219
220 try {
221 Resource resource = getResource(
222 webDAVRequest, rootPath, token, classNameId);
223
224 if (resource == null) {
225 return addResource(webDAVRequest, classNameId);
226 }
227
228 Object model = resource.getModel();
229
230 if (model instanceof DDMStructure) {
231 DDMStructure structure = (DDMStructure)model;
232
233 HttpServletRequest request =
234 webDAVRequest.getHttpServletRequest();
235
236 String xsd = StringUtil.read(request.getInputStream());
237
238 DDMStructureServiceUtil.updateStructure(
239 structure.getGroupId(), structure.getParentStructureId(),
240 structure.getClassNameId(), structure.getStructureKey(),
241 structure.getNameMap(), structure.getDescriptionMap(), xsd,
242 new ServiceContext());
243
244 return HttpServletResponse.SC_CREATED;
245 }
246 else if (model instanceof DDMTemplate) {
247 DDMTemplate template = (DDMTemplate)model;
248
249 HttpServletRequest request =
250 webDAVRequest.getHttpServletRequest();
251
252 String script = StringUtil.read(request.getInputStream());
253
254 DDMTemplateServiceUtil.updateTemplate(
255 template.getTemplateId(), template.getNameMap(),
256 template.getDescriptionMap(), template.getType(),
257 template.getMode(), template.getLanguage(), script,
258 template.isCacheable(), template.isSmallImage(),
259 template.getSmallImageURL(), null, new ServiceContext());
260
261 return HttpServletResponse.SC_CREATED;
262 }
263 else {
264 return HttpServletResponse.SC_FORBIDDEN;
265 }
266 }
267 catch (PortalException pe) {
268 return HttpServletResponse.SC_FORBIDDEN;
269 }
270 catch (Exception e) {
271 throw new WebDAVException(e);
272 }
273 }
274
275 public static Resource toResource(
276 WebDAVRequest webDAVRequest, DDMStructure structure, String rootPath,
277 boolean appendPath) {
278
279 String parentPath = rootPath + webDAVRequest.getPath();
280
281 String name = StringPool.BLANK;
282
283 if (appendPath) {
284 name = String.valueOf(structure.getStructureId());
285 }
286
287 return new DDMStructureResourceImpl(structure, parentPath, name);
288 }
289
290 public static Resource toResource(
291 WebDAVRequest webDAVRequest, DDMTemplate template, String rootPath,
292 boolean appendPath) {
293
294 String parentPath = rootPath + webDAVRequest.getPath();
295
296 String name = StringPool.BLANK;
297
298 if (appendPath) {
299 name = String.valueOf(template.getTemplateId());
300 }
301
302 return new DDMTemplateResourceImpl(template, parentPath, name);
303 }
304
305 public static Resource toResource(
306 WebDAVRequest webDAVRequest, String type, String rootPath,
307 boolean appendPath) {
308
309 String parentPath = rootPath + webDAVRequest.getPath();
310
311 String name = StringPool.BLANK;
312
313 if (appendPath) {
314 name = type;
315 }
316
317 Resource resource = new BaseResourceImpl(parentPath, name, type);
318
319 resource.setModel(type);
320
321 return resource;
322 }
323
324 }