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(), new ServiceContext());
183
184 return HttpServletResponse.SC_CREATED;
185 }
186 else {
187 return HttpServletResponse.SC_FORBIDDEN;
188 }
189 }
190 catch (PortalException pe) {
191 return HttpServletResponse.SC_FORBIDDEN;
192 }
193 catch (Exception e) {
194 throw new WebDAVException(e);
195 }
196 }
197
198 protected List<Resource> getStructures(WebDAVRequest webDavRequest)
199 throws Exception {
200
201 List<Resource> resources = new ArrayList<Resource>();
202
203 long groupId = webDavRequest.getGroupId();
204
205 List<DDMStructure> structures =
206 DDMStructureLocalServiceUtil.getStructures(groupId);
207
208 for (DDMStructure structure : structures) {
209 Resource resource = toResource(webDavRequest, structure, true);
210
211 resources.add(resource);
212 }
213
214 return resources;
215 }
216
217 protected List<Resource> getTemplates(WebDAVRequest webDavRequest)
218 throws Exception {
219
220 List<Resource> resources = new ArrayList<Resource>();
221
222 long groupId = webDavRequest.getGroupId();
223 long classNameId = PortalUtil.getClassNameId(
224 DDMStructure.class.getName());
225
226 List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.getTemplates(
227 groupId, classNameId);
228
229 for (DDMTemplate template : templates) {
230 Resource resource = toResource(webDavRequest, template, true);
231
232 resources.add(resource);
233 }
234
235 return resources;
236 }
237
238 protected Resource toResource(
239 WebDAVRequest webDavRequest, DDMStructure structure,
240 boolean appendPath) {
241
242 String parentPath = getRootPath() + webDavRequest.getPath();
243
244 String name = StringPool.BLANK;
245
246 if (appendPath) {
247 name = structure.getName();
248 }
249
250 return new DDMStructureResourceImpl(structure, parentPath, name);
251 }
252
253 protected Resource toResource(
254 WebDAVRequest webDavRequest, DDMTemplate template, boolean appendPath) {
255
256 String parentPath = getRootPath() + webDavRequest.getPath();
257
258 String name = StringPool.BLANK;
259
260 if (appendPath) {
261 name = template.getName();
262 }
263
264 return new DDMTemplateResourceImpl(template, parentPath, name);
265 }
266
267 private static final String _TYPE_STRUCTURES = "ddmStructures";
268
269 private static final String _TYPE_TEMPLATES = "ddmTemplates";
270
271 }