001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.incoming;
024:
025: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
026: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import javax.xml.stream.XMLStreamException;
031: import javax.xml.stream.XMLStreamWriter;
032: import org.jvnet.staxex.NamespaceContextEx;
033: import com.sun.xml.stream.buffer.XMLStreamBuffer;
034:
035: /**
036: *
037: * @author K.Venugopal@sun.com
038: */
039: public class StreamWriterData implements
040: com.sun.xml.ws.security.opt.crypto.StreamWriterData {
041:
042: private GenericSecuredHeader gsh = null;
043: private SecurityHeaderElement she = null;
044: private SWDNamespaceContextEx nce = new SWDNamespaceContextEx();
045: private HashMap<String, String> nsDecls = null;
046: private XMLStreamBuffer xmlBuffer = null;
047:
048: /** Creates a new instance of StreamWriterData */
049: public StreamWriterData(GenericSecuredHeader gsh,
050: HashMap<String, String> nsDecls) {
051: this .gsh = (GenericSecuredHeader) gsh;
052: this .nsDecls = nsDecls;
053: addNSDecls();
054: }
055:
056: public StreamWriterData(SecurityHeaderElement she,
057: HashMap<String, String> nsDecls) {
058: this .she = (SecurityHeaderElement) she;
059: this .nsDecls = nsDecls;
060: addNSDecls();
061: }
062:
063: public StreamWriterData(XMLStreamBuffer buffer) {
064: this .xmlBuffer = buffer;
065: }
066:
067: public Object getDereferencedObject() {
068: if (she != null)
069: return she;
070: else
071: return gsh;
072: }
073:
074: private void addNSDecls() {
075: Iterator<String> itr = nsDecls.keySet().iterator();
076: while (itr.hasNext()) {
077: String prefix = itr.next();
078: String uri = nsDecls.get(prefix);
079: nce.add(prefix, uri);
080: }
081: }
082:
083: public NamespaceContextEx getNamespaceContext() {
084: return nce;
085: }
086:
087: public void write(XMLStreamWriter writer) throws XMLStreamException {
088: if (xmlBuffer != null) {
089: xmlBuffer.writeToXMLStreamWriter(writer);
090: } else if (gsh != null) {
091: gsh.writeTo(writer);
092: } else {
093: ((SecurityElementWriter) she).writeTo(writer);
094: }
095: }
096:
097: static class SWDNamespaceContextEx implements
098: org.jvnet.staxex.NamespaceContextEx {
099:
100: private ArrayList<org.jvnet.staxex.NamespaceContextEx.Binding> list = new ArrayList<org.jvnet.staxex.NamespaceContextEx.Binding>();
101:
102: /** Creates a new instance of NamespaceContextEx */
103: public SWDNamespaceContextEx() {
104: }
105:
106: public SWDNamespaceContextEx(boolean soap12Version) {
107: if (soap12Version) {
108: this
109: .add("S",
110: "http://www.w3.org/2003/05/soap-envelope");//SOAP 12
111: } else {
112: this .add("S",
113: "http://schemas.xmlsoap.org/soap/envelope/");
114: }
115: }
116:
117: public void add(String prefix, String uri) {
118: list.add(new BindingImpl(prefix, uri));
119: }
120:
121: public Iterator<org.jvnet.staxex.NamespaceContextEx.Binding> iterator() {
122: return list.iterator();
123: }
124:
125: public String getNamespaceURI(String prefix) {
126: for (org.jvnet.staxex.NamespaceContextEx.Binding binding : list) {
127: if (prefix.equals(binding.getPrefix())) {
128: return binding.getNamespaceURI();
129: }
130: }
131: return null;
132: }
133:
134: public String getPrefix(String namespaceURI) {
135: for (org.jvnet.staxex.NamespaceContextEx.Binding binding : list) {
136: if (namespaceURI.equals(binding.getNamespaceURI())) {
137: return binding.getPrefix();
138: }
139: }
140: return null;
141: }
142:
143: public Iterator getPrefixes(final String namespaceURI) {
144: return new Iterator() {
145: int index = 0;
146:
147: public boolean hasNext() {
148: if (index++ < list.size() && move()) {
149: return true;
150: }
151: return false;
152: }
153:
154: public Object next() {
155: return list.get(index).getPrefix();
156: }
157:
158: public void remove() {
159: throw new UnsupportedOperationException();
160: }
161:
162: private boolean move() {
163: boolean found = false;
164: do {
165: if (namespaceURI.equals(list.get(index)
166: .getNamespaceURI())) {
167: found = true;
168: break;
169: } else {
170: index++;
171: }
172: } while (index < list.size());
173: return found;
174: }
175: };
176: }
177:
178: static class BindingImpl implements
179: org.jvnet.staxex.NamespaceContextEx.Binding {
180: private String prefix = "";
181: private String uri = "";
182:
183: public BindingImpl(String prefix, String uri) {
184: this .prefix = prefix;
185: this .uri = uri;
186: if (this .prefix == null) {
187: this .prefix = "";
188: }
189: }
190:
191: public String getPrefix() {
192: return prefix;
193: }
194:
195: public String getNamespaceURI() {
196: return uri;
197: }
198: }
199: }
200:
201: }
|