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.api.addressing;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.message.AbstractHeaderImpl;
042: import com.sun.xml.ws.util.xml.XmlUtil;
043: import org.xml.sax.ContentHandler;
044: import org.xml.sax.ErrorHandler;
045: import org.xml.sax.SAXException;
046:
047: import javax.xml.namespace.QName;
048: import javax.xml.soap.SOAPException;
049: import javax.xml.soap.SOAPMessage;
050: import javax.xml.stream.XMLStreamConstants;
051: import javax.xml.stream.XMLStreamException;
052: import javax.xml.stream.XMLStreamReader;
053: import javax.xml.stream.XMLStreamWriter;
054: import javax.xml.transform.Transformer;
055: import javax.xml.transform.dom.DOMResult;
056:
057: /**
058: * Used to represent outbound endpoint reference header,
059: * such as <ReplyTo> and <FaultTo>.
060: * Used from {@link WSEndpointReference}.
061: *
062: * @author Kohsuke Kawaguchi
063: * @see WSEndpointReference
064: */
065: final class EPRHeader extends AbstractHeaderImpl {
066:
067: private final String nsUri, localName;
068: private final WSEndpointReference epr;
069:
070: EPRHeader(QName tagName, WSEndpointReference epr) {
071: this .nsUri = tagName.getNamespaceURI();
072: this .localName = tagName.getLocalPart();
073: this .epr = epr;
074: }
075:
076: public @NotNull
077: String getNamespaceURI() {
078: return nsUri;
079: }
080:
081: public @NotNull
082: String getLocalPart() {
083: return localName;
084: }
085:
086: @Nullable
087: public String getAttribute(@NotNull
088: String nsUri, @NotNull
089: String localName) {
090: try {
091: XMLStreamReader sr = epr
092: .read("EndpointReference"/*doesn't matter*/);
093: while (sr.getEventType() != XMLStreamConstants.START_ELEMENT)
094: sr.next();
095:
096: return sr.getAttributeValue(nsUri, localName);
097: } catch (XMLStreamException e) {
098: // since we are reading from buffer, this can't happen.
099: throw new AssertionError(e);
100: }
101: }
102:
103: public XMLStreamReader readHeader() throws XMLStreamException {
104: return epr.read(localName);
105: }
106:
107: public void writeTo(XMLStreamWriter w) throws XMLStreamException {
108: epr.writeTo(localName, w);
109: }
110:
111: public void writeTo(SOAPMessage saaj) throws SOAPException {
112: try {
113: // TODO what about in-scope namespaces
114: // Not very efficient consider implementing a stream buffer
115: // processor that produces a DOM node from the buffer.
116: Transformer t = XmlUtil.newTransformer();
117: t.transform(epr.asSource(localName), new DOMResult(saaj
118: .getSOAPHeader()));
119: } catch (Exception e) {
120: throw new SOAPException(e);
121: }
122: }
123:
124: public void writeTo(ContentHandler contentHandler,
125: ErrorHandler errorHandler) throws SAXException {
126: epr.writeTo(localName, contentHandler, errorHandler, true);
127: }
128: }
|