Package org.yaac.shared.file

Examples of org.yaac.shared.file.FileDownloadPath


  /**
   * @param blobKeyString
   * @return
   */
  public static FileDownloadPath blobDownloadPath(String blobKeyString) {
    FileDownloadPath path = abf.fileDownloadPath().as();
    path.setType(Type.BLOBSTORE_BLOB);
    path.setKeyStr(blobKeyString);
    path.setFieldName(null);
    path.setIndex(null);
    return path;
  }
View Full Code Here


  /**
   * @param blobKeyString
   * @return
   */
  public static String blobDownladPathString(String blobKeyString) {
    FileDownloadPath path = blobDownloadPath(blobKeyString);
    return encode(FileDownloadPath.class, path);
  }
View Full Code Here

   * @param cacheKey
   * @param size
   * @return
   */
  public static FileDownloadPath cacheDownloadPath(String cacheKey, Integer size) {
    FileDownloadPath path = abf.fileDownloadPath().as();
    path.setType(Type.MEMCACHE);
    path.setKeyStr(cacheKey);
    path.setFieldName(null);
    path.setIndex(null);
    path.setSize(size);
    return path;
  }
View Full Code Here

   * @param cacheString
   * @param size
   * @return
   */
  public static String cacheDownladPathString(String cacheString, Integer size) {
    FileDownloadPath path = cacheDownloadPath(cacheString, size);
    return encode(FileDownloadPath.class, path);
  }
