PageRenderTime 489ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/chapter3/mongodb-image/app/utils/GridFSSerializer.java

https://github.com/spinscale/play-cookbook
Java | 39 lines | 32 code | 7 blank | 0 comment | 0 complexity | 86bc44145373f5b8352a5b1968005654 MD5 | raw file
  1. package utils;
  2. import java.lang.reflect.Type;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import play.Logger;
  7. import play.mvc.Router;
  8. import com.google.gson.JsonArray;
  9. import com.google.gson.JsonElement;
  10. import com.google.gson.JsonObject;
  11. import com.google.gson.JsonSerializationContext;
  12. import com.google.gson.JsonSerializer;
  13. import com.google.gson.reflect.TypeToken;
  14. import com.mongodb.gridfs.GridFSDBFile;
  15. public class GridFSSerializer implements JsonSerializer<GridFSDBFile> {
  16. @Override
  17. public JsonElement serialize(GridFSDBFile file, Type type,
  18. JsonSerializationContext ctx) {
  19. String url = createUriForFile(file);
  20. JsonObject obj = new JsonObject();
  21. obj.addProperty("thumb", url);
  22. obj.addProperty("large", url);
  23. obj.addProperty("title", (String)file.get("title"));
  24. obj.addProperty("link", url);
  25. return obj;
  26. }
  27. private String createUriForFile(GridFSDBFile file) {
  28. Map<String,Object> map = new HashMap<String,Object>();
  29. map.put("id", file.getId().toString());
  30. return Router.getFullUrl("Application.showImage", map);
  31. }
  32. }