@POST
 @Path("login/facebook")
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
 public Response realizarLoginFacebook(String json) {
   Response response = null;
   try {
     Prestador prestador = new Prestador();
     configurarUsuario(prestador, new JSONObject(json));
     configurarPrestador(prestador);
     Prestador usuarioPesquisado = prestadorSevice.findByEmail(prestador.getEmail());
     if (usuarioPesquisado == null) {
       prestadorSevice.create(prestador);
     } else {
       if (!usuarioPesquisado.getFacebookId().equals(prestador.getFacebookId())) {
         prestadorSevice.update(prestador);
         ;
       }
       prestador.setSenha(usuarioPesquisado.getSenha());
     }
     response = configurarResponse(prestador);
   } catch (Exception e) {
     response =
         CallBackUtil.setResponseError(
             Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
   }
   return response;
 }
Exemplo n.º 2
0
  public CustomException(int status, String message) {
    super(message, Response.status(new CustomStatus(status, message)).build());

    if (status == Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
      log.severe(status + ", " + message);
    } else {
      log.warning(status + ", " + message);
    }
  }
  @Override
  public Response toResponse(Throwable exception) {
    ErrorMessage errorMessage = new ErrorMessage();
    errorMessage.setErrorCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
    errorMessage.setErrorMessage(exception.getMessage());
    errorMessage.setDocumentation("http://<url-with-more-details>");

    return Response.status(Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
  }
  @Test
  public void addPlantillaWithoutNombre() {
    Plantilla plantilla = preparaPlantilla();
    plantilla.setNombre(null);

    ClientResponse response =
        resource.type("application/json").post(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

    ResultatOperacio resultatOperacio = response.getEntity(ResultatOperacio.class);
    Assert.assertEquals(
        CampoRequeridoException.REQUIRED_FIELD + "Nombre", resultatOperacio.getDescripcio());
  }
Exemplo n.º 5
0
  /**
   * Handle exception.
   *
   * @param exception the exception
   * @return the response
   */
  public static Response handleException(Exception exception) {
    ResponseBuilder builder = null;
    ObjectFactory factory = new ObjectFactory();

    /* Construct error response jaxb */
    ApiErrorResponse errResponse = factory.createApiErrorResponse();
    errResponse.setMessage(exception.getMessage());
    errResponse.setTrace(ExceptionUtils.getStackTrace(exception));
    errResponse.setStatus(Status.INTERNAL_SERVER_ERROR.getStatusCode());

    /* Build the error response */
    builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errResponse);

    /* Return the response */
    return builder.build();
  }
 private Response configurarResponse(Usuario usuario) throws ServiceException {
   Token token = tokenGeneratorService.create(usuario);
   String retorno = BLANK_RETURN;
   Response response = null;
   if (token != null) {
     Gson gson = new GsonBuilder().setExclusionStrategies(new TokenExclusionStrategy()).create();
     retorno = gson.toJson(token);
     response = CallBackUtil.setResponseOK(retorno, MediaType.APPLICATION_JSON);
   } else {
     response =
         CallBackUtil.setResponseError(
             Status.INTERNAL_SERVER_ERROR.getStatusCode(),
             MessageUtil.getMessageFromBundle("exception.token_nao_encontrado"));
   }
   return response;
 }
 @POST
 @Path("login")
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
 public Response realizarLogin(String json) {
   Response response = null;
   try {
     Usuario usuario = new Usuario();
     configurarUsuario(usuario, new JSONObject(json));
     response = configurarResponse(usuario);
   } catch (ServiceException e) {
     response = CallBackUtil.setResponseError(Status.BAD_REQUEST.getStatusCode(), e.getMessage());
   } catch (Exception e) {
     response =
         CallBackUtil.setResponseError(
             Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
   }
   return response;
 }
  @Test
  @Ignore
  public void updatePlantillaAndRemoveNombre() {
    Plantilla plantilla = preparaPlantilla();
    ClientResponse response =
        resource.type("application/json").post(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    RestResponse restResponse = response.getEntity(new GenericType<RestResponse>() {});

    String id = getFieldFromRestResponse(restResponse, "id");
    Assert.assertNotNull(id);

    plantilla.setNombre("");
    response = resource.path(id).type("application/json").put(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

    ResultatOperacio parResponseMessage = response.getEntity(ResultatOperacio.class);
    Assert.assertEquals(
        CampoRequeridoException.REQUIRED_FIELD + "Nombre", parResponseMessage.getDescripcio());
  }
  /**
   * This method is supposed to be used as exception mapper from <code>WebApplicationException
   * </code>, sent in REST response, to <code>AuxiliaryStorageException</code>.
   *
   * @param exception Exception to convert from.
   */
  private void handleWebException(WebApplicationException exception) {

    Response response = exception.getResponse();
    if (response == null) {
      throw new AuxiliaryStorageException("Mapping exception error: response is null");
    }

    int responseStatus = response.getStatus();

    if (Status.BAD_REQUEST.getStatusCode() == responseStatus) {
      throw new IllegalParameterException("Bad request server error");
    } else if (Status.NOT_FOUND.getStatusCode() == responseStatus) {
      throw new ObjectNotFoundException("Object not found in auxiliary storage");
    } else if (Status.CONFLICT.getStatusCode() == responseStatus) {
      throw new ObjectAlreadyExistsException("Object already exists in auxiliary storage");
    } else if (Status.INTERNAL_SERVER_ERROR.getStatusCode() == responseStatus) {
      throw new AuxiliaryStorageException("Internal server error");
    } else {
      throw new AuxiliaryStorageException("Unknown server error");
    }
  }