📄 webdavrequestimpl.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.webdav;import org.apache.jackrabbit.webdav.header.CodedUrlHeader;import org.apache.jackrabbit.webdav.header.DepthHeader;import org.apache.jackrabbit.webdav.header.IfHeader;import org.apache.jackrabbit.webdav.header.LabelHeader;import org.apache.jackrabbit.webdav.header.OverwriteHeader;import org.apache.jackrabbit.webdav.header.TimeoutHeader;import org.apache.jackrabbit.webdav.header.PollTimeoutHeader;import org.apache.jackrabbit.webdav.lock.LockInfo;import org.apache.jackrabbit.webdav.lock.Scope;import org.apache.jackrabbit.webdav.lock.Type;import org.apache.jackrabbit.webdav.observation.ObservationConstants;import org.apache.jackrabbit.webdav.observation.SubscriptionInfo;import org.apache.jackrabbit.webdav.ordering.OrderPatch;import org.apache.jackrabbit.webdav.ordering.OrderingConstants;import org.apache.jackrabbit.webdav.ordering.Position;import org.apache.jackrabbit.webdav.property.DavProperty;import org.apache.jackrabbit.webdav.property.DavPropertyName;import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;import org.apache.jackrabbit.webdav.property.DavPropertySet;import org.apache.jackrabbit.webdav.property.DefaultDavProperty;import org.apache.jackrabbit.webdav.transaction.TransactionConstants;import org.apache.jackrabbit.webdav.transaction.TransactionInfo;import org.apache.jackrabbit.webdav.version.LabelInfo;import org.apache.jackrabbit.webdav.version.MergeInfo;import org.apache.jackrabbit.webdav.version.OptionsInfo;import org.apache.jackrabbit.webdav.version.UpdateInfo;import org.apache.jackrabbit.webdav.version.report.ReportInfo;import org.apache.jackrabbit.webdav.xml.DomUtil;import org.apache.jackrabbit.webdav.xml.ElementIterator;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletInputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.ParserConfigurationException;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URI;import java.net.URISyntaxException;import java.security.Principal;import java.util.Enumeration;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.ArrayList;import java.util.List;/** * <code>WebdavRequestImpl</code>... */public class WebdavRequestImpl implements WebdavRequest, DavConstants { private static Logger log = LoggerFactory.getLogger(WebdavRequestImpl.class); private final HttpServletRequest httpRequest; private final DavLocatorFactory factory; private final IfHeader ifHeader; private final String hrefPrefix; private DavSession session; private int propfindType = PROPFIND_ALL_PROP; private DavPropertyNameSet propfindProps; private DavPropertySet proppatchSet; private DavPropertyNameSet proppatchRemove; private List proppatchList; /** * Creates a new <code>DavServletRequest</code> with the given parameters. * * @param httpRequest * @param factory */ public WebdavRequestImpl(HttpServletRequest httpRequest, DavLocatorFactory factory) { this.httpRequest = httpRequest; this.factory = factory; this.ifHeader = new IfHeader(httpRequest); String host = getHeader("Host"); String scheme = getScheme(); hrefPrefix = scheme + "://" + host + getContextPath(); } /** * Sets the session field and adds all lock tokens present with either the * Lock-Token header or the If header to the given session object. * * @param session * @see DavServletRequest#setDavSession(DavSession) */ public void setDavSession(DavSession session) { this.session = session; // set lock-tokens from header to the current session if (session != null) { String lt = getLockToken(); if (lt != null) { session.addLockToken(lt); } // add all token present in the the If header to the session as well. Iterator it = ifHeader.getAllTokens(); while (it.hasNext()) { String ifHeaderToken = (String) it.next(); session.addLockToken(ifHeaderToken); } } } /** * @see DavServletRequest#getDavSession() */ public DavSession getDavSession() { return session; } /** * Return a <code>DavResourceLocator</code> representing the request handle. * * @return locator of the requested resource * @see DavServletRequest#getRequestLocator() */ public DavResourceLocator getRequestLocator() { String path = getRequestURI(); String ctx = getContextPath(); if (path.startsWith(ctx)) { path = path.substring(ctx.length()); } return factory.createResourceLocator(hrefPrefix, path); } /** * Parse the destination header field and return the path of the destination * resource. * * @return path of the destination resource. * @see #HEADER_DESTINATION * @see DavServletRequest#getDestinationLocator */ public DavResourceLocator getDestinationLocator() { String destination = httpRequest.getHeader(HEADER_DESTINATION); if (destination != null) { try { URI uri = new URI(destination); if (uri.getAuthority().equals(httpRequest.getHeader("Host"))) { destination = uri.getRawPath(); } } catch (URISyntaxException e) { log.debug("Destination is path is not a valid URI (" + e.getMessage() + "."); int pos = destination.lastIndexOf(":"); if (pos > 0) { destination = destination.substring(destination.indexOf("/", pos)); log.debug("Tried to retrieve resource destination path from invalid URI: " + destination); } } // cut off the context path String contextPath = httpRequest.getContextPath(); if (destination.startsWith(contextPath)) { destination = destination.substring(contextPath.length()); } } return factory.createResourceLocator(hrefPrefix, destination); } /** * Return true if the overwrite header does not inhibit overwriting. * * @return true if the overwrite header requests 'overwriting' * @see #HEADER_OVERWRITE * @see DavServletRequest#isOverwrite() */ public boolean isOverwrite() { return new OverwriteHeader(httpRequest).isOverwrite(); } /** * @see DavServletRequest#getDepth(int) */ public int getDepth(int defaultValue) { return DepthHeader.parse(httpRequest, defaultValue).getDepth(); } /** * @see DavServletRequest#getDepth() */ public int getDepth() { return getDepth(DEPTH_INFINITY); } /** * Parse the Timeout header and return a long representing the value. * {@link #UNDEFINED_TIMEOUT} is used as default value if no header * is available or if the parsing fails. * * @return milliseconds indicating length of the timeout. * @see DavServletRequest#getTimeout() * @see TimeoutHeader#parse(javax.servlet.http.HttpServletRequest, long) */ public long getTimeout() { return TimeoutHeader.parse(httpRequest, UNDEFINED_TIMEOUT).getTimeout(); } /** * Retrive the lock token from the 'Lock-Token' header. * * @return String representing the lock token sent in the Lock-Token header. * @throws IllegalArgumentException If the value has not the correct format. * @see #HEADER_LOCK_TOKEN * @see DavServletRequest#getLockToken() */ public String getLockToken() { return CodedUrlHeader.parse(httpRequest, HEADER_LOCK_TOKEN).getCodedUrl(); } /** * @see DavServletRequest#getRequestDocument() */ public Document getRequestDocument() throws DavException { Document requestDocument = null; /* Don't attempt to parse the body if the contentlength header is 0. NOTE: a value of -1 indicates that the length is unknown, thus we have to parse the body. Note that http1.1 request using chunked transfer coding will therefore not be detected here. */ if (httpRequest.getContentLength() == 0) { return requestDocument; } // try to parse the request body try { InputStream in = httpRequest.getInputStream(); if (in != null) { // use a buffered input stream to find out whether there actually // is a request body InputStream bin = new BufferedInputStream(in); bin.mark(1); boolean isEmpty = -1 == bin.read(); bin.reset(); if (!isEmpty) { DocumentBuilder docBuilder = DomUtil.BUILDER_FACTORY.newDocumentBuilder(); requestDocument = docBuilder.parse(bin); } } } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Unable to build an XML Document from the request body: " + e.getMessage()); } throw new DavException(DavServletResponse.SC_BAD_REQUEST); } catch (ParserConfigurationException e) { if (log.isDebugEnabled()) { log.debug("Unable to build an XML Document from the request body: " + e.getMessage()); } throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR); } catch (SAXException e) { if (log.isDebugEnabled()) { log.debug("Unable to build an XML Document from the request body: " + e.getMessage()); } throw new DavException(DavServletResponse.SC_BAD_REQUEST); } return requestDocument; } /** * Returns the type of PROPFIND as indicated by the request body. * * @return type of the PROPFIND request. Default value is {@link #PROPFIND_ALL_PROP allprops} * @see DavServletRequest#getPropFindType() */ public int getPropFindType() throws DavException { if (propfindProps == null) { parsePropFindRequest(); } return propfindType; } /** * Returns the set of properties requested by the PROPFIND body or an * empty set if the {@link #getPropFindType type} is either 'allprop' or * 'propname'. * * @return set of properties requested by the PROPFIND body or an empty set. * @see DavServletRequest#getPropFindProperties() */ public DavPropertyNameSet getPropFindProperties() throws DavException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -