Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.server.endpoint.adapter;
 
 import java.util.Map;
 
 
Adapter that supports endpoint methods that use XPath expressions. Supports methods with the following signature:
 void handleMyMessage(@XPathParam("/root/child/text")String param);
 
or
 Source handleMyMessage(@XPathParam("/root/child/text")String param1, 

Deprecated:
as of Spring Web Services 2.0, in favor of DefaultMethodEndpointAdapter and org.springframework.ws.server.endpoint.adapter.method.XPathParamMethodArgumentResolver.
Author(s):
Arjen Poutsma
Since:
1.0.0
XPathParam:
"/root/child/number")double param2);
I.e. methods that return either void or a javax.xml.transform.Source, and have parameters annotated with org.springframework.ws.server.endpoint.annotation.XPathParam that specify the XPath expression that should be bound to that parameter. The parameter can be of the following types:
 
         implements InitializingBean {
 
     private XPathFactory xpathFactory;
 
     private Map<StringStringnamespaces;

    
Sets namespaces used in the XPath expression. Maps prefixes to namespaces.
 
     public void setNamespaces(Map<StringStringnamespaces) {
         this. = namespaces;
     }
 
     public void afterPropertiesSet() throws Exception {
          = XPathFactory.newInstance();
     }

    
Supports methods with

XPathParam:
parameters, and return either Source or nothing.
 
     @Override
     protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
         Method method = methodEndpoint.getMethod();
         if (!(Source.class.isAssignableFrom(method.getReturnType()) || ..equals(method.getReturnType()))) {
             return false;
         }
         Class<?>[] parameterTypes = method.getParameterTypes();
         for (int i = 0; i < parameterTypes.lengthi++) {
             if (getXPathParamAnnotation(methodi) == null || !isSupportedType(parameterTypes[i])) {
                 return false;
             }
         }
         return true;
     }
 
     private XPathParam getXPathParamAnnotation(Method methodint paramIdx) {
         Annotation[][] paramAnnotations = method.getParameterAnnotations();
         for (int annIdx = 0; annIdx < paramAnnotations[paramIdx].lengthannIdx++) {
            if (paramAnnotations[paramIdx][annIdx].annotationType().equals(XPathParam.class)) {
                return (XPathParamparamAnnotations[paramIdx][annIdx];
            }
        }
        return null;
    }
    private boolean isSupportedType(Class<?> clazz) {
        return Boolean.class.isAssignableFrom(clazz) || ..isAssignableFrom(clazz) ||
                Double.class.isAssignableFrom(clazz) || ..isAssignableFrom(clazz) ||
                Node.class.isAssignableFrom(clazz) || NodeList.class.isAssignableFrom(clazz) ||
                String.class.isAssignableFrom(clazz);
    }
    @Override
    protected void invokeInternal(MessageContext messageContextMethodEndpoint methodEndpointthrows Exception {
        Element payloadElement = getRootElement(messageContext.getRequest().getPayloadSource());
        Object[] args = getMethodArguments(payloadElementmethodEndpoint.getMethod());
        Object result = methodEndpoint.invoke(args);
        if (result != null && result instanceof Source) {
            Source responseSource = (Sourceresult;
            WebServiceMessage response = messageContext.getResponse();
            transform(responseSourceresponse.getPayloadResult());
        }
    }
    private Object[] getMethodArguments(Element payloadElementMethod methodthrows XPathExpressionException {
        Class<?>[] parameterTypes = method.getParameterTypes();
        XPath xpath = createXPath();
        Object[] args = new Object[parameterTypes.length];
        for (int i = 0; i < parameterTypes.lengthi++) {
            String expression = getXPathParamAnnotation(methodi).value();
            QName conversionType;
            if (Boolean.class.isAssignableFrom(parameterTypes[i]) || ..isAssignableFrom(parameterTypes[i])) {
                conversionType = .;
            }
            else
            if (Double.class.isAssignableFrom(parameterTypes[i]) || ..isAssignableFrom(parameterTypes[i])) {
                conversionType = .;
            }
            else if (Node.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = .;
            }
            else if (NodeList.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = .;
            }
            else if (String.class.isAssignableFrom(parameterTypes[i])) {
                conversionType = .;
            }
            else {
                throw new IllegalArgumentException("Invalid parameter type [" + parameterTypes[i] + "]. " +
                        "Supported are: Boolean, Double, Node, NodeList, and String.");
            }
            args[i] = xpath.evaluate(expressionpayloadElementconversionType);
        }
        return args;
    }
    private synchronized XPath createXPath() {
        XPath xpath = .newXPath();
        if ( != null) {
            SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
            namespaceContext.setBindings();
            xpath.setNamespaceContext(namespaceContext);
        }
        return xpath;
    }

    
Returns the root element of the given source.

Parameters:
source the source to get the root element from
Returns:
the root element
    private Element getRootElement(Source sourcethrows TransformerException {
        DOMResult domResult = new DOMResult();
        transform(sourcedomResult);
        Document document = (DocumentdomResult.getNode();
        return document.getDocumentElement();
    }
New to GrepCode? Check out our FAQ X