QManMessageHandler.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.wsdm.capabilities;
022 
023 import java.lang.reflect.Method;
024 
025 import javax.xml.namespace.QName;
026 
027 import org.apache.muse.core.routing.ReflectionMessageHandler;
028 import org.apache.muse.core.serializer.Serializer;
029 import org.apache.muse.core.serializer.SerializerRegistry;
030 import org.apache.muse.util.xml.XmlUtils;
031 import org.apache.muse.ws.addressing.soap.SoapFault;
032 import org.apache.qpid.management.wsdm.muse.serializer.ByteArraySerializer;
033 import org.w3c.dom.Element;
034 
035 /**
036  * JMXConnectionListener_example custom implementation of Muse message handler to properly deal with 
037  * byte arrays.
038  
039  @author Andrea Gazzarini
040  */
041 public class QManMessageHandler extends ReflectionMessageHandler
042 {
043 
044   /**
045    * Builds a new message handler with the given arguments.
046    
047    @param actionURI the action URI.
048    @param requestQName the qname of the incoming request.
049    @param returnValueName the qname of the result value.
050    */
051   public QManMessageHandler(String actionURI, QName requestQName,
052       QName returnValueName)
053   {
054     super(actionURI, requestQName, returnValueName);
055   }
056 
057   /**
058    * Transforms the given xml element in the corresponding 
059    * object representation.
060    
061    @throws SoapFaul when unmarshal operation fails.
062    */
063   @SuppressWarnings("unchecked")
064   public Object[] fromXML(Element xmlthrows SoapFault
065   {
066     Method method = getMethod();
067 
068     if (xml == null )
069     {
070       return EMPTY_REQUEST;
071     }
072 
073     Class[] parameters = method.getParameterTypes();
074     Object[] objects = new Object[parameters.length];
075 
076     Element[] elements = XmlUtils.getAllElements(xml);
077 
078     if (parameters.length == && elements.length == 0)
079     {
080       elements = new Element[]{ xml };
081     }
082 
083     if (elements.length != parameters.length)
084     {
085       throw new SoapFault("IncorrectParams");
086     }
087 
088     SerializerRegistry registry = SerializerRegistry.getInstance();
089 
090     for (int i = 0; i < elements.length; ++i)
091     {
092       Class clazz = parameters[i];
093       if (clazz == byte[].class)
094       {
095         objects[inew ByteArraySerializer().fromXML(elements[i]);
096       else
097       {
098         Serializer ser = registry.getSerializer(parameters[i]);
099         objects[i= ser.fromXML(elements[i]);
100       }
101     }
102     return objects;
103   }
104 }