2 Replies Latest reply on Apr 14, 2005 8:03 AM by Thomas Diesler

    Compression Handler

    Pawel Polyakov Newbie

      Hi! I'm trying to compress the response of my WS. This is the code of handler

      import java.io.ByteArrayOutputStream;
      import java.net.URL;
      import java.util.zip.GZIPOutputStream;
      
      import javax.xml.namespace.QName;
      import javax.xml.rpc.handler.GenericHandler;
      
      import org.apache.axis.Message;
      import org.apache.log4j.Logger;
      import org.apache.log4j.xml.DOMConfigurator;
      
      public class CompressionHandler extends GenericHandler {
      
       static private Logger logger = Logger.getLogger(CompressionHandler.class);
      
       public QName[] getHeaders() {
       return new QName[0];
       }
      
       public boolean handleResponse(org.apache.axis.MessageContext msgContext) {
       try {
       URL log4jConfig = CompressionHandler.class.getResource("log4j.xml");
       DOMConfigurator.configure(log4jConfig);
       logger.info("Logger initialized");
      
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       GZIPOutputStream gz = new GZIPOutputStream(bos);
      
       Message msg = msgContext.getResponseMessage();
       logger.info("length:"+msg.getContentLength());
       gz.write(msg.getSOAPPartAsBytes());
       gz.close();
       bos.close();
      
       Message msg2 = new Message(bos.toByteArray());
      
       msgContext.setResponseMessage(msg2);
      
      
       } catch (Exception e) {
       e.printStackTrace();
       }
       return true;
       }
      
       public boolean handleRequest(org.apache.axis.MessageContext msgContext) {
       try {
       URL log4jConfig = CompressionHandler.class.getResource("log4j.xml");
       DOMConfigurator.configure(log4jConfig);
       logger.info("Logger initialized");
      
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       GZIPOutputStream gz = new GZIPOutputStream(bos);
      
       Message msg = msgContext.getRequestMessage();
       logger.info("length:"+msg.getContentLength());
       gz.write(msg.getSOAPPartAsBytes());
       gz.close();
       bos.close();
      
       Message msg2 = new Message(bos.toByteArray());
      
       msgContext.setRequestMessage(msg2);
      
      
       } catch (Exception e) {
       e.printStackTrace();
       }
       return true;
       }
      
       public CompressionHandler() {
       }
      }


      This is the part of my webservices.xml:
       <webservice-description>
       <webservice-description-name>HelloBeanService</webservice-description-name>
       <wsdl-file>META-INF/wsdl/hello.wsdl</wsdl-file>
      <jaxrpc-mapping-file>META-INF/mapping.xml</jaxrpc-mapping-file>
       <port-component>
       <port-component-name>HelloBean</port-component-name>
       <wsdl-port>HelloBeanEndpoint</wsdl-port>
       <service-endpoint-interface>
       ru.zenith.interfaces.HelloBeanEndpoint
       </service-endpoint-interface>
       <service-impl-bean>
       <ejb-link>HelloBean</ejb-link>
       </service-impl-bean>
       <handler>
       <handler-name>CompressionHandler</handler-name>
       <handler-class>ru.zenith.handlers.CompressionHandler</handler-class>
      
       </handler>
       </port-component>
       </webservice-description>
      


      After deployment I've found this in server.log:
      2005-04-01 17:01:33,984 DEBUG [org.jboss.webservice.server.InvokerProvider] Adding server side handler to service 'HelloBeanService': [class=ru.zenith.handlers.CompressionHandler,headers=[],config={}]
      ...
      2005-04-01 17:01:34,015 DEBUG [org.jboss.webservice.handler.HandlerChainBaseImpl] Handle request: [state=METHOD_READY,handler=ru.zenith.handlers.CompressionHandler@b94bd7]
      2005-04-01 17:01:34,015 DEBUG [org.jboss.webservice.handler.HandlerChainBaseImpl] Exit: doHandleRequest with status: true
      

      But the outgoing SOAP message is still uncompressed.


        • 1. Re: Compression Handler
          Pawel Polyakov Newbie

          Hi again. I have modified code of my Handler and now i have included into in such code:

           public boolean handleResponse(MessageContext msgContext) {
           try {
           logger.debug("CompressionHandler handleResponse");
           SOAPMessage mss=((SOAPMessageContext)msgContext).getMessage();
           logger.debug("after getting message");
           logger.debug(mss.toString());
           org.apache.axis.MessageContext mc=(org.apache.axis.MessageContext)msgContext;
           logger.debug("after cast to org.apache.axis.MessageContext");
           logger.debug(mc.toString());
           ByteArrayOutputStream bos = new ByteArrayOutputStream();
           GZIPOutputStream gz = new GZIPOutputStream(bos);
           logger.debug("after creating Streams");
           Message msg =(Message) mss;
           mss.writeTo(System.out);
           msg.writeTo(System.out);
           gz.write(msg.getSOAPPartAsBytes());
           gz.close();
           bos.close();
           logger.debug("after closing streams");
           Message msg2 = new Message(bos.toByteArray());
          
           mc.setResponseMessage(msg2);
          
           } catch (Exception e) {
           e.printStackTrace();
           logger.error(e);
           return false;
           }
           return true;
           }
          

          There are a lot of unuseful code here but the problems is not in that. I can't compress the message. This is the part of my logfile output:
          ...
          2005-04-04 12:55:14,890 INFO [STDOUT] <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <soapenv:Body>
           <ns1:getComplexResponse xmlns:ns1="http://interfaces.zenith.ru">
           <getComplexReturn>
           <username>sigma</username>
           <cod>6004955</cod>
           </getComplexReturn>
           </ns1:getComplexResponse>
           </soapenv:Body>
          </soapenv:Envelope>
          2005-04-04 12:55:14,890 ERROR [org.apache.axis.MessagePart] Cannot convert 3 to bytes
          2005-04-04 12:55:14,890 INFO [STDOUT] java.lang.NullPointerException
          2005-04-04 12:55:14,890 INFO [STDOUT] at ...
          

          This part of output was produced by this code
           msg.writeTo(System.out);
           gz.write(msg.getSOAPPartAsBytes());
          

          So I don't know whats wrong? Maybe this is the problem of JBOSS axis impl? I'm using jboss4.0. I'm saying this because method
          //msg instanceof org.apache.axis.Message
          msg.getContentLength();
          

          throwing NullPointerException too.

          • 2. Re: Compression Handler
            Thomas Diesler Master

            Can you relieably reproduce the NPE in Message.getContentLength() ?

            If so, you might want to create a jira issue.

            See http://www.jboss.org/index.html?module=bb&op=viewtopic&t=62678
            for instructions