← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6240: Web api, caching uploaded documents/resources for 14 days

 

------------------------------------------------------------
revno: 6240
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-03-10 11:01:52 +0100
message:
  Web api, caching uploaded documents/resources for 14 days
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DocumentController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java


--
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/api/controller/ChartController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2012-03-09 16:45:37 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2012-03-10 10:01:52 +0000
@@ -58,8 +58,11 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ * @author Lars Helge Overland
  */
 @Controller
 @RequestMapping( value = ChartController.RESOURCE_PATH )
@@ -143,7 +146,7 @@
         
         String filename = CodecUtils.filenameEncode( chart.getName() ) + ".png";
         
-        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, true, filename, false );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.NO_CACHE, filename, false );
         
         ChartUtilities.writeChartAsPNG( response.getOutputStream(), jFreeChart, width, height );
     }
@@ -171,7 +174,7 @@
             chart = chartService.getJFreeOrganisationUnitChart( indicator, unit, !skipTitle, i18nManager.getI18nFormat() );
         }
 
-        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, true, "chart.png", false );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.NO_CACHE, "chart.png", false );
         
         ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2012-03-02 12:39:20 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2012-03-10 10:01:52 +0000
@@ -54,9 +54,11 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping( value = "/dataSets" )
+@RequestMapping( value = DataSetController.RESOURCE_PATH )
 public class DataSetController
 {
+    public static final String RESOURCE_PATH = "/dataSets";
+    
     @Autowired
     private DataSetService dataSetService;
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DocumentController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DocumentController.java	2012-03-02 12:39:20 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DocumentController.java	2012-03-10 10:01:52 +0000
@@ -53,8 +53,12 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+import static org.hisp.dhis.api.utils.ContextUtils.*;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ * @author Lars Helge Overland
  */
 @Controller
 @RequestMapping( value = DocumentController.RESOURCE_PATH )
@@ -132,7 +136,11 @@
         }
         else
         {
-            ContextUtils.configureResponse( response, document.getContentType(), true, document.getUrl(), true );
+            String ct = document.getContentType();
+            
+            boolean attachment = !( CONTENT_TYPE_PDF.equals( ct ) || CONTENT_TYPE_JPG.equals( ct ) || CONTENT_TYPE_PNG.equals( ct ) );
+            
+            ContextUtils.configureResponse( response, document.getContentType(), CacheStrategy.CACHE_TWO_WEEKS, document.getUrl(), attachment );
 
             InputStream in = locationManager.getInputStream( document.getUrl(), DocumentService.DIR );
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java	2012-03-09 16:50:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java	2012-03-10 10:01:52 +0000
@@ -56,6 +56,12 @@
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseStatus;
 
+import static org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ * @author Lars Helge Overland
+ */
 @Controller
 @RequestMapping( value = MapController.RESOURCE_PATH )
 public class MapController
@@ -194,7 +200,7 @@
         
         if ( image != null )
         {
-            ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, true, "mapview.png", false );
+            ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.NO_CACHE, "mapview.png", false );
             
             ImageIO.write( image, "PNG", response.getOutputStream() );
         }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java	2012-01-24 14:43:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java	2012-03-10 10:01:52 +0000
@@ -54,6 +54,12 @@
 import java.util.Date;
 import java.util.List;
 
+import static org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ * @author Lars Helge Overland
+ */
 @Controller
 @RequestMapping( value = ReportController.RESOURCE_PATH )
 public class ReportController
@@ -159,7 +165,7 @@
         Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
 
         String filename = CodecUtils.filenameEncode( report.getName() ) + "." + type;
-        ContextUtils.configureResponse( response, contentType, true, filename, attachment );
+        ContextUtils.configureResponse( response, contentType, CacheStrategy.NO_CACHE, filename, attachment );
 
         reportService.renderReport( response.getOutputStream(), uid, date, organisationUnitUid, type,
             i18nManager.getI18nFormat() );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2012-03-02 12:39:20 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2012-03-10 10:01:52 +0000
