SoapHeaderElementMethodArgumentResolver implements MethodArgumentResolver { @Override public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception { Assert.isInstanceOf(SoapMessage.class, messageContext.getRequest()); SoapMessage request = (SoapMessage) messageContext.getRequest(); org.springframework.ws.soap.SoapHeader soapHeader = request.getSoapHeader(); String paramValue = parameter.getParameterAnnotation(SoapHeader.class).value(); Assert.isTrue(QNameUtils.validateQName(paramValue), "Invalid header qualified name [" + paramValue + "]. " + "QName must be of the form '{namespace}localPart'."); QName qname = QName.valueOf(paramValue); Class<?> parameterType = parameter.getParameterType(); if (SoapHeaderElement.class.equals(parameterType)) { return extractSoapHeader(qname, soapHeader); } else if (List.class.equals(parameterType)) { return extractSoapHeaderList(qname, soapHeader); } throw new UnsupportedOperationException(); }  @Override boolean supportsParameter(MethodParameter parameter); @Override Object resolveArgument(MessageContext messageContext, MethodParameter parameter);  }
@Test(expected = IllegalArgumentException.class) public void failOnEmptyValue() throws Exception { resolver.resolveArgument(messageContext, soapHeaderWithEmptyValue); }
@Test public void resolveSoapHeaderElement() throws Exception { Object result = resolver.resolveArgument(messageContext, soapHeaderElementParameter); assertTrue("result must be a SoapHeaderElement", SoapHeaderElement.class.isAssignableFrom(result.getClass())); SoapHeaderElement element = (SoapHeaderElement) result; assertTrue("headers must be equal", element.getName().equals(HEADER_QNAME)); assertEquals("header text must be equal to [" + HEADER_CONTENT + "0]", HEADER_CONTENT + "0", element.getText()); }
@Test @SuppressWarnings("unchecked") public void resolveSoapHeaderElementList() throws Exception { Object result = resolver.resolveArgument(messageContext, soapHeaderElementListParameter); assertTrue("result must be a List", List.class.isAssignableFrom(result.getClass())); List<SoapHeaderElement> elements = (List) result; assertTrue("size", elements.size() > 1); for (int i = 0; i < elements.size(); i++) { SoapHeaderElement element = elements.get(i); assertTrue("headers must be equal", element.getName().equals(HEADER_QNAME)); assertEquals("header must be equal to [" + HEADER_CONTENT + i + "]", HEADER_CONTENT + i, elements.get(i).getText()); } }
@Test public void resolveSoapHeaderMismatch() throws Exception { Object result = resolver.resolveArgument(messageContext, soapHeaderMismatch); assertNull(result); }
@Test public void resolveSoapHeaderMismatchList() throws Exception { Object result = resolver.resolveArgument(messageContext, soapHeaderMismatchList); assertTrue("result must be a List", List.class.isAssignableFrom(result.getClass())); assertTrue("result List must be empty", ((List<?>) result).isEmpty()); }