开发者

How do I set JBoss 5 encoding to behave like former JBoss 4.1?

开发者 https://www.devze.com 2023-01-23 20:39 出处:网络
It is an application that has been built using seam 2.2 and it is running perfectly under Jboss 4.1.2. Because of support from my ISP I will have to upgrade JBoss to version 5.1 . I did try to run sam

It is an application that has been built using seam 2.2 and it is running perfectly under Jboss 4.1.2. Because of support from my ISP I will have to upgrade JBoss to version 5.1 . I did try to run same app under JBoss 5 and everything looked fine but one single thing failed.

It is a piece of code that generates some JSon to feed a Chart built using Open Flash Chart libraries. I also use JOFC libraries to generate the JSon for these charts. Well the approach we have used basically has a seam factory which prints a JSon and than we do reference this factory in a blank .xhtml page from where the chart loads the required JSon to render properly. Follow some snippets:

The xhtml page where the seam factory gets called:

<f:view xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://java.sun.com/jsf/core"
   contentType="text/json; charset=UTF-8">

   #{jsonGraficoPesoUsuario}

</f:view> 

The factory method which generates the JSon string:

@Factory("jsonGraficoPesoUsuario")
 public String graficoPesoUsuario()
 {
...

   String jsonString = chart.toString();
   log.info("JSonString ==>> " + jsonString);
//   try {
//    jsonString = URLEncoder.encode(jsonString, "UTF-8");
//   } catch (UnsupportedEncodingException e) {
//    log.error("Erro ao tentar fazer o encode da string json ==>> " + e.getMessage());
//    e.printStackTrace();
//   }
   return jsonString;

Well the code shows also that we have tryied开发者_如何学JAVA encoding the json string before sending but again no success. In the line where it is printed json to log.info the json string is perfectly preserved, but using JBoss5 what happens is that the " (double quote) chars are replaced by html escape characters.

The follow json string is expected to properly feed the chart and this behaves correctly under jboss4:

{"y_axis":{"min":100,"colour":"#96A9C5","grid-colour":"#DDDEE1","max":112},"title":{"text"
:"Gráfico de evolução de Peso"},"bg_colour":"#FFFFFF","is_decimal_separator_comma":1,"elements":[{"text":"Peso","values":[100.5,101.5,102.5,
102,103,107,109,110.5,108],"font-size":10,"type":"line","tip":"Linha do Peso"}],"num_decimals":2,"is_fixed_num_decimals_forced":1,"x_axis":{
"colour":"#96A9C5","grid-colour":"#DDDEE1","labels":{"rotate":"-24","labels":["01/05/10","15/05/10","25/05/10","01/06/10","09/06/10","25/06/
10","05/07/10","10/07/10","20/07/10"]}},"is_thousand_separator_disabled":0} 

But than using JBoss5 the follow string is printed, notice the html encode replacement for double quotes:

{&quot;y_axis&quot;:{&quot;min&quot;:100,&quot;colour&quot;:&quot;#96A9C5&quot;,&quot;grid-colour&quot;:&quot;#DDDEE1&quot;,&quot;max&quot;:112},&quot;title&quot;:{&quot;text&quot;:&quot;Gr&aacute;fico de evolu&ccedil;&atilde;o de Peso&quot;},&quot;bg_colour&quot;:&quot;#FFFFFF&quot;,&quot;is_decimal_separator_comma&quot;:1,&quot;elements&quot;:[{&quot;text&quot;:&quot;Peso&quot;,&quot;values&quot;:[100.5,101.5,102.5,102,103,107,109,110.5,108],&quot;font-size&quot;:10,&quot;type&quot;:&quot;line&quot;,&quot;tip&quot;:&quot;Linha do Peso&quot;}],&quot;num_decimals&quot;:2,&quot;is_fixed_num_decimals_forced&quot;:1,&quot;x_axis&quot;:{&quot;colour&quot;:&quot;#96A9C5&quot;,&quot;grid-colour&quot;:&quot;#DDDEE1&quot;,&quot;labels&quot;:{&quot;rotate&quot;:&quot;-24&quot;,&quot;labels&quot;:[&quot;01/05/10&quot;,&quot;15/05/10&quot;,&quot;25/05/10&quot;,&quot;01/06/10&quot;,&quot;09/06/10&quot;,&quot;25/06/10&quot;,&quot;05/07/10&quot;,&quot;10/07/10&quot;,&quot;20/07/10&quot;]}},&quot;is_thousand_separator_disabled&quot;:0}

I have already checked O.S locale and also did a configuration on jboss http channel to encode the body in server.xml:

<!-- A HTTP/1.1 Connector on port 8080 -->
      <Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" 
               connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true"/>

It didn't work. We use UTF-8 for all resources and pages in our app.

Any clues?

Tx in advance. []s


I took a look at jsf documentation to review it and found f:verbatim tag. It fixed the issue. Now my .xhtml page where the json string is printed is like:

<f:view xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://java.sun.com/jsf/core"
   contentType="text/json" >

  <f:verbatim> 
   #{jsonGraficoPesoUsuario}
   </f:verbatim>
</f:view>

Now the result json string is not changed and the graphic renders properly.

[]s


I don't know if this will help, but I will try.

In components.xml set

<web:character-encoding-filter encoding="UTF-8" override-client="true" url-pattern="*.seam" />

Create a Filter that will set everything to UTF-8

public class UTF8Filter implements Filter {

  public void destroy() {}

  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)   throws IOException, ServletException {
    // set encoding to UTF-8
    req.setCharacterEncoding("UTF-8");
    chain.doFilter(req, res);
    return;
  }

  public void init(FilterConfig arg0) throws ServletException {}

}

And enable this filter in web.xml

<filter>
  <filter-name>UTF8 Filter</filter-name>
  <filter-class>my.package.filter.UTF8Filter</filter-class>
</filter>

If this doesn't help, you can manually encode the string

 //If you only have problem with &quot; you can do this
 jsonString.replaceAll("&quot;","\\\"");

I am not sure the regex will work correctly, but you can try. You can also perhaps use StringEscapeUtils from Apache Commons to correctly escape all HTML.


String output=new String(input.getBytes(),"UTF-8");

Giving like this solved the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消