Commit e0cb8856 authored by Ole Voldsæter's avatar Ole Voldsæter
Browse files

fixes #1567 -

- error handling
- dynamic mime type
parent c56387d2
......@@ -3,10 +3,9 @@ package com.nesstar.nesstar_rest_api.resources;
import com.nesstar.api.*;
import com.nesstar.api.subset.CaseSubset;
import com.nesstar.api.subset.SubsetSyntaxException;
import com.nesstar.nesstar_rest_api.common.MissingResourceException;
import com.nesstar.nesstar_rest_api.common.ResourceExtractor;
import com.nesstar.nesstar_rest_api.common.ServerHandler;
import com.nesstar.nesstar_rest_api.common.*;
import com.nesstar.nesstar_rest_api.request_objects.DownloadRequest;
import com.yammer.metrics.annotation.Timed;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
......@@ -18,27 +17,32 @@ import java.util.List;
@Path("study/{studyId}/download")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/zip")
public class DownloadResource {
@POST
public Response download(final DownloadRequest downloadRequest, @PathParam("studyId") final String studyId, @Context final HttpServletRequest request) throws IOException, MissingResourceException, SubsetSyntaxException, NotAuthorizedException {
Server server = ServerHandler.getServer(request);
Study study = ResourceExtractor.getStudy(studyId, server);
@POST
@Timed
public Object download(final DownloadRequest downloadRequest, @PathParam("studyId") final String studyId, @Context final HttpServletRequest request) throws IOException, MissingResourceException, SubsetSyntaxException, NotAuthorizedException {
List<Variable> variables = ResourceExtractor.getVariables(downloadRequest.getVariableIds(), server);
return OperationRunner.runWithExceptionHandling(request, new Operation() {
@Override
public Object run() throws Exception {
Server server = ServerHandler.getServer(request);
Study study = ResourceExtractor.getStudy(studyId, server);
String caseSubsetExpression = downloadRequest.getCaseSubsetExpression();
CaseSubset subset = null;
List<Variable> variables = ResourceExtractor.getVariables(downloadRequest.getVariableIds(), server);
if (caseSubsetExpression != null) {
subset = CaseSubset.compile(caseSubsetExpression, server);
}
String caseSubsetExpression = downloadRequest.getCaseSubsetExpression();
CaseSubset subset = null;
FileFormat format = FileFormat.valueOf(downloadRequest.getFormat());
if (caseSubsetExpression != null) {
subset = CaseSubset.compile(caseSubsetExpression, server);
}
ResultStream stream = study.download(format, subset, variables.toArray(new Variable[variables.size()]));
FileFormat format = FileFormat.valueOf(downloadRequest.getFormat());
return Response.ok(stream).build();
}
ResultStream stream = study.download(format, subset, variables.toArray(new Variable[variables.size()]));
return Response.ok(stream).type(stream.getMimeType()).build();
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment