package io.corbel.resources.rem.restor;

import io.corbel.lib.ws.api.error.ErrorResponseFactory;
import io.corbel.resources.rem.dao.RestorDao;
import io.corbel.resources.rem.model.RestorObject;
import io.corbel.resources.rem.model.RestorResourceUri;
import io.corbel.resources.rem.request.RequestParameters;
import io.corbel.resources.rem.request.ResourceId;
import io.corbel.resources.rem.request.ResourceParameters;

import java.io.InputStream;
import java.util.Optional;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

/**
 * @author Rubén Carrasco
 */
public class RestorGetRem extends AbstractRestorRem {

    public RestorGetRem(RestorDao dao) {
        super(dao);
    }

    @Override
    public Response resource(String collection, ResourceId resource, RequestParameters<ResourceParameters> parameters,
            Optional<InputStream> entity) {

        RestorResourceUri resourceUri = new RestorResourceUri(parameters.getRequestedDomain(), getMediaType(parameters), collection,
                resource.getId());

        RestorObject object = dao.getObject(resourceUri);
        if (object != null) {
            return Response.ok().type(object.getMediaType().toString()).entity(object.getInputStream())
                    .header(HttpHeaders.CONTENT_LENGTH, object.getContentLength()).header(HttpHeaders.ETAG, object.getEtag()).header(HttpHeaders.CONTENT_DISPOSITION, parameters.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION)).build();
        }
        return ErrorResponseFactory.getInstance().notFound();
    }
}

@@ -830,16 +830,33 @@
 meters,%0A
+                 
         
@@ -891,16 +891,16 @@
 tity) %7B%0A
-
 %0A       
@@ -1161,22 +1161,58 @@
         
-return
+Response.ResponseBuilder responseBuilder =
  Respons
@@ -1288,16 +1288,17 @@
 tream())
+;
 %0A       
@@ -1302,24 +1302,49 @@
         
-        
+responseBuilder = responseBuilder
 .header(
@@ -1401,51 +1401,200 @@
 h())
-.header(HttpHeaders.ETAG, object.getEtag())
+;%0A            responseBuilder = responseBuilder.header(HttpHeaders.ETAG, object.getEtag());%0A            if (parameters.getHeaders() != null) %7B%0A                responseBuilder = responseBuilder
 .hea
@@ -1696,16 +1696,66 @@
 SITION))
+;%0A            %7D%0A            return responseBuilder
 .build()

fix getHeaders npe