1 package org.objectweb.celtix.systest.ws.addressing; 2 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 import javax.xml.bind.JAXBContext; 11 import javax.xml.bind.JAXBElement; 12 import javax.xml.bind.JAXBException; 13 import javax.xml.bind.Marshaller; 14 import javax.xml.namespace.QName ; 15 import javax.xml.soap.Name ; 16 import javax.xml.soap.SOAPBody ; 17 import javax.xml.soap.SOAPEnvelope ; 18 import javax.xml.soap.SOAPException ; 19 import javax.xml.soap.SOAPHeader ; 20 import javax.xml.soap.SOAPHeaderElement ; 21 import javax.xml.ws.handler.MessageContext; 22 import javax.xml.ws.handler.soap.SOAPHandler; 23 import javax.xml.ws.handler.soap.SOAPMessageContext; 24 25 import org.objectweb.celtix.bus.ws.addressing.ContextUtils; 26 import org.objectweb.celtix.bus.ws.addressing.Names; 27 import org.objectweb.celtix.bus.ws.addressing.soap.VersionTransformer; 28 import org.objectweb.celtix.ws.addressing.AddressingProperties; 29 import org.objectweb.celtix.ws.addressing.AttributedURIType; 30 import org.objectweb.celtix.ws.addressing.v200408.AttributedURI; 31 32 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_OUTBOUND; 33 34 35 38 public class HeaderVerifier implements SOAPHandler<SOAPMessageContext> { 39 VerificationCache verificationCache; 40 String currentNamespaceURI; 41 42 public void init(Map <String , Object > map) { 43 } 44 45 public Set <QName > getHeaders() { 46 return null; 47 } 48 49 public boolean handleMessage(SOAPMessageContext context) { 50 addPartialResponseHeader(context); 51 verify(context); 52 return true; 53 } 54 55 public boolean handleFault(SOAPMessageContext context) { 56 verify(context); 57 return true; 58 } 59 60 public void close(MessageContext context) { 61 } 62 63 public void destroy() { 64 } 65 66 private void addPartialResponseHeader(SOAPMessageContext context) { 67 try { 68 if (isOutgoingPartialResponse(context)) { 70 SOAPEnvelope env = context.getMessage().getSOAPPart().getEnvelope(); 71 SOAPHeader header = env.getHeader() != null 72 ? env.getHeader() 73 : env.addHeader(); 74 marshallFrom("urn:piggyback_responder", header, getMarshaller()); 75 } 76 } catch (Exception e) { 77 verificationCache.put("SOAP header addition failed: " + e); 78 e.printStackTrace(); 79 } 80 } 81 82 private void verify(SOAPMessageContext context) { 83 try { 84 List <String > wsaHeaders = new ArrayList <String >(); 85 SOAPHeader header = 86 context.getMessage().getSOAPPart().getEnvelope().getHeader(); 87 if (header != null) { 88 Iterator headerElements = header.examineAllHeaderElements(); 89 while (headerElements.hasNext()) { 90 Name headerName = ((SOAPHeaderElement )headerElements.next()).getElementName(); 91 if (isAddressingNamespace(headerName.getURI())) { 92 wsaHeaders.add(headerName.getLocalName()); 93 } 94 } 95 } 96 boolean partialResponse = isIncomingPartialResponse(context) 97 || isOutgoingPartialResponse(context); 98 verificationCache.put(MAPTest.verifyHeaders(wsaHeaders, 99 partialResponse)); 100 } catch (SOAPException se) { 101 verificationCache.put("SOAP header verification failed: " + se); 102 } 103 } 104 105 private boolean isAddressingNamespace(String namespace) { 106 boolean isAddressing = false; 107 if (Names.WSA_NAMESPACE_NAME.equals(namespace) 108 || VersionTransformer.Names200408.WSA_NAMESPACE_NAME.equals( 109 namespace)) { 110 currentNamespaceURI = namespace; 111 isAddressing = true; 112 } 113 return isAddressing; 114 } 115 116 private boolean isOutgoingPartialResponse(SOAPMessageContext context) { 117 AddressingProperties maps = 118 (AddressingProperties)context.get(SERVER_ADDRESSING_PROPERTIES_OUTBOUND); 119 return ContextUtils.isOutbound(context) 120 && ContextUtils.isRequestor(context) 121 && Names.WSA_ANONYMOUS_ADDRESS.equals(maps.getTo().getValue()); 122 } 123 124 private boolean isIncomingPartialResponse(SOAPMessageContext context) 125 throws SOAPException { 126 SOAPBody body = 127 context.getMessage().getSOAPPart().getEnvelope().getBody(); 128 return !ContextUtils.isOutbound(context) 129 && ContextUtils.isRequestor(context) 130 && !body.getChildElements().hasNext(); 131 } 132 133 private Marshaller getMarshaller() throws JAXBException { 134 JAXBContext jaxbContext = 135 VersionTransformer.getExposedJAXBContext(currentNamespaceURI); 136 Marshaller marshaller = jaxbContext.createMarshaller(); 137 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 138 return marshaller; 139 } 140 141 private void marshallFrom(String from, SOAPHeader header, Marshaller marshaller) 142 throws JAXBException { 143 if (Names.WSA_NAMESPACE_NAME.equals(currentNamespaceURI)) { 144 AttributedURIType value = 145 ContextUtils.getAttributedURI("urn:piggyback_responder"); 146 marshaller.marshal( 147 new JAXBElement<AttributedURIType>(Names.WSA_FROM_QNAME, 148 AttributedURIType.class, 149 value), 150 header); 151 } else if (VersionTransformer.Names200408.WSA_NAMESPACE_NAME.equals( 152 currentNamespaceURI)) { 153 AttributedURI value = 154 VersionTransformer.Names200408.WSA_OBJECT_FACTORY.createAttributedURI(); 155 value.setValue(from); 156 QName qname = new QName (VersionTransformer.Names200408.WSA_NAMESPACE_NAME, 157 Names.WSA_FROM_NAME); 158 marshaller.marshal( 159 new JAXBElement<AttributedURI>(qname, 160 AttributedURI.class, 161 value), 162 header); 163 164 } 165 166 } 167 168 public void setVerificationCache(VerificationCache cache) { 169 verificationCache = cache; 170 } 171 } 172 | Popular Tags |