Пример #1
0
  public static MyResponse ok(Object content, boolean isEncryp) {

    MyResponse response = new MyResponse();
    if (isEncryp) {
      response.encryp = true;

      JsonMapper jsonMapper = new JsonMapper();
      String jsonContent = jsonMapper.toJson(content);

      try {
        // gzip压缩
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(jsonContent.getBytes());
        gzip.close();

        byte[] vi = Cryptos.generateIV();
        byte[] encryByte = Cryptos.aesEncrypt(out.toByteArray(), response.getCurrentToken(), vi);
        String encrypContent = Base64.encodeBase64String(encryByte);
        String viStr = Base64.encodeBase64String(vi);
        response.setContent(viStr + ":" + encrypContent);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    } else {
      response.setContent(content);
    }

    response.setStatus(STATUS_OK);

    return response;
  }
Пример #2
0
 public static MyResponse err(MyErrorCode errorCode) {
   MyResponse response = new MyResponse();
   response.setStatus(STATUS_ERROR);
   Error error = new Error();
   error.setErrorCode(errorCode.getErrorCode());
   error.setErrorInfo(errorCode.getErrorInfo());
   response.setError(error);
   return response;
 }
Пример #3
0
 public static MyResponse noContent() {
   MyResponse response = new MyResponse();
   response.setStatus(204); // 错误码204
   response.setContent(null);
   return response;
 }
Пример #4
0
 public static MyResponse create() {
   MyResponse response = new MyResponse();
   response.setStatus(201);
   return response;
 }
Пример #5
0
 public static MyResponse ok(Object content) {
   MyResponse response = new MyResponse();
   response.setContent(content);
   response.setStatus(STATUS_OK);
   return response;
 }