1/*
2 * Copyright (c) 1997, 2013, 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
26package com.sun.xml.internal.ws.message;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.bind.api.Bridge;
30import com.sun.xml.internal.bind.api.BridgeContext;
31import com.sun.xml.internal.ws.api.SOAPVersion;
32import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
33import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
34import com.sun.xml.internal.ws.api.message.Header;
35import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
36import com.sun.xml.internal.ws.spi.db.XMLBridge;
37
38import org.xml.sax.helpers.AttributesImpl;
39
40import javax.xml.bind.JAXBException;
41import javax.xml.bind.Unmarshaller;
42import javax.xml.namespace.QName;
43import javax.xml.stream.XMLStreamException;
44import javax.xml.stream.XMLStreamReader;
45import java.util.Set;
46
47/**
48 * Partial default implementation of {@link Header}.
49 *
50 * <p>
51 * This is meant to be a convenient base class
52 * for {@link Header}-derived classes.
53 *
54 * @author Kohsuke Kawaguchi
55 */
56public abstract class AbstractHeaderImpl implements Header {
57
58    protected AbstractHeaderImpl() {
59    }
60
61    /**
62     * @deprecated
63     */
64    public final <T> T readAsJAXB(Bridge<T> bridge, BridgeContext context) throws JAXBException {
65        return readAsJAXB(bridge);
66    }
67
68    public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
69        try {
70            return (T)unmarshaller.unmarshal(readHeader());
71        } catch (Exception e) {
72            throw new JAXBException(e);
73        }
74    }
75    /** @deprecated */
76    public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
77        try {
78            return bridge.unmarshal(readHeader());
79        } catch (XMLStreamException e) {
80            throw new JAXBException(e);
81        }
82    }
83
84    public <T> T readAsJAXB(XMLBridge<T> bridge) throws JAXBException {
85        try {
86            return bridge.unmarshal(readHeader(), null);
87        } catch (XMLStreamException e) {
88            throw new JAXBException(e);
89        }
90    }
91
92    /**
93     * Default implementation that copies the infoset. Not terribly efficient.
94     */
95    public WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException {
96        XMLStreamReader xsr = readHeader();
97        WSEndpointReference epr = new WSEndpointReference(xsr, expected);
98        XMLStreamReaderFactory.recycle(xsr);
99        return epr;
100    }
101
102    public boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles) {
103        // check mustUnderstand
104        String v = getAttribute(soapVersion.nsUri, "mustUnderstand");
105        if(v==null || !parseBool(v)) return true;
106
107        if (roles == null) return true;
108
109        // now role
110        return !roles.contains(getRole(soapVersion));
111    }
112
113    public @NotNull String getRole(@NotNull SOAPVersion soapVersion) {
114        String v = getAttribute(soapVersion.nsUri, soapVersion.roleAttributeName);
115        if(v==null)
116            v = soapVersion.implicitRole;
117        return v;
118    }
119
120    public boolean isRelay() {
121        String v = getAttribute(SOAPVersion.SOAP_12.nsUri,"relay");
122        if(v==null) return false;   // on SOAP 1.1 message there shouldn't be such an attribute, so this works fine
123        return parseBool(v);
124    }
125
126    public String getAttribute(QName name) {
127        return getAttribute(name.getNamespaceURI(),name.getLocalPart());
128    }
129
130    /**
131     * Parses a string that looks like {@code xs:boolean} into boolean.
132     *
133     * This method assumes that the whilespace normalization has already taken place.
134     */
135    protected final boolean parseBool(String value) {
136        if(value.length()==0)
137            return false;
138
139        char ch = value.charAt(0);
140        return ch=='t' || ch=='1';
141    }
142
143    public String getStringContent() {
144        try {
145            XMLStreamReader xsr = readHeader();
146            xsr.nextTag();
147            return xsr.getElementText();
148        } catch (XMLStreamException e) {
149            return null;
150        }
151    }
152
153    protected static final AttributesImpl EMPTY_ATTS = new AttributesImpl();
154}
155