Example usage for javax.servlet.http HttpServletResponse SC_CONFLICT

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

Introduction

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

Prototype

int SC_CONFLICT

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

Click Source Link

Document

Status code (409) indicating that the request could not be completed due to a conflict with the current state of the resource.

Usage

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

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

    try {/*from   w ww  . j a  v a  2 s .  c  o m*/
        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;
    }
    // it is forbidden to write data on a folder
    if (object.exists() && FileType.FOLDER.equals(object.getType())) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    FileObject parent = object.getParent();
    if (!parent.exists()) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (!FileType.FOLDER.equals(parent.getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    InputStream is = request.getInputStream();
    OutputStream os = object.getContent().getOutputStream();
    long bytesCopied = IOUtils.copyLarge(is, os);
    String contentLengthHeader = request.getHeader("Content-length");
    LOG.debug(String.format("sent %d/%s bytes", bytesCopied,
            contentLengthHeader == null ? "unknown" : contentLengthHeader));
    os.flush();
    object.close();

    response.setStatus(HttpServletResponse.SC_CREATED);
}

From source file:org.opentestsystem.delivery.testreg.rest.StudentPackageController.java

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/studentpackage", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
@Secured({ "ROLE_Entity Read" })
@ResponseBody// w  w w  .  j  av  a2 s. c  om
public void extractStudentPackage(@RequestParam(value = "ssid", required = false) final String studentId,
        @RequestParam(value = "externalId", required = false) final String externalSsid,
        @RequestParam("stateabbreviation") final String stateAbbreviation, final HttpServletResponse response)
        throws IOException {
    StopWatch sw = new StopWatch();
    sw.start();
    Student student = null;

    if (hasText(studentId) && hasText(externalSsid)) {
        response.setStatus(HttpServletResponse.SC_CONFLICT);
    } else if (hasText(studentId)) {
        student = studentService.findByStudentIdAndStateAbbreviation(studentId, stateAbbreviation);
    } else if (hasText(externalSsid)) {
        student = studentService.findByExternalSsidAndStateAbbreviation(externalSsid, stateAbbreviation);
    }

    if (student != null) {
        String studentPackage = studentPackageService.exportStudentPackage(student);
        response.setContentType(MediaType.APPLICATION_XML_VALUE);
        ServletOutputStream out = response.getOutputStream();
        IOUtils.copy(new ByteArrayInputStream(studentPackage.getBytes()), out);
        out.flush();
    } else {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
    sw.stop();
    this.metricClient.sendPerformanceMetricToMna("StudentPackage for " + externalSsid + " (ms) ", sw.getTime());
}

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

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    boolean overwrite = getOverwrite(request);
    FileObject object = getResourceManager().getFileObject(request.getPathInfo());
    FileObject targetObject = getDestination(request);

    try {/* ww w  .j  a  v  a 2  s.  c  om*/
        // check that we can write the target
        LockManager.getInstance().checkCondition(targetObject, getIf(request));
        // if we move, check that we can actually write on the source
        if ("MOVE".equals(request.getMethod())) {
            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 (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);
    }

    copyOrMove(object, targetObject, getDepth(request));
}

From source file:org.eclipse.ecr.core.storage.sql.net.BinaryManagerServlet.java

/**
 * Creates a new binary.//from  w  w w.  ja  va2s .  com
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        String digest = getDigest(req);
        Binary binary = binaryManager.getBinary(req.getInputStream());
        if (!binary.getDigest().equals(digest)) {
            resp.sendError(HttpServletResponse.SC_CONFLICT,
                    "Digest mismatch: '" + digest + "' vs '" + binary.getDigest() + "'");
        } else {
            resp.setStatus(HttpServletResponse.SC_CREATED);
        }
    } catch (IOException e) {
        log.error(e, e);
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
}

From source file:com.devicehive.websockets.WebSocketResponseBuilder.java

public JsonObject buildResponse(JsonObject request, WebSocketSession session) {
    JsonObject response;//from w ww.ja  v  a  2s .c  o m
    try {
        response = requestProcessor.process(request, session).getResponseAsJson();
    } catch (BadCredentialsException ex) {
        logger.error("Unauthorized access", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_UNAUTHORIZED, "Invalid credentials").build();
    } catch (AccessDeniedException ex) {
        logger.error("Access to action is denied", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized").build();
    } catch (HiveException ex) {
        logger.error("Error executing the request", ex);
        response = JsonMessageBuilder.createError(ex).build();
    } catch (ConstraintViolationException ex) {
        logger.error("Error executing the request", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()).build();
    } catch (org.hibernate.exception.ConstraintViolationException ex) {
        logger.error("Error executing the request", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_CONFLICT, ex.getMessage()).build();
    } catch (JsonParseException ex) {
        logger.error("Error executing the request", ex);
        response = JsonMessageBuilder.createErrorResponseBuilder(HttpServletResponse.SC_BAD_REQUEST,
                Messages.INVALID_REQUEST_PARAMETERS).build();
    } catch (OptimisticLockException ex) {
        logger.error("Error executing the request", ex);
        logger.error("Data conflict", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_CONFLICT, Messages.CONFLICT_MESSAGE).build();
    } catch (PersistenceException ex) {
        if (ex.getCause() instanceof org.hibernate.exception.ConstraintViolationException) {
            response = JsonMessageBuilder
                    .createErrorResponseBuilder(HttpServletResponse.SC_CONFLICT, ex.getMessage()).build();
        } else {
            response = JsonMessageBuilder
                    .createErrorResponseBuilder(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage())
                    .build();
        }
    } catch (Exception ex) {
        logger.error("Error executing the request", ex);
        response = JsonMessageBuilder
                .createErrorResponseBuilder(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage())
                .build();
    }

    return new JsonMessageBuilder().addAction(request.get(JsonMessageBuilder.ACTION))
            .addRequestId(request.get(JsonMessageBuilder.REQUEST_ID)).include(response).build();
}

From source file:de.tub.av.pe.xcapsrv.XCAPResultFactory.java

private static XCAPResult conflictUniquenessFailure() {
    XCAPResult result = new XCAPResult();
    result.setStatusCode(HttpServletResponse.SC_CONFLICT);
    StringBuilder content = new StringBuilder(ERROR_DOCUMENT_PREFIX);
    content.append("<" + XDMSConstants.UNIQUENESS_FAILURE + "/>");
    content.append(ERROR_DOCUMENT_SUFFIX);
    result.setBody(content.toString());/*ww w . j av  a  2  s .co  m*/
    result.setMimeType(XDMSConstants.MIME_TYPE_CONFLICT);
    return result;
}

From source file:au.org.ala.biocache.web.AbstractSecureController.java

/**
  * Returns true when the operation should be performed.
  * @param apiKey/*  ww  w  . ja  v  a 2s  . c  o  m*/
  * @param response
  * @return
  * @throws Exception
  */
public boolean shouldPerformOperation(String apiKey, HttpServletResponse response, boolean checkReadOnly)
        throws Exception {
    if (checkReadOnly && Store.isReadOnly()) {
        response.sendError(HttpServletResponse.SC_CONFLICT, "Server is in read only mode.  Try again later.");
    } else if (apiKey == null || !isValidKey(apiKey)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "An invalid API Key was provided.");
    }
    return !response.isCommitted();
}

