← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16030: Web API, impl support for including custom javascripts and css styles into web app. Useful for cu...

 

------------------------------------------------------------
revno: 16030
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-07-08 21:02:06 +0200
message:
  Web API, impl support for including custom javascripts and css styles into web app. Useful for custom forms and HTML reports where you want to centralize scripts/styles and keep them out of the forms/reports themselves.
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileController.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm


--
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
=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileController.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileController.java	2014-07-08 19:02:06 +0000
@@ -0,0 +1,130 @@
+package org.hisp.dhis.webapi.controller;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.StringUtils;
+import org.hisp.dhis.setting.SystemSettingManager;
+import org.hisp.dhis.webapi.utils.ContextUtils;
+import org.hisp.dhis.webapi.utils.ContextUtils.CacheStrategy;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+/**
+ * @author Lars Helge Overland
+ */
+@Controller
+@RequestMapping( value = FileController.RESOURCE_PATH )
+public class FileController
+{
+    public static final String RESOURCE_PATH = "/files";
+    
+    private static final String KEY_CUSTOM_JS = "keyCustomJs";
+    private static final String KEY_CUSTOM_CSS = "keyCustomCss";
+
+    @Autowired
+    private SystemSettingManager systemSettingManager;
+    
+    @Autowired
+    private ContextUtils contextUtils;
+
+    // -------------------------------------------------------------------------
+    // Custom script
+    // -------------------------------------------------------------------------
+
+    @RequestMapping( value = "/script", method = RequestMethod.GET )
+    public void getCustomScript( HttpServletResponse response, Writer writer )
+        throws IOException
+    {
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JAVASCRIPT, CacheStrategy.CACHE_TWO_WEEKS );
+        
+        String content = (String) systemSettingManager.getSystemSetting( KEY_CUSTOM_JS, StringUtils.EMPTY );
+        
+        writer.write( content );
+    }
+
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/script", method = RequestMethod.POST, consumes = "application/javascript" )
+    public void postCustomScript( @RequestBody String content, HttpServletResponse response )
+    {
+        if ( content != null )
+        {
+            systemSettingManager.saveSystemSetting( KEY_CUSTOM_JS, content );
+            ContextUtils.okResponse( response, "Custom script created" );
+        }
+    }
+
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/script", method = RequestMethod.DELETE )
+    public void removeCustomScript( HttpServletResponse response )
+    {
+        systemSettingManager.deleteSystemSetting( KEY_CUSTOM_JS );
+    }
+    
+    // -------------------------------------------------------------------------
+    // Custom style
+    // -------------------------------------------------------------------------
+
+    @RequestMapping( value = "/style", method = RequestMethod.GET )
+    public void getCustomStyle( HttpServletResponse response, Writer writer )
+        throws IOException
+    {
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSS, CacheStrategy.CACHE_TWO_WEEKS );
+        
+        String content = (String) systemSettingManager.getSystemSetting( KEY_CUSTOM_CSS, StringUtils.EMPTY );
+        
+        writer.write( content );
+    }
+
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/style", method = RequestMethod.POST, consumes = { "text/html", "text/plain", "text/css" } )
+    public void postCustomStyle( @RequestBody String content, HttpServletResponse response )
+    {
+        if ( content != null )
+        {
+            systemSettingManager.saveSystemSetting( KEY_CUSTOM_CSS, content );
+            ContextUtils.okResponse( response, "Custom style created" );
+        }
+    }
+
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/style", method = RequestMethod.DELETE )
+    public void removeCustomStyle( HttpServletResponse response )
+    {
+        systemSettingManager.deleteSystemSetting( KEY_CUSTOM_CSS );
+    }    
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2014-07-08 16:48:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2014-07-08 19:02:06 +0000
@@ -68,6 +68,7 @@
     public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
     public static final String CONTENT_TYPE_HTML = "text/html; charset=UTF-8";
     public static final String CONTENT_TYPE_TEXT = "text/plain; charset=UTF-8";
+    public static final String CONTENT_TYPE_CSS = "text/css; charset=UTF-8";
     public static final String CONTENT_TYPE_XML = "application/xml; charset=UTF-8";
     public static final String CONTENT_TYPE_CSV = "application/csv; charset=UTF-8";
     public static final String CONTENT_TYPE_PNG = "image/png";

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm	2014-06-26 14:38:54 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm	2014-07-08 19:02:06 +0000
@@ -18,6 +18,7 @@
     #foreach ( $style in $stylesheets )
     <link type="text/css" rel="stylesheet" href="${style}?_rev=$!{buildRevision}">
     #end
+    <link type="text/css" rel="stylesheet" href="../api/files/style?_rev=$!{buildRevision}" />
     <link rel="shortcut icon" href="../favicon.ico" />
 
     <script type="text/javascript">
@@ -57,13 +58,10 @@
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.select.js?_rev=$!{buildRevision}"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/jQuery/calendars/jquery.calendars.min.js?_rev=$!{buildRevision}"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/jQuery/calendars/jquery.calendars.plus.min.js?_rev=$!{buildRevision}"></script>
-
     #if( $keyCalendar == "coptic" || $keyCalendar == "ethiopian" || $keyCalendar == "islamic"  || $keyCalendar == "julian" || $keyCalendar == "nepali" || $keyCalendar == "thai" )
-      <script type="text/javascript" src="../dhis-web-commons/javascripts/jQuery/calendars/jquery.calendars.${keyCalendar}.min.js?_rev=$!{buildRevision}"></script>
+    <script type="text/javascript" src="../dhis-web-commons/javascripts/jQuery/calendars/jquery.calendars.${keyCalendar}.min.js?_rev=$!{buildRevision}"></script>
     #end
-
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.period.js?_rev=$!{buildRevision}"></script>
-
     <script type="text/javascript" src="../dhis-web-commons/javascripts/jQuery/calendars/jquery.calendars.picker.min.js?_rev=$!{buildRevision}"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.selected.js?_rev=$!{buildRevision}"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/dhis2/dhis2.comparator.js?_rev=$!{buildRevision}"></script>
@@ -87,6 +85,7 @@
     #foreach( $javascript in $javascripts )
     <script type="text/javascript" src="${javascript}?_rev=$!{buildRevision}"></script>
     #end
+    <script type="text/javascript" src="../api/files/script?_rev=$!{buildRevision}"></script>
     <!-- Create global system calendar -->
     <script>
       #if( $keyCalendar == 'iso8601')