View Full Code Here

    if (isNullOrEmpty(encodedPath)) {
      logger.severe("Can't download file with empty path");
      return;
    }
   
    FileDownloadPath path = AutoBeanUtil.decode(FileDownloadPath.class, encodedPath);
   
    switch(path.getType()) {
    case BLOBSTORE_BLOB:
      BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService();
      BlobKey blobKey = new BlobKey(path.getKeyStr());
      blobstore.serve(blobKey, resp);
      break;
    case DATASTORE_BLOB:
    case DATASTORE_TEXT:
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      Key key = KeyFactory.stringToKey(path.getKeyStr());
      try {
        Entity entity = datastore.get(key);
        Object property = entity.getProperty(path.getFieldName());
        if (property instanceof Blob) {
          serve(((Blob) property).getBytes(), resp);
        } else if (property instanceof Text) {
          serve(((Text) property).getValue().getBytes(), resp);
        } else if (property instanceof List) {
          Object obj = ((List) property).get(path.getIndex());
          if (obj instanceof Blob) {
            serve(((Blob) obj).getBytes(), resp)
          } else if (obj instanceof Text) {
            serve(((Text) obj).getValue().getBytes(), resp);
          }
        }
      } catch (EntityNotFoundException e) {
        e.printStackTrace();
        logger.log(Level.SEVERE, e.getMessage(), e);
      }
     
      break;
    case MEMCACHE:
      String cacheKey = path.getKeyStr();
      byte [] cache = (byte[]) memcache.get(cacheKey);
     
      if (cache == null) {
        serve("Cache expired, please upload again".getBytes(), resp)
      } else {
View Full Code Here

    } else if (obj instanceof ShortBlob) {
      result = new StringPropertyInfo(new String(((ShortBlob) obj).getBytes()));
    } else if (obj instanceof Blob) {
      Blob b = (Blob) obj;
      String fileName = index == null ? propertyName : propertyName + "[" + index + "]";
      FileDownloadPath downloadPath = AutoBeanUtil.newFileDownloadPath(
          FileDownloadPath.Type.DATASTORE_BLOB, keyString, propertyName, index, fileName, b.getBytes().length);
      String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, downloadPath);
      result = new BlobPropertyInfo(b.getBytes().length, pathStr);
    } else if (obj instanceof BlobStringWrapper) {  // user has edited a blob, in string form, it's not in memcache, nor datastore
      result = new BlobPropertyInfo(((BlobStringWrapper) obj).getRawString().getBytes());
    } else if (obj instanceof BlobFileRefWrapper) {  // user has uploaded the blob, but still stay in memcache
      FileDownloadPath path = ((BlobFileRefWrapper) obj).getRef();
      String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, path);
      result = new BlobPropertyInfo(path.getSize(), pathStr);
    } else if (obj instanceof BlobKey) {
      String blobKeyString = ((BlobKey) obj).getKeyString();
      result = new BlobKeyPropertyInfo(blobKeyString);
    } else if (obj instanceof Key) {
      result = convert((Key)obj);
    } else if (obj instanceof Link) {
      result = new StringPropertyInfo(((Link) obj).getValue());
    } else if (obj instanceof IMHandle) {
      String protocol = ((IMHandle) obj).getProtocol();
      String address = ((IMHandle) obj).getAddress();
      result = new IMHandlePropertyInfo(protocol, address);
    } else if (obj instanceof PostalAddress) {
      result = new StringPropertyInfo(((PostalAddress) obj).getAddress());
    } else if (obj instanceof Rating) {
      result = new LongPropertyInfo(((Rating) obj).getRating());
    } else if (obj instanceof PhoneNumber) {
      result = new StringPropertyInfo(((PhoneNumber) obj).getNumber());
    } else if (obj instanceof Text) {
      Text t = (Text)obj;
      String fileName = index == null ? propertyName : propertyName + "[" + index + "]";
      FileDownloadPath downloadPath = AutoBeanUtil.newFileDownloadPath(
          FileDownloadPath.Type.DATASTORE_TEXT, keyString, propertyName, index,
          fileName, t.getValue().length());
      String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, downloadPath);
      String fullStr = ((Text) obj).getValue();
      result = new TextPropertyInfo(fullStr, pathStr);
    } else if (obj instanceof TextStringWrapper) {
      result = new TextPropertyInfo(((TextStringWrapper) obj).getRawString());
    } else if (obj instanceof TextFileRefWrapper) {
      FileDownloadPath path = ((TextFileRefWrapper) obj).getRef();
      String pathStr = AutoBeanUtil.encode(FileDownloadPath.class, path);
      result = new TextPropertyInfo("No preview available", path.getSize(), pathStr);
    } else if (obj instanceof List) {
      @SuppressWarnings("rawtypes")
      List list = (List) obj;
      ListPropertyInfo propertyInfo = new ListPropertyInfo();
     
View Full Code Here

  public static FileDownloadPath newFileDownloadPath(
      FileDownloadPath.Type type,
      String keyStr,
      String fieldName, Integer index,
      String fileName, Integer size) {
    FileDownloadPath path = abf.fileDownloadPath().as();
    path.setType(type);
    path.setKeyStr(keyStr);
    path.setFieldName(fieldName);
    path.setIndex(index);
    path.setFileName(fileName);
    path.setSize(size);
    return path;
  }
View Full Code Here

  /**
   * @param cacheKey
   * @return
   */
  public static FileDownloadPath cacheDownloadPath(String cacheKey) {
    FileDownloadPath path = abf.fileDownloadPath().as();
    path.setType(Type.MEMCACHE);
    path.setKeyStr(cacheKey);
    path.setFieldName(null);
    path.setIndex(null);
    return path;
  }
View Full Code Here

  /**
   * @param cacheString
   * @return
   */
  public static String cacheDownladPathString(String cacheString) {
    FileDownloadPath path = cacheDownloadPath(cacheString);
    return encode(FileDownloadPath.class, path);
  }
View Full Code Here

    switch (type) {
    case BLOB:
      BlobPropertyInfo blobInfo = (BlobPropertyInfo) info;
     
      if (blobInfo.getRawData() == null) {
        FileDownloadPath path = AutoBeanUtil.decode(FileDownloadPath.class, blobInfo.getDownloadPath());

        switch (path.getType()) {
        case MEMCACHE:
          byte [] data = (byte[]) memcache.get(path.getKeyStr());
          // throw exception when rawData == null? cache get expired is possible
          return new Blob(data);
        case DATASTORE_BLOB:
          // simply return existing one
          Object val = e.getProperty(path.getFieldName());
          if (val instanceof Blob) {
            return val;
          } else if (val instanceof List) {
            return ((List)val).get(path.getIndex());
          } else {
            // should not happen
            throw new IllegalArgumentException("Can't locate blob via path " + path.getFieldName() + " " + path.getIndex());
          }
        default:
          // should not happen
          throw new IllegalArgumentException("Can't locate blob via path " + path.getFieldName() + " " + path.getIndex());
        }
      } else {
        // edit blob value directly
        return new Blob(blobInfo.getRawData());
      }
    case BLOB_KEY:
      return new BlobKey(((BlobKeyPropertyInfo) info).getBlobKey());
    case BOOL:
      return ((BooleanPropertyInfo) info).getPayload();
    case CATEGORY:
      return new Category(((CategoryPropertyInfo) info).getPayload());
    case DOUBLE:
      return ((DoublePropertyInfo) info).getPayload();
    case EMAIL:
      return new Email(((EmailPropertyInfo) info).getPayload());
    case GEOPT:
      GeoPtPropertyInfo geoPtInfo = (GeoPtPropertyInfo) info;
      return new GeoPt(geoPtInfo.getLatitude(), geoPtInfo.getLongitude());
    case IM_HANDLE:
      // TODO
      return null;
    case KEY:
      return DatastoreUtil.convert((KeyInfo)info);
    case LINK:
      return new Link(((LinkPropertyInfo) info).getPayload());
    case LIST:
      ListPropertyInfo listInfo = (ListPropertyInfo) info;
      List list = newLinkedList();
      for (PropertyInfo i : listInfo.getPayload()) {
        list.add(convert(i, e));
      }
      return list;
    case LONG:
      return ((LongPropertyInfo) info).getPayload();
    case NULL:
      return null;
    case PHONE_NO:
      return new PhoneNumber(((PhoneNumberPropertyInfo) info).getPayload());
    case POSTTAL_ADDRESS:
      return new PostalAddress(((PostalAddressPropertyInfo) info).getPayload());
    case SHORT_BLOB:
      return new ShortBlob(((ShortBlobPropertyInfo) info).getPayload().getBytes());
    case STRING:
      return ((StringPropertyInfo) info).getPayload();
    case TEXT:     
      TextPropertyInfo textInfo = (TextPropertyInfo) info;
     
      if (textInfo.getRawText() == null) {
        FileDownloadPath path = AutoBeanUtil.decode(FileDownloadPath.class, textInfo.getDownloadPath());

        switch (path.getType()) {
        case MEMCACHE:
          byte [] data = (byte[]) memcache.get(path.getKeyStr());
          // throw exception when rawData == null? cache get expired is possible
          return new Text(new String(data));
        case DATASTORE_TEXT:
          // simply return existing one
          Object val = e.getProperty(path.getFieldName());
          if (val instanceof Text) {
            return val;
          } else if (val instanceof List) {
            return ((List)val).get(path.getIndex());
          } else {
            // should not happen
            throw new IllegalArgumentException("Can't locate text via path " + path.getFieldName() + " " + path.getIndex());
          }
        default:
          // should not happen
          throw new IllegalArgumentException("Can't locate text via path " + path.getFieldName() + " " + path.getIndex());
        }
      } else {
        // edit text value directly
        return new Text(textInfo.getRawText());
      }
View Full Code Here

TOP

Related Classes of org.yaac.shared.file.FileDownloadPath

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.