Example usage for javax.servlet.http HttpServletResponse SC_PRECONDITION_FAILED

List of usage examples for javax.servlet.http HttpServletResponse SC_PRECONDITION_FAILED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_PRECONDITION_FAILED.

Prototype

int SC_PRECONDITION_FAILED

To view the source code for javax.servlet.http HttpServletResponse SC_PRECONDITION_FAILED.

Click Source Link

Document

Status code (412) indicating that the precondition given in one or more of the request-header fields evaluated to false when it was tested on the server.

Usage

From source file:com.thinkberg.moxo.dav.DeleteHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = getResourceManager().getFileObject(request.getPathInfo());
    if (request instanceof Request) {
        String fragment = ((Request) request).getUri().getFragment();
        if (fragment != null) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }//  w w  w .  j  a  va  2 s. c  o  m
    }

    try {
        LockManager.getInstance().checkCondition(object, getIf(request));
    } catch (LockException e) {
        if (e.getLocks() != null) {
            response.sendError(SC_LOCKED);
        } else {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
        return;
    }

    if (object.exists()) {
        if (object.delete(ALL_FILES_SELECTOR) > 0) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:com.thinkberg.webdav.CopyMoveBase.java

/**
 * Handle a COPY or MOVE request.//  w w w .ja v  a 2 s.com
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error executing this request
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    boolean overwrite = getOverwrite(request);
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());
    FileObject targetObject = getDestination(request);

    try {
        final LockManager lockManager = LockManager.getInstance();
        LockManager.EvaluationResult evaluation = lockManager.evaluateCondition(targetObject, getIf(request));
        if (!evaluation.result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        if ("MOVE".equals(request.getMethod())) {
            evaluation = lockManager.evaluateCondition(object, getIf(request));
            if (!evaluation.result) {
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return;
            }
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (null == targetObject) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (object.equals(targetObject)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (targetObject.exists()) {
        if (!overwrite) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } else {
        FileObject targetParent = targetObject.getParent();
        if (!targetParent.exists() || !FileType.FOLDER.equals(targetParent.getType())) {
            response.sendError(HttpServletResponse.SC_CONFLICT);
        }
        response.setStatus(HttpServletResponse.SC_CREATED);
    }

    // delegate the actual execution to a sub class
    copyOrMove(object, targetObject, getDepth(request));
}

From source file:org.squashtest.tm.web.internal.exceptionresolver.HandlerGenericValidation.java

@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) {
    if (exceptionIsHandled(ex) && clientAcceptsJson(request)) {
        response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);

        ConstraintViolationException cve = (ConstraintViolationException) ex; // NOSONAR Type was checked earlier
        List<FieldValidationErrorModel> errors = buildFieldValidationErrors(cve);

        return new ModelAndView(new MappingJackson2JsonView(), "fieldValidationErrors", errors);
    }// w ww  . j  a v a 2 s.com

    return null;
}

From source file:com.thinkberg.webdav.DeleteHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {// w  w  w  . j a v a  2  s  .co  m
        String fragment = new URI(request.getRequestURI()).getFragment();
        if (fragment != null) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    } catch (URISyntaxException e) {
        throw new IOException(e.getMessage());
    }

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        int deletedObjects = object.delete(ALL_FILES_SELECTOR);
        LOG.debug("deleted " + deletedObjects + " objects");
        if (deletedObjects > 0) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.efaps.webdav4vfs.handler.AbstractCopyMoveBaseHandler.java

/**
 * Handle a COPY or MOVE request./*from  w w  w . j  a  v  a 2 s. co m*/
 *
 * @param _request      HTTP servlet request
 * @param _response     HTTP servlet response
 * @throws IOException if there is an error executing this request
 */
@Override
public void service(final HttpServletRequest _request, final HttpServletResponse _response) throws IOException {
    boolean overwrite = getOverwrite(_request);
    FileObject object = VFSBackend.resolveFile(_request.getPathInfo());
    FileObject targetObject = getDestination(_request);

    try {
        final LockManager lockManager = LockManager.getInstance();
        LockManager.EvaluationResult evaluation = lockManager.evaluateCondition(targetObject, getIf(_request));
        if (!evaluation.result) {
            _response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        if ("MOVE".equals(_request.getMethod())) {
            evaluation = lockManager.evaluateCondition(object, getIf(_request));
            if (!evaluation.result) {
                _response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return;
            }
        }
    } catch (LockException e) {
        _response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        _response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (null == targetObject) {
        _response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (object.equals(targetObject)) {
        _response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (targetObject.exists()) {
        if (!overwrite) {
            _response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        _response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } else {
        FileObject targetParent = targetObject.getParent();
        if (!targetParent.exists() || !FileType.FOLDER.equals(targetParent.getType())) {
            _response.sendError(HttpServletResponse.SC_CONFLICT);
        }
        _response.setStatus(HttpServletResponse.SC_CREATED);
    }

    // delegate the actual execution to a sub class
    this.copyOrMove(object, targetObject, getDepth(_request));
}

From source file:com.thinkberg.webdav.LockHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {// www.  j av  a2  s  .c o m
        final LockManager manager = LockManager.getInstance();
        final LockManager.EvaluationResult evaluation = manager.evaluateCondition(object, getIf(request));
        if (!evaluation.result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        } else {
            if (!evaluation.locks.isEmpty()) {
                LOG.debug(String.format("discovered locks: %s", evaluation.locks));
                sendLockAcquiredResponse(response, evaluation.locks.get(0));
                return;
            }
        }
    } catch (LockConflictException e) {
        List<Lock> locks = e.getLocks();
        for (Lock lock : locks) {
            if (Lock.EXCLUSIVE.equals(lock.getType())) {
                response.sendError(SC_LOCKED);
                return;
            }
        }
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    try {
        SAXReader saxReader = new SAXReader();
        Document lockInfo = saxReader.read(request.getInputStream());
        //log(lockInfo);

        Element rootEl = lockInfo.getRootElement();
        String lockScope = null, lockType = null;
        Object owner = null;
        Iterator elIt = rootEl.elementIterator();
        while (elIt.hasNext()) {
            Element el = (Element) elIt.next();
            if (TAG_LOCKSCOPE.equals(el.getName())) {
                lockScope = el.selectSingleNode("*").getName();
            } else if (TAG_LOCKTYPE.equals(el.getName())) {
                lockType = el.selectSingleNode("*").getName();
            } else if (TAG_OWNER.equals(el.getName())) {
                // TODO correctly handle owner
                Node subEl = el.selectSingleNode("*");
                if (subEl != null && TAG_HREF.equals(subEl.getName())) {
                    owner = new URL(el.selectSingleNode("*").getText());
                } else {
                    owner = el.getText();
                }
            }
        }

        LOG.debug("LOCK(" + lockType + ", " + lockScope + ", " + owner + ")");

        Lock requestedLock = new Lock(object, lockType, lockScope, owner, getDepth(request),
                getTimeout(request));
        try {
            LockManager.getInstance().acquireLock(requestedLock);
            sendLockAcquiredResponse(response, requestedLock);
        } catch (LockConflictException e) {
            response.sendError(SC_LOCKED);
        } catch (IllegalArgumentException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } catch (DocumentException e) {
        e.printStackTrace();
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:com.thinkberg.webdav.PropPatchHandler.java

/**
 * Handle a PROPPATCH request./*from  w ww  . j  ava2s . c o  m*/
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error that cannot be handled normally
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        SAXReader saxReader = new SAXReader();
        try {
            Document propDoc = saxReader.read(request.getInputStream());
            logXml(propDoc);

            Element propUpdateEl = propDoc.getRootElement();
            List<Element> requestedProperties = new ArrayList<Element>();
            for (Object elObject : propUpdateEl.elements()) {
                Element el = (Element) elObject;
                String command = el.getName();
                if (AbstractDavResource.TAG_PROP_SET.equals(command)
                        || AbstractDavResource.TAG_PROP_REMOVE.equals(command)) {
                    for (Object propElObject : el.elements()) {
                        for (Object propNameElObject : ((Element) propElObject).elements()) {
                            Element propNameEl = (Element) propNameElObject;
                            requestedProperties.add(propNameEl);
                        }
                    }
                }
            }

            // respond as XML encoded multi status
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(SC_MULTI_STATUS);

            Document multiStatusResponse = getMultiStatusResponse(object, requestedProperties,
                    getBaseUrl(request));

            logXml(multiStatusResponse);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(multiStatusResponse);
            writer.flush();
            writer.close();

        } catch (DocumentException e) {
            LOG.error("invalid request: " + e.getMessage());
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        LOG.error(object.getName().getPath() + " NOT FOUND");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.efaps.webdav4vfs.handler.DeleteHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {//from  ww w  .j a v  a  2 s  .c  om
        String fragment = new URI(request.getRequestURI()).getFragment();
        if (fragment != null) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    } catch (URISyntaxException e) {
        throw new IOException(e.getMessage());
    }

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        int deletedObjects = object.delete(ALL_FILES_SELECTOR);
        LOG.debug("deleted " + deletedObjects + " objects");
        if (deletedObjects > 0) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.efaps.webdav4vfs.handler.PropPatchHandler.java

/**
 * Handle a PROPPATCH request./*from w  w  w. j a  v  a 2  s. c  o  m*/
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error that cannot be handled normally
 */
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        SAXReader saxReader = new SAXReader();
        try {
            Document propDoc = saxReader.read(request.getInputStream());
            logXml(propDoc);

            Element propUpdateEl = propDoc.getRootElement();
            List<Element> requestedProperties = new ArrayList<Element>();
            for (Object elObject : propUpdateEl.elements()) {
                Element el = (Element) elObject;
                String command = el.getName();
                if (AbstractDavResource.TAG_PROP_SET.equals(command)
                        || AbstractDavResource.TAG_PROP_REMOVE.equals(command)) {
                    for (Object propElObject : el.elements()) {
                        for (Object propNameElObject : ((Element) propElObject).elements()) {
                            Element propNameEl = (Element) propNameElObject;
                            requestedProperties.add(propNameEl);
                        }
                    }
                }
            }

            // respond as XML encoded multi status
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(SC_MULTI_STATUS);

            Document multiStatusResponse = getMultiStatusResponse(object, requestedProperties,
                    getBaseUrl(request));

            logXml(multiStatusResponse);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(multiStatusResponse);
            writer.flush();
            writer.close();

        } catch (DocumentException e) {
            LOG.error("invalid request: " + e.getMessage());
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        LOG.error(object.getName().getPath() + " NOT FOUND");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.efaps.webdav4vfs.handler.LockHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {//w ww.  jav  a  2s.  c om
        final LockManager manager = LockManager.getInstance();
        final LockManager.EvaluationResult evaluation = manager.evaluateCondition(object, getIf(request));
        if (!evaluation.result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        } else {
            if (!evaluation.locks.isEmpty()) {
                LOG.debug(String.format("discovered locks: %s", evaluation.locks));
                sendLockAcquiredResponse(response, evaluation.locks.get(0));
                return;
            }
        }
    } catch (LockConflictException e) {
        List<Lock> locks = e.getLocks();
        for (Lock lock : locks) {
            if (Lock.EXCLUSIVE.equals(lock.getType())) {
                response.sendError(SC_LOCKED);
                return;
            }
        }
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    try {
        SAXReader saxReader = new SAXReader();
        Document lockInfo = saxReader.read(request.getInputStream());
        //log(lockInfo);

        Element rootEl = lockInfo.getRootElement();
        String lockScope = null, lockType = null;
        Object owner = null;
        Iterator<?> elIt = rootEl.elementIterator();
        while (elIt.hasNext()) {
            Element el = (Element) elIt.next();
            if (TAG_LOCKSCOPE.equals(el.getName())) {
                lockScope = el.selectSingleNode("*").getName();
            } else if (TAG_LOCKTYPE.equals(el.getName())) {
                lockType = el.selectSingleNode("*").getName();
            } else if (TAG_OWNER.equals(el.getName())) {
                // TODO correctly handle owner
                Node subEl = el.selectSingleNode("*");
                if (subEl != null && TAG_HREF.equals(subEl.getName())) {
                    owner = new URL(el.selectSingleNode("*").getText());
                } else {
                    owner = el.getText();
                }
            }
        }

        LOG.debug("LOCK(" + lockType + ", " + lockScope + ", " + owner + ")");

        Lock requestedLock = new Lock(object, lockType, lockScope, owner, getDepth(request),
                getTimeout(request));
        try {
            LockManager.getInstance().acquireLock(requestedLock);
            sendLockAcquiredResponse(response, requestedLock);
        } catch (LockConflictException e) {
            response.sendError(SC_LOCKED);
        } catch (IllegalArgumentException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } catch (DocumentException e) {
        e.printStackTrace();
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}