예제 #1
0
  /** @param role Role to be checked */
  public boolean isUserInRole(String role) {
    String[] roles = StringUtils.split(role, ",");
    List<String> list = java.util.Arrays.asList(roles);

    if (user != null) {
      if (user.getUtype() == CodeUserType.USER && list.contains("USER")) {
        return true;
      } else if (user.getUtype() == CodeUserType.OWER && list.contains("OWER")) {
        return true;
      }
    }

    if (waiter != null && list.contains("WAITER")) {
      return true;
    }
    return false;
  }
  @POST
  @RolesAllowed({"OWER"})
  @Consumes("multipart/form-data")
  public Response update(
      @FormDataParam("name") String name,
      @FormDataParam("telephone") String telephone,
      @FormDataParam("address") String address,
      @DefaultValue("-1000") @FormDataParam("x") double x,
      @DefaultValue("-1000") @FormDataParam("y") double y,
      @FormDataParam("image") InputStream upImg,
      @FormDataParam("image") FormDataContentDisposition fileDetail,
      @Context SecurityContext securityContext) {

    RestaurantDao dao = new RestaurantDao();

    // TODO 需要判断该restaurant是否是该用户的,如果不是,则无权限修改
    User user = PublicHelper.getLoginUser(securityContext);
    if (user.getId() != r.getUser().getId()) {
      throw new WebApplicationException(Response.Status.FORBIDDEN);
    }

    r.setName(name);
    r.setAddress(address);
    r.setTelephone(telephone);
    if (x != -1000) {
      r.setX(x);
    }
    if (y != -1000) {
      r.setY(y);
    }
    r.setStatus(0);

    if (upImg != null && !StringUtils.isEmpty(fileDetail.getFileName())) {
      try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = upImg.read(data, 0, data.length)) != -1) {
          buffer.write(data, 0, nRead);
        }
        buffer.flush();
        byte[] bs = buffer.toByteArray();

        if (bs.length > 0) {
          String id = UUID.randomUUID().toString();
          String image = id + ".png";
          r.setImage(image);

          BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bs));

          File file = new File(PublicConfig.getImagePath() + image);
          if (file.isDirectory()) {
            ImageIO.write(bi, "png", file);
          } else {
            file.mkdirs();
            ImageIO.write(bi, "png", file);
          }
        }

      } catch (IOException e) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("创建菜单失败")
            .type(MediaType.TEXT_PLAIN)
            .build();
      }
    }

    dao.saveOrUpdate(r);

    return Response.status(Response.Status.OK).build();
  }