Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-3651

Struts 2 is calling response.setLocale even though it will not handle the request

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.3
    • Fix Version/s: 2.3.16
    • Component/s: Dispatch Filter
    • Labels:
      None
    • Environment:

      Windows 7, Java 1.6

      Description

      The org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter --> org.apache.struts2.dispatcher.Dispatcher.prepare(HttpServletRequest request, HttpServletResponse response) is calling response.setLocale(locale) when you specify a default locale (using struts.properties struts.locale). This is wrong because consider the following example:

      This is a static resource stored in the following path: www.mydomain.com/myApp/scripts/utils.js where myApp is the webcontext and /scripts/utils.js is a java script file.

      1. A request to /myApp/scripts/utils.js.
      1. Even though Struts 2 is not going to handle the request org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter calls prepare.setEncodingAndLocale(request, response);
      which sets the Request Encoding and the Response Locale.
      2. Servlet Container Response setLocale obtains the character encoding corresponding to that locale and assign a character encoding to the response. This behavior is correct according to the spec: http://download.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#setLocale%28java.util.Locale%29
      3. Struts Execute filter doesn't handle the request so chain.doFilter(request, response); is called.
      4. Once all filters are called the servlet container DefaultServlet is called to handle the request and send the content of the file assigning the response header Content-Type in which this case it will use the encoding type that was set before by the StrutsPrepareFilter. This might not correspond to the actual encoding of the file.

      An example of the Header Response Content-Type:
      Content-Type application/x-javascript;charset=ISO-8859-1

      This only happens when you specify the default locale in the struts.properties because of these lines in Dispatcher.prepare():

      String encoding = null;
      if (defaultEncoding != null) {
          encoding = defaultEncoding;
      }
      Locale locale = null;
      if (defaultLocale != null) {
          locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
      }
      

      I think that locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale()); should be removed because setting locale will set the character encoding. And this has side effects when requests are made to static resources.

        Issue Links

          Activity

          Hide
          lukaszlenart Lukasz Lenart added a comment -

          I think WW-4199 is the only possible fix - if something isn't handled by Struts, Struts cannot change HttpServletResponse.

          Show
          lukaszlenart Lukasz Lenart added a comment - I think WW-4199 is the only possible fix - if something isn't handled by Struts, Struts cannot change HttpServletResponse.
          Hide
          jlmagc Jose L Martinez-Avial added a comment -

          That's exactly what the fix for WW-4199 does.

          Show
          jlmagc Jose L Martinez-Avial added a comment - That's exactly what the fix for WW-4199 does.
          Hide
          aosorio Alfredo Osorio added a comment -

          Struts 2 shouldn't be intrusive if it is not going to handle the request.

          Show
          aosorio Alfredo Osorio added a comment - Struts 2 shouldn't be intrusive if it is not going to handle the request.
          Hide
          jlmagc Jose L Martinez-Avial added a comment -

          I believe this issues is related to https://issues.apache.org/jira/browse/WW-4199, and that the fix for WW-4199 will also solve this.

          Show
          jlmagc Jose L Martinez-Avial added a comment - I believe this issues is related to https://issues.apache.org/jira/browse/WW-4199 , and that the fix for WW-4199 will also solve this.
          Hide
          rgielen Rene Gielen added a comment - - edited

          I thing not calling setLocale has the side effect of, well, not setting the response locale. It will only have a side effect on encoding if not character encoding is set so far:

          It also sets the response's character encoding appropriately for the locale, 
          if the character encoding has not been explicitly set using setContentType
          or setCharacterEncoding, getWriter hasn't been called yet, and the response
          hasn't been committed yet.
          

          Setting the locale is really important to our framework users. Serving static resource with various encodings on the other hand is not the usual 80% use case. I think you should chose out of these two:

          1. use UTF-8 all the way, and set struts.i18n.encoding=UTF-8. This should take precedence over the encoding side effect of setLocale - if not, this is a bug to address.
          2. place your custom filter after StrutsPrepareFilter and address the encoding issues you are facing as you specifically need it

          I really recommend option 1, UTF-8 is the only true source nowadays
          But dropping setLocale is really not what we want.

          Show
          rgielen Rene Gielen added a comment - - edited I thing not calling setLocale has the side effect of, well, not setting the response locale. It will only have a side effect on encoding if not character encoding is set so far: It also sets the response's character encoding appropriately for the locale, if the character encoding has not been explicitly set using setContentType or setCharacterEncoding, getWriter hasn't been called yet, and the response hasn't been committed yet. Setting the locale is really important to our framework users. Serving static resource with various encodings on the other hand is not the usual 80% use case. I think you should chose out of these two: 1. use UTF-8 all the way, and set struts.i18n.encoding=UTF-8 . This should take precedence over the encoding side effect of setLocale - if not, this is a bug to address. 2. place your custom filter after StrutsPrepareFilter and address the encoding issues you are facing as you specifically need it I really recommend option 1, UTF-8 is the only true source nowadays But dropping setLocale is really not what we want.

            People

            • Assignee:
              Unassigned
              Reporter:
              aosorio Alfredo Osorio
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development