001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.workspaces.dav; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.time.format.DateTimeFormatter; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024import javax.xml.parsers.DocumentBuilderFactory; 025import javax.xml.parsers.ParserConfigurationException; 026 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.Response; 031import org.apache.cocoon.environment.http.HttpEnvironment; 032import org.apache.cocoon.generation.AbstractGenerator; 033import org.apache.cocoon.xml.XMLUtils; 034import org.w3c.dom.Document; 035import org.w3c.dom.Node; 036import org.w3c.dom.NodeList; 037import org.xml.sax.SAXException; 038 039import org.ametys.cms.transformation.xslt.ResolveURIComponent; 040import org.ametys.core.util.DateUtils; 041import org.ametys.plugins.explorer.resources.Resource; 042 043/** 044 * Reader for WebDAV PROFIND method 045 * 046 */ 047public class WebdavPropfindGenerator extends AbstractGenerator 048{ 049 /** The webdav namespace */ 050 public static final String WEBDAV_NAMESPACE = "DAV:"; 051 052 public void generate() throws IOException, SAXException, ProcessingException 053 { 054 Request request = ObjectModelHelper.getRequest(objectModel); 055 Resource resource = (Resource) request.getAttribute("resource"); 056 057 HttpServletRequest httpRequest = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 058 059 Document document = null; 060 try (InputStream is = httpRequest.getInputStream()) 061 { 062 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 063 documentBuilderFactory.setNamespaceAware(true); 064 document = documentBuilderFactory.newDocumentBuilder().parse(is); 065 } 066 catch (ParserConfigurationException e) 067 { 068 throw new ProcessingException(e); 069 } 070 071 Response response = ObjectModelHelper.getResponse(objectModel); 072 response.setHeader("Content-Type", "application/xml; charset=utf-8"); 073 074 HttpServletResponse httpResponse = (HttpServletResponse) objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 075 httpResponse.setStatus(207); 076 077 contentHandler.startDocument(); 078 contentHandler.startPrefixMapping("d", WEBDAV_NAMESPACE); 079 080 XMLUtils.startElement(contentHandler, "d:multistatus"); 081 XMLUtils.startElement(contentHandler, "d:response"); 082 083 String href = ResolveURIComponent.resolve("webdav-project-resource", resource.getId(), false, true); 084 XMLUtils.createElement(contentHandler, "d:href", href); 085 086 XMLUtils.startElement(contentHandler, "d:propstat"); 087 XMLUtils.createElement(contentHandler, "d:status", "HTTP/1.1 200 OK"); 088 XMLUtils.startElement(contentHandler, "d:prop"); 089 090 // get all asked properties 091 NodeList childNodes = document.getDocumentElement().getFirstChild().getChildNodes(); 092 093 for (int i = 0; i < childNodes.getLength(); i++) 094 { 095 Node node = childNodes.item(i); 096 String name = node.getLocalName(); 097 String namespace = node.getNamespaceURI(); 098 099 if (namespace.equals(WEBDAV_NAMESPACE) && name.equals("creationdate")) 100 { 101 String lastModified = DateTimeFormatter.ISO_DATE_TIME.format(DateUtils.asLocalDateTime(resource.getLastModified())); 102 XMLUtils.createElement(contentHandler, "d:creationdate", lastModified); 103 } 104 else if (namespace.equals(WEBDAV_NAMESPACE) && name.equals("getlastmodified")) 105 { 106 String lastModified = DateTimeFormatter.ISO_DATE_TIME.format(DateUtils.asLocalDateTime(resource.getLastModified())); 107 XMLUtils.createElement(contentHandler, "d:getlastmodified", lastModified); 108 } 109 else if (namespace.equals(WEBDAV_NAMESPACE) && name.equals("getcontentlength")) 110 { 111 XMLUtils.createElement(contentHandler, "d:getcontentlength", String.valueOf(resource.getLength())); 112 } 113 } 114 115 XMLUtils.endElement(contentHandler, "d:prop"); 116 XMLUtils.endElement(contentHandler, "d:propstat"); 117 XMLUtils.endElement(contentHandler, "d:response"); 118 XMLUtils.endElement(contentHandler, "d:multistatus"); 119 120 contentHandler.endDocument(); 121 } 122}