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.message;
024:
025: import com.sun.xml.ws.api.message.HeaderList;
026: import com.sun.xml.ws.api.message.Message;
027: import com.sun.xml.ws.security.opt.api.SecurityElement;
028: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
029: import com.sun.xml.ws.security.opt.impl.outgoing.SecurityHeader;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.NoSuchElementException;
033:
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.stream.XMLStreamReader;
036: import javax.xml.stream.XMLStreamWriter;
037: import org.jvnet.staxex.NamespaceContextEx;
038: import com.sun.xml.wss.impl.MessageConstants;
039: import com.sun.xml.wss.XWSSecurityException;
040: import com.sun.xml.ws.api.message.AttachmentSet;
041: import com.sun.xml.ws.api.SOAPVersion;
042: import com.sun.xml.ws.api.message.Header;
043:
044: /**
045: *
046: * @author K.Venugopal@sun.com
047: */
048:
049: public class SecuredMessage {
050:
051: ArrayList headers;
052: Message msg;
053: SOAPBody body = null;
054:
055: boolean isOneWay = false;
056: SecurityElement securedBody = null;
057: AttachmentSet attachments = null;
058: private NamespaceContextEx context = null;
059: SOAPVersion soapVersion = SOAPVersion.SOAP_11;
060: private SecurityHeader sh = null;
061:
062: /** Creates a new instance of SecuredMessage */
063: public SecuredMessage(Message msg, SecurityHeader sh,
064: SOAPVersion soapVersion) {
065: this (msg, sh);
066: this .body = new SOAPBody(msg, soapVersion);
067: this .soapVersion = soapVersion;
068: boolean isSOAP12 = (soapVersion == SOAPVersion.SOAP_12) ? true
069: : false;
070: }
071:
072: public SecuredMessage(Message msg, SecurityHeader sh) {
073: HeaderList hl = msg.getHeaders();
074: headers = new ArrayList(hl);
075: this .msg = msg;
076: this .body = new SOAPBody(msg);
077: attachments = msg.getAttachments();
078: this .sh = sh;
079: }
080:
081: public ArrayList getHeaders() {
082: return headers;
083: }
084:
085: public void setRootElements(NamespaceContextEx ne) {
086: this .context = ne;
087: }
088:
089: public boolean isOneWay() {
090: return this .isOneWay;
091: }
092:
093: public Iterator getHeaders(final String localName, final String uri) {
094: return new Iterator() {
095: int idx = 0;
096: Object next;
097:
098: public boolean hasNext() {
099: if (next == null)
100: fetch();
101: return next != null;
102: }
103:
104: public Object next() {
105: if (next == null) {
106: fetch();
107: if (next == null) {
108: throw new NoSuchElementException();
109: }
110: }
111:
112: Object r = next;
113: next = null;
114: return r;
115: }
116:
117: private void fetch() {
118: while (idx < headers.size()) {
119: Object obj = headers.get(idx++);
120: if (obj instanceof Header) {
121: Header hd = (Header) obj;
122: if ((uri == null && localName.equals(hd
123: .getLocalPart()))
124: || (localName.equals(hd.getLocalPart()) && uri
125: .equals(hd.getNamespaceURI()))) {
126: next = hd;
127: break;
128: }
129: } else if (obj instanceof SecurityElement) {
130: SecurityElement she = (SecurityElement) obj;
131: if ((uri == null && localName.equals(she
132: .getLocalPart()))
133: || (localName
134: .equals(she.getLocalPart()) && uri
135: .equals(she.getNamespaceURI()))) {
136: next = she;
137: break;
138: }
139: }
140: }
141: }
142:
143: public void remove() {
144: throw new UnsupportedOperationException();
145: }
146: };
147:
148: }
149:
150: public Iterator getHeaders(final String uri) {
151: return new Iterator() {
152: int idx = 0;
153: Object next;
154:
155: public boolean hasNext() {
156: if (next == null)
157: fetch();
158: return next != null;
159: }
160:
161: public Object next() {
162: if (next == null) {
163: fetch();
164: if (next == null) {
165: throw new NoSuchElementException();
166: }
167: }
168:
169: Object r = next;
170: next = null;
171: return r;
172: }
173:
174: private void fetch() {
175: while (idx < headers.size()) {
176: Object obj = headers.get(idx++);
177: if (obj instanceof Header) {
178: Header hd = (Header) obj;
179: if (uri.equals(hd.getNamespaceURI())) {
180: next = hd;
181: break;
182: }
183: } else if (obj instanceof SecurityElement) {
184: SecurityElement she = (SecurityElement) obj;
185: if (uri.equals(she.getNamespaceURI())) {
186: next = she;
187: break;
188: }
189: }
190: }
191: }
192:
193: public void remove() {
194: throw new UnsupportedOperationException();
195: }
196: };
197: }
198:
199: public boolean replaceHeader(Object header1, Object header2) {
200: boolean replaced = false;
201: for (int i = 0; i < headers.size(); i++) {
202: Object obj = headers.get(i);
203: if (obj == header1 && obj.equals(header1)) {
204: headers.set(i, header2);
205: replaced = true;
206: break;
207: }
208: }
209: return replaced;
210: }
211:
212: public Object getHeader(String id) {
213: Object hdr = null;
214: for (int i = 0; i < headers.size(); i++) {
215: Object obj = headers.get(i);
216: if (obj instanceof Header) {
217: Header hd = (Header) obj;
218: String wsuId = hd.getAttribute(
219: MessageConstants.WSSE_NS, "Id");
220: if (id.equals(wsuId)) {
221: hdr = hd;
222: break;
223: }
224: } else if (obj instanceof SecurityElement) {
225: SecurityElement she = (SecurityElement) obj;
226: if (id.equals(she.getId())) {
227: hdr = she;
228: break;
229: }
230: }
231: }
232: return hdr;
233: }
234:
235: public String getPayloadNamespaceURI() {
236: if (body != null) {
237: return body.getPayloadNamespaceURI();
238: }
239: if (securedBody != null) {
240: return securedBody.getNamespaceURI();
241: }
242: return null;
243: }
244:
245: public String getPayloadLocalPart() {
246: if (body != null) {
247: return body.getPayloadLocalPart();
248: }
249: if (securedBody != null) {
250: return securedBody.getLocalPart();
251: }
252: return null;
253: }
254:
255: public XMLStreamReader readPayload() throws XMLStreamException {
256: if (body != null) {
257: return body.read();
258: }
259:
260: if (securedBody != null) {
261: return securedBody.readHeader();
262: }
263: throw new XMLStreamException("No Payload found");
264: }
265:
266: public void writePayloadTo(XMLStreamWriter sw)
267: throws XMLStreamException {
268: if (body != null) {
269: body.writeTo(sw);
270: } else if (securedBody != null) {
271: ((SecurityElementWriter) securedBody).writeTo(sw);
272: } else {
273: throw new XMLStreamException("No Payload found");
274: }
275: return;
276: }
277:
278: public Object getBody() throws XWSSecurityException {
279: if (body != null) {
280: return body;
281: } else if (securedBody != null) {
282: return securedBody;
283: } else {
284: throw new XWSSecurityException("No body present in message");
285: }
286: }
287:
288: public void replaceBody(SecurityElement she) {
289: msg = null;
290: securedBody = she;
291: body = null;
292: }
293:
294: public void replaceBody(SOAPBody sb) {
295: msg = null;
296: body = sb;
297: securedBody = null;
298: }
299:
300: public AttachmentSet getAttachments() {
301: return attachments;
302: }
303:
304: public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
305: sw.writeStartDocument();
306: sw.writeStartElement("S", "Envelope", soapVersion.nsUri);
307: Iterator<org.jvnet.staxex.NamespaceContextEx.Binding> itr = context
308: .iterator();
309:
310: while (itr.hasNext()) {
311: com.sun.xml.ws.security.opt.impl.util.NamespaceContextEx.Binding binding = itr
312: .next();
313: sw.writeNamespace(binding.getPrefix(), binding
314: .getNamespaceURI());
315: }
316:
317: sw.writeStartElement("S", "Header", soapVersion.nsUri);
318: for (int i = 0; i < headers.size(); i++) {
319: Object hdr = headers.get(i);
320: if (hdr instanceof Header) {
321: ((Header) hdr).writeTo(sw);
322: } else {
323: ((SecurityElementWriter) hdr).writeTo(sw);
324: }
325: }
326:
327: sh.writeTo(sw);
328: sw.writeEndElement();
329: if (securedBody != null) {
330: ((SecurityElementWriter) securedBody).writeTo(sw);
331: } else if (body != null) {
332: body.writeTo(sw);
333: }
334:
335: sw.writeEndDocument();
336: }
337: }
|