📄 abstractresource.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.jcr;import org.apache.jackrabbit.server.io.IOUtil;import org.apache.jackrabbit.webdav.DavConstants;import org.apache.jackrabbit.webdav.DavException;import org.apache.jackrabbit.webdav.DavLocatorFactory;import org.apache.jackrabbit.webdav.DavResource;import org.apache.jackrabbit.webdav.DavResourceFactory;import org.apache.jackrabbit.webdav.DavResourceLocator;import org.apache.jackrabbit.webdav.DavServletResponse;import org.apache.jackrabbit.webdav.DavSession;import org.apache.jackrabbit.webdav.MultiStatus;import org.apache.jackrabbit.webdav.MultiStatusResponse;import org.apache.jackrabbit.webdav.DavCompliance;import org.apache.jackrabbit.webdav.jcr.search.SearchResourceImpl;import org.apache.jackrabbit.webdav.jcr.transaction.TxLockManagerImpl;import org.apache.jackrabbit.webdav.jcr.version.report.NodeTypesReport;import org.apache.jackrabbit.webdav.jcr.version.report.LocateByUuidReport;import org.apache.jackrabbit.webdav.jcr.version.report.RegisteredNamespacesReport;import org.apache.jackrabbit.webdav.jcr.version.report.RepositoryDescriptorsReport;import org.apache.jackrabbit.webdav.lock.ActiveLock;import org.apache.jackrabbit.webdav.lock.LockDiscovery;import org.apache.jackrabbit.webdav.lock.LockInfo;import org.apache.jackrabbit.webdav.lock.LockManager;import org.apache.jackrabbit.webdav.lock.Scope;import org.apache.jackrabbit.webdav.lock.SupportedLock;import org.apache.jackrabbit.webdav.lock.Type;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.property.HrefProperty;import org.apache.jackrabbit.webdav.property.ResourceType;import org.apache.jackrabbit.webdav.property.DavPropertyNameIterator;import org.apache.jackrabbit.webdav.property.DavPropertyIterator;import org.apache.jackrabbit.webdav.search.QueryGrammerSet;import org.apache.jackrabbit.webdav.search.SearchInfo;import org.apache.jackrabbit.webdav.search.SearchResource;import org.apache.jackrabbit.webdav.transaction.TransactionConstants;import org.apache.jackrabbit.webdav.transaction.TransactionInfo;import org.apache.jackrabbit.webdav.transaction.TransactionResource;import org.apache.jackrabbit.webdav.transaction.TxLockManager;import org.apache.jackrabbit.webdav.version.DeltaVConstants;import org.apache.jackrabbit.webdav.version.DeltaVResource;import org.apache.jackrabbit.webdav.version.OptionsInfo;import org.apache.jackrabbit.webdav.version.OptionsResponse;import org.apache.jackrabbit.webdav.version.SupportedMethodSetProperty;import org.apache.jackrabbit.webdav.version.report.Report;import org.apache.jackrabbit.webdav.version.report.ReportInfo;import org.apache.jackrabbit.webdav.version.report.ReportType;import org.apache.jackrabbit.webdav.version.report.SupportedReportSetProperty;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.jcr.Item;import javax.jcr.RepositoryException;import javax.jcr.Session;import javax.jcr.observation.EventListener;import javax.jcr.observation.Event;import javax.jcr.observation.EventIterator;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;/** * <code>AbstractResource</code> provides functionality common to all * resources. */abstract class AbstractResource implements DavResource, TransactionResource, DeltaVResource, SearchResource { private static Logger log = LoggerFactory.getLogger(AbstractResource.class); private static final String COMPLIANCE_CLASSES = DavCompliance.concatComplianceClasses(new String[] { DavCompliance._1_, DavCompliance._2_, DavCompliance.VERSION_CONTROL, DavCompliance.VERSION_HISTORY, DavCompliance.CHECKOUT_IN_PLACE, DavCompliance.LABEL, DavCompliance.MERGE, DavCompliance.UPDATE, DavCompliance.WORKSPACE }); private final DavResourceLocator locator; private final JcrDavSession session; private final DavResourceFactory factory; private TxLockManagerImpl txMgr; private String transactionId; protected boolean initedProps; protected DavPropertySet properties = new DavPropertySet(); protected SupportedLock supportedLock = new SupportedLock(); protected SupportedReportSetProperty supportedReports = new SupportedReportSetProperty(); /** * Create a new <code>AbstractResource</code> * * @param locator * @param session */ AbstractResource(DavResourceLocator locator, JcrDavSession session, DavResourceFactory factory) { if (session == null) { throw new IllegalArgumentException("Creating AbstractItemResource: DavSession must not be null and must provide a JCR session."); } this.locator = locator; this.session = session; this.factory = factory; } /** * Returns a string listing the compliance classes for this resource as it * is required for the DAV response header. This includes DAV 1, 2 which * is supported by all derived classes as well as a subset of the * classes defined by DeltaV: version-control, version-history, checkout-in-place, * label, merge, update and workspace.<br> * Those compliance classes are added as required by RFC3253 since all * all resources in the jcr-server support at least the reporting and some * basic versioning functionality. * * @return string listing the compliance classes. * @see org.apache.jackrabbit.webdav.DavResource#getComplianceClass() */ public String getComplianceClass() { return COMPLIANCE_CLASSES; } /** * @see org.apache.jackrabbit.webdav.DavResource#getLocator() */ public DavResourceLocator getLocator() { return locator; } /** * Returns the path of the underlying repository item or the item to * be created (PUT/MKCOL). If the resource exists but does not represent * a repository item <code>null</code> is returned. * * @return path of the underlying repository item. * @see DavResource#getResourcePath() * @see org.apache.jackrabbit.webdav.DavResourceLocator#getResourcePath() */ public String getResourcePath() { return locator.getResourcePath(); } /** * @see DavResource#getHref() * @see DavResourceLocator#getHref(boolean) */ public String getHref() { return locator.getHref(isCollection()); } /** * @see org.apache.jackrabbit.webdav.DavResource#getPropertyNames() */ public DavPropertyName[] getPropertyNames() { return getProperties().getPropertyNames(); } /** * @see org.apache.jackrabbit.webdav.DavResource#getProperty(org.apache.jackrabbit.webdav.property.DavPropertyName) */ public DavProperty getProperty(DavPropertyName name) { return getProperties().get(name); } /** * @see org.apache.jackrabbit.webdav.DavResource#getProperties() */ public DavPropertySet getProperties() { if (!initedProps) { initProperties(); } return properties; } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * * @param property * @throws DavException Always throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * @see org.apache.jackrabbit.webdav.DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty) */ public void setProperty(DavProperty property) throws DavException { throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED); } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * * @param propertyName * @throws DavException Always throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * @see org.apache.jackrabbit.webdav.DavResource#removeProperty(org.apache.jackrabbit.webdav.property.DavPropertyName) */ public void removeProperty(DavPropertyName propertyName) throws DavException { throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED); } /** * Builds a single List from the properties to set and the properties to * remove and delegates the list to {@link AbstractResource#alterProperties(List)}; * * @see DavResource#alterProperties(org.apache.jackrabbit.webdav.property.DavPropertySet, org.apache.jackrabbit.webdav.property.DavPropertyNameSet) */ public MultiStatusResponse alterProperties(DavPropertySet setProperties, DavPropertyNameSet removePropertyNames) throws DavException { List changeList = new ArrayList(); if (removePropertyNames != null) { DavPropertyNameIterator it = removePropertyNames.iterator(); while (it.hasNext()) { changeList.add(it.next()); } } if (setProperties != null) { DavPropertyIterator it = setProperties.iterator(); while (it.hasNext()) { changeList.add(it.next()); } } return alterProperties(changeList); } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * * @see DavResource#alterProperties(List) */ public MultiStatusResponse alterProperties(List changeList) throws DavException { throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED); } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * * @param destination * @throws DavException Always throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * @see DavResource#move(org.apache.jackrabbit.webdav.DavResource) */ public void move(DavResource destination) throws DavException { throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED); } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * * @param destination * @param shallow * @throws DavException Always throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} * @see DavResource#copy(org.apache.jackrabbit.webdav.DavResource, boolean) */ public void copy(DavResource destination, boolean shallow) throws DavException { throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED); } /** * Returns true, if the {@link SupportedLock} property contains an entry * with the given type and scope. By default resources allow for {@link org.apache.jackrabbit.webdav.transaction.TransactionConstants.XML_TRANSACTION * transaction} lock only. * * @param type * @param scope * @return true if this resource may be locked by the given type and scope. * @see DavResource#isLockable(org.apache.jackrabbit.webdav.lock.Type, org.apache.jackrabbit.webdav.lock.Scope) */ public boolean isLockable(Type type, Scope scope) { return supportedLock.isSupportedLock(type, scope); } /** * Returns true if this resource has a lock applied with the given type and scope. * * @param type * @param scope * @return true if this resource has a lock applied with the given type and scope. * @see DavResource#hasLock(Type, Scope) */ public boolean hasLock(Type type, Scope scope) { return getLock(type, scope) != null; } /** * @see DavResource#getLock(Type, Scope) */ public ActiveLock getLock(Type type, Scope scope) { ActiveLock lock = null; if (TransactionConstants.TRANSACTION.equals(type)) { lock = txMgr.getLock(type, scope, this); } return lock; } /** * @see DavResource#getLocks() * todo improve.... */ public ActiveLock[] getLocks() { List locks = new ArrayList(); // tx locks ActiveLock l = getLock(TransactionConstants.TRANSACTION, TransactionConstants.LOCAL); if (l != null) { locks.add(l); } l = getLock(TransactionConstants.TRANSACTION, TransactionConstants.GLOBAL); if (l != null) { locks.add(l); } // write lock (either exclusive or session-scoped). l = getLock(Type.WRITE, Scope.EXCLUSIVE); if (l != null) { locks.add(l); } else { l = getLock(Type.WRITE, ItemResourceConstants.EXCLUSIVE_SESSION); if (l != null) { locks.add(l); } } return (ActiveLock[]) locks.toArray(new ActiveLock[locks.size()]); } /** * @see DavResource#lock(org.apache.jackrabbit.webdav.lock.LockInfo) */ public ActiveLock lock(LockInfo reqLockInfo) throws DavException { if (isLockable(reqLockInfo.getType(), reqLockInfo.getScope())) { return txMgr.createLock(reqLockInfo, this); } else { throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED); } } /** * Only transaction lock may be available on this resource. * * @param info * @param lockToken * @throws DavException * @see DavResource#refreshLock(org.apache.jackrabbit.webdav.lock.LockInfo, String) */ public ActiveLock refreshLock(LockInfo info, String lockToken) throws DavException { return txMgr.refreshLock(info, lockToken, this); } /** * Throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} since only transaction * locks may be present on this resource, that need to be released by calling * {@link TransactionResource#unlock(String, org.apache.jackrabbit.webdav.transaction.TransactionInfo)}. * * @param lockToken * @throws DavException Always throws {@link DavServletResponse#SC_METHOD_NOT_ALLOWED} */ public void unlock(String lockToken) throws DavException { throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED); } /** * @see DavResource#addLockManager(org.apache.jackrabbit.webdav.lock.LockManager)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -