001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.soap;
019    
020    import org.codehaus.activesoap.SoapFault;
021    
022    import javax.xml.namespace.QName;
023    import javax.xml.stream.XMLStreamException;
024    import javax.xml.stream.XMLStreamWriter;
025    import java.io.PrintWriter;
026    import java.io.StringWriter;
027    
028    /**
029     * Represents the SOAP 1.2 version
030     *
031     * @version $Revision: 1.6 $
032     */
033    public class Soap12 implements SoapVersion {
034        public static final String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
035    
036        private static Soap12 instance = new Soap12();
037    
038        private double version = 1.2;
039        private String namespace = "http://www.w3.org/2003/05/soap-envelope";
040        private String prefix = "env";
041        private String noneRole = namespace + "/role/none";
042        private String ultimateReceiverRole = namespace + "/role/ultimateReceiver";
043        private String nextRole = namespace + "/role/next";
044        private String soapEncodingStyle = "http://www.w3.org/2003/05/soap-encoding";
045    
046        private QName envelope = new QName(namespace, "Envelope", prefix);
047        private QName header = new QName(namespace, "Header", prefix);
048        private QName body = new QName(namespace, "Body", prefix);
049        private boolean debug = false;
050    
051    
052        public static Soap12 getInstance() {
053            return instance;
054        }
055    
056        public void writeStartElement(XMLStreamWriter out, String localName) throws XMLStreamException {
057            out.writeStartElement(prefix, localName, namespace);
058        }
059    
060        public void writeFault(XMLStreamWriter out, SoapFault fault) throws XMLStreamException {
061            writeStartElement(out, "Fault");
062    
063            String code = fault.getCode();
064            if (code != null) {
065                writeStartElement(out, "Code");
066                writeStartElement(out, "Value");
067                out.writeCharacters(prefix + ":" + code);
068                out.writeEndElement();
069                String subcode = fault.getSubcode();
070                if (subcode != null) {
071                    writeStartElement(out, "Subcode");
072                    writeStartElement(out, "Value");
073                    out.writeCharacters(subcode);
074                    out.writeEndElement();
075                    out.writeEndElement();
076                }
077                out.writeEndElement();
078            }
079            String reason = fault.getReason();
080            if (reason == null) {
081                reason = fault.getCause().toString();
082            }
083            writeStartElement(out, "Reason");
084            writeStartElement(out, "Text");
085            out.writeAttribute("xml", XML_NAMESPACE, "lang", "en-US");
086            out.writeCharacters(reason);
087            out.writeEndElement();
088            out.writeEndElement();
089    
090            String node = fault.getNode();
091            if (node != null) {
092                writeStartElement(out, "Node");
093                out.writeCharacters(node);
094                out.writeEndElement();
095            }
096    
097            String role = fault.getRole();
098            if (role != null) {
099                writeStartElement(out, "Role");
100                out.writeCharacters(role);
101                out.writeEndElement();
102            }
103    
104            if (debug) {
105                StringWriter buffer = new StringWriter();
106                fault.printStackTrace(new PrintWriter(buffer));
107                out.writeStartElement("StackTrace");
108                out.writeCharacters(buffer.toString());
109                out.writeEndElement();
110            }
111        }
112    
113        public double getVersion() {
114            return version;
115        }
116    
117        public String getNamespace() {
118            return namespace;
119        }
120    
121        public String getPrefix() {
122            return prefix;
123        }
124    
125        public QName getEnvelope() {
126            return envelope;
127        }
128    
129        public QName getHeader() {
130            return header;
131        }
132    
133        public QName getBody() {
134            return body;
135        }
136    
137    
138        public String getSoapEncodingStyle() {
139            return soapEncodingStyle;
140        }
141    
142        // Role URIs
143        //-------------------------------------------------------------------------
144        public String getNoneRole() {
145            return noneRole;
146        }
147    
148        public String getUltimateReceiverRole() {
149            return ultimateReceiverRole;
150        }
151    
152        public String getNextRole() {
153            return nextRole;
154        }
155    }