Search in sources :

Example 1 with WebdavRequest

use of org.apache.jackrabbit.webdav.WebdavRequest in project archiva by apache.

the class RepositoryServlet method service.

/**
 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
 * to support different realms
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws java.io.IOException
 */
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    WebdavRequest webdavRequest = new WebdavRequestImpl(request, getLocatorFactory());
    // 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);
    DavResource resource = null;
    try {
        // make sure there is a authenticated user
        if (!getDavSessionProvider().attachSession(webdavRequest)) {
            return;
        }
        // check matching if=header for lock-token relevant operations
        resource = getResourceFactory().createResource(webdavRequest.getRequestLocator(), webdavRequest, webdavResponse);
        if (!isPreconditionValid(webdavRequest, resource)) {
            webdavResponse.sendError(DavServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        if (!execute(webdavRequest, webdavResponse, methodCode, resource)) {
            super.service(request, response);
        }
    } catch (UnauthorizedDavException e) {
        webdavResponse.setHeader("WWW-Authenticate", getAuthenticateHeaderValue(e.getRepositoryName()));
        webdavResponse.sendError(e.getErrorCode(), e.getStatusPhrase());
    } catch (BrowserRedirectException e) {
        response.sendRedirect(e.getLocation());
    } catch (DavException e) {
        if (e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED) {
            final String msg = "Should throw " + UnauthorizedDavException.class.getName();
            log.error(msg);
            webdavResponse.sendError(e.getErrorCode(), msg);
        } else if (e.getCause() != null) {
            webdavResponse.sendError(e.getErrorCode(), e.getCause().getMessage());
        } else {
            webdavResponse.sendError(e.getErrorCode(), e.getMessage());
        }
    } finally {
        getDavSessionProvider().releaseSession(webdavRequest);
    }
}
Also used : WebdavRequest(org.apache.jackrabbit.webdav.WebdavRequest) DavResource(org.apache.jackrabbit.webdav.DavResource) WebdavResponseImpl(org.apache.jackrabbit.webdav.WebdavResponseImpl) DavException(org.apache.jackrabbit.webdav.DavException) WebdavRequestImpl(org.apache.jackrabbit.webdav.WebdavRequestImpl) WebdavResponse(org.apache.jackrabbit.webdav.WebdavResponse)

Example 2 with WebdavRequest

use of org.apache.jackrabbit.webdav.WebdavRequest 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);
    }
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) WebdavResponseImpl(org.apache.jackrabbit.webdav.WebdavResponseImpl) DavException(org.apache.jackrabbit.webdav.DavException) WebdavRequestImpl(org.apache.jackrabbit.webdav.WebdavRequestImpl) IOException(java.io.IOException) ContentCodingAwareRequest(org.apache.jackrabbit.webdav.ContentCodingAwareRequest) WebdavRequest(org.apache.jackrabbit.webdav.WebdavRequest) WebdavResponse(org.apache.jackrabbit.webdav.WebdavResponse)

Example 3 with WebdavRequest

use of org.apache.jackrabbit.webdav.WebdavRequest in project jackrabbit by apache.

the class AbstractReport method normalizeResourceHref.

/**
 * Normalize the resource {@code href}. For example, remove contextPath prefix if found.
 * @param href resource href
 * @return normalized resource {@code href}
 */
protected String normalizeResourceHref(final String href) {
    if (href == null) {
        return href;
    }
    final WebdavRequestContext requestContext = WebdavRequestContextHolder.getContext();
    final WebdavRequest request = (requestContext != null) ? requestContext.getRequest() : null;
    if (request == null) {
        log.error("WebdavRequest is unavailable in the current execution context.");
        return href;
    }
    final String contextPath = request.getContextPath();
    if (!contextPath.isEmpty() && href.startsWith(contextPath)) {
        return href.substring(contextPath.length());
    }
    return href;
}
Also used : WebdavRequest(org.apache.jackrabbit.webdav.WebdavRequest) WebdavRequestContext(org.apache.jackrabbit.webdav.WebdavRequestContext)

Example 4 with WebdavRequest

use of org.apache.jackrabbit.webdav.WebdavRequest in project jackrabbit by apache.

the class AbstractResource method normalizeResourceHref.

/**
 * Normalize the resource {@code href}. For example, remove contextPath prefix if found.
 * @param href resource href
 * @return normalized resource {@code href}
 */
protected String normalizeResourceHref(final String href) {
    if (href == null) {
        return href;
    }
    final WebdavRequestContext requestContext = WebdavRequestContextHolder.getContext();
    final WebdavRequest request = (requestContext != null) ? requestContext.getRequest() : null;
    if (request == null) {
        log.error("WebdavRequest is unavailable in the current execution context.");
        return href;
    }
    final String contextPath = request.getContextPath();
    if (!contextPath.isEmpty() && href.startsWith(contextPath)) {
        return href.substring(contextPath.length());
    }
    return href;
}
Also used : WebdavRequest(org.apache.jackrabbit.webdav.WebdavRequest) WebdavRequestContext(org.apache.jackrabbit.webdav.WebdavRequestContext)

Aggregations

WebdavRequest (org.apache.jackrabbit.webdav.WebdavRequest)4 DavException (org.apache.jackrabbit.webdav.DavException)2 DavResource (org.apache.jackrabbit.webdav.DavResource)2 WebdavRequestContext (org.apache.jackrabbit.webdav.WebdavRequestContext)2 WebdavRequestImpl (org.apache.jackrabbit.webdav.WebdavRequestImpl)2 WebdavResponse (org.apache.jackrabbit.webdav.WebdavResponse)2 WebdavResponseImpl (org.apache.jackrabbit.webdav.WebdavResponseImpl)2 IOException (java.io.IOException)1 ContentCodingAwareRequest (org.apache.jackrabbit.webdav.ContentCodingAwareRequest)1