001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Tuple;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.webdav.WebDAVException;
024    import com.liferay.portal.kernel.webdav.WebDAVRequest;
025    import com.liferay.portal.kernel.webdav.WebDAVUtil;
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.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            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
047                    try {
048                            Set<Tuple> props = getProps(webDavRequest);
049    
050                            return writeResponseXML(webDavRequest, props);
051                    }
052                    catch (InvalidRequestException ire) {
053                            return HttpServletResponse.SC_BAD_REQUEST;
054                    }
055                    catch (Exception e) {
056                            throw new WebDAVException(e);
057                    }
058            }
059    
060            protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
061                    throws InvalidRequestException {
062    
063                    try {
064                            Set<Tuple> props = new HashSet<Tuple>();
065    
066                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
067    
068                            String xml = new String(
069                                    FileUtil.getBytes(request.getInputStream()));
070    
071                            if (Validator.isNull(xml)) {
072    
073                                    // Windows XP does not generate an xml request so the PROPFIND
074                                    // must be generated manually. See LEP-4920.
075    
076                                    return generateProps(props);
077                            }
078    
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug(
081                                            "Request XML: \n" +
082                                                    XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
083                            }
084    
085                            Document doc = SAXReaderUtil.read(xml);
086    
087                            Element root = doc.getRootElement();
088    
089                            if (root.element(ALLPROP) != null) {
090    
091                                    // Generate props if <allprop> tag is used. See LEP-6162.
092    
093                                    return generateProps(props);
094                            }
095    
096                            Element prop = root.element("prop");
097    
098                            Iterator<Element> itr = prop.elements().iterator();
099    
100                            while (itr.hasNext()) {
101                                    Element el = itr.next();
102    
103                                    String prefix = el.getNamespacePrefix();
104                                    String uri = el.getNamespaceURI();
105    
106                                    Namespace namespace = null;
107    
108                                    if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
109                                            namespace = WebDAVUtil.DAV_URI;
110                                    }
111                                    else if (Validator.isNull(prefix)) {
112                                            namespace = SAXReaderUtil.createNamespace(uri);
113                                    }
114                                    else {
115                                            namespace = SAXReaderUtil.createNamespace(prefix, uri);
116                                    }
117    
118                                    props.add(new Tuple(el.getName(), namespace));
119                            }
120    
121                            return props;
122                    }
123                    catch (Exception e) {
124                            throw new InvalidRequestException(e);
125                    }
126            }
127    
128            protected Set<Tuple> generateProps(Set<Tuple> props) {
129                    props.add(DISPLAYNAME_PAIR);
130                    props.add(RESOURCETYPE_PAIR);
131                    props.add(GETCONTENTTYPE_PAIR);
132                    props.add(GETCONTENTLENGTH_PAIR);
133                    props.add(GETLASTMODIFIED_PAIR);
134                    props.add(LOCKDISCOVERY_PAIR);
135    
136                    // RFC 3253 Currently Unsupported
137    
138                    //props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
139                    //props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
140                    //props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
141    
142                    return props;
143            }
144    
145            private static Log _log = LogFactoryUtil.getLog(PropfindMethodImpl.class);
146    
147    }