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 26 package com.sun.xml.internal.ws.message; 27 28 import com.sun.istack.internal.NotNull; 29 import com.sun.xml.internal.bind.api.Bridge; 30 import com.sun.xml.internal.bind.api.BridgeContext; 31 import com.sun.xml.internal.ws.api.SOAPVersion; 32 import com.sun.xml.internal.ws.api.addressing.AddressingVersion; 33 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 34 import com.sun.xml.internal.ws.api.message.Header; 35 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; 36 import com.sun.xml.internal.ws.spi.db.XMLBridge; 37 38 import org.xml.sax.helpers.AttributesImpl; 39 40 import javax.xml.bind.JAXBException; 41 import javax.xml.bind.Unmarshaller; 42 import javax.xml.namespace.QName; 43 import javax.xml.stream.XMLStreamException; 44 import javax.xml.stream.XMLStreamReader; 45 import 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 */ 56 public 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 <tt>xs:boolean</tt> 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 }