From source file:be.usgictprofessionals.usgfinancewebapp.restresources.RESTDataResources.java

/**
 *
 * @param response/*from   w  w w. ja v  a2 s  .co m*/
 * @param id
 * @return XmlRootElement class which will automatically be translated into
 * JSON. 409 error code will be send if there hasn't been any input
 * get the data required to fill in the fields on the input page after data submission
 */
@GET
@Path("/input/{id}")
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<InputData> returnInput(@Context final HttpServletResponse response,
        @PathParam("id") String id) {
    if (!DataDAO.getInstance().inputHasBeenReceived()) {
        response.setStatus(HttpServletResponse.SC_CONFLICT);
    }
    return DataDAO.getInstance().getInputReturn(Integer.parseInt(id));
}

From source file:alfio.controller.api.admin.UtilsApiController.java

@RequestMapping(value = "/short-name/validate", method = POST)
public boolean validateShortName(@RequestParam("shortName") String shortName, HttpServletResponse response) {
    boolean unique = eventNameManager.isUnique(shortName);
    if (!unique) {
        response.setStatus(HttpServletResponse.SC_CONFLICT);
    }//  ww w.  jav  a 2 s .  c  o m
    return unique;
}

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

/**
 * Handle a COPY or MOVE request.//from  www.j a va2  s  .c om
 *
 * @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));
}