2 Replies Latest reply on Apr 4, 2008 8:40 AM by cavani

    Charset encoding problem from Linux to Windows with Dispatch

    cavani

      Hi,

      I am using JBossWS Native for client code implementation in a set of Eclipse plugins. At first, I used WSDL2Java approach to generate client code - this works fine with WS-Security and all. But now, I need take advantage of BIRT's WS/XML ODA driver (that uses something like XPath to map XML as tables).

      So, I converted all generated code to Dispatch approach with StreamSource. My first problem was with WS-Security with Dispatch not decrypting response. This was fixed with 2.0.4, and works perfectly when server and client are on Linux machines. But when I ran the client on a Windows XP, no accent character is read, but the message are decrypted.

      The server is an unmodified AS 4.2.2 (running on linux). Java 5 (13 for linux, 15 for windows bundled together).

      I start digging into JBossWS code but could not find an fix yet.

      My code (it work fine on linux-linux):

       QName serviceName = new QName("http://tlon.com.br", local);
       QName portName = new QName("http://tlon.com.br", local + "Port");
      
       Service service = Service.create(wsdlLocation, serviceName);
      
       Dispatch<StreamSource> dispatch = service.createDispatch(portName, StreamSource.class, Mode.MESSAGE);
      
       Map<String, Object> reqContext = dispatch.getRequestContext();
       reqContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
       reqContext.put(BindingProvider.USERNAME_PROPERTY, username);
       reqContext.put(BindingProvider.PASSWORD_PROPERTY, "");
      
       ConfigProvider config = (ConfigProvider) dispatch;
       config.setSecurityConfig(DispatchFactory.class.getClassLoader().getResource("META-INF/jboss-wsse-client.xml").toExternalForm());
       config.setConfigName("Standard WSSecurity Client");
      


       Reader requestReader = new StringReader(requestMessage);
       StreamSource request = new StreamSource(requestReader);
       StreamSource response = dispatch.invoke(request);
       requestReader.close();
       Reader responseReader = response.getReader();
       StringWriter responseWriter = new StringWriter();
       char[] buffer = new char[1024];
       int read = -1;
       while ((read = responseReader.read(buffer)) != -1)
       responseWriter.write(buffer, 0, read);
       responseReader.close();
       return responseWriter.toString();
      


      If someone has a good clue, please let me know... if I find a fix I will report here.

      Thanks,

        • 1. Re: Charset encoding problem from Linux to Windows with Disp
          cavani

          It was just my stupidity that prevent me to success on this... sorry!

          I updated xmlsec to last version 1.4.1 (from 1.3.0 that is distributed with JBossWS), but this didn't help.

          After some reading about charset and encoding, The code that really works is this:

           Reader requestReader = new StringReader(requestMessage);
           StreamSource request = new StreamSource(requestReader);
           StreamSource response = dispatch.invoke(request);
           requestReader.close();
           TransformerFactory factory = TransformerFactory.newInstance();
           Transformer transformer = factory.newTransformer();
           BufferedStreamResult result = new BufferedStreamResult();
           transformer.transform(response, result);
           String responseMessage = result.toString();
           byte[] raw = responseMessage.getBytes("UTF-8");
           return new String(raw);
          


          Thanks,

          • 2. Re: Charset encoding problem from Linux to Windows with Disp
            cavani

            There is something that I am not figuring out. Some characters just are misinterpreted... anyway... I change to SOAPMessage and set the default encoding to UTF-8 (from windows-1252 on Windows)... now it really works!

            for JVM:-Dfile.encoding=UTF-8

             private Dispatch<SOAPMessage> dispatch;
            
             private MessageFactory messageFactory;
            
             /* package */ DispatchHandler(Dispatch<SOAPMessage> dispatch) throws Exception
             {
             this.dispatch = dispatch;
            
             messageFactory = MessageFactory.newInstance();
             }
            
             public SOAPMessage createRequest(String request) throws Exception
             {
             ByteArrayInputStream stream = new ByteArrayInputStream(request.getBytes());
             SOAPMessage message = messageFactory.createMessage(null, stream);
             stream.close();
             return message;
             }
            
             public String createResponse(SOAPMessage message) throws Exception
             {
             ByteArrayOutputStream stream = new ByteArrayOutputStream(1024);
             message.writeTo(stream);
             String response = stream.toString();
             stream.close();
             return response;
             }
            
             public String invoke(String request) throws Exception
             {
             try
             {
             SOAPMessage requestMessage = createRequest(request);
            
             SOAPMessage responseMessage = dispatch.invoke(requestMessage);
            
             return createResponse(responseMessage);
             }