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.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
021 import com.liferay.portal.kernel.webdav.Resource;
022 import com.liferay.portal.kernel.webdav.WebDAVException;
023 import com.liferay.portal.kernel.webdav.WebDAVRequest;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.util.PortalUtil;
026 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
027 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
028 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
029 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
030 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
031 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
032
033 import java.util.ArrayList;
034 import java.util.List;
035
036 import javax.servlet.http.HttpServletRequest;
037 import javax.servlet.http.HttpServletResponse;
038
039
042 public class DDMWebDAVStorageImpl extends BaseWebDAVStorageImpl {
043
044 @Override
045 public int deleteResource(WebDAVRequest webDavRequest)
046 throws WebDAVException {
047
048 try {
049 Resource resource = getResource(webDavRequest);
050
051 if (resource == null) {
052 return HttpServletResponse.SC_NOT_FOUND;
053 }
054
055 Object model = resource.getModel();
056
057 if (model instanceof DDMStructure) {
058 DDMStructure structure = (DDMStructure)model;
059
060 DDMStructureServiceUtil.deleteStructure(
061 structure.getStructureId());
062
063 return HttpServletResponse.SC_NO_CONTENT;
064 }
065 else if (model instanceof DDMTemplate) {
066 DDMTemplate template = (DDMTemplate)model;
067
068 DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId());
069
070 return HttpServletResponse.SC_NO_CONTENT;
071 }
072 else {
073 return HttpServletResponse.SC_FORBIDDEN;
074 }
075 }
076 catch (PortalException pe) {
077 return HttpServletResponse.SC_FORBIDDEN;
078 }
079 catch (Exception e) {
080 throw new WebDAVException(e);
081 }
082 }
083
084 public Resource getResource(WebDAVRequest webDavRequest) {
085 String[] pathArray = webDavRequest.getPathArray();
086
087 if (pathArray.length == 4) {
088 String type = pathArray[2];
089 String typeId = pathArray[3];
090
091 if (type.equals(_TYPE_STRUCTURES)) {
092 try {
093 DDMStructure structure =
094 DDMStructureLocalServiceUtil.getStructure(
095 Long.valueOf(typeId));
096
097 return toResource(webDavRequest, structure, false);
098 }
099 catch (Exception e) {
100 return null;
101 }
102 }
103 else if (type.equals(_TYPE_TEMPLATES)) {
104 try {
105 DDMTemplate template =
106 DDMTemplateLocalServiceUtil.getTemplate(
107 Long.valueOf(typeId));
108
109 return toResource(webDavRequest, template, false);
110 }
111 catch (Exception e) {
112 return null;
113 }
114 }
115 }
116
117 return null;
118 }
119
120 public List<Resource> getResources(WebDAVRequest webDavRequest)
121 throws WebDAVException {
122
123 try {
124 String[] pathArray = webDavRequest.getPathArray();
125
126 if (pathArray.length == 3) {
127 String type = pathArray[2];
128
129 if (type.equals(_TYPE_STRUCTURES)) {
130 return getStructures(webDavRequest);
131 }
132 else if (type.equals(_TYPE_TEMPLATES)) {
133 return getTemplates(webDavRequest);
134 }
135 }
136
137 return new ArrayList<Resource>();
138 }
139 catch (Exception e) {
140 throw new WebDAVException(e);
141 }
142 }
143
144 @Override
145 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
146 try {
147 Resource resource = getResource(webDavRequest);
148
149 if (resource == null) {
150 return HttpServletResponse.SC_NOT_FOUND;
151 }
152
153 Object model = resource.getModel();
154
155 if (model instanceof DDMStructure) {
156 DDMStructure structure = (DDMStructure)model;
157
158 HttpServletRequest request =
159 webDavRequest.getHttpServletRequest();
160
161 String xsd = StringUtil.read(request.getInputStream());
162
163 DDMStructureServiceUtil.updateStructure(
164 structure.getStructureId(),
165 structure.getParentStructureId(), structure.getNameMap(),
166 structure.getDescriptionMap(), xsd, new ServiceContext());
167
168 return HttpServletResponse.SC_CREATED;
169 }
170 else if (model instanceof DDMTemplate) {
171 DDMTemplate template = (DDMTemplate)model;
172
173 HttpServletRequest request =
174 webDavRequest.getHttpServletRequest();
175
176 String script = StringUtil.read(request.getInputStream());
177
178 DDMTemplateServiceUtil.updateTemplate(
179 template.getTemplateId(), template.getNameMap(),
180 template.getDescriptionMap(), template.getType(),
181 template.getMode(), template.getLanguage(), script,
182 template.isCacheable(), template.isSmallImage(),
183 template.getSmallImageURL(), null, new ServiceContext());
184
185 return HttpServletResponse.SC_CREATED;
186 }
187 else {
188 return HttpServletResponse.SC_FORBIDDEN;
189 }
190 }
191 catch (PortalException pe) {
192 return HttpServletResponse.SC_FORBIDDEN;
193 }
194 catch (Exception e) {
195 throw new WebDAVException(e);
196 }
197 }
198
199 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
200 throws Exception {
201
202 List<Resource> resources = new ArrayList<Resource>();
203
204 long groupId = webDavRequest.getGroupId();
205
206 List<DDMStructure> structures =
207 DDMStructureLocalServiceUtil.getStructures(groupId);
208
209 for (DDMStructure structure : structures) {
210 Resource resource = toResource(webDavRequest, structure, true);
211
212 resources.add(resource);
213 }
214
215 return resources;
216 }
217
218 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
219 throws Exception {
220
221 List<Resource> resources = new ArrayList<Resource>();
222
223 long groupId = webDavRequest.getGroupId();
224 long classNameId = PortalUtil.getClassNameId(
225 DDMStructure.class.getName());
226
227 List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.getTemplates(
228 groupId, classNameId);
229
230 for (DDMTemplate template : templates) {
231 Resource resource = toResource(webDavRequest, template, true);
232
233 resources.add(resource);
234 }
235
236 return resources;
237 }
238
239 protected Resource toResource(
240 WebDAVRequest webDavRequest, DDMStructure structure,
241 boolean appendPath) {
242
243 String parentPath = getRootPath() + webDavRequest.getPath();
244
245 String name = StringPool.BLANK;
246
247 if (appendPath) {
248 name = structure.getName();
249 }
250
251 return new DDMStructureResourceImpl(structure, parentPath, name);
252 }
253
254 protected Resource toResource(
255 WebDAVRequest webDavRequest, DDMTemplate template, boolean appendPath) {
256
257 String parentPath = getRootPath() + webDavRequest.getPath();
258
259 String name = StringPool.BLANK;
260
261 if (appendPath) {
262 name = template.getName();
263 }
264
265 return new DDMTemplateResourceImpl(template, parentPath, name);
266 }
267
268 private static final String _TYPE_STRUCTURES = "ddmStructures";
269
270 private static final String _TYPE_TEMPLATES = "ddmTemplates";
271
272 }