Package gotnames.web.st

Source Code of gotnames.web.st.PictureTask

package gotnames.web.st;

import gotnames.Utils;
import gotnames.dm.ProfilePicture;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.commons.io.IOUtils;

import com.medallia.spider.Task;
import com.medallia.tiny.CollUtils;

/**
* Task which serves the image data; the input is the md5 of the picture.
*/
public class PictureTask extends Task {
 
  @Input interface Params {
    String t();
  }
 
  PostAction action(Params p, PersistenceManager pm) {
    String pictureMd5 = p.t();
    if (pictureMd5 == null)
      throw new IllegalArgumentException("Missing argument");
   
    Query query = pm.newQuery(ProfilePicture.class);
    query.setFilter("md5 == fieldParam");
    query.declareParameters("String fieldParam");
    query.setRange(0, 1);
    ProfilePicture pp = CollUtils.getOneOrNull(Utils.<Collection<ProfilePicture>>cast(query.execute(pictureMd5)));
   
    if (pp == null)
      throw new IllegalArgumentException("Invalid token");
   
    final byte[] image = pp.getPicture().getBytes();
     
    return new BinaryDataPostAction() {
      @Override protected boolean isCacheForever() {
        return true;
      }
      @Override protected String getContentType() {
        return "image/jpeg";
      }
      @Override protected void writeTo(OutputStream out) throws IOException {
        IOUtils.write(image, out);
      }
    };
  }

}
TOP

Related Classes of gotnames.web.st.PictureTask

TOP
Copyright © 2018 www.massapi.com. 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.