package gov.usgs.cida.wqp.webservice;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import gov.usgs.cida.wqp.dao.BlobDao;
import gov.usgs.cida.wqp.parameter.FilterParameters;
import gov.usgs.cida.wqp.util.HttpConstants;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
public class BlobController {

	protected final BlobDao blobDao;

	@Autowired
	public BlobController(BlobDao blobDao) {
		this.blobDao = blobDao;
	}

	@ApiOperation(value="Return zipped artifact with all files available for the specified organization/project.")
	@GetMapping(value="/organizations/{organization}/projects/{projectIdentifier}/files")
	public void projectBlobFilesGetRestRequest(HttpServletRequest request, HttpServletResponse response, @ApiIgnore FilterParameters filter,
			@PathVariable("organization") String organization, @PathVariable("projectIdentifier") String projectIdentifier) throws IOException {
		response.setHeader(HttpConstants.HEADER_CONTENT_DISPOSITION,"attachment; filename=project.zip");
		OutputStream out = response.getOutputStream();
		ZipOutputStream target = new ZipOutputStream(out);
		blobDao.getFiles(target, organization, projectIdentifier);
		target.finish();
		target.close();
		out.flush();
	}

}
@@ -89,16 +89,96 @@
 Stream;%0A
+import java.math.BigDecimal;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -640,16 +640,109 @@
 meters;%0A
+import gov.usgs.cida.wqp.parameter.Parameters;%0Aimport gov.usgs.cida.wqp.service.ILogService;%0A
 import g
@@ -957,16 +957,128 @@
 blobDao;
+%0A%09protected final ILogService logService;%0A%09%0A%09private static ThreadLocal%3CBigDecimal%3E logId = new ThreadLocal%3C%3E();
 %0A%0A%09@Auto
@@ -1121,16 +1121,42 @@
  blobDao
+, ILogService inLogService
 ) %7B%0A%09%09th
@@ -1177,16 +1177,188 @@
 lobDao;%0A
+%09%09logService = inLogService;%0A%09%7D%0A%09%0A%09public static BigDecimal getLogId() %7B%0A%09%09return logId.get();%0A%09%7D%0A%0A%09public static void setLogId(BigDecimal inLogId) %7B%0A%09%09logId.set(inLogId);%0A
 %09%7D%0A%0A%09@Ap
@@ -1826,16 +1826,61 @@
 ption %7B%0A
+%09%09List%3CString%3E projects = new ArrayList%3C%3E();%0A
 %09%09respon
@@ -1970,16 +1970,144 @@
 .zip%22);%0A
+%09%09projects.add(projectIdentifier);%0A%09%09filter.setProject(projects);%0A%09%09setLogId(logService.logRequest(request, response, filter));%0A
 %09%09Output
@@ -2309,17 +2309,100 @@
 ut.flush();%0A
+%09%09logService.logRequestComplete(getLogId(), String.valueOf(response.getStatus()));%0A
 %09%7D%0A%0A%7D

Add logging to blob service