← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13656: implemented last-modified for apps controller (app serving)

 

------------------------------------------------------------
revno: 13656
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-01-08 13:07:41 +0100
message:
  implemented last-modified for apps controller (app serving)
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AppController.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/AppController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AppController.java	2014-01-08 11:18:13 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AppController.java	2014-01-08 12:07:41 +0000
@@ -29,11 +29,11 @@
  */
 
 import com.google.common.collect.Lists;
-import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.appmanager.App;
 import org.hisp.dhis.appmanager.AppManager;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.system.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.DefaultResourceLoader;
 import org.springframework.core.io.Resource;
@@ -48,12 +48,14 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.context.request.ServletWebRequest;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.IOException;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -69,7 +71,7 @@
 
     private ResourceLoader resourceLoader = new DefaultResourceLoader();
 
-    @RequestMapping( value = RESOURCE_PATH, method = RequestMethod.GET )
+    @RequestMapping(value = RESOURCE_PATH, method = RequestMethod.GET)
     public String getApps( Model model )
     {
         List<App> apps = appManager.getApps();
@@ -79,10 +81,10 @@
         return "apps";
     }
 
-    @RequestMapping( value = RESOURCE_PATH, method = RequestMethod.POST )
-    @ResponseStatus( HttpStatus.NO_CONTENT )
-    @PreAuthorize( "hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')" )
-    public void installApp( @RequestParam( "file" ) MultipartFile file, HttpServletRequest request ) throws IOException
+    @RequestMapping(value = RESOURCE_PATH, method = RequestMethod.POST)
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    @PreAuthorize("hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')")
+    public void installApp( @RequestParam("file") MultipartFile file, HttpServletRequest request ) throws IOException
     {
         File tempFile = File.createTempFile( "IMPORT_", "_ZIP" );
         file.transferTo( tempFile );
@@ -90,17 +92,17 @@
         appManager.installApp( tempFile, file.getOriginalFilename(), getBaseUrl( request ) );
     }
 
-    @RequestMapping( value = RESOURCE_PATH, method = RequestMethod.PUT )
-    @ResponseStatus( HttpStatus.NO_CONTENT )
-    @PreAuthorize( "hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')" )
+    @RequestMapping(value = RESOURCE_PATH, method = RequestMethod.PUT)
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    @PreAuthorize("hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')")
     public void reloadApps()
     {
         appManager.reloadApps();
     }
 
-    @RequestMapping( value = "/apps/{app}/**", method = RequestMethod.GET )
-    @PreAuthorize( "hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')" )
-    public void renderApp( @PathVariable( "app" ) String app, HttpServletRequest request, HttpServletResponse response ) throws IOException, NotFoundException
+    @RequestMapping(value = "/apps/{app}/**", method = RequestMethod.GET)
+    @PreAuthorize("hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')")
+    public void renderApp( @PathVariable("app") String app, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
         Iterable<Resource> locations = Lists.newArrayList(
             resourceLoader.getResource( "file:" + appManager.getAppFolderPath() + "/" + app + "/" ),
@@ -111,7 +113,8 @@
 
         if ( manifest == null )
         {
-            throw new NotFoundException();
+            response.sendError( HttpServletResponse.SC_NOT_FOUND );
+            return;
         }
 
         App application = JacksonUtils.getJsonMapper().readValue( manifest.getInputStream(), App.class );
@@ -128,22 +131,35 @@
             }
         }
 
-        Resource page = findResource( locations, pageName );
+        Resource resource = findResource( locations, pageName );
 
-        if ( page == null )
+        if ( resource == null )
         {
-            page = findResource( locations, application.getLaunchPath() );
+            resource = findResource( locations, application.getLaunchPath() );
 
-            if ( page == null )
+            if ( resource == null )
             {
-                throw new NotFoundException();
+                response.sendError( HttpServletResponse.SC_NOT_FOUND );
+                return;
             }
         }
 
-        String mimeType = request.getSession().getServletContext().getMimeType( page.getFilename() );
-        response.setContentType( mimeType );
-
-        StreamUtils.copy( page.getInputStream(), response.getOutputStream() );
+        if ( new ServletWebRequest( request ).checkNotModified( resource.lastModified() ) )
+        {
+            response.setStatus( HttpStatus.NOT_MODIFIED.value() );
+            return;
+        }
+
+        String mimeType = request.getSession().getServletContext().getMimeType( resource.getFilename() );
+
+        if ( mimeType != null )
+        {
+            response.setContentType( mimeType );
+        }
+
+        response.setContentLength( (int) resource.contentLength() );
+        response.setHeader( "Last-Modified", DateUtils.getHttpDateString( new Date( resource.lastModified() ) ) );
+        StreamUtils.copy( resource.getInputStream(), response.getOutputStream() );
     }
 
     @RequestMapping( value = "/apps/{app}", method = RequestMethod.DELETE )