summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/main/java/org/apache/qpid/management/web/action/BrokersManagementAction.java
blob: 509c86c08beeff07c985ebdac99716b16e388060 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 *
 * 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.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.qpid.management.Names;
import org.apache.qpid.management.configuration.BrokerAlreadyConnectedException;
import org.apache.qpid.management.configuration.BrokerConnectionData;
import org.apache.qpid.management.configuration.BrokerConnectionException;
import org.apache.qpid.management.domain.services.ManagementClient;
import org.apache.qpid.management.domain.services.QMan;

/**
 * This controller is responsible to :
 * 
 * <ul>
 * 	<li> prepare data for the page that is showing all connected brokers.</li>.
 * 	</li> connect QMan with a broker on demand.
 * </ul>
 * 
 * @author Andrea Gazzarini
 */
public class BrokersManagementAction extends HttpServlet
{
	private static final long serialVersionUID = -2411413147821629363L;
	 
	/**
	 * Retrieves all connected brokers (their connection data) and prepare the model that
	 * is then forwarded to the appropriate view page.
	 * 
	 * @param request the http request.
	 * @param response the http response.
	 * @throws ServletException in case of failure while forwarding to the view component.
	 * @throws IOException in case of failure while forwarding to the view component.
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try 
		{	
			QMan qman = (QMan)getServletContext().getAttribute(Names.APPLICATION_NAME);
			List<ManagementClient> managementClients = qman.getManagementClients();
			
			List<BrokerConnectionData> brokers = new ArrayList<BrokerConnectionData>(managementClients.size());
			
			if (!managementClients.isEmpty())
			{
				for (ManagementClient managementClient : managementClients)
				{
					brokers.add(managementClient.getBrokerConnectionData());
				}
				request.setAttribute("model", brokers);		
			}
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
			dispatcher.forward(request,response);
		} catch(Exception exception)
		{
			request.setAttribute("errorMessage","Unable to detect the exact cause Please look at the reported stack trace below.");
			request.setAttribute("exception",exception);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/error_page.jsp");
			dispatcher.forward(request,response);			
		}
	}
	
	/**
	 * Connects QMan with a new broker and forwards to 
	 * the brokers list view page.
	 * 
	 * @param request the http request.
	 * @param response the http response.
	 * @throws ServletException in case of failure while forwarding to the view component.
	 * @throws IOException in case of failure while forwarding to the view component.
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try 
		{	
			QMan qman = (QMan)getServletContext().getAttribute(Names.APPLICATION_NAME);
			
			String host = request.getParameter("host");
			String portString = request.getParameter("port");
			String virtualHost = request.getParameter("virtualHost");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			String initialCapacityString = request.getParameter("initialCapacity");
			String maxCapacityString = request.getParameter("maxCapacity");
			String maxWaitTimeoutString = request.getParameter("maxWaitTimeout");
			
			List<String> errors = new LinkedList<String>();
			int port = 0;
			int initialPoolCapacity = 0;
			int maxPoolCapacity = 0;
			long maxWaitTimeout = 0;
			
			if(host== null || host.trim().length()==0)
			{
				errors.add("Invalid value for \"host\" attribute. Must be not null.");				
			}

			if(virtualHost == null || virtualHost.trim().length()==0)
			{
				errors.add("Invalid value for \"virtualHost\" attribute. Must be not null.");				
			}
			
			try
			{
				port = Integer.parseInt(portString);
			} catch(Exception exception) 
			{
				errors.add("Invalid value for \"port\" attribute. Must be not null and must be a number.");
			}
			
			try
			{
				initialPoolCapacity = Integer.parseInt(initialCapacityString);
			} catch(Exception exception) 
			{
				errors.add("Invalid value for \"Initial Pool Capacity\" attribute. Must be not null and must be a number.");
			}
			
			try
			{
				maxPoolCapacity = Integer.parseInt(maxCapacityString);
			} catch(Exception exception) 
			{
				errors.add("Invalid value for \"Max Pool Capacity\" attribute. Must be not null and must be a number.");
			}

			try
			{
				maxWaitTimeout = Long.parseLong(maxWaitTimeoutString);
			} catch(Exception exception) 
			{
				errors.add("Invalid value for \"Max Wait Timeout\" attribute. Must be not null and must be a number.");
			}
			
			request.setAttribute("errors", errors);
			
			if (errors.isEmpty()) 
			{
				qman.addBroker(
						host, 
						port, 
						username, 
						password, 
						virtualHost, 
						initialPoolCapacity, 
						maxPoolCapacity, 
						maxWaitTimeout);
			}
			doGet(request, response);
		}catch(BrokerAlreadyConnectedException exception)
		{
			request.setAttribute("errorMessage","Supplied data refers to an already connected broker...");
			RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
			dispatcher.forward(request,response);			
		}
		catch(BrokerConnectionException exception)
		{
			request.setAttribute("errorMessage","Unable to connect with the requested broker...");
			RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
			dispatcher.forward(request,response);			
		} catch(Exception exception)
		{
			request.setAttribute("errorMessage","Unable to detect the exact cause Please look at the reported stack trace below.");
			request.setAttribute("exception",exception);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/error_page.jsp");
			dispatcher.forward(request,response);			
		}
	}	
}