You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2013/08/04 11:04:45 UTC
svn commit: r1510114 - in /webservices/axiom/trunk/modules/axiom-api/src:
main/java/org/apache/axiom/mime/ main/java/org/apache/axiom/soap/
test/java/org/apache/axiom/mime/
Author: veithen
Date: Sun Aug 4 09:04:45 2013
New Revision: 1510114
URL: http://svn.apache.org/r1510114
Log:
* Added MediaType constants for the most common media types.
* Added a builder for ContentType objects.
* Added a method to SOAPVersion that allows to determine the media type used by a given SOAP version.
Added:
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java (with props)
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java (with props)
Modified:
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentType.java
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/MediaType.java
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Version.java
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Version.java
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPVersion.java
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java
Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentType.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentType.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentType.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentType.java Sun Aug 4 09:04:45 2013
@@ -20,7 +20,10 @@ package org.apache.axiom.mime;
import java.text.ParseException;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
+import java.util.Map;
import javax.activation.MimeType;
@@ -106,6 +109,17 @@ public final class ContentType {
this.parameters = (String[])parameters.toArray(new String[parameters.size()]);
}
+ ContentType(MediaType mediaType, Map parameters) {
+ this.mediaType = mediaType;
+ this.parameters = new String[parameters.size()*2];
+ int i = 0;
+ for (Iterator it = parameters.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry entry = (Map.Entry)it.next();
+ this.parameters[i++] = (String)entry.getKey();
+ this.parameters[i++] = (String)entry.getValue();
+ }
+ }
+
/**
* Get the media type this content type refers to.
*
@@ -131,4 +145,40 @@ public final class ContentType {
}
return null;
}
+
+ /**
+ * Create a string representation of this content type suitable as the value for a
+ * <tt>Content-Type</tt> header as specified by RFC 2045. Note that this method serializes all
+ * parameter values as quoted strings, even values that could be represented as tokens. This is
+ * compatible with R1109 in WS-I Basic Profile 1.2 and 2.0.
+ *
+ * @return the string representation of this content type
+ */
+ public String toString() {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append(mediaType.getPrimaryType());
+ buffer.append('/');
+ buffer.append(mediaType.getSubType());
+ for (int i=0; i<parameters.length; ) {
+ buffer.append("; ");
+ buffer.append(parameters[i++]);
+ buffer.append("=\"");
+ String value = parameters[i++];
+ for (int j=0, l=value.length(); j<l; j++) {
+ char c = value.charAt(j);
+ if (c == '"' || c == '\\') {
+ buffer.append('\\');
+ }
+ buffer.append(c);
+ }
+ buffer.append('"');
+ }
+ return buffer.toString();
+ }
+
+ void getParameters(Map map) {
+ for (int i=0; i<parameters.length; i+=2) {
+ map.put(parameters[i].toLowerCase(Locale.ENGLISH), parameters[i+1]);
+ }
+ }
}
Added: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java?rev=1510114&view=auto
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java (added)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java Sun Aug 4 09:04:45 2013
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.mime;
+
+import java.text.ParseException;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+
+/**
+ * Builder for {@link ContentType} objects. This class can be used to construct {@link ContentType}
+ * objects or as a mutable alternative to {@link ContentType} (which is designed to be immutable).
+ */
+public final class ContentTypeBuilder {
+ private MediaType mediaType;
+ private final LinkedHashMap/*<String,String>*/ parameters = new LinkedHashMap();
+
+ /**
+ * Constructor that initializes the builder with a media type and no parameters.
+ *
+ * @param mediaType
+ * the media type
+ */
+ public ContentTypeBuilder(MediaType mediaType) {
+ this.mediaType = mediaType;
+ }
+
+ /**
+ * Constructor that initializes the builder with the media type and parameters from an existing
+ * {@link ContentType} object.
+ *
+ * @param type
+ * the content type
+ */
+ public ContentTypeBuilder(ContentType type) {
+ this(type.getMediaType());
+ type.getParameters(parameters);
+ }
+
+ /**
+ * Constructor that parses a <tt>Content-Type</tt> header value.
+ *
+ * @param type
+ * the value of the <tt>Content-Type</tt> header conforming to RFC 2045
+ * @throws ParseException
+ * if the value is invalid and could not be parsed
+ */
+ public ContentTypeBuilder(String type) throws ParseException {
+ this(new ContentType(type));
+ }
+
+ /**
+ * Get the media type.
+ *
+ * @return the media type
+ */
+ public MediaType getMediaType() {
+ return mediaType;
+ }
+
+ /**
+ * Set the media type.
+ *
+ * @param mediaType
+ * the media type
+ */
+ public void setMediaType(MediaType mediaType) {
+ this.mediaType = mediaType;
+ }
+
+ /**
+ * Get the specified parameter value.
+ *
+ * @param name
+ * the parameter name
+ * @return the parameter value, or <code>null</code> if no parameter with the given name was
+ * found
+ */
+ public String getParameter(String name) {
+ return (String)parameters.get(name.toLowerCase(Locale.ENGLISH));
+ }
+
+ /**
+ * Set the specified parameter value. If a parameter with the given name already exists, it will
+ * be replaced. Note that parameter names are case insensitive.
+ *
+ * @param name
+ * the parameter name
+ * @param value
+ * the parameter value
+ */
+ public void setParameter(String name, String value) {
+ parameters.put(name.toLowerCase(Locale.ENGLISH), value);
+ }
+
+ /**
+ * Remove all parameters.
+ */
+ public void clearParameters() {
+ parameters.clear();
+ }
+
+ /**
+ * Build the {@link ContentType} object.
+ *
+ * @return the {@link ContentType} object
+ */
+ public ContentType build() {
+ return new ContentType(mediaType, parameters);
+ }
+
+ /**
+ * Create a string representation of the content type. This method uses the same conventions as
+ * {@link ContentType#toString()}.
+ *
+ * @return the string representation of this content type
+ */
+ public String toString() {
+ return build().toString();
+ }
+}
Propchange: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/ContentTypeBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/MediaType.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/MediaType.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/MediaType.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/mime/MediaType.java Sun Aug 4 09:04:45 2013
@@ -29,6 +29,31 @@ package org.apache.axiom.mime;
* to allow comparing media types as described by RFC 2045, i.e. in a case insensitive way.
*/
public final class MediaType {
+ /**
+ * The media type for <tt>text/xml</tt>.
+ */
+ public static final MediaType TEXT_XML = new MediaType("text", "xml");
+
+ /**
+ * The media type for <tt>application/xml</tt>.
+ */
+ public static final MediaType APPLICATION_XML = new MediaType("application", "xml");
+
+ /**
+ * The media type for <tt>application/soap+xml</tt>.
+ */
+ public static final MediaType APPLICATION_SOAP_XML = new MediaType("application", "soap+xml");
+
+ /**
+ * The media type for <tt>application/xop+xml</tt>.
+ */
+ public static final MediaType APPLICATION_XOP_XML = new MediaType("application", "xop+xml");
+
+ /**
+ * The media type for <tt>multipart/related</tt>.
+ */
+ public static final MediaType MULTIPART_RELATED = new MediaType("multipart", "related");
+
private final String primaryType;
private final String subType;
Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Version.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Version.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Version.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Version.java Sun Aug 4 09:04:45 2013
@@ -21,6 +21,8 @@ package org.apache.axiom.soap;
import javax.xml.namespace.QName;
+import org.apache.axiom.mime.MediaType;
+
/**
* Version-specific stuff for SOAP 1.1
*/
@@ -74,4 +76,8 @@ public class SOAP11Version implements SO
public QName getFaultRoleQName() {
return QNAME_FAULT_ROLE;
}
+
+ public MediaType getMediaType() {
+ return MediaType.TEXT_XML;
+ }
}
Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Version.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Version.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Version.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Version.java Sun Aug 4 09:04:45 2013
@@ -21,6 +21,8 @@ package org.apache.axiom.soap;
import javax.xml.namespace.QName;
+import org.apache.axiom.mime.MediaType;
+
/**
* Version-specific stuff for SOAP 1.2
*/
@@ -74,4 +76,8 @@ public class SOAP12Version implements SO
public QName getFaultRoleQName() {
return QNAME_FAULT_ROLE;
}
+
+ public MediaType getMediaType() {
+ return MediaType.APPLICATION_SOAP_XML;
+ }
}
Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPVersion.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPVersion.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPVersion.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPVersion.java Sun Aug 4 09:04:45 2013
@@ -21,6 +21,8 @@ package org.apache.axiom.soap;
import javax.xml.namespace.QName;
+import org.apache.axiom.mime.MediaType;
+
/**
* A generic way to get at SOAP-version-specific values. As long as we can get
* one of these from a SOAP element, we can get at the right
@@ -91,4 +93,11 @@ public interface SOAPVersion {
* @return the QName for the fault role/actor element
*/
QName getFaultRoleQName();
+
+ /**
+ * Obtain the media type for this version of SOAP.
+ *
+ * @return the media type
+ */
+ MediaType getMediaType();
}
Added: webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java?rev=1510114&view=auto
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java (added)
+++ webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java Sun Aug 4 09:04:45 2013
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.mime;
+
+import junit.framework.TestCase;
+
+public class ContentTypeBuilderTest extends TestCase {
+ public void testBasic() {
+ ContentTypeBuilder builder = new ContentTypeBuilder(MediaType.TEXT_XML);
+ builder.setParameter("charset", "utf-8");
+ ContentType contentType = builder.build();
+ assertEquals(MediaType.TEXT_XML, contentType.getMediaType());
+ assertEquals("utf-8", contentType.getParameter("charset"));
+ }
+
+ public void testFromExistingContentType() {
+ ContentType contentType = new ContentType(MediaType.TEXT_XML, new String[] { "charset", "utf-8" });
+ ContentTypeBuilder builder = new ContentTypeBuilder(contentType);
+ assertEquals(MediaType.TEXT_XML, builder.getMediaType());
+ assertEquals("utf-8", builder.getParameter("charset"));
+ }
+
+ public void testParse() throws Exception {
+ ContentTypeBuilder builder = new ContentTypeBuilder("text/xml; charset=utf-8");
+ assertEquals(MediaType.TEXT_XML, builder.getMediaType());
+ assertEquals("utf-8", builder.getParameter("charset"));
+ }
+
+ public void testSetMediaType() throws Exception {
+ ContentTypeBuilder builder = new ContentTypeBuilder("text/xml; charset=utf-8");
+ builder.setMediaType(MediaType.APPLICATION_XML);
+ assertEquals("application/xml; charset=\"utf-8\"", builder.toString());
+ }
+
+ public void testGetParameterIgnoresCase() {
+ ContentTypeBuilder builder = new ContentTypeBuilder(MediaType.TEXT_XML);
+ builder.setParameter("charset", "utf-8");
+ assertEquals("utf-8", builder.getParameter("CHARSET"));
+ }
+
+ public void testSetParameterIgnoresCase() {
+ ContentTypeBuilder builder = new ContentTypeBuilder(MediaType.TEXT_XML);
+ builder.setParameter("charset", "utf-8");
+ builder.setParameter("CHARSET", "us-ascii");
+ assertEquals("us-ascii", builder.getParameter("charset"));
+ }
+
+ public void testClearParameters() {
+ ContentTypeBuilder builder = new ContentTypeBuilder(MediaType.TEXT_XML);
+ builder.setParameter("charset", "utf-8");
+ builder.clearParameters();
+ assertNull(builder.getParameter("charset"));
+ }
+}
Propchange: webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeBuilderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java?rev=1510114&r1=1510113&r2=1510114&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java Sun Aug 4 09:04:45 2013
@@ -25,13 +25,13 @@ import junit.framework.TestCase;
public class ContentTypeTest extends TestCase {
public void testImmutable() {
String[] parameters = { "charset", "utf-8" };
- ContentType ct = new ContentType(new MediaType("text", "xml"), parameters);
+ ContentType ct = new ContentType(MediaType.TEXT_XML, parameters);
parameters[1] = "ascii";
assertEquals("utf-8", ct.getParameter("charset"));
}
public void testGetParameterIgnoresCase() {
- ContentType ct = new ContentType(new MediaType("text", "xml"), new String[] { "charset", "utf-8" });
+ ContentType ct = new ContentType(MediaType.TEXT_XML, new String[] { "charset", "utf-8" });
assertEquals("utf-8", ct.getParameter("CHARSET"));
}
@@ -43,7 +43,7 @@ public class ContentTypeTest extends Tes
+ "start-info=\"application/soap+xml\"; "
+ "charset=UTF-8;"
+ "action=\"urn:myAction\"");
- assertEquals(new MediaType("multipart", "related"), ct.getMediaType());
+ assertEquals(MediaType.MULTIPART_RELATED, ct.getMediaType());
assertEquals("boundaryA3ADBAEE51A1A87B2A11443668160701", ct.getParameter("boundary"));
assertEquals("application/xop+xml", ct.getParameter("type"));
assertEquals("<A3...@apache.org>", ct.getParameter("start"));
@@ -54,13 +54,13 @@ public class ContentTypeTest extends Tes
public void testParseWithExtraSemicolon() throws Exception {
ContentType ct = new ContentType("text/xml; charset=utf-8;");
- assertEquals(new MediaType("text", "xml"), ct.getMediaType());
+ assertEquals(MediaType.TEXT_XML, ct.getMediaType());
assertEquals("utf-8", ct.getParameter("charset"));
}
public void testParseWithExtraSpaces() throws Exception {
ContentType ct = new ContentType("text/xml ; charset = utf-8 ");
- assertEquals(new MediaType("text", "xml"), ct.getMediaType());
+ assertEquals(MediaType.TEXT_XML, ct.getMediaType());
assertEquals("utf-8", ct.getParameter("charset"));
}
@@ -131,4 +131,21 @@ public class ContentTypeTest extends Tes
// Expected
}
}
+
+ public void testToString() {
+ ContentType ct = new ContentType(MediaType.TEXT_XML, new String[] { "charset", "utf-8" });
+ assertEquals("text/xml; charset=\"utf-8\"", ct.toString());
+ }
+
+ public void testToStringWithQuote() {
+ ContentType ct = new ContentType(new MediaType("application", "x-some-format"),
+ new String[] { "comment", "this is not a \"quote\""});
+ assertEquals("application/x-some-format; comment=\"this is not a \\\"quote\\\"\"", ct.toString());
+ }
+
+ public void testToStringWithBackslash() {
+ ContentType ct = new ContentType(new MediaType("application", "x-some-format"),
+ new String[] { "filename", "c:\\temp\\test.dat"});
+ assertEquals("application/x-some-format; filename=\"c:\\\\temp\\\\test.dat\"", ct.toString());
+ }
}