001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.message.stream;
038:
039: import com.sun.istack.FinalArrayList;
040: import com.sun.istack.NotNull;
041: import com.sun.xml.stream.buffer.XMLStreamBuffer;
042: import com.sun.xml.stream.buffer.XMLStreamBufferException;
043: import com.sun.xml.ws.api.message.Header;
044: import com.sun.xml.ws.message.AbstractHeaderImpl;
045: import org.xml.sax.ContentHandler;
046: import org.xml.sax.ErrorHandler;
047: import org.xml.sax.SAXException;
048:
049: import javax.xml.soap.SOAPException;
050: import javax.xml.soap.SOAPMessage;
051: import javax.xml.stream.XMLStreamException;
052: import javax.xml.stream.XMLStreamReader;
053: import javax.xml.stream.XMLStreamWriter;
054: import javax.xml.ws.WebServiceException;
055:
056: /**
057: * Used to represent outbound header created from {@link XMLStreamBuffer}.
058: *
059: * <p>
060: * This is optimized for outbound use, so it implements some of the methods lazily,
061: * in a slow way.
062: *
063: * @author Kohsuke Kawaguchi
064: */
065: public final class OutboundStreamHeader extends AbstractHeaderImpl {
066: private final XMLStreamBuffer infoset;
067: private final String nsUri, localName;
068:
069: /**
070: * The attributes on the header element.
071: * Lazily parsed.
072: * Null if not parsed yet.
073: */
074: private FinalArrayList<Attribute> attributes;
075:
076: public OutboundStreamHeader(XMLStreamBuffer infoset, String nsUri,
077: String localName) {
078: this .infoset = infoset;
079: this .nsUri = nsUri;
080: this .localName = localName;
081: }
082:
083: public @NotNull
084: String getNamespaceURI() {
085: return nsUri;
086: }
087:
088: public @NotNull
089: String getLocalPart() {
090: return localName;
091: }
092:
093: public String getAttribute(String nsUri, String localName) {
094: if (attributes == null)
095: parseAttributes();
096: for (int i = attributes.size() - 1; i >= 0; i--) {
097: Attribute a = attributes.get(i);
098: if (a.localName.equals(localName) && a.nsUri.equals(nsUri))
099: return a.value;
100: }
101: return null;
102: }
103:
104: /**
105: * We don't really expect this to be used, but just to satisfy
106: * the {@link Header} contract.
107: *
108: * So this is rather slow.
109: */
110: private void parseAttributes() {
111: try {
112: XMLStreamReader reader = readHeader();
113:
114: attributes = new FinalArrayList<Attribute>();
115:
116: for (int i = 0; i < reader.getAttributeCount(); i++) {
117: final String localName = reader
118: .getAttributeLocalName(i);
119: final String namespaceURI = reader
120: .getAttributeNamespace(i);
121: final String value = reader.getAttributeValue(i);
122:
123: attributes.add(new Attribute(namespaceURI, localName,
124: value));
125: }
126: } catch (XMLStreamException e) {
127: throw new WebServiceException(
128: "Unable to read the attributes for {" + nsUri + "}"
129: + localName + " header", e);
130: }
131: }
132:
133: public XMLStreamReader readHeader() throws XMLStreamException {
134: return infoset.readAsXMLStreamReader();
135: }
136:
137: public void writeTo(XMLStreamWriter w) throws XMLStreamException {
138: infoset.writeToXMLStreamWriter(w, true);
139: }
140:
141: public void writeTo(SOAPMessage saaj) throws SOAPException {
142: try {
143: infoset.writeTo(saaj.getSOAPHeader());
144: } catch (XMLStreamBufferException e) {
145: throw new SOAPException(e);
146: }
147: }
148:
149: public void writeTo(ContentHandler contentHandler,
150: ErrorHandler errorHandler) throws SAXException {
151: infoset.writeTo(contentHandler, errorHandler);
152: }
153:
154: /**
155: * Keep the information about an attribute on the header element.
156: */
157: static final class Attribute {
158: /**
159: * Can be empty but never null.
160: */
161: final String nsUri;
162: final String localName;
163: final String value;
164:
165: public Attribute(String nsUri, String localName, String value) {
166: this .nsUri = fixNull(nsUri);
167: this .localName = localName;
168: this .value = value;
169: }
170:
171: /**
172: * Convert null to "".
173: */
174: private static String fixNull(String s) {
175: if (s == null)
176: return "";
177: else
178: return s;
179: }
180: }
181:
182: /**
183: * We the performance paranoid people in the JAX-WS RI thinks
184: * saving three bytes is worth while...
185: */
186: private static final String TRUE_VALUE = "1";
187: private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter";
188: }
|