← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16027: Moved some methods in the ContextUtils classes. Removed dependency on dhis-web-commons from dhis-...

 

------------------------------------------------------------
revno: 16027
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-07-08 18:48:00 +0200
message:
  Moved some methods in the ContextUtils classes. Removed dependency on dhis-web-commons from dhis-web-api.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/CodecUtils.java
  dhis-2/dhis-web/dhis-web-api/pom.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/CodecUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/CodecUtils.java	2014-07-07 10:06:36 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/CodecUtils.java	2014-07-08 16:48:00 +0000
@@ -148,6 +148,13 @@
         return string;
     }
     
+    /**
+     * Encodes the given string by removing chars which are illegal on most file 
+     * systems.
+     * 
+     * @param string the string to encode.
+     * @return an encoded filename string.
+     */
     public static String filenameEncode( String string )
     {
         if ( string != null )
@@ -163,6 +170,14 @@
         return string;
     }
     
+    /**
+     * Returns a basic authentication string which is generated by prepending 
+     * "Basic " and base64-encoding username:password.
+     * 
+     * @param username the username to use for authentication.
+     * @param password the password to use for authentication.
+     * @return the 
+     */
     public static String getBasicAuthString( String username, String password )
     {        
         String string = username + ":" + password;
@@ -170,4 +185,3 @@
         return "Basic " + Base64.encodeBase64String( string.getBytes() );
     }    
 }
-

=== modified file 'dhis-2/dhis-web/dhis-web-api/pom.xml'
--- dhis-2/dhis-web/dhis-web-api/pom.xml	2014-07-07 07:29:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/pom.xml	2014-07-08 16:48:00 +0000
@@ -19,10 +19,6 @@
   <dependencies>
     <dependency>
       <groupId>org.hisp.dhis</groupId>
-      <artifactId>dhis-web-commons</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.hisp.dhis</groupId>
       <artifactId>dhis-api</artifactId>
     </dependency>
     <dependency>

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java	2014-05-22 12:40:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java	2014-07-08 16:48:00 +0000
@@ -205,7 +205,7 @@
 
     private String getBaseUrl( HttpServletRequest request )
     {
-        String baseUrl = org.hisp.dhis.util.ContextUtils.getBaseUrl( request );
+        String baseUrl = ContextUtils.getBaseUrl( request );
         return baseUrl.substring( 0, baseUrl.length() - 1 );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java	2014-06-11 20:27:54 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java	2014-07-08 16:48:00 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 import org.hisp.dhis.system.filter.OrganisationUnitWithValidCoordinatesFilter;
 import org.hisp.dhis.system.util.FilterUtils;
+import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.webapi.webdomain.WebOptions;
 import org.hisp.dhis.webapi.webdomain.GeoFeature;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,6 +51,7 @@
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -61,8 +63,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import static org.hisp.dhis.util.ContextUtils.clearIfNotModified;
-
 /**
  * @author Lars Helge Overland
  */
@@ -106,7 +106,7 @@
 
         FilterUtils.filter( organisationUnits, new OrganisationUnitWithValidCoordinatesFilter() );
 
-        boolean modified = !clearIfNotModified( request, response, organisationUnits );
+        boolean modified = !ContextUtils.clearIfNotModified( request, response, organisationUnits );
 
         if ( !modified )
         {

=== 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-06-30 17:11:33 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java	2014-07-08 16:48:00 +0000
@@ -36,6 +36,7 @@
 import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Calendar;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -46,11 +47,13 @@
 
 import org.apache.commons.io.IOUtils;
 import org.hisp.dhis.common.DimensionalObjectUtils;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.setting.SystemSettingManager;
+import org.hisp.dhis.system.util.CodecUtils;
 import org.hisp.dhis.system.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
-import org.springframework.security.crypto.codec.Base64;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
@@ -80,9 +83,11 @@
     public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
     public static final String HEADER_LOCATION = "Location";
     
-    public static final String DATE_PATTERN = "yyyy-MM-dd";
-    
+    public static final String DATE_PATTERN = "yyyy-MM-dd";    
     public static final String QUERY_PARAM_SEP = ";";
+    public static final String HEADER_IF_NONE_MATCH = "If-None-Match";
+    public static final String HEADER_ETAG = "ETag";
+    private static final String QUOTE = "\"";
 
     @Autowired
     private SystemSettingManager systemSettingManager;
@@ -310,6 +315,23 @@
     }
 
     /**
+     * Returns the base URL for the given request.
+     * 
+     * @param request the HTTP servlet request.
+     * @return the base URL.
+     */
+    public static String getBaseUrl( HttpServletRequest request )
+    {
+        String server = request.getServerName();
+        
+        String scheme = request.getScheme();
+        int port = request.getServerPort();
+        String baseUrl = scheme + "://" + server + ":" + port + "/";
+
+        return baseUrl;
+    }
+
+    /**
      * Adds basic authentication by adding an Authorization header to the
      * given HttpHeaders object.
      *
@@ -319,8 +341,39 @@
      */
     public static void setBasicAuth( HttpHeaders headers, String username, String password )
     {
-        String authorisation = username + ":" + password;
-        byte[] encodedAuthorisation = Base64.encode( authorisation.getBytes() );
-        headers.add( "Authorization", "Basic " + new String( encodedAuthorisation ) );
+        headers.add( "Authorization", CodecUtils.getBasicAuthString( username, password ) );
+    }
+
+    /**
+     * Clears the given collection if it is not modified according to the HTTP
+     * cache validation. This method looks up the ETag sent in the request from 
+     * the "If-None-Match" header value, generates an ETag based on the given 
+     * collection of IdentifiableObjects and compares them for equality. If this
+     * evaluates to true, it will set status code 304 Not Modified on the response
+     * and remove all elements from the given list. It will also set the ETag header
+     * on the response in any case.
+     * 
+     * @param request the HttpServletRequest.
+     * @param response the HttpServletResponse.
+     * @return true if the eTag values are equals, false otherwise.
+     */
+    public static boolean clearIfNotModified( HttpServletRequest request, HttpServletResponse response, Collection<? extends IdentifiableObject> objects )
+    {
+        String tag = QUOTE + IdentifiableObjectUtils.getLastUpdatedTag( objects ) + QUOTE;
+        
+        response.setHeader( HEADER_ETAG, tag );
+        
+        String inputTag = request.getHeader( HEADER_IF_NONE_MATCH );
+
+        if ( objects != null && inputTag != null && inputTag.equals( tag ) )
+        {
+            response.setStatus( HttpServletResponse.SC_NOT_MODIFIED );
+            
+            objects.clear();
+            
+            return true;
+        }
+        
+        return false;
     }
 }
\ No newline at end of file

=== 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	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2014-07-08 16:48:00 +0000
@@ -67,9 +67,6 @@
     public static final String HEADER_IF_NONE_MATCH = "If-None-Match";
     public static final String HEADER_ETAG = "ETag";
 
-    private static final String SEPARATOR = "/";
-    private static final String PORT_SEPARATOR = ":";
-    private static final String PROTOCOL = "http://";;
     private static final String QUOTE = "\"";
     
     private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<String, String>() 
@@ -108,17 +105,6 @@
         return params;
     }
 
-    public static String getBaseUrl( HttpServletRequest request )
-    {
-        String server = request.getServerName();
-
-        int port = request.getServerPort();
-
-        String baseUrl = PROTOCOL + server + PORT_SEPARATOR + port + SEPARATOR;
-
-        return baseUrl;
-    }
-
     public static void configureResponse( HttpServletResponse response, String contentType, boolean disallowCache,
         String filename, boolean attachment )
     {