You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/11/30 20:30:17 UTC

svn commit: r1208718 - in /myfaces/shared/trunk/core/src/main: java/org/apache/myfaces/shared/config/ java/org/apache/myfaces/shared/renderkit/ java/org/apache/myfaces/shared/renderkit/html/ resources/META-INF/

Author: lu4242
Date: Wed Nov 30 19:30:16 2011
New Revision: 1208718

URL: http://svn.apache.org/viewvc?rev=1208718&view=rev
Log:
synch with impl shared

Added:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/ContentTypeUtils.java
Modified:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
    myfaces/shared/trunk/core/src/main/resources/META-INF/myfaces-metadata.xml

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java?rev=1208718&r1=1208717&r2=1208718&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java Wed Nov 30 19:30:16 2011
@@ -340,6 +340,16 @@ public class MyfacesConfig
     public final static String INIT_PARAM_STRICT_JSF_2_CC_EL_RESOLVER = "org.apache.myfaces.STRICT_JSF_2_CC_EL_RESOLVER";
     public final static boolean INIT_PARAM_STRICT_JSF_2_CC_EL_RESOLVER_DEFAULT = false;
     
+    /**
+     * Define the default content type that the default ResponseWriter generates, when no match can be derived from
+     * HTTP Accept Header.
+     */
+    @JSFWebConfigParam(since="2.0.11,2.1.5", expectedValues="text/html, application/xhtml+xml", 
+            defaultValue="text/html", group="render")
+    public final static String INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE = 
+        "org.apache.myfaces.DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE";
+    public final static String INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE_DEFAULT = "text/html";
+    
     private boolean _prettyHtml;
     private boolean _detectJavascript;
     private boolean _allowJavascript;
@@ -365,6 +375,7 @@ public class MyfacesConfig
     private boolean _debugPhaseListenerEnabled;
     private boolean _strictJsf2RefreshTargetAjax;
     private boolean _strictJsf2CCELResolver;
+    private String _defaultResponseWriterContentTypeMode;
 
     private static final boolean TOMAHAWK_AVAILABLE;
     private static final boolean MYFACES_IMPL_AVAILABLE;
@@ -459,6 +470,7 @@ public class MyfacesConfig
         setDebugPhaseListenerEnabled(INIT_PARAM_DEBUG_PHASE_LISTENER_DEFAULT);
         setStrictJsf2RefreshTargetAjax(INIT_PARAM_STRICT_JSF_2_REFRESH_TARGET_AJAX_DEFAULT);
         setStrictJsf2CCELResolver(INIT_PARAM_STRICT_JSF_2_CC_EL_RESOLVER_DEFAULT);
+        setDefaultResponseWriterContentTypeMode(INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE_DEFAULT);
     }
 
     private static MyfacesConfig createAndInitializeMyFacesConfig(ExternalContext extCtx)
@@ -547,6 +559,10 @@ public class MyfacesConfig
         
         myfacesConfig.setStrictJsf2CCELResolver(WebConfigParamUtils.getBooleanInitParameter(extCtx, 
                 INIT_PARAM_STRICT_JSF_2_CC_EL_RESOLVER, INIT_PARAM_STRICT_JSF_2_CC_EL_RESOLVER_DEFAULT));
+        
+        myfacesConfig.setDefaultResponseWriterContentTypeMode(WebConfigParamUtils.getStringInitParameter(
+                extCtx, INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE,
+                INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE_DEFAULT));
 
         if (TOMAHAWK_AVAILABLE)
         {
@@ -1003,4 +1019,15 @@ public class MyfacesConfig
     {
         this._strictJsf2CCELResolver = strictJsf2CCELResolver;
     }
+
+    public String getDefaultResponseWriterContentTypeMode()
+    {
+        return _defaultResponseWriterContentTypeMode;
+    }
+
+    public void setDefaultResponseWriterContentTypeMode(
+            String defaultResponseWriterContentTypeMode)
+    {
+        this._defaultResponseWriterContentTypeMode = defaultResponseWriterContentTypeMode;
+    }
 }

