You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@ws.apache.org by sn...@apache.org on 2002/11/14 17:22:09 UTC

cvs commit: xml-soap/java/src/org/apache/soap/util/mime MimeUtils.java

snichol     2002/11/14 08:22:09

  Modified:    java/src/org/apache/soap Constants.java
               java/src/org/apache/soap/rpc Call.java SOAPContext.java
               java/src/org/apache/soap/transport SOAPTransport.java
                        TransportMessage.java
               java/src/org/apache/soap/util/mime MimeUtils.java
  Log:
  Submitted by: Pavel Ausianik <Pa...@epam.com>
  
  Here is server time picture taken on the Tomcat server , processing
  ComplexRequest.
  The red ellipses show that MimePart  initialization takes 10-15% of CPU
  load.
  The blue ellipses show that ContentType is also quite expensive for benefits
  it provide. I prepared patch for caching ContentType...
  
  Some small comment modifications to SOAPTransport.java by Scott Nichol are
  also included in this commit.
  
  Revision  Changes    Path
  1.29      +7 -6      xml-soap/java/src/org/apache/soap/Constants.java
  
  Index: Constants.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/Constants.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- Constants.java	14 Nov 2002 05:00:42 -0000	1.28
  +++ Constants.java	14 Nov 2002 16:22:06 -0000	1.29
  @@ -59,6 +59,7 @@
   
   import org.apache.soap.util.xml.QName;
   import javax.mail.internet.ContentType;
  +import org.apache.soap.util.mime.MimeUtils;
   
   /**
    * <em>SOAP</em> constants.
  @@ -85,7 +86,7 @@
       "http://schemas.xmlsoap.org/soap/envelope/";
     public static final String NS_URI_SOAP_ENC =
       "http://schemas.xmlsoap.org/soap/encoding/";
  -  
  +
     public static final String NS_URI_1999_SCHEMA_XSI =
       "http://www.w3.org/1999/XMLSchema-instance";
     public static final String NS_URI_1999_SCHEMA_XSD =
  @@ -143,7 +144,7 @@
     public static final String HEADERVAL_CONTENT_ENCODING = "gzip";
   
     // XML Declaration string
  -  public static final String XML_DECL = 
  +  public static final String XML_DECL =
       "<?xml version='1.0' encoding='UTF-8'?>\r\n";
   
     // Element names.
  @@ -308,9 +309,9 @@
     public static final QName object2001QName =
       new QName(Constants.NS_URI_2001_SCHEMA_XSD, "anyType");
   
  -  public static final ContentType CTYPE_TEXT_ALL = new ContentType("text", "*", null);
  -  public static final ContentType CTYPE_TEXT_XML = new ContentType("text", "xml", null);
  -  public static final ContentType CTYPE_MULTIPART =
  -          new ContentType(HEADERVAL_CONTENT_TYPE_MULTIPART_PRIMARY, "*", null);
  +  public static final ContentType CTYPE_TEXT_ALL = MimeUtils.getContentType("text/*");
  +  public static final ContentType CTYPE_TEXT_XML = MimeUtils.getContentType("text/xml");
  +  public static final ContentType CTYPE_MULTIPART = MimeUtils.getContentType(
  +          HEADERVAL_CONTENT_TYPE_MULTIPART_PRIMARY + "/*");
   
   }
  
  
  
  1.21      +2 -4      xml-soap/java/src/org/apache/soap/rpc/Call.java
  
  Index: Call.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/rpc/Call.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Call.java	12 Nov 2002 14:34:56 -0000	1.20
  +++ Call.java	14 Nov 2002 16:22:08 -0000	1.21
  @@ -66,6 +66,7 @@
   import org.xml.sax.*;
   import org.apache.soap.util.xml.*;
   import org.apache.soap.util.IOUtils;
  +import org.apache.soap.util.mime.MimeUtils;
   import org.apache.soap.*;
   import org.apache.soap.encoding.*;
   import org.apache.soap.transport.*;
  @@ -273,10 +274,7 @@
       MimeBodyPart rootPart = respCtx.getRootPart();
       String ctype = rootPart.getContentType();
       ContentType type = null;
  -    try {
  -        type = new ContentType(ctype);
  -    }
  -    catch (ParseException e) {}
  +    type = MimeUtils.getContentType(ctype);
   
       if (type != null && Constants.CTYPE_TEXT_ALL.match(type)) {
         // Get the input stream to read the response envelope from.
  
  
  
  1.14      +1 -1      xml-soap/java/src/org/apache/soap/rpc/SOAPContext.java
  
  Index: SOAPContext.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/rpc/SOAPContext.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- SOAPContext.java	6 Nov 2002 18:08:04 -0000	1.13
  +++ SOAPContext.java	14 Nov 2002 16:22:08 -0000	1.14
  @@ -476,7 +476,7 @@
       public MimeBodyPart getRootPart() throws MessagingException {
           MimeBodyPart rootPart = null;
           if (getCount() > 1) {
  -            String startCid = new ContentType(
  +            String startCid = MimeUtils.getContentType(
                   parts.getContentType()).getParameter("start");
               if (startCid != null)
                   rootPart = getBodyPart(MimeUtils.decode(startCid));
  
  
  
  1.8       +2 -2      xml-soap/java/src/org/apache/soap/transport/SOAPTransport.java
  
  Index: SOAPTransport.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/transport/SOAPTransport.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SOAPTransport.java	8 Jan 2001 12:25:25 -0000	1.7
  +++ SOAPTransport.java	14 Nov 2002 16:22:09 -0000	1.8
  @@ -89,10 +89,10 @@
       throws SOAPException;
   
     /**
  -   * Return a buffered reader to receive back the response to whatever
  +   * Return a buffered reader to receive back the response envelope to whatever
      * was sent to whatever.
      *
  -   * @return a reader to read the results from or null if that's not
  +   * @return a reader to read the envelope from or null if that's not
      *         possible.
      */
     public BufferedReader receive ();
  
  
  
  1.20      +10 -13    xml-soap/java/src/org/apache/soap/transport/TransportMessage.java
  
  Index: TransportMessage.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/transport/TransportMessage.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- TransportMessage.java	14 Nov 2002 05:00:42 -0000	1.19
  +++ TransportMessage.java	14 Nov 2002 16:22:09 -0000	1.20
  @@ -169,7 +169,7 @@
           if (contentLength != 0) {
               int offset = 0;
               int bytesRead = 0;
  -    
  +
               // We're done reading when we get all the content OR when the stream
               // returns a -1.
               while ((contentLength < 0 || offset < contentLength) && (bytesRead >= 0)) {
  @@ -258,15 +258,12 @@
           // Parse and validate content type.
           ContentType cType = null;
           if (contentType != null) {
  -            try {
  -                // Hack since WebSphere puts ;; instead of just ;
  -                int pos = contentType.indexOf( ";;" );
  -                if ( pos != -1 )
  -                  contentType = contentType.substring(0,pos) +
  -                                contentType.substring(pos+1) ;
  -                cType = new ContentType(contentType);
  -            } catch(ParseException pe) {
  -            }
  +            // Hack since WebSphere puts ;; instead of just ;
  +            int pos = contentType.indexOf( ";;" );
  +            if ( pos != -1 )
  +              contentType = contentType.substring(0,pos) +
  +                            contentType.substring(pos+1) ;
  +            cType = MimeUtils.getContentType(contentType);
           }
           if (cType == null)
               throw new SOAPException(Constants.FAULT_CODE_PROTOCOL,
  @@ -321,7 +318,7 @@
   
               // Find root part.
               rootPart = ctx.getRootPart();
  -            rootContentType = new ContentType(rootPart.getContentType());
  +            rootContentType = MimeUtils.getContentType(rootPart.getContentType());
               ByteArrayDataSource bads = new ByteArrayDataSource(
                   rootPart.getInputStream(), null);
               rootBytes = bads.toByteArray();
  @@ -420,7 +417,7 @@
                   new ByteArrayOutputStream(65536);
               ctx.writeTo(payload);
               bytes = payload.toByteArray();
  -    
  +
               // Now strip off the headers. (Grmbl, get rid of JavaMail
               // for MIME support). Just intercept the Content-Type
               // header. We don't want any of the MIME headers, and we know the
  @@ -514,7 +511,7 @@
                   MimeBodyPart rootPart = ctx.getRootPart();
                   if (rootPart != null) {
                       String ctype = rootPart.getContentType();
  -                    ContentType type = new ContentType(ctype);
  +                    ContentType type = MimeUtils.getContentType(ctype);
                       if (type != null && Constants.CTYPE_TEXT_ALL.match(type)) {
                           ByteArrayDataSource ds = new ByteArrayDataSource(
                               rootPart.getInputStream(), ctype);
  
  
  
  1.6       +22 -4     xml-soap/java/src/org/apache/soap/util/mime/MimeUtils.java
  
  Index: MimeUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/mime/MimeUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MimeUtils.java	6 Sep 2002 17:50:27 -0000	1.5
  +++ MimeUtils.java	14 Nov 2002 16:22:09 -0000	1.6
  @@ -70,6 +70,8 @@
    */
   
   public class MimeUtils {
  +
  +    private static HashMap contentTypes = new HashMap();
       /**
        * Get a unique value.
        *
  @@ -100,11 +102,8 @@
        */
       public static String getEncoding(String type, String defaultEncoding) {
           String encoding = null;
  -        try {
           if (type != null && !type.equals(""))
  -            encoding = new ContentType(type).getParameter("charset");
  -        } catch(ParseException pe) {
  -        }
  +            encoding = getContentType(type).getParameter("charset");
           if (encoding == null)
               encoding = defaultEncoding;
           else
  @@ -135,5 +134,24 @@
               }
           }
           return ret.toString();
  +    }
  +
  +    /**
  +     * Return a content type from shared pool
  +     */
  +    public static ContentType getContentType(String ctype) {
  +        synchronized (contentTypes) {
  +            ContentType type = (ContentType)contentTypes.get(ctype);
  +            if (type == null) {
  +                try {
  +                    type = new ContentType(ctype);
  +                }
  +                catch (ParseException e) {
  +                    return null;
  +                }
  +                contentTypes.put(ctype, type);
  +            }
  +            return type;
  +        }
       }
   }