Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: [S2] how to read request parameter in windows-1257 encoding

ros

2007-02-26

Replies:


In web.xml I found CharacterEncodingFilter with UTF8 and force=true.
That's why character encoding of the request was UFT-8
(getRequest().getCharacterEncoding() = UFT8)

I've tested all decode combinations with this code:

    Set<String> encs =
java.nio.charset.Charset.availableCharsets().keySet();
    Iterator<String> itrFrom = encs.iterator();
    while (itrFrom.hasNext()) {
       String encFrom = itrFrom.next();
       Iterator<String> itrTo = encs.iterator();
       while (itrTo.hasNext()) {
          String encTo = itrTo.next();
          try {
            String value = new
String(getRequest().getParameter("PARAM").getBytes(encFrom), encTo);
            if (value.indexOf("Ļ") != -1 || value.indexOf("ļ") !=
-1) {
               log.debug(encFrom+" -> "+encTo+" = "+value);
            }
          } catch (Exception e) {
            log.debug(encFrom+" -> "+encTo+" = ???");
          }
       }
    }

and found that parameter can not be decoded.

Because I have only one form in win1257 encoding (all others utf8) I've made
custom URI bases set enconding filter:

public class UriEncodingFilter extends CharacterEncodingFilter {

  String forceUriEncodings[];

  /**
  * List of URI=ENCODING values seprated by ';'
  * Filter will set request enconding to the ENCODING value if in request
uri will find URI substring.
  *
  * @param values
  */
  public void setForceUriEncodings(String values) {
    this.forceUriEncodings = values.split(";");
  }

  protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain) throws
ServletException, IOException {

    for (int i=0;i<forceUriEncodings.length;i++)
    {
       int e = forceUriEncodings[i].lastIndexOf("=")-1;
       if ( e != -1 &&
request.getRequestURI().indexOf(forceUriEncodings[i].substring(0, e)) != -1
) {
         
request.setCharacterEncoding(forceUriEncodings[i].substring(e+2));
          filterChain.doFilter(request, response);
          return;
       }
    }

    super.doFilterInternal(request, response, filterChain);

  }
}

and made settings in web.xml

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>...package.UriEncodingFilter</filter-class>
    <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
    </init-param>
    <init-param>
       <param-name>forceUriEncodings</param-name>
       <param-value>/aaa.html=Cp1257;/bbb.html=Cp1256;</param-value>
    </init-param>
  </filter>

now it's ok.

Thank you all for help!
--
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.