Author: veithen
Date: Mon May 24 09:35:18 2010
New Revision: 947578

URL: http://svn.apache.org/viewvc?rev=947578&view=rev
Log:
Make sure that we strictly adhere to RFC2392 (Content-ID and Message-ID Uniform 
Resource Locators).

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodingStreamWriter.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java?rev=947578&r1=947577&r2=947578&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
 Mon May 24 09:35:18 2010
@@ -32,6 +32,7 @@ import org.apache.axiom.om.impl.builder.
 import org.apache.axiom.soap.SOAPFactory;
 import org.apache.axiom.soap.SOAPHeaderBlock;
 import org.apache.axiom.util.stax.TextFromElementReader;
+import org.apache.axiom.util.stax.xop.XOPUtils;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -41,9 +42,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
-import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-import java.net.URLDecoder;
 import java.util.Iterator;
 
 /**
@@ -167,19 +166,7 @@ public class ElementHelper {
      * @return the corresponding content ID
      */
     public static String getContentIDFromHref(String href) {
-        if (href.startsWith("cid:")) {
-            try {
-                // URIs should always be decoded using UTF-8 (see 
WSCOMMONS-429). On the
-                // other hand, since non ASCII characters are not allowed in 
content IDs,
-                // we can simply decode using ASCII (which is a subset of 
UTF-8)
-                return URLDecoder.decode(href.substring(4), "ascii");
-            } catch (UnsupportedEncodingException ex) {
-                // We should never get here
-                throw new OMException(ex);
-            }
-        } else {
-            throw new OMException("href attribute didn't contain a valid cid: 
URI");
-        }
+        return XOPUtils.getContentIDFromURL(href);
     }
     
     /**

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodingStreamWriter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodingStreamWriter.java?rev=947578&r1=947577&r2=947578&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodingStreamWriter.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodingStreamWriter.java
 Mon May 24 09:35:18 2010
@@ -75,7 +75,7 @@ public class XOPEncodingStreamWriter ext
             parent.setPrefix(XOPConstants.DEFAULT_PREFIX, 
XOPConstants.NAMESPACE_URI);
             parent.writeNamespace(XOPConstants.DEFAULT_PREFIX, 
XOPConstants.NAMESPACE_URI);
         }
-        parent.writeAttribute(XOPConstants.HREF, "cid:" + contentID); // TODO: 
wrong; need to URI encode
+        parent.writeAttribute(XOPConstants.HREF, 
XOPUtils.getURLForContentID(contentID));
         parent.writeEndElement();
     }
     

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java?rev=947578&r1=947577&r2=947578&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
 Mon May 24 09:35:18 2010
@@ -20,6 +20,9 @@
 package org.apache.axiom.util.stax.xop;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 
 import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamReader;
@@ -40,6 +43,44 @@ public class XOPUtils {
     private XOPUtils() {}
     
     /**
+     * Extract the content ID from a URL following the cid scheme defined by 
RFC2392.
+     * 
+     * @param url the URL
+     * @return the corresponding content ID
+     * @throws IllegalArgumentException if the URL doesn't use the cid scheme
+     */
+    public static String getContentIDFromURL(String url) {
+        if (url.startsWith("cid:")) {
+            try {
+                // URIs should always be decoded using UTF-8 (see 
WSCOMMONS-429). On the
+                // other hand, since non ASCII characters are not allowed in 
content IDs,
+                // we can simply decode using ASCII (which is a subset of 
UTF-8)
+                return URLDecoder.decode(url.substring(4), "ascii");
+            } catch (UnsupportedEncodingException ex) {
+                // We should never get here
+                throw new Error(ex);
+            }
+        } else {
+            throw new IllegalArgumentException("The URL doesn't use the cid 
scheme");
+        }
+    }
+    
+    /**
+     * Build a cid URL from the given content ID as described in RFC2392.
+     * 
+     * @param contentID the content ID (without enclosing angle brackets)
+     * @return the corresponding URL in the cid scheme
+     */
+    public static String getURLForContentID(String contentID) {
+        try {
+            return "cid:" + URLEncoder.encode(contentID, "ascii");
+        } catch (UnsupportedEncodingException ex) {
+            // We should never get here
+            throw new Error(ex);
+        }
+    }
+    
+    /**
      * Get an XOP encoded stream for a given stream reader. Depending on its
      * type and characteristics, this method may wrap or unwrap the stream
      * reader:

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java?rev=947578&r1=947577&r2=947578&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
 Mon May 24 09:35:18 2010
@@ -24,8 +24,8 @@ import java.io.IOException;
 import javax.activation.DataHandler;
 import javax.xml.bind.attachment.AttachmentUnmarshaller;
 
-import org.apache.axiom.om.util.ElementHelper;
 import org.apache.axiom.util.stax.xop.MimePartProvider;
+import org.apache.axiom.util.stax.xop.XOPUtils;
 
 public class AttachmentUnmarshallerImpl extends AttachmentUnmarshaller {
     private final MimePartProvider mimePartProvider;
@@ -45,7 +45,7 @@ public class AttachmentUnmarshallerImpl 
     public DataHandler getAttachmentAsDataHandler(String cid) {
         try {
             accessed = true;
-            return 
mimePartProvider.getDataHandler(ElementHelper.getContentIDFromHref(cid));
+            return 
mimePartProvider.getDataHandler(XOPUtils.getContentIDFromURL(cid));
         } catch (IOException ex) {
             throw new RuntimeException(ex);
         }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java?rev=947578&r1=947577&r2=947578&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java
 Mon May 24 09:35:18 2010
@@ -35,6 +35,7 @@ import org.apache.axiom.om.util.StAXUtil
 import org.apache.axiom.soap.SOAP12Constants;
 import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
 import org.apache.axiom.util.stax.XMLStreamReaderUtils;
+import org.apache.axiom.util.stax.xop.XOPUtils;
 
 import javax.activation.DataHandler;
 import javax.xml.namespace.QName;
@@ -46,7 +47,6 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -284,13 +284,13 @@ public class MTOMStAXSOAPModelBuilderTes
         String contentTypeString =
                 "multipart/Related; charset=\"UTF-8\"; 
type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; 
start=\"SOAPPart\"";
         String originalCID = 
"1.urn:uuid:[email protected]";
-        String encodedCID = URLEncoder.encode(originalCID, "UTF-8"); // URIs 
are always encoded using UTF-8 (see WSCOMMONS-429)
+        String cidURL = XOPUtils.getURLForContentID(originalCID);
         String xmlPlusMime1 = "------=_AxIs2_Def_boundary_=42214532\r\n" +
                 "Content-Type: application/xop+xml; charset=UTF-16\r\n" +
                 "Content-Transfer-Encoding: 8bit\r\n" +
                 "Content-ID: SOAPPart\r\n" +
                 "\r\n";
-        String xmlPlusMime2 = "<soapenv:Envelope 
xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\";><soapenv:Body><m:data 
xmlns:m=\"http://www.example.org/stuff\";><m:name 
m:contentType=\"text/plain\"><xop:Include 
xmlns:xop=\"http://www.w3.org/2004/08/xop/include\"; href=\"cid:" + encodedCID + 
"\"></xop:Include></m:name></m:data></soapenv:Body></soapenv:Envelope>\r\n";
+        String xmlPlusMime2 = "<soapenv:Envelope 
xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\";><soapenv:Body><m:data 
xmlns:m=\"http://www.example.org/stuff\";><m:name 
m:contentType=\"text/plain\"><xop:Include 
xmlns:xop=\"http://www.w3.org/2004/08/xop/include\"; href=\"" + cidURL + 
"\"></xop:Include></m:name></m:data></soapenv:Body></soapenv:Envelope>\r\n";
         String xmlPlusMime3 = "\r\n------=_AxIs2_Def_boundary_=42214532\r\n" +
                 "Content-Transfer-Encoding: binary\r\n" +
                 "Content-ID: " + originalCID + "\r\n" +


Reply via email to