001: /*
002: * $Id: CanonicalizerFactory.java,v 1.4 2007/01/08 09:28:48 ashutoshshahi Exp $
003: * $Revision: 1.4 $
004: * $Date: 2007/01/08 09:28:48 $
005: */
006:
007: /*
008: * The contents of this file are subject to the terms
009: * of the Common Development and Distribution License
010: * (the License). You may not use this file except in
011: * compliance with the License.
012: *
013: * You can obtain a copy of the license at
014: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * See the License for the specific language governing
016: * permissions and limitations under the License.
017: *
018: * When distributing Covered Code, include this CDDL
019: * Header Notice in each file and include the License file
020: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
021: * If applicable, add the following below the CDDL Header,
022: * with the fields enclosed by brackets [] replaced by
023: * you own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.wss.impl.c14n;
030:
031: import java.util.HashMap;
032: import java.nio.charset.Charset;
033: import javax.mail.internet.ContentType;
034: import com.sun.xml.wss.XWSSecurityException;
035:
036: import com.sun.xml.wss.swa.MimeConstants;
037:
038: /**
039: *
040: * @author XWS-Security Team
041: */
042: public class CanonicalizerFactory {
043:
044: static MimeHeaderCanonicalizer _mhCanonicalizer = null;
045:
046: static HashMap _canonicalizers = new HashMap(10);
047:
048: public static final Canonicalizer getCanonicalizer(String mimeType)
049: throws Exception {
050: ContentType contentType = new ContentType(mimeType);
051: String baseMimeType = contentType.getBaseType();
052:
053: if (baseMimeType
054: .equalsIgnoreCase(MimeConstants.TEXT_PLAIN_TYPE)) {
055: ensureRegisteredCharset(contentType);
056: }
057:
058: // use primaryMimeType as the key.
059: // i.e. text canonicalizer will apply to text/* etc.
060: String primaryMimeType = contentType.getPrimaryType();
061: Canonicalizer _canonicalizer = (Canonicalizer) _canonicalizers
062: .get(primaryMimeType);
063:
064: if (_canonicalizer == null) {
065: _canonicalizer = newCanonicalizer(primaryMimeType);
066: }
067:
068: // defaults to US-ASCII
069: String charset = contentType.getParameter("charset");
070: if (charset != null)
071: _canonicalizer.setCharset(charset);
072:
073: return _canonicalizer;
074: }
075:
076: /*
077: * Primary MimeType is the key.
078: * ImageCanonicalizer is sufficient for all image/** MIME types and so on.
079: *
080: * Finer grained processing as per section 4.1.4 RFC2046 has not been
081: * incorporated yet. I don't think so much processing is required at this time.
082: *
083: */
084: public static final Canonicalizer newCanonicalizer(
085: String primaryMimeType) {
086: Canonicalizer canonicalizer = null;
087:
088: if (primaryMimeType.equalsIgnoreCase("text"))
089: canonicalizer = new TextPlainCanonicalizer();
090: else if (primaryMimeType.equalsIgnoreCase("image"))
091: canonicalizer = new ImageCanonicalizer();
092: else if (primaryMimeType.equalsIgnoreCase("application"))
093: canonicalizer = new ApplicationCanonicalizer();
094: else
095: ; // log n throw exception
096:
097: _canonicalizers.put(primaryMimeType, canonicalizer);
098:
099: return canonicalizer;
100: }
101:
102: public static final MimeHeaderCanonicalizer getMimeHeaderCanonicalizer(
103: String charset) {
104: if (_mhCanonicalizer == null)
105: _mhCanonicalizer = new MimeHeaderCanonicalizer();
106: _mhCanonicalizer.setCharset(charset);
107: return _mhCanonicalizer;
108: }
109:
110: public static void registerCanonicalizer(String baseMimeType,
111: Canonicalizer implementingClass) {
112: _canonicalizers.put(baseMimeType, implementingClass);
113: }
114:
115: public static void registerCanonicalizer(String baseMimeType,
116: String implementingClass) throws XWSSecurityException {
117: try {
118: Class _class = Class.forName(implementingClass);
119: Canonicalizer canonicalizer = (Canonicalizer) _class
120: .newInstance();
121: _canonicalizers.put(baseMimeType, canonicalizer);
122: } catch (Exception e) {
123: // log
124: throw new XWSSecurityException(e);
125: }
126: }
127:
128: /*
129: * Ensure that the charset is a registered charset - see RFC 2633.
130: * This assumes the complete content-type string will be input as the parameter.
131: * (including charset param value).
132: *
133: * Section 4.1.2. Charset Parameter [RFC 2046]
134: * Example of charset parameter as per the RFC definition is -
135: * Content-type: text/plain; charset=iso-8859-1
136: */
137: public static boolean ensureRegisteredCharset(
138: ContentType contentType) throws XWSSecurityException {
139: String charsetName = contentType.getParameter("charset");
140: if (charsetName != null) {
141: return Charset.forName(charsetName).isRegistered();
142: }
143: return true;
144: }
145: }
|