package app;

import com.darwino.commons.json.JsonException;
import com.darwino.jsonstore.Attachment;
import com.darwino.jsonstore.Database;
import com.darwino.jsonstore.Document;
import com.darwino.jsonstore.Store;
import frostillicus.blog.app.AppDatabaseDef;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

@Path(MediaResource.PATH)
public class MediaResource {
    public static final String PATH = "media";

    @Inject
    Database database;

    @GET
    @Path("{mediaId}/{mediaName}")
    public Response get(@PathParam("mediaId") String mediaId) throws JsonException {
        Store store = database.getStore(AppDatabaseDef.STORE_MEDIA);
        Document doc = store.loadDocument(mediaId);

        Attachment att = doc.getAttachments()[0];

        return Response.ok(att.getInputStream()).header(HttpHeaders.CONTENT_TYPE, att.getMimeType()).build();
    }
}

@@ -390,53 +390,9 @@
 ore.
-HttpHeaders;%0Aimport javax.ws.rs.core.Response
+*
 ;%0A%0A@
@@ -529,16 +529,51 @@
 abase;%0A%0A
+    @Context%0A    Request request;%0A%0A
     @GET
@@ -878,14 +878,186 @@
     
-return
+EntityTag etag = new EntityTag(att.getETag());%0A        Response.ResponseBuilder builder = request.evaluatePreconditions(etag);%0A        if(builder == null) %7B%0A            builder =
  Res
@@ -1086,16 +1086,37 @@
 tream())
+%0A                    
 .header(
@@ -1159,16 +1159,241 @@
 eType())
+%0A                    .header(HttpHeaders.ETAG, etag);%0A        %7D%0A%0A        CacheControl cc = new CacheControl();%0A        cc.setMaxAge(5 * 24 * 60 * 60);%0A%0A        return builder%0A                .cacheControl(cc)%0A                
 .build()

Implement caching in MediaResource