myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfreed...@apache.org
Subject svn commit: r811172 - /myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java
Date Thu, 03 Sep 2009 23:31:40 GMT
Author: mfreedman
Date: Thu Sep  3 23:31:39 2009
New Revision: 811172

URL: http://svn.apache.org/viewvc?rev=811172&view=rev
Log:
Updated HeaderMap impls to always construct/use the portlet defined values for the content-type/accept
headers as they commonly don't match what some portlet containers show through in the operation
that gets the headers/properties.

Modified:
    myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java

Modified: myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java
URL: http://svn.apache.org/viewvc/myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java?rev=811172&r1=811171&r2=811172&view=diff
==============================================================================
--- myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java
(original)
+++ myfaces/portlet-bridge/core/trunk_2.0.x/impl/src/main/java/org/apache/myfaces/portlet/faces/util/map/PortletRequestHeaders.java
Thu Sep  3 23:31:39 2009
@@ -48,7 +48,7 @@
     initHeaderMap();
 
     List<String> headerVals = mHeaders.get(name.toUpperCase());
-    return headerVals == null ? null : (String) headerVals.get(0);
+    return headerVals == null? null: (String) headerVals.get(0);
   }
 
   public Enumeration<String> getHeaders(String name)
@@ -57,12 +57,12 @@
     initHeaderMap();
 
     List<String> headerVals = mHeaders.get(name.toUpperCase());
-    
+
     if (headerVals == null)
     {
       headerVals = Collections.emptyList();
     }
-    
+
     return Collections.enumeration(headerVals);
   }
 
@@ -77,177 +77,190 @@
   /**
    * Does 'lazy' initialization of Map of 'properties', i.e. mime headers.
    */
