Exemplo n.º 1
0
  public void handle(FileItem item, Map<String, Object> result) {
    BlobWriter writer = blobManager.newFile(item.getContentType());
    OutputStream out = writer.getOutputStream();
    InputStream in = null;
    try {
      in = item.getInputStream();

      try {
        int size = 0;

        while ((size = in.read(buffer)) != -1) {
          out.write(buffer, 0, size);
        }
      } finally {
        IOTools.close(in, out);
      }

      BlobInfo info = writer.getBlobInfo();
      result.put("fileId", info.id());
      result.put("contentType", info.contentType());
      result.put("md5", info.md5());
      result.put("sucessful", "true");

      blobManager.commit(writer);
    } catch (Exception ex) {
      result.put("sucessful", "false");
      blobManager.cancel(writer);
    }
  }
Exemplo n.º 2
0
    public void finish(MessageDoc ctx, boolean closeStream) {
      if (!ctx.getReturnCode().equals("core.ok")) {
        throw new ServantException(ctx.getReturnCode(), ctx.getReason());
      }
      OutputStream out = null;
      try {
        out = ctx.getOutputStream();
      } catch (IOException e) {

      } finally {
        if (closeStream) {
          IOTools.close(out);
        }
      }
    }
Exemplo n.º 3
0
 private StringBuffer loadFromInputStream(StringBuffer buf, InputStream in) {
   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
   String line = null;
   try {
     while ((line = reader.readLine()) != null) {
       buf.append(line);
       buf.append("\n");
     }
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     IOTools.closeStream(in, reader);
   }
   return buf;
 }
Exemplo n.º 4
0
  public int actionProcess(Context ctx) throws Exception {
    ctx.asMessage(BlobMessage.class);

    String fileId = getArgument("fileId", ctx);
    String domain = getArgument("domain", "default", ctx);

    BlobManager manager = BlobTool.getBlobManager(domain);
    if (manager == null) {
      throw new ServantException(
          "core.blob_not_found", "Can not find a blob manager named " + domain);
    }

    BlobReader reader = manager.getFile(fileId);
    if (reader == null) {
      throw new ServantException("core.blob_not_found", "Can not find a blob file named " + fileId);
    }

    BlobInfo info = reader.getBlobInfo();

    ctx.setResponseContentType(info.contentType());

    InputStream in = reader.getInputStream(0);
    OutputStream out = ctx.getOutputStream();

    try {
      int size = 0;

      while ((size = in.read(buffer)) != -1) {
        out.write(buffer, 0, size);
      }
    } finally {
      IOTools.close(in);
    }

    return 0;
  }