use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class AbstractWebdavServlet method doLock.
/**
* The LOCK method
*
* @param request
* @param response
* @param resource
* @throws IOException
* @throws DavException
*/
protected void doLock(WebdavRequest request, WebdavResponse response, DavResource resource) throws IOException, DavException {
LockInfo lockInfo = request.getLockInfo();
if (lockInfo.isRefreshLock()) {
// refresh any matching existing locks
ActiveLock[] activeLocks = resource.getLocks();
List<ActiveLock> lList = new ArrayList<ActiveLock>();
for (ActiveLock activeLock : activeLocks) {
// adjust lockinfo with type/scope retrieved from the lock.
lockInfo.setType(activeLock.getType());
lockInfo.setScope(activeLock.getScope());
DavProperty<?> etagProp = resource.getProperty(DavPropertyName.GETETAG);
String etag = etagProp != null ? String.valueOf(etagProp.getValue()) : "";
if (request.matchesIfHeader(resource.getHref(), activeLock.getToken(), etag)) {
lList.add(resource.refreshLock(lockInfo, activeLock.getToken()));
}
}
if (lList.isEmpty()) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
ActiveLock[] refreshedLocks = lList.toArray(new ActiveLock[lList.size()]);
response.sendRefreshLockResponse(refreshedLocks);
} else {
int status = HttpServletResponse.SC_OK;
if (!resource.exists()) {
// lock-empty requires status code 201 (Created)
status = HttpServletResponse.SC_CREATED;
}
// create a new lock
ActiveLock lock = resource.lock(lockInfo);
CodedUrlHeader header = new CodedUrlHeader(DavConstants.HEADER_LOCK_TOKEN, lock.getToken());
response.setHeader(header.getHeaderName(), header.getHeaderValue());
DavPropertySet propSet = new DavPropertySet();
propSet.add(new LockDiscovery(lock));
response.sendXmlResponse(propSet, status);
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class AbstractWebdavServlet method service.
/**
* Service the given request.
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebdavRequest webdavRequest = new WebdavRequestImpl(request, getLocatorFactory(), isCreateAbsoluteURI());
// DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
int methodCode = DavMethods.getMethodCode(request.getMethod());
boolean noCache = DavMethods.isDeltaVMethod(webdavRequest) && !(DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode);
WebdavResponse webdavResponse = new WebdavResponseImpl(response, noCache);
try {
WebdavRequestContextHolder.setContext(new WebdavRequestContextImpl(webdavRequest));
// make sure there is a authenticated user
if (!getDavSessionProvider().attachSession(webdavRequest)) {
return;
}
// perform referrer host checks if CSRF protection is enabled
if (!csrfUtil.isValidRequest(webdavRequest)) {
webdavResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// support it (see JCR-4166)
if (!(webdavRequest instanceof ContentCodingAwareRequest)) {
List<String> ces = getContentCodings(request);
if (!ces.isEmpty()) {
webdavResponse.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
webdavResponse.setHeader("Accept-Encoding", "identity");
webdavResponse.setContentType("text/plain; charset=UTF-8");
webdavResponse.getWriter().println("Content-Encodings not supported, but received: " + ces);
webdavResponse.getWriter().flush();
}
}
// check matching if=header for lock-token relevant operations
DavResource resource = getResourceFactory().createResource(webdavRequest.getRequestLocator(), webdavRequest, webdavResponse);
if (!isPreconditionValid(webdavRequest, resource)) {
webdavResponse.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
if (!execute(webdavRequest, webdavResponse, methodCode, resource)) {
super.service(request, response);
}
} catch (DavException e) {
handleDavException(webdavRequest, webdavResponse, e);
} catch (IOException ex) {
Throwable cause = ex.getCause();
if (cause instanceof DavException) {
handleDavException(webdavRequest, webdavResponse, (DavException) cause);
} else {
throw ex;
}
} finally {
WebdavRequestContextHolder.clearContext();
getDavSessionProvider().releaseSession(webdavRequest);
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class SupportedPrivilege method getSupportedPrivilege.
/**
* Factory method to create/retrieve a <code>SupportedPrivilege</code> from the given
* DAV:privilege element.
*
* @param privilege
* @return
*/
static SupportedPrivilege getSupportedPrivilege(Element supportedPrivilege) throws DavException {
if (!DomUtil.matches(supportedPrivilege, XML_SUPPORTED_PRIVILEGE, SecurityConstants.NAMESPACE)) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:supported-privilege element expected.");
}
boolean isAbstract = false;
Privilege privilege = null;
String description = null;
String descriptionLanguage = null;
List<SupportedPrivilege> sp = new ArrayList<SupportedPrivilege>();
ElementIterator children = DomUtil.getChildren(supportedPrivilege);
while (children.hasNext()) {
Element child = children.next();
if (child.getLocalName().equals(XML_ABSTRACT)) {
isAbstract = true;
} else if (child.getLocalName().equals(Privilege.XML_PRIVILEGE)) {
privilege = Privilege.getPrivilege(child);
} else if (child.getLocalName().equals(XML_DESCRIPTION)) {
description = child.getLocalName();
if (child.hasAttribute(descriptionLanguage)) {
descriptionLanguage = child.getAttribute(descriptionLanguage);
}
} else if (child.getLocalName().equals(XML_SUPPORTED_PRIVILEGE)) {
sp.add(getSupportedPrivilege(child));
}
}
return new SupportedPrivilege(privilege, description, descriptionLanguage, isAbstract, sp.toArray(new SupportedPrivilege[sp.size()]));
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class LatestActivityVersionReport method init.
/**
* Check all the preconditions for this report.
*
* @throws DavException if a precondition is not met.
* @see Report#init(DavResource, ReportInfo)
*/
public void init(DavResource resource, ReportInfo info) throws DavException {
// validate info
if (!getType().isRequestedReportType(info)) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version element expected.");
}
// make sure the report is applied to a vh-resource
if (resource != null && (resource instanceof VersionHistoryResource)) {
vhResource = (VersionHistoryResource) resource;
} else {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version report can only be created for a version history resource.");
}
// make sure the DAV:href element inside the request body points to
// an activity resource (precondition for this report).
String activityHref = normalizeResourceHref(DomUtil.getText(info.getContentElement(DavConstants.XML_HREF, DavConstants.NAMESPACE)));
DavResourceLocator vhLocator = resource.getLocator();
DavResourceLocator activityLocator = vhLocator.getFactory().createResourceLocator(vhLocator.getPrefix(), activityHref);
activity = resource.getFactory().createResource(activityLocator, resource.getSession());
if (!(activity instanceof ActivityResource)) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version report: The DAV:href in the request body MUST identify an activity.");
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class ReportType method createReport.
/**
* Creates a new {@link Report} with this type.
*
* @return
* @throws DavException
*/
public Report createReport(DeltaVResource resource, ReportInfo info) throws DavException {
try {
Report report = reportClass.newInstance();
report.init(resource, info);
return report;
} catch (IllegalAccessException e) {
// should never occur
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create new report (" + reportClass.getName() + ") from class: " + e.getMessage());
} catch (InstantiationException e) {
// should never occur
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create new report (" + reportClass.getName() + ") from class: " + e.getMessage());
}
}
Aggregations