Added: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/ContentTypeUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/ContentTypeUtils.java?rev=1208718&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/ContentTypeUtils.java (added)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/ContentTypeUtils.java Wed Nov 30 19:30:16 2011
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.shared.renderkit;
+
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.shared.util.StringUtils;
+
+/**
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+public class ContentTypeUtils
+{
+    public static final String HTML_CONTENT_TYPE = "text/html";
+    public static final String TEXT_ANY_CONTENT_TYPE = "text/*";
+    public static final String ANY_CONTENT_TYPE = "*/*";
+
+    public static final String[] HTML_ALLOWED_CONTENT_TYPES = {HTML_CONTENT_TYPE, ANY_CONTENT_TYPE, TEXT_ANY_CONTENT_TYPE};
+    
+    public static final String XHTML_CONTENT_TYPE = "application/xhtml+xml";
+    public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml";
+    public static final String TEXT_XML_CONTENT_TYPE = "text/xml";
+    
+    public static final String[] XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE, APPLICATION_XML_CONTENT_TYPE, TEXT_XML_CONTENT_TYPE};
+    
+    public static final String[] AJAX_XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE};
+
+
+    /**
+     * Indicate if the passes content type match one of the options passed. 
+     */
+    public static boolean containsContentType(String contentType, String[] allowedContentTypes)
+    {
+        if (allowedContentTypes == null)
+        {
+            return false;
+        }
+        for (int i = 0; i < allowedContentTypes.length; i++)
+        {
+            if (allowedContentTypes[i].indexOf(contentType) != -1)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static String chooseWriterContentType(String contentTypeListString, String[] htmlContentTypes, String[] xhtmlContentTypes)
+    {
+        String[] contentTypeList = splitContentTypeListString(contentTypeListString);
+        String[] supportedContentTypeArray = HtmlRendererUtils.getSupportedContentTypes();
+        String selectedContentType = null;
+        for (int i = 0; i < supportedContentTypeArray.length; i++)
+        {
+            String supportedContentType = supportedContentTypeArray[i].trim();
+
+            for (int j = 0; j < contentTypeList.length; j++)
+            {
+                String contentType = (String) contentTypeList[j];
+
+                if (contentType.indexOf(supportedContentType) != -1)
+                {
+                    if (containsContentType(contentType, htmlContentTypes))
+                    {
+                        selectedContentType = HTML_CONTENT_TYPE;
+                    }
+                    else if (containsContentType(contentType, xhtmlContentTypes))
+                    {
+                        selectedContentType = XHTML_CONTENT_TYPE;
+                    }
+                    break;
+                }
+            }
+            if (selectedContentType != null)
+            {
+                break;
+            }
+        }
+        return selectedContentType;
+    }
+    
+    public static String[] splitContentTypeListString(String contentTypeListString)
+    {
+        String[] splittedArray = StringUtils.splitShortString(contentTypeListString, ',');
+        for (int i = 0; i < splittedArray.length; i++)
+        {
+            int semicolonIndex = splittedArray[i].indexOf(";");
+            if (semicolonIndex != -1)
+            {
+                splittedArray[i] = splittedArray[i].substring(0,semicolonIndex);
+            }
+        }
+        return splittedArray;
+    }
+    
+    public static String getContentTypeFromAcceptHeader(FacesContext context)
+    {
+        String contentTypeListString = (String) context.getExternalContext()
+            .getRequestHeaderMap().get("Accept");
+        // There is a windows mobile IE client (6.12) sending
+        // "application/vnd.wap.mms-message;*/*"
+        // Note that the Accept header should be written as 
+        // "application/vnd.wap.mms-message,*/*" ,
+        // so this is bug of the client. Anyway, this is a workaround ...
+        if (contentTypeListString != null
+                && contentTypeListString
+                        .startsWith("application/vnd.wap.mms-message;*/*"))
+        {
+            contentTypeListString = "*/*";
+        }
+        return contentTypeListString;
+    }
+
+}

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java?rev=1208718&r1=1208717&r2=1208718&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java Wed Nov 30 19:30:16 2011
@@ -1652,6 +1652,10 @@ public final class HtmlRendererUtils
             XHTML_CONTENT_TYPE, APPLICATION_XML_CONTENT_TYPE,
             TEXT_XML_CONTENT_TYPE, TEXT_ANY_CONTENT_TYPE, ANY_CONTENT_TYPE };
 
+    /**
+     * @deprecated use ContentTypeUtils instead
+     */
+    @Deprecated
     public static String selectContentType(String contentTypeListString)
     {
         if (contentTypeListString == null)

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java?rev=1208718&r1=1208717&r2=1208718&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java Wed Nov 30 19:30:16 2011
@@ -30,6 +30,7 @@ import javax.faces.FacesException;
 import javax.faces.component.UIComponent;
 import javax.faces.context.ResponseWriter;
 
+import org.apache.myfaces.shared.renderkit.ContentTypeUtils;
 import org.apache.myfaces.shared.renderkit.RendererUtils;
 import org.apache.myfaces.shared.renderkit.html.util.UnicodeEncoder;
 import org.apache.myfaces.shared.util.CommentUtils;
@@ -73,6 +74,8 @@ public class HtmlResponseWriterImpl
     
     private String _contentType;
     
+    private String _writerContentTypeMode;
+    
     /**
      * This var prevents check if the contentType is for xhtml multiple times.
      */
@@ -131,7 +134,15 @@ public class HtmlResponseWriterImpl
     }
 
     public HtmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding,
