KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > samples > OrganizationClientServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.samples;
23
24 import org.jboss.logging.Logger;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.xml.rpc.Service JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35
36 /**
37  * An example of a webservice client servlet
38  *
39  * @author Thomas.Diesler@jboss.org
40  * @since 26-Apr-2004
41  */

42 public class OrganizationClientServlet extends HttpServlet JavaDoc
43 {
44    // provide logging
45
private static final Logger log = Logger.getLogger(OrganizationClientServlet.class);
46
47    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc
48    {
49       String JavaDoc info = null;
50
51       String JavaDoc endpoint = req.getParameter("endpoint");
52       String JavaDoc method = req.getParameter("method");
53       String JavaDoc organization = req.getParameter("organization");
54       if ("EJB".equals(endpoint))
55       {
56          if ("info".equals(method))
57             info = getContactInfoEJB(organization);
58       }
59       if ("JSE".equals(endpoint))
60       {
61          if ("info".equals(method))
62             info = getContactInfoJSE(organization);
63       }
64
65       PrintWriter JavaDoc out = res.getWriter();
66       out.print(info);
67       out.close();
68    }
69
70    /** Get the contact info from the EJB service */
71    public String JavaDoc getContactInfoEJB(String JavaDoc organization) throws ServletException JavaDoc
72    {
73       Service JavaDoc service = null;
74       try
75       {
76          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
77          service = (Service JavaDoc)iniCtx.lookup("java:/comp/env/service/OrganizationServiceEJB");
78          Organization endpoint = (Organization)service.getPort(Organization.class);
79          String JavaDoc info = endpoint.getContactInfo(organization);
80          return info;
81       }
82       catch (NamingException JavaDoc e)
83       {
84          throw new ServletException JavaDoc(e);
85       }
86       catch (Exception JavaDoc e)
87       {
88          throw new ServletException JavaDoc("Cannot invoke webservice", e);
89       }
90    }
91
92    /** Get the contact info from the JSE service */
93    public String JavaDoc getContactInfoJSE(String JavaDoc organization) throws ServletException JavaDoc
94    {
95       try
96       {
97          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
98          OrganizationService service = (OrganizationService)iniCtx.lookup("java:comp/env/service/OrganizationServiceJSE");
99          Organization endpoint = (Organization)service.getOrganizationPort();
100          String JavaDoc info = endpoint.getContactInfo(organization);
101          return info;
102       }
103       catch (NamingException JavaDoc e)
104       {
105          throw new ServletException JavaDoc(e);
106       }
107       catch (Exception JavaDoc e)
108       {
109          throw new ServletException JavaDoc("Cannot invoke webservice", e);
110       }
111    }
112 }
113
Popular Tags