001: /*
002: * JAXBCryptoContext.java
003: *
004: * Created on January 24, 2006, 11:53 AM
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.ws.security.opt.crypto.jaxb;
030:
031: import java.util.HashMap;
032: import javax.xml.crypto.KeySelector;
033: import javax.xml.crypto.URIDereferencer;
034:
035: /**
036: *
037: * @author Abhijit Das
038: */
039: public class JAXBCryptoContext implements
040: javax.xml.crypto.XMLCryptoContext {
041:
042: private String baseURI = null;
043: private KeySelector keySelector = null;
044: private URIDereferencer uriDereferencer = null;
045: private HashMap namespacePrefix = null;
046: private String defaultNamespacePrefix = null;
047: private HashMap property = null;
048: private HashMap context = null;
049:
050: /** Creates a new instance of JAXBCryptoContext */
051: public JAXBCryptoContext() {
052: }
053:
054: /**
055: * Returns the base URI.
056: *
057: * @return the base URI, or <code>null</code> if not specified
058: * @see #setBaseURI(String)
059: */
060: public String getBaseURI() {
061: return baseURI;
062: }
063:
064: /**
065: * Sets the base URI.
066: *
067: * @param baseURI the base URI, or <code>null</code> to remove current
068: * value
069: * @throws IllegalArgumentException if <code>baseURI</code> is not RFC
070: * 2396 compliant
071: * @see #getBaseURI
072: */
073: public void setBaseURI(String baseURI) {
074: this .baseURI = baseURI;
075: }
076:
077: /**
078: * Returns the key selector for finding a key.
079: *
080: * @return the key selector, or <code>null</code> if not specified
081: * @see #setKeySelector(KeySelector)
082: */
083: public KeySelector getKeySelector() {
084: return keySelector;
085: }
086:
087: /**
088: * Sets the key selector for finding a key.
089: *
090: * @param ks the key selector, or <code>null</code> to remove the current
091: * setting
092: * @see #getKeySelector
093: */
094: public void setKeySelector(KeySelector keySelector) {
095: this .keySelector = keySelector;
096: }
097:
098: /**
099: * Returns a <code>URIDereferencer</code> that is used to dereference
100: * {@link URIReference}s.
101: *
102: * @return the <code>URIDereferencer</code>, or <code>null</code> if not
103: * specified
104: * @see #setURIDereferencer(URIDereferencer)
105: */
106: public URIDereferencer getURIDereferencer() {
107: return uriDereferencer;
108: }
109:
110: /**
111: * Sets a <code>URIDereferencer</code> that is used to dereference
112: * {@link URIReference}s. The specified <code>URIDereferencer</code>
113: * is used in place of an implementation's default
114: * <code>URIDereferencer</code>.
115: *
116: * @param dereferencer the <code>URIDereferencer</code>, or
117: * <code>null</code> to remove any current setting
118: * @see #getURIDereferencer
119: */
120: public void setURIDereferencer(URIDereferencer uriDereferencer) {
121: this .uriDereferencer = uriDereferencer;
122: }
123:
124: /**
125: * Returns the namespace prefix that the specified namespace URI is
126: * associated with. Returns the specified default prefix if the specified
127: * namespace URI has not been bound to a prefix. To bind a namespace URI
128: * to a prefix, call the {@link #putNamespacePrefix putNamespacePrefix}
129: * method.
130: *
131: * @param namespaceURI a namespace URI
132: * @param defaultPrefix the prefix to be returned in the event that the
133: * the specified namespace URI has not been bound to a prefix.
134: * @return the prefix that is associated with the specified namespace URI,
135: * or <code>defaultPrefix</code> if the URI is not registered. If
136: * the namespace URI is registered but has no prefix, an empty string
137: * (<code>""</code>) is returned.
138: * @throws NullPointerException if <code>namespaceURI</code> is
139: * <code>null</code>
140: * @see #putNamespacePrefix(String, String)
141: */
142: public String getNamespacePrefix(String namespaceURI,
143: String defaultPrefix) {
144: if (namespacePrefix == null) {
145: return defaultPrefix;
146: }
147: Object prefix = namespacePrefix.get(namespaceURI);
148: if (prefix == null) {
149: return defaultPrefix;
150: } else if (prefix.equals("")) {
151: return defaultPrefix;
152: } else {
153: return prefix.toString();
154: }
155: }
156:
157: /**
158: * Maps the specified namespace URI to the specified prefix. If there is
159: * already a prefix associated with the specified namespace URI, the old
160: * prefix is replaced by the specified prefix.
161: *
162: * @param namespaceURI a namespace URI
163: * @param prefix a namespace prefix (or <code>null</code> to remove any
164: * existing mapping). Specifying the empty string (<code>""</code>)
165: * binds no prefix to the namespace URI.
166: * @return the previous prefix associated with the specified namespace
167: * URI, or <code>null</code> if there was none
168: * @throws NullPointerException if <code>namespaceURI</code> is
169: * <code>null</code>
170: * @see #getNamespacePrefix(String, String)
171: */
172: public String putNamespacePrefix(String namespaceURI, String prefix) {
173: if (namespaceURI == null) {
174: return null;
175: }
176:
177: if (namespacePrefix == null) {
178: namespacePrefix = new HashMap();
179: }
180:
181: //Get the old prefix
182: Object oldPrefix = namespacePrefix.get(namespaceURI);
183:
184: if (prefix == null && oldPrefix != null) {
185: //Remove the mapping and return the oldprefix
186: return namespacePrefix.remove(namespaceURI).toString();
187: }
188:
189: if (prefix != "")
190: namespacePrefix.put(namespaceURI, prefix);
191:
192: if (oldPrefix != null)
193: return oldPrefix.toString();
194: return null;
195: }
196:
197: /**
198: * Returns the default namespace prefix. The default namespace prefix
199: * is the prefix for all namespace URIs not explicitly set by the
200: * {@link #putNamespacePrefix putNamespacePrefix} method.
201: *
202: * @return the default namespace prefix, or <code>null</code> if none has
203: * been set.
204: * @see #setDefaultNamespacePrefix(String)
205: */
206: public String getDefaultNamespacePrefix() {
207: return defaultNamespacePrefix;
208: }
209:
210: /**
211: * Sets the default namespace prefix. This sets the namespace prefix for
212: * all namespace URIs not explicitly set by the {@link #putNamespacePrefix
213: * putNamespacePrefix} method.
214: *
215: * @param defaultPrefix the default namespace prefix, or <code>null</code>
216: * to remove the current setting. Specify the empty string
217: * (<code>""</code>) to bind no prefix.
218: * @see #getDefaultNamespacePrefix
219: */
220: public void setDefaultNamespacePrefix(String defaultNamespacePrefix) {
221: this .defaultNamespacePrefix = defaultNamespacePrefix;
222: }
223:
224: /**
225: * Sets the specified property.
226: *
227: * @param name the name of the property
228: * @param value the value of the property to be set
229: * @return the previous value of the specified property, or
230: * <code>null</code> if it did not have a value
231: * @throws NullPointerException if <code>name</code> is <code>null</code>
232: * @see #getProperty(String)
233: */
234: public Object setProperty(String name, Object value) {
235: if (property == null) {
236: property = new HashMap();
237: }
238: return property.put(name, value);
239: }
240:
241: /**
242: * Returns the value of the specified property.
243: *
244: * @param name the name of the property
245: * @return the current value of the specified property, or
246: * <code>null</code> if it does not have a value
247: * @throws NullPointerException if <code>name</code> is <code>null</code>
248: * @see #setProperty(String, Object)
249: */
250: public Object getProperty(String name) {
251: if (property != null) {
252: return property.get(name);
253: } else {
254: return null;
255: }
256: }
257:
258: /**
259: * Returns the value to which this context maps the specified key.
260: *
261: * <p>More formally, if this context contains a mapping from a key
262: * <code>k</code> to a value <code>v</code> such that
263: * <code>(key==null ? k==null : key.equals(k))</code>, then this method
264: * returns <code>v</code>; otherwise it returns <code>null</code>. (There
265: * can be at most one such mapping.)
266: *
267: * <p>This method is useful for retrieving arbitrary information that is
268: * specific to the cryptographic operation that this context is used for.
269: *
270: * @param key the key whose associated value is to be returned
271: * @return the value to which this context maps the specified key, or
272: * <code>null</code> if there is no mapping for the key
273: * @see #put(Object, Object)
274: */
275: public Object get(Object key) {
276: if (context != null) {
277: return context.get(key);
278: } else {
279: return null;
280: }
281: }
282:
283: /**
284: * Associates the specified value with the specified key in this context.
285: * If the context previously contained a mapping for this key, the old
286: * value is replaced by the specified value.
287: *
288: * <p>This method is useful for storing arbitrary information that is
289: * specific to the cryptographic operation that this context is used for.
290: *
291: * @param key key with which the specified value is to be associated with
292: * @param value value to be associated with the specified key
293: * @return the previous value associated with the key, or <code>null</code>
294: * if there was no mapping for the key
295: * @throws IllegalArgumentException if some aspect of this key or value
296: * prevents it from being stored in this context
297: * @see #get(Object)
298: */
299: public Object put(Object key, Object value) {
300: if (context == null) {
301: context = new HashMap();
302: }
303:
304: return context.put(key, value);
305: }
306:
307: }
|