summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/example/org/apache/qpid/management/example/GetWSDLMetadataExample.java
blob: ecda6e8fb17b57a917f3a95047bb022505ab2a79 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *
 * 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.example;

import javax.xml.namespace.QName;

import org.apache.muse.core.proxy.ProxyHandler;
import org.apache.muse.core.proxy.ReflectionProxyHandler;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.resource.remote.WsResourceClient;
import org.apache.muse.ws.resource.sg.remote.ServiceGroupClient;
import org.w3c.dom.Element;

/**
 * This example shows how to get metadata from a WS-Resource.
 * The service supports different kinds of metadata. 
 * User who wants to receive metadata of a WS-Resource must 
 * send a GetMetadataRequesta specifying the requested dialect. 
 *  
 * Supported metadata that could be requested are
 * 
 * <ul>
 * 	<li>
 * 		WSDL : requested using "http://schemas.xmlsoap.org/wsdl/" as dialect..
 * 	<li>
 * 	<li>
 * 		RDM (Resource Metadata Descriptor) : requested using "http://docs.oasis-open.org/wsrf/rmd-1 "as dialect.
 * 	</li>
 * </ul>
 * 
 * Note that this example focuses on WSDL Metadata only; another example is dedicated to RDM.
 * 
 * @author Andrea Gazzarini
 */
public class GetWSDLMetadataExample extends AbstractQManExample
{

	/**
	 * First, sends a request to WS-DM Adapter in order to get the list of managed resources.
	 * If the list is not empty, then takes the first member and sends it a GetMetadataRequest 
	 * in order to get its WSDL.
	 * 
	 * @param host the host where QMan is running.
	 * @param port the port where QMan is running.
	 * @throws Exception when the example fails (not at application level).
	 */
	void executeExample(String host, int port) throws Exception
	{		
	
		// 1) Creates an endpoint reference of the adapter service...
		EndpointReference adapterEndpointReference = getAdapterEndpointReference(host, port);
		
		// 2) Creates the Adapter service client...
		ServiceGroupClient adapterClient = new ServiceGroupClient(adapterEndpointReference);
		adapterClient.setTrace(true);
		
		// 3) Retrieves the all registered members (QMan WS-Resources)
		WsResourceClient [] resources = adapterClient.getMembers();

		// Sanity check : we cannot proceed if there are no WS-Resources.
		if (resources.length == 0)
		{
			System.out.println("----------------------------WARNING---------------------------");
			System.out.println("Cannot proceed with the example... it seems");
			System.out.println("that there are no managed WS-Resources on QMan.");
			System.out.println("Please check QMan in order to see that it is really");
			System.out.println("connected with a broker.");
			System.out.println("-------------------------------------------------------------------");
			System.exit(0);
		} 
		
		// 4) Creates a proxy handler for service invocation.
		ProxyHandler metadataProxyHandler = createProxyHandler();
		
		// 5) ..and invokes the GetMetadata on the first member.
		WsResourceClient firstMember = resources[0];
		firstMember.setTrace(true);
		
		// Dialect is WSDL for this example
		String dialect = "http://schemas.xmlsoap.org/wsdl/";
		Object [] inputParameters = {dialect};
		
		// WSDL is the first element of the returned array. We don't need to print out it here
		// because at this point it should have been already printed out (line 96 : firstMember.setTrace(true))
		@SuppressWarnings("unused")
		Element [] metadata = (Element[]) firstMember.invoke(metadataProxyHandler, inputParameters);
	}
	
	/**
	 * Prints out a description of this example.
	 */
	void printOutExampleDescription()
	{
		System.out.println("                 "+getClass().getSimpleName()+" ");
		System.out.println("-------------------------------------------------------------------");
		System.out.println();
		 System.out.println("The example shows how to get metadata from a");
		 System.out.println("WS-Resource.");
		 System.out.println("A QMan WS-Resource has different kinds of metadata.");
		 System.out.println("(see below)");
		 System.out.println("User who wants to receive metadata of a WS-Resource");
		 System.out.println("must send a GetMetadataRequesta specifying the");
		 System.out.println("associated dialect."); 
		 System.out.println("Supported metadata that could be requested are : ");
		 System.out.println();
		 System.out.println("- WSDL : in this case dialect is \"http://schemas.xmlsoap.org/wsdl/\";");
		 System.out.println("- RDM (Resource Metadata Descriptor) : in this case dialect is \"http://docs.oasis-open.org/wsrf/rmd-1 \".");
		System.out.println();
		System.out.println("Note that this examples focuses on WSDL Metadata only;");
		System.out.println("another one is dedicated to RDM.");
		System.out.println("-------------------------------------------------------------------");
		System.out.println();
	}

	/**
	 * A proxy handler is a module needed in order to make a capability 
	 * service invocation.
	 * It contains logic to serialize and deserialize request, response, input and 
	 * output parameters during a web service invocation.
	 * 
	 * @return a proxy handler.
	 */
	private ProxyHandler createProxyHandler()
	{
        ProxyHandler handler = new ReflectionProxyHandler();
        handler.setAction("http://schemas.xmlsoap.org/ws/2004/09/mex/GetMetadata");
        handler.setRequestName(new QName("http://schemas.xmlsoap.org/ws/2004/09/mex", "GetMetadata", PREFIX));
        handler.setRequestParameterNames(new QName[]{new QName("http://schemas.xmlsoap.org/ws/2004/09/mex", "Dialect", PREFIX)});
        handler.setResponseName(new QName("http://schemas.xmlsoap.org/ws/2004/09/mex", "Metadata", PREFIX));
        handler.setReturnType(Element[].class);
        return handler;
	}
	
	public static void main(String[] arguments)
	{
		new GetWSDLMetadataExample().execute(arguments);
	}
}