summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/main/java/org/apache/qpid/management/wsdm/capabilities/QManMessageHandler.java
blob: 95f54ef5b53bbb35026cb4839a51b32de89ebca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.qpid.management.wsdm.capabilities;

import java.lang.reflect.Method;

import javax.xml.namespace.QName;

import org.apache.muse.core.routing.ReflectionMessageHandler;
import org.apache.muse.core.serializer.Serializer;
import org.apache.muse.core.serializer.SerializerRegistry;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.qpid.management.wsdm.muse.serializer.ByteArraySerializer;
import org.w3c.dom.Element;

/**
 * JMXConnectionListener_example custom implementation of Muse message handler to properly deal with 
 * byte arrays.
 * 
 * @author Andrea Gazzarini
 */
public class QManMessageHandler extends ReflectionMessageHandler
{

	/**
	 * Builds a new message handler with the given arguments.
	 * 
	 * @param actionURI the action URI.
	 * @param requestQName the qname of the incoming request.
	 * @param returnValueName the qname of the result value.
	 */
	public QManMessageHandler(String actionURI, QName requestQName,
			QName returnValueName)
	{
		super(actionURI, requestQName, returnValueName);
	}

	/**
	 * Transforms the given xml element in the corresponding 
	 * object representation.
	 * 
	 * @throws SoapFaul when unmarshal operation fails.
	 */
	@SuppressWarnings("unchecked")
	public Object[] fromXML(Element xml) throws SoapFault
	{
		Method method = getMethod();

		if (xml == null )
		{
			return EMPTY_REQUEST;
		}

		Class[] parameters = method.getParameterTypes();
		Object[] objects = new Object[parameters.length];

		Element[] elements = XmlUtils.getAllElements(xml);

		if (parameters.length == 1 && elements.length == 0)
		{
			elements = new Element[]{ xml };
		}

		if (elements.length != parameters.length)
		{
			throw new SoapFault("IncorrectParams");
		}

		SerializerRegistry registry = SerializerRegistry.getInstance();

		for (int i = 0; i < elements.length; ++i)
		{
			Class clazz = parameters[i];
			if (clazz == byte[].class)
			{
				objects[i] = new ByteArraySerializer().fromXML(elements[i]);
			} else
			{
				Serializer ser = registry.getSerializer(parameters[i]);
				objects[i] = ser.fromXML(elements[i]);
			}
		}
		return objects;
	}
}