vgritsenko    2002/06/19 21:30:03

  Modified:    src/java/org/apache/cocoon/components/language/markup/xsp
                        Tag: cocoon_2_0_3_branch SOAPHelper.java
  Log:
  Optimize inline object handling.
  Fix InputSource handling: it can have either byte or character stream.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.7.2.1   +108 -98   
xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/SOAPHelper.java
  
  Index: SOAPHelper.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/SOAPHelper.java,v
  retrieving revision 1.7
  retrieving revision 1.7.2.1
  diff -u -r1.7 -r1.7.2.1
  --- SOAPHelper.java   22 Feb 2002 07:00:08 -0000      1.7
  +++ SOAPHelper.java   20 Jun 2002 04:30:03 -0000      1.7.2.1
  @@ -50,16 +50,9 @@
   */
   package org.apache.cocoon.components.language.markup.xsp;
   
  -import java.io.BufferedReader;
  -import java.io.IOException;
  -import java.io.InputStream;
  -import java.io.InputStreamReader;
  -import java.io.OutputStream;
  -import java.net.MalformedURLException;
  -import java.net.URL;
  -import java.util.HashMap;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
  +
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.xscript.XScriptManager;
   import org.apache.cocoon.components.xscript.XScriptObject;
  @@ -71,106 +64,123 @@
   import org.apache.commons.httpclient.methods.PostMethod;
   import org.xml.sax.InputSource;
   
  +import java.io.InputStreamReader;
  +import java.io.Reader;
  +import java.net.MalformedURLException;
  +import java.net.URL;
  +import java.util.HashMap;
  +
   /**
    * Helper for the SOAP logicsheet.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Ovidiu Predescu</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
    * @version CVS $Id$
    * @since July 16, 2001
    */
  -public class SOAPHelper
  -{
  -  XScriptManager xscriptManager;
  -  URL url;
  -  String action = "";
  -  XScriptObject xscriptObject;
  -
  -  public SOAPHelper(ComponentManager manager, String urlContext, String url,
  -                    String action, XScriptObject xscriptObject)
  -    throws MalformedURLException, ComponentException
  -  {
  -    this.xscriptManager = 
(XScriptManager)manager.lookup(XScriptManager.ROLE);
  -    URL context = new URL(urlContext);
  -    this.url = new URL(context, url);
  -    this.action = action;
  -    this.xscriptObject = xscriptObject;
  -  }
  -
  -  public XScriptObject invoke()
  -    throws ProcessingException
  -  {
  -    HttpConnection conn = null;
  -
  -    try {
  -      if (action == null || action.equals(""))
  -        action = "\"\"";
  -
  -      String host = url.getHost();
  -      int port = url.getPort();
  -
  -      if (System.getProperty("http.proxyHost") != null) {
  -        String proxyHost = System.getProperty("http.proxyHost");
  -        int proxyPort = 
Integer.parseInt(System.getProperty("http.proxyPort"));
  -        conn = new HttpConnection(proxyHost, proxyPort, host, port);
  -      }
  -      else {
  -        conn = new HttpConnection(host, port);
  -      }
  -
  -      PostMethod method = new PostMethod(url.getFile()) {
  -          protected String generateRequestBody(HashMap params)
  -          {
  -            try {
  -              StringBuffer bodyBuffer
  -                = new StringBuffer(super.generateRequestBody(params));
  +public class SOAPHelper {
  +    XScriptManager xscriptManager;
  +    URL url;
  +    String action = "";
  +    XScriptObject xscriptObject;
  +
  +    public SOAPHelper(ComponentManager manager, String urlContext, String 
url,
  +                      String action, XScriptObject xscriptObject)
  +            throws MalformedURLException, ComponentException
  +    {
  +        this.xscriptManager = (XScriptManager) 
manager.lookup(XScriptManager.ROLE);
  +        URL context = new URL(urlContext);
  +        this.url = new URL(context, url);
  +        this.action = action;
  +        this.xscriptObject = xscriptObject;
  +    }
   
  -              // Write the SOAP request
  -              InputSource saxInputStream = xscriptObject.getInputSource();
  -              InputStream is = saxInputStream.getByteStream();
  -              InputStreamReader isr = new InputStreamReader(is);
  -
  -              char[] buffer = new char[1024];
  -              int len;
  -              while ((len = isr.read(buffer)) > 0)
  -                bodyBuffer.append(buffer, 0, len);
  -              isr.close();
  -              is.close();
  -              return bodyBuffer.toString();
  +    public XScriptObject invoke() throws ProcessingException
  +    {
  +        HttpConnection conn = null;
  +
  +        try {
  +            if (action == null || action.equals("")) {
  +                action = "\"\"";
               }
  -            catch (Exception ex) {
  -              return null;
  +
  +            String host = url.getHost();
  +            int port = url.getPort();
  +
  +            if (System.getProperty("http.proxyHost") != null) {
  +                String proxyHost = System.getProperty("http.proxyHost");
  +                int proxyPort = 
Integer.parseInt(System.getProperty("http.proxyPort"));
  +                conn = new HttpConnection(proxyHost, proxyPort, host, port);
  +            } else {
  +                conn = new HttpConnection(host, port);
               }
  -          }
  -        };
   
  -        method.setRequestHeader(
  -                new Header("Content-type", "text/xml; charset=\"utf-8\""));
  -        method.setRequestHeader(new Header("SOAPAction", action));
  -        method.setUseDisk(false);
  -
  -        method.execute(new HttpState(), conn);
  -
  -        String ret = method.getResponseBodyAsString();
  -        int startOfXML = ret.indexOf("<?xml");
  -        if(startOfXML == -1) { // No xml?!
  -            throw new ProcessingException("Invalid response - no xml");
  -        }
  +            PostMethod method = new PostMethod(url.getFile()) {
  +                protected String generateRequestBody(HashMap params) {
  +                    try {
  +                        StringBuffer bodyBuffer
  +                                = new 
StringBuffer(super.generateRequestBody(params));
  +
  +                        // Write the SOAP request body
  +                        if (xscriptObject instanceof XScriptObjectInlineXML) 
{
  +                            // Skip overhead
  +                            bodyBuffer.append(((XScriptObjectInlineXML) 
xscriptObject).getContent());
  +                        } else {
  +                            InputSource saxSource = 
xscriptObject.getInputSource();
  +
  +                            Reader r = null;
  +                            // Byte stream or character stream?
  +                            if (saxSource.getByteStream() != null) {
  +                                r = new 
InputStreamReader(saxSource.getByteStream());
  +                            } else {
  +                                r = saxSource.getCharacterStream();
  +                            }
  +
  +                            try {
  +                                char[] buffer = new char[1024];
  +                                int len;
  +                                while ((len = r.read(buffer)) > 0)
  +                                    bodyBuffer.append(buffer, 0, len);
  +                            } finally {
  +                                if (r != null) {
  +                                    r.close();
  +                                }
  +                            }
  +                        }
  +
  +                        return bodyBuffer.toString();
  +                    } catch (Exception ex) {
  +                        // FIXME (VG): Will result in NPE in httpClient code
  +                        return null;
  +                    }
  +                }
  +            };
  +
  +            method.setRequestHeader(
  +                    new Header("Content-type", "text/xml; 
charset=\"utf-8\""));
  +            method.setRequestHeader(new Header("SOAPAction", action));
  +            method.setUseDisk(false);
  +
  +            method.execute(new HttpState(), conn);
  +
  +            String ret = method.getResponseBodyAsString();
  +            int startOfXML = ret.indexOf("<?xml");
  +            if (startOfXML == -1) { // No xml?!
  +                throw new ProcessingException("Invalid response - no xml");
  +            }
   
  -        return new XScriptObjectInlineXML(
  -            xscriptManager,
  -            ret.substring(startOfXML));
  -    }
  -    catch (Exception ex) {
  -      throw new ProcessingException("Error invoking remote service: " + ex,
  -                                    ex);
  -    }
  -    finally {
  -      try {
  -        if (conn != null)
  -          conn.close();
  -      }
  -      catch (Exception ex) {
  -      }
  +            return new XScriptObjectInlineXML(
  +                    xscriptManager,
  +                    ret.substring(startOfXML));
  +        } catch (Exception ex) {
  +            throw new ProcessingException("Error invoking remote service: " 
+ ex,
  +                    ex);
  +        } finally {
  +            try {
  +                if (conn != null)
  +                    conn.close();
  +            } catch (Exception ex) {
  +            }
  +        }
       }
  -  }
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to