public JsonObject execute(JsonObject request, Session session) {
    JsonObject response = null;
    try {
      logger.debug("[execute] ");
      ThreadLocalVariablesKeeper.setRequest(request);
      ThreadLocalVariablesKeeper.setSession(session);
      response = tryExecute(request, session);
      logger.debug("[execute] building final response");
    } catch (HiveException ex) {
      response = JsonMessageBuilder.createError(ex).build();
    } catch (ConstraintViolationException ex) {
      response =
          JsonMessageBuilder.createErrorResponseBuilder(
                  HttpServletResponse.SC_BAD_REQUEST, ex.getMessage())
              .build();
    } catch (org.hibernate.exception.ConstraintViolationException ex) {
      response =
          JsonMessageBuilder.createErrorResponseBuilder(
                  HttpServletResponse.SC_CONFLICT, ex.getMessage())
              .build();
    } catch (JsonParseException ex) {
      response =
          JsonMessageBuilder.createErrorResponseBuilder(
                  HttpServletResponse.SC_BAD_REQUEST, Messages.INVALID_REQUEST_PARAMETERS)
              .build();
    } catch (OptimisticLockException 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) {
      response =
          JsonMessageBuilder.createErrorResponseBuilder(
                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage())
              .build();
    } finally {
      ThreadLocalVariablesKeeper.setRequest(null);
      ThreadLocalVariablesKeeper.setSession(null);
    }

    logger.debug("[execute] building final response");
    return new JsonMessageBuilder()
        .addAction(request.get(JsonMessageBuilder.ACTION).getAsString())
        .addRequestId(request.get(JsonMessageBuilder.REQUEST_ID))
        .include(response)
        .build();
  }
Exemple #2
0
  public void testInsertWithEmbedded() {
    try {
      EntityManager em = getEM();
      EntityTransaction tx = em.getTransaction();
      try {
        tx.begin();
        ValidatedOwner owner = new ValidatedOwner(1, "First Owner");
        ValidatedPet pet = new ValidatedPet(null);
        owner.setPet(pet);
        em.persist(owner);
        tx.commit();
        fail("Should have thrown exception with null embedded field, but persisted!");
      } catch (ConstraintViolationException cve) {
        // Expected
        LOG.info(
            "Threw exception when attempting persist of object with null NotNull embedded field");
      } finally {
        if (tx.isActive()) {
          tx.rollback();
        }
        em.close();
      }

      em = getEM();
      tx = em.getTransaction();
      try {
        tx.begin();
        ValidatedOwner owner = new ValidatedOwner(1, "First Owner");
        ValidatedPet pet = new ValidatedPet("Fluffy");
        owner.setPet(pet);
        em.persist(owner);
        tx.commit();
      } catch (ConstraintViolationException cve) {
        LOG.error("Exception in persist of object with transient not null field", cve);
        fail("Should have persisted ok but exception thrown : " + cve.getMessage());
      } finally {
        if (tx.isActive()) {
          tx.rollback();
        }
        em.close();
      }
    } finally {
      clean(ValidatedOwner.class);
    }
  }
Exemple #3
0
  public void testInsertTransientNotNull() {
    try {
      EntityManager em = getEM();
      EntityTransaction tx = em.getTransaction();
      try {
        tx.begin();
        ValidatedPerson2 p1 = new ValidatedPerson2(1, "Fred", "Bloggs");
        em.persist(p1);
        tx.commit();
        fail("Should have thrown exception with null transient field, but persisted!");
      } catch (ConstraintViolationException cve) {
        // Expected
        LOG.info(
            "Threw exception when attempting persist of object with null NotNull transient field");
      } finally {
        if (tx.isActive()) {
          tx.rollback();
        }
        em.close();
      }

      em = getEM();
      tx = em.getTransaction();
      try {
        tx.begin();
        ValidatedPerson2 p1 = new ValidatedPerson2(1, "Fred", "Bloggs");
        p1.setPassword("topsecret");
        em.persist(p1);
        tx.commit();
      } catch (ConstraintViolationException cve) {
        LOG.error("Exception in persist of object with transient not null field", cve);
        fail("Should have persisted ok but exception thrown : " + cve.getMessage());
      } finally {
        if (tx.isActive()) {
          tx.rollback();
        }
        em.close();
      }
    } finally {
      clean(ValidatedPerson2.class);
    }
  }
 /**
  * {@inheritDoc}
  *
  * @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
  */
 @Override
 public Response toResponse(ConstraintViolationException e) {
   BasicLogger.logError(e.getClass().getName(), e.getMessage());
   return Response.status(Status.BAD_REQUEST).build();
 }
 @JsonProperty("cause")
 public String getCause() {
   return cve.getMessage();
 }