KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > ListingAgent


1 /*
2 * Copyright 2004,2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Runtime state of the engine
17 */

18 package org.apache.axis2.transport.http;
19
20 import org.apache.axis2.Constants;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.deployment.util.DeploymentData;
23 import org.apache.axis2.description.OperationDescription;
24 import org.apache.axis2.description.ServiceDescription;
25 import org.apache.axis2.engine.AxisConfigurationImpl;
26 import org.apache.axis2.engine.AxisFault;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.xml.namespace.QName JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.HashMap JavaDoc;
38
39 public class ListingAgent {
40
41     /**
42      * Field LIST_MULTIPLE_SERVICE_JSP_NAME
43      */

44     private static final String JavaDoc LIST_MULTIPLE_SERVICE_JSP_NAME = "listServices.jsp";
45
46     private static final String JavaDoc LIST_SRVICES_JSP_NAME = "listService.jsp";
47
48     private static final String JavaDoc SELECT_SERVICE_JSP_NAME = "SelectService.jsp";
49
50     private static final String JavaDoc ADMIN_JSP_NAME = "admin.jsp";
51
52     private static final String JavaDoc LIST_AVAILABLE_MODULES_JSP_NAME = "listModules.jsp";
53
54     private static final String JavaDoc LIST_GLOABLLY_ENGAGED_MODULES_JSP_NAME = "globalModules.jsp";
55
56     private static final String JavaDoc LIST_PHASES_JSP_NAME = "viewphases.jsp";
57
58     private static final String JavaDoc ENGAGING_MODULE_GLOBALLY_JSP_NAME = "engagingglobally.jsp";
59
60     private static final String JavaDoc ENGAGING_MODULE_TO_SERVICE_JSP_NAME = "engagingtoaservice.jsp";
61
62     /**
63      * Field LIST_SINGLE_SERVICE_JSP_NAME
64      */

65     private static final String JavaDoc LIST_SINGLE_SERVICE_JSP_NAME = "listSingleService.jsp";
66     private static final String JavaDoc VIEW_GLOBAL_HANDLERS_JSP_NAME = "ViewGlobalHandlers.jsp";
67     private static final String JavaDoc VIEW_SERVICE_HANDLERS_JSP_NAME = "ViewServiceHandlers.jsp";
68
69     private static final String JavaDoc ENGAGE_TO_OPERATION_JSP_NAME = "enaggingtoanopeartion.jsp";
70
71     public ListingAgent(ConfigurationContext configContext) {
72         this.configContext = configContext;
73     }
74
75     private ConfigurationContext configContext;
76     /**
77      * Field allowListServices
78      */

79     private final boolean allowListServices = true;
80
81     /**
82      * Field allowListSingleService
83      */

84     private final boolean allowListSingleService = true;
85     private OutputStream JavaDoc out = null;
86
87     public void handle(
88             HttpServletRequest JavaDoc httpServletRequest,
89             HttpServletResponse JavaDoc httpServletResponse,OutputStream JavaDoc out)
90             throws IOException JavaDoc {
91         this.out = out;
92         String JavaDoc filePart = httpServletRequest.getRequestURL().toString();
93         if ((filePart != null) && filePart.endsWith(Constants.ADMIN_LISTSERVICES)) {
94             listAdminServices(httpServletRequest, httpServletResponse);
95             return;
96         } else if ((filePart != null) && filePart.endsWith(Constants.LIST_MODULES)) {
97             listModules(httpServletRequest, httpServletResponse);
98             return;
99         } else if (
100                 (filePart != null) && filePart.endsWith(Constants.LIST_GLOABLLY_ENGAGED_MODULES)) {
101             listGloballyModules(httpServletRequest, httpServletResponse);
102             return;
103         } else if ((filePart != null) && filePart.endsWith(Constants.LIST_PHASES)) {
104             listPhases(httpServletRequest, httpServletResponse);
105             return;
106         } else if ((filePart != null) && filePart.endsWith(Constants.ENGAGE_GLOBAL_MODULE)) {
107             engageModulesGlobally(httpServletRequest, httpServletResponse);
108             return;
109         } else if ((filePart != null) && filePart.endsWith(Constants.ENGAGE_MODULE_TO_SERVICE)) {
110             engageModulesToService(httpServletRequest, httpServletResponse);
111             return;
112         } else if ((filePart != null) && filePart.endsWith(Constants.ADMIN_LOGGING)) {
113             adminLogging(httpServletRequest, httpServletResponse);
114             return;
115         } else if ((filePart != null) && filePart.endsWith(Constants.VIEW_GLOBAL_HANDLERS)) {
116             viewGlobalHandlers(httpServletRequest, httpServletResponse);
117             return;
118         } else if ((filePart != null) && filePart.endsWith(Constants.SELECT_SERVICE)) {
119             selectService(httpServletRequest, httpServletResponse);
120             return;
121         } else if ((filePart != null) && filePart.endsWith(Constants.VIEW_SERVICE_HANDLERS)) {
122             viewServiceHandlers(httpServletRequest, httpServletResponse);
123             return;
124         } else if (
125                 (filePart != null) && filePart.endsWith(Constants.LIST_SERVICE_FOR_MODULE_ENGAMNET)) {
126             lsitServiceformodules(httpServletRequest, httpServletResponse);
127             return;
128         } else if (
129                 (filePart != null) && filePart.endsWith(Constants.LIST_OPERATIONS_FOR_THE_SERVICE)) {
130             engageModulesToOpeartion(httpServletRequest, httpServletResponse);
131             return;
132         }
133
134         if (allowListServices && (filePart != null) && filePart.endsWith(Constants.LISTSERVICES)) {
135             listServices(httpServletRequest, httpServletResponse);
136             return;
137         } else {
138             if (allowListSingleService) {
139                 listService(httpServletRequest, httpServletResponse, filePart);
140                 return;
141             }
142         }
143     }
144
145     /**
146      * Method listServices
147      *
148      * @param req
149      * @param res
150      * @throws IOException
151      */

152     private void listServices(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
153         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
154         req.getSession().setAttribute(Constants.SERVICE_MAP, services);
155         req.getSession().setAttribute(
156                 Constants.ERROR_SERVICE_MAP,
157                 configContext.getAxisConfiguration().getFaulytServices());
158         res.sendRedirect(LIST_MULTIPLE_SERVICE_JSP_NAME);
159     }
160
161     /**
162      *
163      * @param req
164      * @param res
165      * @throws IOException
166      */

167     private void listAdminServices(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
168             throws IOException JavaDoc {
169         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
170         req.getSession().setAttribute(Constants.SERVICE_MAP, services);
171         req.getSession().setAttribute(
172                 Constants.ERROR_SERVICE_MAP,
173                 configContext.getAxisConfiguration().getFaulytServices());
174         res.sendRedirect(LIST_SRVICES_JSP_NAME);
175     }
176
177     private void selectService(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
178             throws IOException JavaDoc {
179         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
180         req.getSession().setAttribute(Constants.SERVICE_MAP, services);
181         req.getSession().setAttribute(Constants.MODULE_ENGAMENT, null);
182         res.sendRedirect(SELECT_SERVICE_JSP_NAME);
183     }
184     private void adminLogging(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
185         String JavaDoc username = req.getParameter("userName");
186         String JavaDoc password = req.getParameter("password");
187         if (username == null
188                 || password == null
189                 || username.trim().equals("")
190                 || password.trim().equals("")) {
191             throw new AxisFault("invalid user name");
192         }
193         String JavaDoc adminUserName =
194                 (String JavaDoc) ((AxisConfigurationImpl) configContext.getAxisConfiguration())
195                 .getParameter(Constants.USER_NAME)
196                 .getValue();
197         String JavaDoc adminPassword =
198                 (String JavaDoc) ((AxisConfigurationImpl) configContext.getAxisConfiguration())
199                 .getParameter(Constants.PASSWORD)
200                 .getValue();
201         if (username != null
202                 && password != null
203                 && username.equals(adminUserName)
204                 && password.equals(adminPassword)) {
205             req.getSession().setAttribute(Constants.LOGGED, "Yes");
206             res.sendRedirect(ADMIN_JSP_NAME);
207         } else {
208             throw new AxisFault("invalid user name");
209         }
210     }
211
212     private void listModules(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
213         HashMap JavaDoc modules =
214                 ((AxisConfigurationImpl) configContext.getAxisConfiguration()).getModules();
215         req.getSession().setAttribute(Constants.MODULE_MAP, modules);
216         req.getSession().setAttribute(
217                 Constants.ERROR_MODULE_MAP,
218                 configContext.getAxisConfiguration().getFaulytModules());
219         res.sendRedirect(LIST_AVAILABLE_MODULES_JSP_NAME);
220     }
221
222     private void engageModulesGlobally(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
223             throws IOException JavaDoc {
224         HashMap JavaDoc modules =
225                 ((AxisConfigurationImpl) configContext.getAxisConfiguration()).getModules();
226         req.getSession().setAttribute(Constants.MODULE_MAP, modules);
227         String JavaDoc moduleName = (String JavaDoc) req.getParameter("modules");
228         req.getSession().setAttribute(Constants.ENGAGE_STATUS, null);
229         if (moduleName != null) {
230             try {
231                 configContext.getAxisConfiguration().engageModule(new QName JavaDoc(moduleName));
232                 req.getSession().setAttribute(
233                         Constants.ENGAGE_STATUS,
234                         moduleName + " module engaged globally Successfully");
235             } catch (AxisFault axisFault) {
236                 req.getSession().setAttribute(Constants.ENGAGE_STATUS, axisFault.getMessage());
237             }
238         }
239         req.getSession().setAttribute("modules", null);
240         res.sendRedirect(ENGAGING_MODULE_GLOBALLY_JSP_NAME);
241     }
242
243     private void engageModulesToOpeartion(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
244             throws IOException JavaDoc {
245         HashMap JavaDoc modules =
246                 ((AxisConfigurationImpl) configContext.getAxisConfiguration()).getModules();
247         req.getSession().setAttribute(Constants.MODULE_MAP, modules);
248         String JavaDoc moduleName = (String JavaDoc) req.getParameter("modules");
249
250         req.getSession().setAttribute(Constants.ENGAGE_STATUS, null);
251         req.getSession().setAttribute("modules", null);
252
253         String JavaDoc serviceName = (String JavaDoc) req.getParameter("service");
254         if (serviceName != null) {
255             req.getSession().setAttribute("service", serviceName);
256         } else {
257             serviceName = (String JavaDoc) req.getSession().getAttribute("service");
258         }
259         req.getSession().setAttribute(
260                 Constants.OPEARTION_MAP,
261                 configContext
262                 .getAxisConfiguration()
263                 .getService(new QName JavaDoc(serviceName))
264                 .getOperations());
265         req.getSession().setAttribute(Constants.ENGAGE_STATUS, null);
266         String JavaDoc operationName = (String JavaDoc) req.getParameter("operation");
267         if (serviceName != null && moduleName != null && operationName != null) {
268             try {
269                 OperationDescription od =
270                         configContext.getAxisConfiguration().getService(
271                                 new QName JavaDoc(serviceName)).getOperation(
272                                         new QName JavaDoc(operationName));
273                 od.engageModule(
274                         configContext.getAxisConfiguration().getModule(new QName JavaDoc(moduleName)));
275                 req.getSession().setAttribute(
276                         Constants.ENGAGE_STATUS,
277                         moduleName + " module engaged to the operation Successfully");
278             } catch (AxisFault axisFault) {
279                 req.getSession().setAttribute(Constants.ENGAGE_STATUS, axisFault.getMessage());
280             }
281         }
282         req.getSession().setAttribute("operation", null);
283         res.sendRedirect(ENGAGE_TO_OPERATION_JSP_NAME);
284     }
285     private void engageModulesToService(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
286             throws IOException JavaDoc {
287         HashMap JavaDoc modules =
288                 ((AxisConfigurationImpl) configContext.getAxisConfiguration()).getModules();
289         req.getSession().setAttribute(Constants.MODULE_MAP, modules);
290         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
291         req.getSession().setAttribute(Constants.SERVICE_MAP, services);
292         String JavaDoc moduleName = (String JavaDoc) req.getParameter("modules");
293         req.getSession().setAttribute(Constants.ENGAGE_STATUS, null);
294         req.getSession().setAttribute("modules", null);
295         String JavaDoc serviceName = (String JavaDoc) req.getParameter("service");
296         req.getSession().setAttribute(Constants.ENGAGE_STATUS, null);
297         if (serviceName != null && moduleName != null) {
298             try {
299
300                 configContext.getAxisConfiguration().getService(
301                         new QName JavaDoc(serviceName)).engageModule(
302                                 configContext.getAxisConfiguration().getModule(new QName JavaDoc(moduleName)));
303                 req.getSession().setAttribute(
304                         Constants.ENGAGE_STATUS,
305                         moduleName + " module engaged to the service Successfully");
306             } catch (AxisFault axisFault) {
307                 req.getSession().setAttribute(Constants.ENGAGE_STATUS, axisFault.getMessage());
308             }
309         }
310         req.getSession().setAttribute("service", null);
311         res.sendRedirect(ENGAGING_MODULE_TO_SERVICE_JSP_NAME);
312     }
313
314     private void listGloballyModules(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
315             throws IOException JavaDoc {
316         Collection JavaDoc modules =
317                 ((AxisConfigurationImpl) configContext.getAxisConfiguration()).getEngadgedModules();
318         req.getSession().setAttribute(Constants.MODULE_MAP, modules);
319         res.sendRedirect(LIST_GLOABLLY_ENGAGED_MODULES_JSP_NAME);
320     }
321
322     private void lsitServiceformodules(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
323             throws IOException JavaDoc {
324         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
325         req.getSession().setAttribute(Constants.SERVICE_MAP, services);
326         req.getSession().setAttribute(Constants.MODULE_ENGAMENT, "Yes");
327         res.sendRedirect(SELECT_SERVICE_JSP_NAME);
328     }
329
330     private void viewGlobalHandlers(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
331             throws IOException JavaDoc {
332         req.getSession().setAttribute(
333                 Constants.GLOBAL_HANDLERS,
334                 configContext.getAxisConfiguration());
335         res.sendRedirect(VIEW_GLOBAL_HANDLERS_JSP_NAME);
336     }
337
338     private void viewServiceHandlers(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
339             throws IOException JavaDoc {
340         String JavaDoc service = (String JavaDoc) req.getParameter("service");
341         if (service != null) {
342             req.getSession().setAttribute(
343                     Constants.SERVICE_HANDLERS,
344                     configContext.getAxisConfiguration().getService(new QName JavaDoc(service)));
345         }
346         res.sendRedirect(VIEW_SERVICE_HANDLERS_JSP_NAME);
347     }
348
349     private void listPhases(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc {
350         ArrayList JavaDoc phaselist = new ArrayList JavaDoc();
351         DeploymentData depdata = DeploymentData.getInstance();
352         phaselist.add(depdata.getINPhases());
353         phaselist.add(depdata.getIN_FaultPhases());
354         phaselist.add(depdata.getOUTPhases());
355         phaselist.add(depdata.getOUT_FaultPhases());
356
357         phaselist.add(depdata.getOperationInPhases());
358         phaselist.add(depdata.getOperationInFaultPhases());
359         phaselist.add(depdata.getOperationOutPhases());
360         phaselist.add(depdata.getOperationOutFaultPhases());
361
362         req.getSession().setAttribute(Constants.PHASE_LIST, phaselist);
363         res.sendRedirect(LIST_PHASES_JSP_NAME);
364     }
365
366     /**
367      * Method listService
368      *
369      * @param req
370      * @param res
371      * @param filePart
372      * @throws IOException
373      */

374     private void listService(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res, String JavaDoc filePart)
375             throws IOException JavaDoc {
376         String JavaDoc serviceName = filePart.substring(filePart.lastIndexOf("/") + 1, filePart.length());
377         HashMap JavaDoc services = configContext.getAxisConfiguration().getServices();
378         String JavaDoc wsdl = req.getParameter("wsdl");
379         if ((services != null) && !services.isEmpty()) {
380             Object JavaDoc serviceObj = services.get(new QName JavaDoc(serviceName));
381             if (serviceObj != null) {
382                 if(wsdl != null){
383                     // StringWriter writer = new StringWriter();
384
res.setContentType("text/xml");
385                     PrintWriter JavaDoc out_writer = new PrintWriter JavaDoc(out);
386                     ((ServiceDescription)serviceObj).printWSDL(out_writer,filePart);
387                     // String wsdl_value = writer.toString().trim() ;
388
// if(wsdl_value == null || wsdl_value.trim().equals("")){
389
// wsdl_value = "WSDL is not available!!!";
390
// }
391

392                     // req.getSession().setAttribute(Constants.WSDL_CONTENT, wsdl_value);
393
// PrintWriter out_writer = new PrintWriter(out);
394
// System.out.println("wsdl_value = " + wsdl_value);
395
// out_writer.write(wsdl_value);
396
out.flush();
397                     out.close();
398                     wsdl = null;
399                     return;
400                 } else {
401                     req.getSession().setAttribute(Constants.SINGLE_SERVICE, serviceObj);
402                 }
403             }
404         }
405         String JavaDoc URI = req.getRequestURI();
406         URI = URI.substring(0, URI.indexOf("services"));
407         res.sendRedirect(URI + LIST_SINGLE_SERVICE_JSP_NAME);
408     }
409
410 }
411
Popular Tags