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.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.Namespace;
028    import com.liferay.portal.kernel.xml.QName;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portal.webdav.InvalidRequestException;
031    import com.liferay.util.xml.XMLFormatter;
032    
033    import java.util.HashSet;
034    import java.util.Iterator;
035    import java.util.Set;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Alexander Chow
043     */
044    public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
045    
046            @Override
047            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
048                    try {
049                            Set<QName> props = getProps(webDavRequest);
050    
051                            return writeResponseXML(webDavRequest, props);
052                    }
053                    catch (InvalidRequestException ire) {
054                            return HttpServletResponse.SC_BAD_REQUEST;
055                    }
056                    catch (Exception e) {
057                            throw new WebDAVException(e);
058                    }
059            }
060    
061            protected Set<QName> generateProps(Set<QName> props) {
062                    props.add(DISPLAYNAME);
063                    props.add(RESOURCETYPE);
064                    props.add(GETCONTENTTYPE);
065                    props.add(GETCONTENTLENGTH);
066                    props.add(GETLASTMODIFIED);
067                    props.add(LOCKDISCOVERY);
068    
069                    // RFC 3253 Currently Unsupported
070    
071                    //props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
072                    //props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
073                    //props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
074    
075                    return props;
076            }
077    
078            protected Set<QName> getProps(WebDAVRequest webDavRequest)
079                    throws InvalidRequestException {
080    
081                    try {
082                            Set<QName> props = new HashSet<QName>();
083    
084                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
085    
086                            String xml = new String(
087                                    FileUtil.getBytes(request.getInputStream()));
088    
089                            if (Validator.isNull(xml)) {
090    
091                                    // Windows XP does not generate an xml request so the PROPFIND
092                                    // must be generated manually. See LEP-4920.
093    
094                                    return generateProps(props);
095                            }
096    
097                            if (_log.isDebugEnabled()) {
098                                    _log.debug(
099                                            "Request XML: \n" +
100                                                    XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
101                            }
102    
103                            Document doc = SAXReaderUtil.read(xml);
104    
105                            Element root = doc.getRootElement();
106    
107                            if (root.element(ALLPROP.getName()) != null) {
108    
109                                    // Generate props if <allprop> tag is used. See LEP-6162.
110    
111                                    return generateProps(props);
112                            }
113    
114                            Element prop = root.element("prop");
115    
116                            Iterator<Element> itr = prop.elements().iterator();
117    
118                            while (itr.hasNext()) {
119                                    Element el = itr.next();
120    
121                                    String prefix = el.getNamespacePrefix();
122                                    String uri = el.getNamespaceURI();
123    
124                                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
125    
126                                    props.add(SAXReaderUtil.createQName(el.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    }