001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.webdav.WebDAVException;
023    import com.liferay.portal.kernel.webdav.WebDAVRequest;
024    import com.liferay.portal.kernel.webdav.WebDAVUtil;
025    import com.liferay.portal.kernel.webdav.methods.Method;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.kernel.xml.Namespace;
029    import com.liferay.portal.kernel.xml.QName;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portal.webdav.InvalidRequestException;
032    import com.liferay.util.xml.XMLFormatter;
033    
034    import java.util.HashSet;
035    import java.util.List;
036    import java.util.Set;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Alexander Chow
044     */
045    public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
046    
047            @Override
048            public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
049                    try {
050                            Set<QName> props = getProps(webDAVRequest);
051    
052                            return writeResponseXML(webDAVRequest, props);
053                    }
054                    catch (InvalidRequestException ire) {
055                            return HttpServletResponse.SC_BAD_REQUEST;
056                    }
057                    catch (Exception e) {
058                            throw new WebDAVException(e);
059                    }
060            }
061    
062            protected Set<QName> generateProps(Set<QName> props) {
063                    props.add(DISPLAYNAME);
064                    props.add(RESOURCETYPE);
065                    props.add(GETCONTENTTYPE);
066                    props.add(GETCONTENTLENGTH);
067                    props.add(GETLASTMODIFIED);
068                    props.add(LOCKDISCOVERY);
069    
070                    // RFC 3253 Currently Unsupported
071    
072                    //props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
073                    //props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
074                    //props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
075    
076                    return props;
077            }
078    
079            protected Set<QName> getProps(WebDAVRequest webDAVRequest)
080                    throws InvalidRequestException {
081    
082                    try {
083                            Set<QName> props = new HashSet<QName>();
084    
085                            HttpServletRequest request = webDAVRequest.getHttpServletRequest();
086    
087                            String xml = new String(
088                                    FileUtil.getBytes(request.getInputStream()));
089    
090                            if (Validator.isNull(xml)) {
091    
092                                    // Windows XP does not generate an xml request so the PROPFIND
093                                    // must be generated manually. See LEP-4920.
094    
095                                    return generateProps(props);
096                            }
097    
098                            if (_log.isDebugEnabled()) {
099                                    _log.debug(
100                                            "Request XML: \n" +
101                                                    XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
102                            }
103    
104                            Document document = SAXReaderUtil.read(xml);
105    
106                            Element rootElement = document.getRootElement();
107    
108                            if (rootElement.element(ALLPROP.getName()) != null) {
109    
110                                    // Generate props if <allprop> tag is used. See LEP-6162.
111    
112                                    return generateProps(props);
113                            }
114    
115                            Element propElement = rootElement.element("prop");
116    
117                            List<Element> elements = propElement.elements();
118    
119                            for (Element element : elements) {
120                                    String prefix = element.getNamespacePrefix();
121                                    String uri = element.getNamespaceURI();
122    
123                                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
124    
125                                    props.add(
126                                            SAXReaderUtil.createQName(element.getName(), namespace));
127                            }
128    
129                            return props;
130                    }
131                    catch (Exception e) {
132                            throw new InvalidRequestException(e);
133                    }
134            }
135    
136            private static Log _log = LogFactoryUtil.getLog(PropfindMethodImpl.class);
137    
138    }