001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JOnASJaxRpcClientServlet.java 7546 2005-10-20 15:00:56Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.wssample.servlets.wsclient;
025:
026: import java.io.IOException;
027: import java.io.PrintWriter;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.xml.rpc.ServiceException;
038: import javax.xml.rpc.Stub;
039:
040: import org.objectweb.wssample.gen.jaxrpc.JaxRpcEndpointInterface;
041: import org.objectweb.wssample.gen.jaxrpc.JaxRpcEndpointInterfaceService;
042:
043: /**
044: * @author Guillaume Sauthier
045: */
046: public class JOnASJaxRpcClientServlet extends HttpServlet {
047:
048: /**
049: * Initializes the servlet.
050: * @param config ServletConfig
051: * @throws ServletException thrown by super.init(config);
052: */
053: public void init(ServletConfig config) throws ServletException {
054: super .init(config);
055: }
056:
057: /**
058: * Destroys the servlet.
059: */
060: public void destroy() {
061:
062: }
063:
064: /**
065: * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
066: * methods.
067: *
068: * @param request servlet request
069: * @param response servlet response
070: *
071: * @throws ServletException default servlet exception thrown
072: * @throws IOException servlet exception thrown
073: */
074: protected void processRequest(HttpServletRequest request,
075: HttpServletResponse response) throws ServletException,
076: IOException {
077: response.setContentType("text/html");
078: PrintWriter out = response.getWriter();
079:
080: out.println("<html>");
081: out.println("<head>");
082: out.println("<title>JOnAS JaxRpc WebService Test Page</title>");
083: out
084: .println("<style type=\"text/css\"> @import \"style/ow_jonas.css\"; </style>");
085: out.println("</head>");
086: out.println("<body class=\"bodywelcome\">");
087:
088: out.println("<div class=\"logos\">");
089: out
090: .println("<a href=\"http://jakarta.apache.org\"><img title=\"Jakarta Tomcat\" alt=\"Jakarta Tomcat\" src=\"images/tomcat.gif\" /></a>");
091: out
092: .println("<a href=\"http://jetty.mortbay.org/jetty/\"><img title=\"Mortbay Jetty\" alt=\"Mortbay Jetty\" src=\"images/jetty.gif\" /></a>");
093: out
094: .println("<a href=\"http://jonas.objectweb.org\"><img title=\"JOnAS WebSite\" alt=\"JOnAS WebSite\" src=\"images/ow_jonas_logo.gif\" /></a>");
095: out.println("</div>");
096:
097: // do we need to use the encrypted endpoint or not ?
098: boolean encrypt = false;
099: if ("true".equalsIgnoreCase(request.getParameter("encrypt"))) {
100: encrypt = true;
101: }
102:
103: try {
104:
105: // Get the service instance
106: //---------------------------------------------
107: Context ctx = new InitialContext();
108: JaxRpcEndpointInterfaceService service = null;
109: JaxRpcEndpointInterface port = null;
110:
111: // We can use the same service because the 2 ws use the same interface
112: // Get the port where execute methods
113:
114: if (encrypt) {
115: service = (JaxRpcEndpointInterfaceService) ctx
116: .lookup("java:comp/env/service/crypted-jaxrpc");
117: //---------------------------------------------
118: port = service.getEncryptedJaxRpcEndpoint();
119: } else {
120: service = (JaxRpcEndpointInterfaceService) ctx
121: .lookup("java:comp/env/service/jaxrpc");
122: //---------------------------------------------
123: port = service.getJaxRpcEndpoint1();
124: }
125:
126: // Use the context username for WSS4JHandler
127: ((Stub) port)._setProperty("user", request
128: .getUserPrincipal().getName());
129:
130: // Execute Methods :
131: String hello = port.sayHello(request.getParameter("name"));
132: int cotes = port.getCotes();
133:
134: if (encrypt) {
135: out
136: .println("<div class=\"titlepage\">Results With Encryption</div>");
137: } else {
138: out.println("<div class=\"titlepage\">Results</div>");
139: }
140: out.println("<div class=\"links\">");
141:
142: // Display results :
143: out
144: .println("Working with URL : <i>"
145: + ((Stub) port)
146: ._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY)
147: + "</i><br/>");
148: out.println("<b>result of sayHello(name) method :</b><i>"
149: + hello + "</i><br/>");
150: out.println("<b>result of getCotes() method :</b><i>"
151: + cotes + "</i><br/>");
152:
153: out.println("</div>");
154: } catch (ServiceException se) {
155: out
156: .println("<div class=\"titlepage\">Something goes wrong when calling the WebService<br/>");
157: se.printStackTrace(out);
158: out.println("</div>");
159: } catch (NamingException ne) {
160: out
161: .println("<div class=\"titlepage\">Exception while retrieving InitialContext<br/>");
162: ne.printStackTrace(out);
163: out.println("</div>");
164: }
165:
166: out.println("</body>");
167: out.println("</html>");
168:
169: out.close();
170: }
171:
172: /**
173: * Handles the HTTP <code>GET</code> method.
174: *
175: * @param request servlet request
176: * @param response servlet response
177: *
178: * @throws ServletException default servlet exception thrown
179: * @throws IOException default servlet exception thrown
180: */
181: protected void doGet(HttpServletRequest request,
182: HttpServletResponse response) throws ServletException,
183: IOException {
184: processRequest(request, response);
185: }
186:
187: /**
188: * Handles the HTTP <code>POST</code> method.
189: *
190: * @param request servlet request
191: * @param response servlet response
192: *
193: * @throws ServletException default servlet exception thrown
194: * @throws IOException default servlet exception thrown
195: */
196: protected void doPost(HttpServletRequest request,
197: HttpServletResponse response) throws ServletException,
198: IOException {
199: processRequest(request, response);
200: }
201:
202: /**
203: * @return Returns a short description of the servlet.
204: */
205: public String getServletInfo() {
206: return "Short description";
207: }
208:
209: }
|