-  @SuppressWarnings("unchecked")
-  protected boolean initHeaderMap()
-  {
-    if (mHeaders != null)
-    {
-      return false;
-    }
-
-    mHeaders = Collections.emptyMap();
-    mHeaderNames = Collections.emptyList();
-
-    Enumeration<String> props = mPortletRequest.getPropertyNames();
-    while (props.hasMoreElements())
-    {
-      String name = props.nextElement();
-      Enumeration<String> values = mPortletRequest.getProperties(name);
-      while (values != null && values.hasMoreElements())
-      {
-        addProperty(name, values.nextElement());
-      }
-    }
-    
-    StringBuilder property = null;
-
-    // if they don't already exist, now add in the the required (HTTP)
-    // headers to ensure compatibility with servlets
-    if (!containsHeader(mHeaderNames, "ACCEPT"))
-    {
-      Enumeration<String> contentTypes = mPortletRequest.getResponseContentTypes();
-      property = new StringBuilder(64);
-      
-      boolean addComma = false;
-      while (contentTypes.hasMoreElements())
-      {
-        String type = contentTypes.nextElement();
-        if (type != null)
-        {
-          if (addComma)
-          {
-            property = property.append(',');
-          }
-          else
-          {
-            addComma = true;
-          }
-          
-          property = property.append(type);
-        }
-      }
-
-      if (addComma)
-      {
-        addProperty("ACCEPT", property.toString());
-      }
-    }
-
-    if (!containsHeader(mHeaderNames, "ACCEPT-LANGUAGE"))
-    {
-      Enumeration<Locale> locales = mPortletRequest.getLocales();
-      if (property == null)
-      {
-        property = new StringBuilder(64);
-      }
-      else
-      {
-        property.setLength(0);
-      }
-      
-      boolean addComma = false;
-      while (locales.hasMoreElements())
-      {
-        Locale l = locales.nextElement();
-        if (l != null)
-        {
-          if (addComma)
-          {
-            property = property.append(',');
-          }
-          else
-          {
-            addComma = true;
-          }
-          
-          String s = l.getLanguage();
-          // only add if language not empty
-          if (s.length() > 0)
-          {
-            property = property.append(s);
-            s = l.getCountry();
-            if (s.length() > 0)
-            {
-              property = property.append('-');
-              property = property.append(s);
-            }
-          }
-        }
-      }
-
-      if (addComma)
-      {
-        addProperty("ACCEPT-LANGUAGE", property.toString());
-      }
-    }
-
-    if ((Bridge.PortletPhase) mPortletRequest.getAttribute(Bridge.PORTLET_LIFECYCLE_PHASE)
== Bridge.PortletPhase.ACTION_PHASE)
-    {
-
-      if (!containsHeader(mHeaderNames, "CONTENT-TYPE"))
-      {
-        String contentType = ((ActionRequest) mPortletRequest).getContentType();
-        String charset = ((ActionRequest) mPortletRequest).getCharacterEncoding();
-
-        if (contentType != null)
-        {
-          if (property == null)
-          {
-            property = new StringBuilder(64);
-          }
-          else
-          {
-            property.setLength(0);
-          }
-
-          property = property.append(contentType);
-          if (charset != null)
-          {
-            property = property.append("; charset=");
-            property = property.append(charset);
-          }
-          addProperty("CONTENT-TYPE", property.toString());
-        }
-      }
-
-      if (!containsHeader(mHeaderNames, "CONTENT-LENGTH"))
-      {
-        int contentLength = ((ActionRequest) mPortletRequest).getContentLength();
-
-        if (contentLength != -1)
-        {
-          addProperty("CONTENT-LENGTH", String.valueOf(contentLength));
-        }
-      }
-
-    }
-    // Technically don't need this test here but I will forget to change this code when
-    // JSR 286 is supported and there are more phases.
-    else if ((Bridge.PortletPhase) mPortletRequest.getAttribute(Bridge.PORTLET_LIFECYCLE_PHASE)
== Bridge.PortletPhase.RENDER_PHASE)
-    {
-      // its the RENDER_PHASE -- spec says we must remove the CONTENT_TYPE if 
-      // came in the request -- so it matches null return from
-      // EC.getRequestContentType/CharacterSetEncoding
-      mHeaders.remove("CONTENT-TYPE");
-      mHeaderNames.remove("CONTENT-TYPE");
-    }
-
-    return true;
-  }
-
-  private boolean containsHeader(List<String> headerNames, String key)
-  {
-    for (String name : headerNames)
-    {
-      if (key.toUpperCase().equals(name.toUpperCase()))
-      {
-        return true;
-      }
-    }
-    
-    return false;
-  }
-
+   @SuppressWarnings("unchecked")
+   protected boolean initHeaderMap()
+   {
+     if (mHeaders != null)
+     {
+       return false;
+     }
+
+     mHeaders = Collections.emptyMap();
+     mHeaderNames = Collections.emptyList();
+
+     Enumeration<String> props = mPortletRequest.getPropertyNames();
+     while (props.hasMoreElements())
+     {
+       String name = props.nextElement();
+       Enumeration<String> values = mPortletRequest.getProperties(name);
+       while (values != null && values.hasMoreElements())
+       {
+         addProperty(name, values.nextElement());
+       }
+     }
+
+     StringBuilder property = null;
+
+     // can't assume portlet container overrides these headers to reflect portlet constraints
-- so do so
+     ensurePortletAcceptHeader();
+     ensurePortletAcceptLanguage();
+
+
+     if ((Bridge.PortletPhase) mPortletRequest.getAttribute(Bridge.PORTLET_LIFECYCLE_PHASE)
==
+         Bridge.PortletPhase.ACTION_PHASE)
+     {
+       ensurePortletContentType();
+       ensurePortletContentLength();
+     }
+     // Technically don't need this test here but I will forget to change this code when
+     // JSR 286 is supported and there are more phases.
+     else if ((Bridge.PortletPhase) mPortletRequest.getAttribute(Bridge.PORTLET_LIFECYCLE_PHASE)
==
+              Bridge.PortletPhase.RENDER_PHASE)
+     {
+       // its the RENDER_PHASE -- spec says we must remove the CONTENT_TYPE if
+       // came in the request -- so it matches null return from
+       // EC.getRequestContentType/CharacterSetEncoding
+       mHeaders.remove("CONTENT-TYPE");
+       mHeaderNames.remove("CONTENT-TYPE");
+     }
+
+     return true;
+   }
+
+   private boolean containsHeader(List<String> headerNames, String key)
+   {
+     for (String name : headerNames)
+     {
+       if (key.toUpperCase().equals(name.toUpperCase()))
+       {
+         return true;
+       }
+     }
+     
+     return false;
+   }
+   
+   private void ensurePortletAcceptHeader()
+   {
+     // Make sure its not there
+     mHeaders.remove("ACCEPT");
+     mHeaderNames.remove("ACCEPT");
+     
+     Enumeration<String> contentTypes =
+       mPortletRequest.getResponseContentTypes();
+     StringBuilder property = new StringBuilder(64);
+
+     boolean addComma = false;
+     while (contentTypes.hasMoreElements())
+     {
+       String type = contentTypes.nextElement();
+       if (type != null)
+       {
+         if (addComma)
+         {
+           property = property.append(',');
+         }
+         else
+         {
+           addComma = true;
+         }
+
+         property = property.append(type);
+       }
+     }
+
+     if (addComma)
+     {
+       addProperty("ACCEPT", property.toString());
+     }
+   }
+
+   private void ensurePortletAcceptLanguage()
+   {
+     // Make sure its not there
+     mHeaders.remove("ACCEPT-LANGUAGE");
+     mHeaderNames.remove("ACCEPT-LANGUAGE");
+     
+     StringBuilder property = new StringBuilder(64);
+     Enumeration<Locale> locales = mPortletRequest.getLocales();
+
+     boolean addComma = false;
+     while (locales.hasMoreElements())
+     {
+       Locale l = locales.nextElement();
+       if (l != null)
+       {
+         if (addComma)
+         {
+           property = property.append(',');
+         }
+         else
+         {
+           addComma = true;
+         }
+
+         String s = l.getLanguage();
+         // only add if language not empty
+         if (s.length() > 0)
+         {
+           property = property.append(s);
+           s = l.getCountry();
+           if (s.length() > 0)
+           {
+             property = property.append('-');
+             property = property.append(s);
+           }
+         }
+       }
+     }
+
+     if (addComma)
+     {
+       addProperty("ACCEPT-LANGUAGE", property.toString());
+     }
+   }
+   
+   private void ensurePortletContentType()
+   {
+     // Make sure its not there
+     mHeaders.remove("CONTENT-TYPE");
+     mHeaderNames.remove("CONTENT-TYPE");
+     
+     StringBuilder property = new StringBuilder(64);
+     String contentType =
+       ((ActionRequest) mPortletRequest).getContentType();
+     String charset =
+       ((ActionRequest) mPortletRequest).getCharacterEncoding();
+
+     if (contentType != null)
+     {
+       property = property.append(contentType);
+       if (charset != null)
+       {
+         property = property.append("; charset=");
+         property = property.append(charset);
+       }
+       
+       addProperty("CONTENT-TYPE", property.toString());
+     }
+   }
+
+   private void ensurePortletContentLength()
+   {
+     // Make sure its not there
+     mHeaders.remove("CONTENT-LENGTH");
+     mHeaderNames.remove("CONTENT-LENGTH");
+     
+     int contentLength =
+       ((ActionRequest) mPortletRequest).getContentLength();
+
+     if (contentLength != -1)
+     {
+       addProperty("CONTENT-LENGTH", String.valueOf(contentLength));
+     }
+   }
+   
+   
   protected final void addProperty(String name, String value)
   {
     if (mHeaders == Collections.EMPTY_MAP)



Mime
View raw message