@Override
  public Object create(Map<String, Object> value) {
    String mimeType = (String) value.get("mime-type");
    String encoding = (String) value.get("encoding");
    // long length = -1;
    // Long lengthVal = (Long)value.get("length");
    // if (lengthVal != null) {
    // length = lengthVal.longValue();
    // }
    String filename = (String) value.get("name");
    String digest = (String) value.get("digest");
    Object data = value.get("data");
    StreamSource ss;
    if (data instanceof String) {
      ss = new StringSource((String) data);
    } else if (data instanceof InputStream) {
      ss = new InputStreamSource((InputStream) data);
    } else if (data instanceof byte[]) {
      ss = new ByteArraySource((byte[]) data);
    } else {
      ss = new ByteArraySource(new byte[0]);
    }

    Blob blob = new StreamingBlob(ss);
    blob.setMimeType(mimeType);
    blob.setEncoding(encoding);
    blob.setFilename(filename);
    blob.setDigest(digest);
    return blob;
  }
 @Override
 public Blob getBlob(String uri) throws PropertyException {
   String localPath = getLocalName(uri);
   String path = getFileAbsolutePath(localPath);
   File file = new File(path);
   if (!file.exists()) {
     throw new PropertyException(String.format("Cannot find file at '%s'", path));
   }
   Blob blob = new FileBlob(file);
   blob.setFilename(file.getName());
   return blob;
 }
Exemple #3
0
  private static void checkBlob(Blob blob) throws IOException {
    assertEquals("text/plain", blob.getMimeType());
    assertEquals("UTF-8", blob.getEncoding());
    assertEquals("some content", blob.getString());
    assertTrue(Arrays.equals("some content".getBytes(), blob.getByteArray()));

    InputStream in = blob.getStream();
    String result = StreamBlob.readString(new InputStreamReader(in));
    assertEquals("some content", result);
    in.close();

    Reader reader = blob.getReader();
    result = StreamBlob.readString(reader);
    assertEquals("some content", result);
    reader.close();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    blob.transferTo(baos);
    assertEquals("some content", new String(baos.toByteArray()));
    StringWriter sw = new StringWriter();
    blob.transferTo(sw);
    assertEquals("some content", sw.toString());
  }
Exemple #4
0
 public static HashMap<String, Serializable> createBlobHolderMap(Blob blob) {
   HashMap<String, Serializable> map = new HashMap<String, Serializable>();
   map.put("file", (Serializable) blob);
   map.put("filename", blob.getFilename());
   return map;
 }