1 /*
2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.xml.internal.ws.message;
27 
28 import com.sun.istack.internal.NotNull;
29 import com.sun.istack.internal.Nullable;
30 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
31 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
32 import com.sun.xml.internal.ws.api.message.Header;
33 import org.xml.sax.ContentHandler;
34 import org.xml.sax.ErrorHandler;
35 import org.xml.sax.SAXException;
36 
37 import javax.xml.namespace.QName;
38 import javax.xml.soap.SOAPException;
39 import javax.xml.soap.SOAPHeader;
40 import javax.xml.soap.SOAPHeaderElement;
41 import javax.xml.soap.SOAPMessage;
42 import javax.xml.stream.XMLStreamException;
43 import javax.xml.stream.XMLStreamReader;
44 import javax.xml.stream.XMLStreamWriter;
45 
46 /**
47  * {@link Header} that represents <wsa:ProblemAction>
48  * @author Arun Gupta
49  */
50 public class ProblemActionHeader extends AbstractHeaderImpl {
51     protected @NotNull String action;
52     protected String soapAction;
53     protected @NotNull AddressingVersion av;
54 
55     private static final String actionLocalName = "Action";
56     private static final String soapActionLocalName = "SoapAction";
57 
ProblemActionHeader(@otNull String action, @NotNull AddressingVersion av)58     public ProblemActionHeader(@NotNull String action, @NotNull AddressingVersion av) {
59         this(action,null,av);
60     }
61 
ProblemActionHeader(@otNull String action, String soapAction, @NotNull AddressingVersion av)62     public ProblemActionHeader(@NotNull String action, String soapAction, @NotNull AddressingVersion av) {
63         assert action!=null;
64         assert av!=null;
65         this.action = action;
66         this.soapAction = soapAction;
67         this.av = av;
68     }
69 
70     public
71     @NotNull
getNamespaceURI()72     String getNamespaceURI() {
73         return av.nsUri;
74     }
75 
76     public
77     @NotNull
getLocalPart()78     String getLocalPart() {
79         return "ProblemAction";
80     }
81 
82     @Nullable
getAttribute(@otNull String nsUri, @NotNull String localName)83     public String getAttribute(@NotNull String nsUri, @NotNull String localName) {
84         return null;
85     }
86 
readHeader()87     public XMLStreamReader readHeader() throws XMLStreamException {
88         MutableXMLStreamBuffer buf = new MutableXMLStreamBuffer();
89         XMLStreamWriter w = buf.createFromXMLStreamWriter();
90         writeTo(w);
91         return buf.readAsXMLStreamReader();
92     }
93 
writeTo(XMLStreamWriter w)94     public void writeTo(XMLStreamWriter w) throws XMLStreamException {
95         w.writeStartElement("", getLocalPart(), getNamespaceURI());
96         w.writeDefaultNamespace(getNamespaceURI());
97         w.writeStartElement(actionLocalName);
98         w.writeCharacters(action);
99         w.writeEndElement();
100         if (soapAction != null) {
101             w.writeStartElement(soapActionLocalName);
102             w.writeCharacters(soapAction);
103             w.writeEndElement();
104         }
105         w.writeEndElement();
106     }
107 
writeTo(SOAPMessage saaj)108     public void writeTo(SOAPMessage saaj) throws SOAPException {
109         SOAPHeader header = saaj.getSOAPHeader();
110         if(header == null)
111             header = saaj.getSOAPPart().getEnvelope().addHeader();
112         SOAPHeaderElement she = header.addHeaderElement(new QName(getNamespaceURI(), getLocalPart()));
113         she.addChildElement(actionLocalName);
114         she.addTextNode(action);
115         if (soapAction != null) {
116             she.addChildElement(soapActionLocalName);
117             she.addTextNode(soapAction);
118         }
119     }
120 
writeTo(ContentHandler h, ErrorHandler errorHandler)121     public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
122         String nsUri = getNamespaceURI();
123         String ln = getLocalPart();
124 
125         h.startPrefixMapping("",nsUri);
126         h.startElement(nsUri,ln,ln,EMPTY_ATTS);
127         h.startElement(nsUri,actionLocalName,actionLocalName,EMPTY_ATTS);
128         h.characters(action.toCharArray(),0,action.length());
129         h.endElement(nsUri,actionLocalName,actionLocalName);
130         if (soapAction != null) {
131             h.startElement(nsUri,soapActionLocalName,soapActionLocalName,EMPTY_ATTS);
132             h.characters(soapAction.toCharArray(),0,soapAction.length());
133             h.endElement(nsUri,soapActionLocalName,soapActionLocalName);
134         }
135         h.endElement(nsUri,ln,ln);
136     }
137 }
138