use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class VersionControlledResourceImpl method checkin.
/**
* Calls {@link javax.jcr.Node#checkin()} on the underlying repository node.
*
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.version.VersionControlledResource#checkin()
*/
public String checkin() throws DavException {
if (!exists()) {
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
if (!isVersionControlled()) {
throw new DavException(DavServletResponse.SC_METHOD_NOT_ALLOWED);
}
try {
Version v = getNode().checkin();
String versionHref = getLocatorFromNode(v).getHref(false);
return versionHref;
} catch (RepositoryException e) {
// UnsupportedRepositoryException should not occur
throw new JcrDavException(e);
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class AbstractItemResource method move.
/**
* Moves the underlying repository item to the indicated destination.
*
* @param destination
* @throws DavException
* @see DavResource#move(DavResource)
* @see javax.jcr.Session#move(String, String)
*/
@Override
public void move(DavResource destination) throws DavException {
if (!exists()) {
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
DavResourceLocator destLocator = destination.getLocator();
if (!getLocator().isSameWorkspace(destLocator)) {
throw new DavException(DavServletResponse.SC_FORBIDDEN);
}
try {
String itemPath = getLocator().getRepositoryPath();
String destItemPath = destination.getLocator().getRepositoryPath();
if (getTransactionId() == null) {
// if not part of a transaction directly import on workspace
getRepositorySession().getWorkspace().move(itemPath, destItemPath);
} else {
// changes will not be persisted unless the tx is completed.
getRepositorySession().move(itemPath, destItemPath);
}
// no use in calling 'complete' that would fail for a moved item anyway.
} catch (PathNotFoundException e) {
// according to rfc 2518
throw new DavException(DavServletResponse.SC_CONFLICT, e.getMessage());
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class DefaultItemCollection method addMember.
/**
* If the specified resource represents a collection, a new node is {@link Node#addNode(String)
* added} to the item represented by this resource. If an input stream is specified
* together with a collection resource {@link Session#importXML(String, java.io.InputStream, int)}
* is called instead and this resource path is used as <code>parentAbsPath</code> argument.
* <p>
* However, if the specified resource is not of resource type collection a
* new {@link Property} is set or an existing one is changed by modifying its
* value.<br>
* NOTE: with the current implementation it is not possible to create or
* modify multivalue JCR properties.<br>
* NOTE: if the JCR property represented by the specified resource has an
* {@link PropertyType#UNDEFINED undefined} resource type, its value will be
* changed/set to type {@link PropertyType#BINARY binary}.
*
* @param resource
* @param inputContext
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.DavResource#addMember(org.apache.jackrabbit.webdav.DavResource, InputContext)
* @see Node#addNode(String)
* @see Node#setProperty(String, java.io.InputStream)
*/
@Override
public void addMember(DavResource resource, InputContext inputContext) throws DavException {
/* RFC 2815 states that all 'parents' must exist in order all addition of members */
if (!exists()) {
throw new DavException(DavServletResponse.SC_CONFLICT);
}
File tmpFile = null;
try {
Node n = (Node) item;
InputStream in = (inputContext != null) ? inputContext.getInputStream() : null;
String itemPath = getLocator().getRepositoryPath();
String memberName = getItemName(resource.getLocator().getRepositoryPath());
if (resource.isCollection()) {
if (in == null) {
// MKCOL without a request body, try if a default-primary-type is defined.
n.addNode(memberName);
} else {
// MKCOL, which is not allowed for existing resources
int uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
String str = inputContext.getProperty(IMPORT_UUID_BEHAVIOR);
if (str != null) {
try {
uuidBehavior = Integer.parseInt(str);
} catch (NumberFormatException e) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST);
}
}
if (getTransactionId() == null) {
// if not part of a transaction directly import on workspace
// since changes would be explicitly saved in the
// complete-call.
getRepositorySession().getWorkspace().importXML(itemPath, in, uuidBehavior);
} else {
// changes will not be persisted unless the tx is completed.
getRepositorySession().importXML(itemPath, in, uuidBehavior);
}
}
} else {
if (in == null) {
// PUT: not possible without request body
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Cannot create a new non-collection resource without request body.");
}
// PUT : create new or overwrite existing property.
String ct = inputContext.getContentType();
int type = JcrValueType.typeFromContentType(ct);
if (type != PropertyType.UNDEFINED) {
// no need to create value/values property. instead
// prop-value can be retrieved directly:
int pos = ct.indexOf(';');
String charSet = (pos > -1) ? ct.substring(pos) : "UTF-8";
if (type == PropertyType.BINARY) {
n.setProperty(memberName, inputContext.getInputStream());
} else {
BufferedReader r = new BufferedReader(new InputStreamReader(inputContext.getInputStream(), charSet));
String line;
StringBuffer value = new StringBuffer();
while ((line = r.readLine()) != null) {
value.append(line);
}
n.setProperty(memberName, value.toString(), type);
}
} else {
// try to parse the request body into a 'values' property.
tmpFile = File.createTempFile(TMP_PREFIX + Text.escape(memberName), null, null);
FileOutputStream out = new FileOutputStream(tmpFile);
IOUtil.spool(in, out);
out.close();
// try to parse the request body into a 'values' property.
ValuesProperty vp = buildValuesProperty(new FileInputStream(tmpFile));
if (vp != null) {
if (JCR_VALUE.equals(vp.getName())) {
n.setProperty(memberName, vp.getJcrValue());
} else {
n.setProperty(memberName, vp.getJcrValues());
}
} else {
// request body cannot be parsed into a 'values' property.
// fallback: try to import as single value from stream.
n.setProperty(memberName, new FileInputStream(tmpFile));
}
}
}
if (resource.exists() && resource instanceof AbstractItemResource) {
// PUT may modify value of existing jcr property. thus, this
// node is not modified by the 'addMember' call.
((AbstractItemResource) resource).complete();
} else {
complete();
}
} catch (ItemExistsException e) {
// according to RFC 2518: MKCOL only possible on non-existing/deleted resource
throw new JcrDavException(e, DavServletResponse.SC_METHOD_NOT_ALLOWED);
} catch (RepositoryException e) {
throw new JcrDavException(e);
} catch (IOException e) {
throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, e.getMessage());
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class DefaultItemCollection method getMembers.
/**
* @see org.apache.jackrabbit.webdav.DavResource#getMembers()
*/
@Override
public DavResourceIterator getMembers() {
ArrayList<DavResource> memberList = new ArrayList<DavResource>();
if (exists()) {
try {
Node n = (Node) item;
// add all node members
NodeIterator it = n.getNodes();
while (it.hasNext()) {
Node node = it.nextNode();
DavResourceLocator loc = getLocatorFromItem(node);
memberList.add(createResourceFromLocator(loc));
}
// add all property members
PropertyIterator propIt = n.getProperties();
while (propIt.hasNext()) {
Property prop = propIt.nextProperty();
DavResourceLocator loc = getLocatorFromItem(prop);
memberList.add(createResourceFromLocator(loc));
}
} catch (RepositoryException e) {
// ignore
log.error(e.getMessage());
} catch (DavException e) {
// should never occur.
log.error(e.getMessage());
}
}
return new DavResourceIteratorImpl(memberList);
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class DefaultItemCollection method lock.
/**
* Creates a lock on this resource by locking the underlying
* {@link javax.jcr.Node node}. Except for the {@link org.apache.jackrabbit.webdav.lock.LockInfo#isDeep()} }
* all information included in the <code>LockInfo</code> object is ignored.
* Lock timeout is defined by JCR implementation.
*
* @param reqLockInfo
* @return lock object representing the lock created on this resource.
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.DavResource#lock(org.apache.jackrabbit.webdav.lock.LockInfo)
* @see Node#lock(boolean, boolean)
*/
@Override
public ActiveLock lock(LockInfo reqLockInfo) throws DavException {
if (!isLockable(reqLockInfo.getType(), reqLockInfo.getScope())) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
if (Type.WRITE.equals(reqLockInfo.getType())) {
if (!exists()) {
log.warn("Cannot create a write lock for non-existing JCR node (" + getResourcePath() + ")");
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
try {
boolean sessionScoped = EXCLUSIVE_SESSION.equals(reqLockInfo.getScope());
long timeout = reqLockInfo.getTimeout();
if (timeout == LockInfo.INFINITE_TIMEOUT) {
timeout = Long.MAX_VALUE;
} else {
timeout = timeout / 1000;
}
javax.jcr.lock.LockManager lockMgr = getRepositorySession().getWorkspace().getLockManager();
Lock jcrLock = lockMgr.lock((item).getPath(), reqLockInfo.isDeep(), sessionScoped, timeout, reqLockInfo.getOwner());
ActiveLock lock = new JcrActiveLock(jcrLock);
// add reference to DAVSession for this lock
getSession().addReference(lock.getToken());
return lock;
} catch (RepositoryException e) {
// UnsupportedRepositoryOperationException should not occur...
throw new JcrDavException(e);
}
} else {
return super.lock(reqLockInfo);
}
}
Aggregations