@@ -27,6 +27,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.api.utils.IdentifiableObjectParams;
 import org.hisp.dhis.api.utils.WebLinkPopulator;
 import org.hisp.dhis.common.Grid;
@@ -39,25 +52,22 @@
 import org.hisp.dhis.reporttable.ReportTables;
 import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.system.util.DateUtils;
-import org.hisp.dhis.api.utils.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
-import org.springframework.web.bind.annotation.*;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
-
-import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
-
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ * @author Lars Helge Overland
+ */
 @Controller
 @RequestMapping( value = ReportTableController.RESOURCE_PATH )
 public class ReportTableController
@@ -166,7 +176,7 @@
         Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
 
         String filename = filenameEncode( grid.getTitle() ) + ".pdf";
-        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, true, filename, false );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.NO_CACHE, filename, false );
 
         GridUtils.toPdf( grid, response.getOutputStream() );
     }
@@ -189,7 +199,7 @@
         Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
 
         String filename = filenameEncode( grid.getTitle() ) + ".xls";
-        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, true, filename, true );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE, filename, true );
 
         GridUtils.toXls( grid, response.getOutputStream() );
     }
@@ -212,7 +222,7 @@
         Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
 
         String filename = filenameEncode( grid.getTitle() ) + ".csv";
-        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, true, filename, true );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE, filename, true );
 
         GridUtils.toCsv( grid, response.getOutputStream() );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2012-03-02 09:17:50 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2012-03-10 10:01:52 +0000
@@ -29,6 +29,7 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Calendar;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -48,13 +49,20 @@
     public static final String CONTENT_TYPE_XML = "application/xml";
     public static final String CONTENT_TYPE_CSV = "application/csv";
     public static final String CONTENT_TYPE_PNG = "image/png";
-    public static final String CONTENT_TYPE_JPG = "image/jpg";
+    public static final String CONTENT_TYPE_JPG = "image/jpeg";
     public static final String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";
     public static final String CONTENT_TYPE_JAVASCRIPT = "application/javascript";
 
     public static final String HEADER_USER_AGENT = "User-Agent";
 
-    public static void configureResponse( HttpServletResponse response, String contentType, boolean disallowCache,
+    public enum CacheStrategy
+    {
+        NO_CACHE,
+        CACHE_TWO_WEEKS,
+        RESPECT_SYSTEM_SETTING
+    }
+    
+    public static void configureResponse( HttpServletResponse response, String contentType, CacheStrategy cacheStrategy,
         String filename, boolean attachment )
     {
         if ( contentType != null )
@@ -62,7 +70,7 @@
             response.setContentType( contentType );
         }
 
-        if ( disallowCache )
+        if ( cacheStrategy == null || cacheStrategy.equals( CacheStrategy.NO_CACHE ) )
         {
             // -----------------------------------------------------------------
             // Cache set to expire after 1 second as IE 8 will not save cached
@@ -72,6 +80,14 @@
             response.setHeader( "Cache-Control", "max-age=1" );
             response.setHeader( "Expires", DateUtils.getExpiredHttpDateString() );
         }
+        else if ( cacheStrategy.equals( CacheStrategy.CACHE_TWO_WEEKS ) )
+        {
+            Calendar cal = Calendar.getInstance();
+            cal.add( Calendar.DAY_OF_YEAR, 14 );
+            
+            response.setHeader( "Cache-Control", "max-age=1209600" );
+            response.setHeader( "Expires", DateUtils.getHttpDateString( cal.getTime() ) );
+        }
 
         if ( filename != null )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2012-03-08 14:43:53 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2012-03-10 10:01:52 +0000
@@ -54,7 +54,7 @@
     public static final String CONTENT_TYPE_XML = "application/xml";
     public static final String CONTENT_TYPE_CSV = "application/csv";
     public static final String CONTENT_TYPE_PNG = "image/png";
-    public static final String CONTENT_TYPE_JPG = "image/jpg";
+    public static final String CONTENT_TYPE_JPG = "image/jpeg";
     public static final String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";
     public static final String CONTENT_TYPE_JAVASCRIPT = "application/javascript";