dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #34079
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17491: support gzip for csv import/export (web-api only)
------------------------------------------------------------
revno: 17491
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-11-17 14:37:00 +0700
message:
support gzip for csv import/export (web-api only)
modified:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java
dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java 2014-10-23 16:53:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java 2014-11-17 07:37:00 +0000
@@ -67,6 +67,7 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
+import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -76,11 +77,14 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -128,7 +132,7 @@
// READ
// -------------------------------------------------------------------------
- @RequestMapping( value = "", method = RequestMethod.GET, produces = { "application/csv", "text/csv" } )
+ @RequestMapping( value = "", method = RequestMethod.GET, produces = { "application/csv", "application/csv+gzip", "text/csv" } )
@PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
public void getCsvEvents(
@RequestParam( required = false ) String program,
@@ -141,6 +145,7 @@
@RequestParam( required = false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date startDate,
@RequestParam( required = false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
@RequestParam( required = false ) EventStatus status,
+ @RequestParam( required = false ) String attachment,
@RequestParam( required = false, defaultValue = "false" ) boolean skipHeader,
@RequestParam Map<String, String> parameters, Model model, HttpServletResponse response, HttpServletRequest request ) throws IOException
{
@@ -200,7 +205,14 @@
events.setEvents( PagerUtils.pageCollection( events.getEvents(), pager ) );
}
- csvEventService.writeEvents( response.getOutputStream(), events, !skipHeader );
+ OutputStream outputStream = isGzip( request ) ? new GZIPOutputStream( response.getOutputStream() ) : response.getOutputStream();
+
+ if ( !StringUtils.isEmpty( attachment ) )
+ {
+ response.addHeader( "Content-Disposition", "attachment; filename=" + attachment );
+ }
+
+ csvEventService.writeEvents( outputStream, events, !skipHeader );
response.getOutputStream().flush();
response.getOutputStream().close();
}
@@ -219,6 +231,7 @@
@RequestParam( required = false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
@RequestParam( required = false ) EventStatus status,
@RequestParam( required = false ) boolean skipMeta,
+ @RequestParam( required = false ) String attachment,
@RequestParam Map<String, String> parameters, Model model, HttpServletResponse response, HttpServletRequest request )
{
WebOptions options = new WebOptions( parameters );
@@ -293,6 +306,11 @@
model.addAttribute( "model", events );
model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );
+ if ( !StringUtils.isEmpty( attachment ) )
+ {
+ response.addHeader( "Content-Disposition", "attachment; filename=" + attachment );
+ }
+
return "events";
}
@@ -496,7 +514,9 @@
@RequestParam( required = false, defaultValue = "false" ) boolean skipFirst,
HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws IOException
{
- Events events = csvEventService.readEvents( request.getInputStream(), skipFirst );
+ InputStream inputStream = isGzip( request ) ? new GZIPInputStream( request.getInputStream() ) : request.getInputStream();
+
+ Events events = csvEventService.readEvents( inputStream, skipFirst );
if ( !importOptions.isAsync() )
{
@@ -639,4 +659,11 @@
response.setStatus( HttpServletResponse.SC_NO_CONTENT );
eventService.deleteEvent( event );
}
+
+ private boolean isGzip( HttpServletRequest request )
+ {
+ return request != null && (
+ (request.getPathInfo() != null && request.getPathInfo().endsWith( ".gz" ))
+ || (request.getHeader( "Accept" ) != null && request.getHeader( "Accept" ).contains( "application/csv+gzip" )));
+ }
}
\ No newline at end of file
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2014-10-21 06:26:27 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2014-11-17 07:37:00 +0000
@@ -54,6 +54,7 @@
<entry key="pdf" value="application/pdf" />
<entry key="xls" value="application/vnd.ms-excel" />
<entry key="csv" value="application/csv" />
+ <entry key="csv.gz" value="application/csv+gzip" />
<entry key="geojson" value="application/json+geojson" />
</map>
</constructor-arg>