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;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
042: import com.sun.xml.ws.api.addressing.AddressingVersion;
043: import com.sun.xml.ws.api.message.Header;
044: import org.xml.sax.ContentHandler;
045: import org.xml.sax.ErrorHandler;
046: import org.xml.sax.SAXException;
047:
048: import javax.xml.namespace.QName;
049: import javax.xml.soap.SOAPException;
050: import javax.xml.soap.SOAPHeader;
051: import javax.xml.soap.SOAPHeaderElement;
052: import javax.xml.soap.SOAPMessage;
053: import javax.xml.stream.XMLStreamException;
054: import javax.xml.stream.XMLStreamReader;
055: import javax.xml.stream.XMLStreamWriter;
056:
057: /**
058: * {@link Header} that represents <wsa:ProblemAction>
059: * @author Arun Gupta
060: */
061: public class ProblemActionHeader extends AbstractHeaderImpl {
062: protected @NotNull
063: String action;
064: protected String soapAction;
065: protected @NotNull
066: AddressingVersion av;
067:
068: private static final String actionLocalName = "Action";
069: private static final String soapActionLocalName = "SoapAction";
070:
071: public ProblemActionHeader(@NotNull
072: String action, @NotNull
073: AddressingVersion av) {
074: this (action, null, av);
075: }
076:
077: public ProblemActionHeader(@NotNull
078: String action, String soapAction, @NotNull
079: AddressingVersion av) {
080: assert action != null;
081: assert av != null;
082: this .action = action;
083: this .soapAction = soapAction;
084: this .av = av;
085: }
086:
087: public @NotNull
088: String getNamespaceURI() {
089: return av.nsUri;
090: }
091:
092: public @NotNull
093: String getLocalPart() {
094: return "ProblemAction";
095: }
096:
097: @Nullable
098: public String getAttribute(@NotNull
099: String nsUri, @NotNull
100: String localName) {
101: return null;
102: }
103:
104: public XMLStreamReader readHeader() throws XMLStreamException {
105: MutableXMLStreamBuffer buf = new MutableXMLStreamBuffer();
106: XMLStreamWriter w = buf.createFromXMLStreamWriter();
107: writeTo(w);
108: return buf.readAsXMLStreamReader();
109: }
110:
111: public void writeTo(XMLStreamWriter w) throws XMLStreamException {
112: w.writeStartElement("", getLocalPart(), getNamespaceURI());
113: w.writeDefaultNamespace(getNamespaceURI());
114: w.writeStartElement(actionLocalName);
115: w.writeCharacters(action);
116: w.writeEndElement();
117: if (soapAction != null) {
118: w.writeStartElement(soapActionLocalName);
119: w.writeCharacters(soapAction);
120: w.writeEndElement();
121: }
122: w.writeEndElement();
123: }
124:
125: public void writeTo(SOAPMessage saaj) throws SOAPException {
126: SOAPHeader header = saaj.getSOAPHeader();
127: SOAPHeaderElement she = header.addHeaderElement(new QName(
128: getNamespaceURI(), getLocalPart()));
129: she.addChildElement(actionLocalName);
130: she.addTextNode(action);
131: if (soapAction != null) {
132: she.addChildElement(soapActionLocalName);
133: she.addTextNode(soapAction);
134: }
135: }
136:
137: public void writeTo(ContentHandler h, ErrorHandler errorHandler)
138: throws SAXException {
139: String nsUri = getNamespaceURI();
140: String ln = getLocalPart();
141:
142: h.startPrefixMapping("", nsUri);
143: h.startElement(nsUri, ln, ln, EMPTY_ATTS);
144: h.startElement(nsUri, actionLocalName, actionLocalName,
145: EMPTY_ATTS);
146: h.characters(action.toCharArray(), 0, action.length());
147: h.endElement(nsUri, actionLocalName, actionLocalName);
148: if (soapAction != null) {
149: h.startElement(nsUri, soapActionLocalName,
150: soapActionLocalName, EMPTY_ATTS);
151: h.characters(soapAction.toCharArray(), 0, soapAction
152: .length());
153: h.endElement(nsUri, soapActionLocalName,
154: soapActionLocalName);
155: }
156: h.endElement(nsUri, ln, ln);
157: }
158: }
|