-             boolean wrapScriptContentWithXmlCommentTag)
+            boolean wrapScriptContentWithXmlCommentTag)
+    {
+        this(writer,contentType, characterEncoding, wrapScriptContentWithXmlCommentTag, 
+                contentType != null && HtmlRendererUtils.isXHTMLContentType(contentType) ? 
+                    ContentTypeUtils.XHTML_CONTENT_TYPE : ContentTypeUtils.HTML_CONTENT_TYPE);
+    }
+    
+    public HtmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding,
+             boolean wrapScriptContentWithXmlCommentTag, String writerContentTypeMode)
     throws FacesException
     {
         _outputWriter = writer;
@@ -149,10 +160,11 @@ public class HtmlResponseWriterImpl
             }
             _contentType = DEFAULT_CONTENT_TYPE;
         }
-        _isXhtmlContentType = HtmlRendererUtils.isXHTMLContentType(_contentType);
+        _writerContentTypeMode = writerContentTypeMode;
+        _isXhtmlContentType = writerContentTypeMode.indexOf(ContentTypeUtils.XHTML_CONTENT_TYPE) != -1;
         
-        _useStraightXml = _contentType.indexOf(APPLICATION_XML_CONTENT_TYPE) != -1 ||
-                          _contentType.indexOf(TEXT_XML_CONTENT_TYPE) != -1;
+        _useStraightXml = _isXhtmlContentType && (_contentType.indexOf(APPLICATION_XML_CONTENT_TYPE) != -1 ||
+                          _contentType.indexOf(TEXT_XML_CONTENT_TYPE) != -1);
 
         if (characterEncoding == null)
         {
@@ -200,6 +212,11 @@ public class HtmlResponseWriterImpl
     {
         return _contentType;
     }
+    
+    public String getWriterContentTypeMode()
+    {
+        return _writerContentTypeMode;
+    }
 
     public String getCharacterEncoding()
     {
@@ -861,7 +878,7 @@ public class HtmlResponseWriterImpl
     {
         HtmlResponseWriterImpl newWriter
                 = new HtmlResponseWriterImpl(writer, getContentType(), getCharacterEncoding(), 
-                        _wrapScriptContentWithXmlCommentTag);
+                        _wrapScriptContentWithXmlCommentTag, _writerContentTypeMode);
         //newWriter._writeDummyForm = _writeDummyForm;
         //newWriter._dummyFormParams = _dummyFormParams;
         return newWriter;

Modified: myfaces/shared/trunk/core/src/main/resources/META-INF/myfaces-metadata.xml
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/resources/META-INF/myfaces-metadata.xml?rev=1208718&r1=1208717&r2=1208718&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/resources/META-INF/myfaces-metadata.xml (original)
+++ myfaces/shared/trunk/core/src/main/resources/META-INF/myfaces-metadata.xml Wed Nov 30 19:30:16 2011
@@ -8185,6 +8185,19 @@ To ensure strict compatibility with the 
       <group>EL</group>
     </webConfigParam>
     <webConfigParam>
+      <name>org.apache.myfaces.DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE</name>
+      <fieldName>INIT_PARAM_DEFAULT_RESPONSE_WRITER_CONTENT_TYPE_MODE</fieldName>
+      <desc>Define the default content type that the default ResponseWriter generates, when no match can be derived from
+HTTP Accept Header</desc>
+      <longDesc>Define the default content type that the default ResponseWriter generates, when no match can be derived from
+HTTP Accept Header.</longDesc>
+      <defaultValue>text/html</defaultValue>
+      <expectedValues>text/html, application/xhtml+xml</expectedValues>
+      <sourceClassName>org.apache.myfaces.shared.config.MyfacesConfig</sourceClassName>
+      <since>2.0.11,2.1.5</since>
+      <group>render</group>
+    </webConfigParam>
+    <webConfigParam>
       <name>org.apache.myfaces.FLASH_SCOPE_DISABLED</name>
       <fieldName>FLASH_SCOPE_DISABLED_PARAM</fieldName>
       <desc>Defines whether flash scope is disabled, preventing add the Flash cookie to the response</desc>