use of org.apache.jackrabbit.webdav.jcr.JcrDavException in project jackrabbit by apache.
the class SearchResourceImpl method getQuery.
/**
* Create a query from the information present in the <code>sInfo</code>
* object.<br>The following JCR specific logic is applied:
* <ul>
* <li>If the requested resource represents a node with nodetype nt:query, the
* request body is ignored and the query defined with the node is executed
* instead.</li>
* <li>If the requested resource does not represent an existing item, the
* specified query is persisted by calling {@link Query#storeAsNode(String)}.</li>
* </ul>
* @param sInfo defining the query to be executed
* @return <code>Query</code> object.
* @throws javax.jcr.query.InvalidQueryException if the query defined by <code>sInfo</code> is invalid
* @throws RepositoryException the query manager cannot be accessed or if
* another error occurs.
* @throws DavException if <code>sInfo</code> is <code>null</code> and
* the underlying repository item is not an nt:query node or if an error
* occurs when calling {@link Query#storeAsNode(String)}/
*/
private Query getQuery(SearchInfo sInfo) throws InvalidQueryException, RepositoryException, DavException {
Session session = getRepositorySession();
NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
Node rootNode = session.getRootNode();
QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
// test if query is defined by requested repository node
String itemPath = locator.getRepositoryPath();
if (itemPath != null && !rootNode.getPath().equals(itemPath)) {
String qNodeRelPath = itemPath.substring(1);
if (rootNode.hasNode(qNodeRelPath)) {
Node qNode = rootNode.getNode(qNodeRelPath);
if (qNode.isNodeType(JcrConstants.NT_QUERY)) {
return qMgr.getQuery(qNode);
}
}
}
Query q;
if (sInfo != null) {
// apply namespace mappings to session
Map<String, String> namespaces = sInfo.getNamespaces();
try {
for (Map.Entry<String, String> entry : namespaces.entrySet()) {
String prefix = entry.getKey();
String uri = entry.getValue();
session.setNamespacePrefix(prefix, uri);
}
q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
if (SearchInfo.NRESULTS_UNDEFINED != sInfo.getNumberResults()) {
q.setLimit(sInfo.getNumberResults());
}
if (SearchInfo.OFFSET_UNDEFINED != sInfo.getOffset()) {
q.setOffset(sInfo.getOffset());
}
} finally {
// reset namespace mappings
for (String uri : namespaces.values()) {
try {
session.setNamespacePrefix(nsReg.getPrefix(uri), uri);
} catch (RepositoryException e) {
log.warn("Unable to reset mapping of namespace: " + uri);
}
}
}
} else {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, locator.getResourcePath() + " is not a nt:query node -> searchRequest body required.");
}
/* test if resource path does not exist -> thus indicating that
the query must be made persistent by calling Query.save(String) */
if (itemPath != null && !getRepositorySession().itemExists(itemPath)) {
try {
q.storeAsNode(itemPath);
} catch (RepositoryException e) {
// ItemExistsException should never occur.
throw new JcrDavException(e);
}
}
return q;
}
use of org.apache.jackrabbit.webdav.jcr.JcrDavException in project jackrabbit by apache.
the class DavResourceImpl method bind.
/**
* @see BindableResource#rebind(DavResource, DavResource)
*/
public void bind(DavResource collection, DavResource newBinding) throws DavException {
if (!exists()) {
// DAV:bind-source-exists
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
if (isLocked(collection)) {
// DAV:locked-update-allowed?
throw new DavException(DavServletResponse.SC_LOCKED);
}
if (isFilteredResource(newBinding)) {
throw new DavException(DavServletResponse.SC_FORBIDDEN);
}
checkSameWorkspace(collection.getLocator());
try {
if (!node.isNodeType(MIX_SHAREABLE)) {
if (!node.canAddMixin(MIX_SHAREABLE)) {
// DAV:binding-allowed
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
node.addMixin(MIX_SHAREABLE);
node.save();
}
Workspace workspace = session.getRepositorySession().getWorkspace();
workspace.clone(workspace.getName(), node.getPath(), newBinding.getLocator().getRepositoryPath(), false);
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
}
use of org.apache.jackrabbit.webdav.jcr.JcrDavException in project jackrabbit by apache.
the class DavResourceImpl method addMember.
/**
* Adds a new member to this resource.
*
* @see DavResource#addMember(DavResource, org.apache.jackrabbit.webdav.io.InputContext)
*/
public void addMember(DavResource member, InputContext inputContext) throws DavException {
if (!exists()) {
throw new DavException(DavServletResponse.SC_CONFLICT);
}
if (isLocked(this) || isLocked(member)) {
throw new DavException(DavServletResponse.SC_LOCKED);
}
try {
// item or if the new resource would be filtered out
if (isFilteredResource(member) || node.getDefinition().isProtected()) {
log.debug("Forbidden to add member: " + member.getDisplayName());
throw new DavException(DavServletResponse.SC_FORBIDDEN);
}
String memberName = Text.getName(member.getLocator().getRepositoryPath());
ImportContext ctx = getImportContext(inputContext, memberName);
if (!config.getIOManager().importContent(ctx, member)) {
// any changes should have been reverted in the importer
throw new DavException(DavServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
}
// persist changes after successful import
node.save();
} catch (RepositoryException e) {
log.error("Error while importing resource: " + e.toString());
throw new JcrDavException(e);
} catch (IOException e) {
log.error("Error while importing resource: " + e.toString());
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
use of org.apache.jackrabbit.webdav.jcr.JcrDavException in project jackrabbit by apache.
the class DavSessionProviderImpl method attachSession.
/**
* Acquires a DavSession. Upon success, the WebdavRequest will
* reference that session.
*
* A session will not be available if an exception is thrown.
*
* @param request
* @throws org.apache.jackrabbit.webdav.DavException if a problem occurred while obtaining the session
* @see DavSessionProvider#attachSession(org.apache.jackrabbit.webdav.WebdavRequest)
*/
public boolean attachSession(WebdavRequest request) throws DavException {
try {
// retrieve the workspace name
String workspaceName = request.getRequestLocator().getWorkspaceName();
// empty workspaceName rather means default -> must be 'null'
if (workspaceName != null && "".equals(workspaceName)) {
workspaceName = null;
}
// login to repository
Session repSession = sesProvider.getSession(request, repository, workspaceName);
if (repSession == null) {
log.debug("Could not to retrieve a repository session.");
return false;
}
DavSession ds = new DavSessionImpl(repSession);
log.debug("Attaching session '" + ds + "' to request '" + request + "'");
request.setDavSession(ds);
return true;
} catch (NoSuchWorkspaceException e) {
// which seems not appropriate here
throw new JcrDavException(e, DavServletResponse.SC_NOT_FOUND);
} catch (RepositoryException e) {
throw new JcrDavException(e);
} catch (ServletException e) {
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
use of org.apache.jackrabbit.webdav.jcr.JcrDavException in project jackrabbit by apache.
the class ExportViewReport method init.
/**
* @see Report#init(DavResource, ReportInfo)
*/
@Override
public void init(DavResource resource, ReportInfo info) throws DavException {
// delegate validation to super class
super.init(resource, info);
// report specific validation: resource must represent an existing
// repository node
absNodePath = resource.getLocator().getRepositoryPath();
try {
if (!(getRepositorySession().itemExists(absNodePath) && getRepositorySession().getItem(absNodePath).isNode())) {
throw new JcrDavException(new PathNotFoundException(absNodePath + " does not exist."));
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
}
Aggregations