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.xml.bind.api.Bridge;
041: import com.sun.xml.bind.api.BridgeContext;
042: import com.sun.xml.ws.api.SOAPVersion;
043: import com.sun.xml.ws.api.addressing.AddressingVersion;
044: import com.sun.xml.ws.api.addressing.WSEndpointReference;
045: import com.sun.xml.ws.api.message.Header;
046: import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
047: import org.xml.sax.helpers.AttributesImpl;
048:
049: import javax.xml.bind.JAXBException;
050: import javax.xml.bind.Unmarshaller;
051: import javax.xml.namespace.QName;
052: import javax.xml.stream.XMLStreamException;
053: import javax.xml.stream.XMLStreamReader;
054: import java.util.Set;
055:
056: /**
057: * Partial default implementation of {@link Header}.
058: *
059: * <p>
060: * This is meant to be a convenient base class
061: * for {@link Header}-derived classes.
062: *
063: * @author Kohsuke Kawaguchi
064: */
065: public abstract class AbstractHeaderImpl implements Header {
066:
067: protected AbstractHeaderImpl() {
068: }
069:
070: /**
071: * @deprecated
072: */
073: public final <T> T readAsJAXB(Bridge<T> bridge,
074: BridgeContext context) throws JAXBException {
075: return readAsJAXB(bridge);
076: }
077:
078: public <T> T readAsJAXB(Unmarshaller unmarshaller)
079: throws JAXBException {
080: try {
081: return (T) unmarshaller.unmarshal(readHeader());
082: } catch (Exception e) {
083: throw new JAXBException(e);
084: }
085: }
086:
087: public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
088: try {
089: return bridge.unmarshal(readHeader());
090: } catch (XMLStreamException e) {
091: throw new JAXBException(e);
092: }
093: }
094:
095: /**
096: * Default implementation that copies the infoset. Not terribly efficient.
097: */
098: public WSEndpointReference readAsEPR(AddressingVersion expected)
099: throws XMLStreamException {
100: XMLStreamReader xsr = readHeader();
101: WSEndpointReference epr = new WSEndpointReference(xsr, expected);
102: XMLStreamReaderFactory.recycle(xsr);
103: return epr;
104: }
105:
106: public boolean isIgnorable(@NotNull
107: SOAPVersion soapVersion, @NotNull
108: Set<String> roles) {
109: // check mustUnderstand
110: String v = getAttribute(soapVersion.nsUri, "mustUnderstand");
111: if (v == null || !parseBool(v))
112: return true;
113:
114: // now role
115: return !roles.contains(getRole(soapVersion));
116: }
117:
118: public @NotNull
119: String getRole(@NotNull
120: SOAPVersion soapVersion) {
121: String v = getAttribute(soapVersion.nsUri,
122: soapVersion.roleAttributeName);
123: if (v == null)
124: v = soapVersion.implicitRole;
125: return v;
126: }
127:
128: public boolean isRelay() {
129: String v = getAttribute(SOAPVersion.SOAP_12.nsUri, "relay");
130: if (v == null)
131: return false; // on SOAP 1.1 message there shouldn't be such an attribute, so this works fine
132: return parseBool(v);
133: }
134:
135: public String getAttribute(QName name) {
136: return getAttribute(name.getNamespaceURI(), name.getLocalPart());
137: }
138:
139: /**
140: * Parses a string that looks like <tt>xs:boolean</tt> into boolean.
141: *
142: * This method assumes that the whilespace normalization has already taken place.
143: */
144: protected final boolean parseBool(String value) {
145: if (value.length() == 0)
146: return false;
147:
148: char ch = value.charAt(0);
149: return ch == 't' || ch == '1';
150: }
151:
152: public String getStringContent() {
153: try {
154: XMLStreamReader xsr = readHeader();
155: xsr.nextTag();
156: return xsr.getElementText();
157: } catch (XMLStreamException e) {
158: return null;
159: }
160: }
161:
162: protected static final AttributesImpl EMPTY_ATTS = new AttributesImpl();
163: }
|