KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > wssample > servlets > wsclient > WsClientServlet


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: WsClientServlet.java,v 1.4 2004/12/20 11:07:52 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.wssample.servlets.wsclient;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.xml.rpc.ServiceException JavaDoc;
39
40 import org.objectweb.jonas.common.Log;
41
42 import org.objectweb.util.monolog.api.Logger;
43
44 import org.objectweb.wssample.gen.google.GoogleSearchService;
45 import org.objectweb.wssample.gen.google.GoogleSearchPort;
46 import org.objectweb.wssample.gen.google.GoogleSearchResult;
47 import org.objectweb.wssample.gen.google.ResultElement;
48
49 /**
50  * WebServices Client Servlet
51  *
52  * @author Guillaume Sauthier
53  */

54 public class WsClientServlet extends HttpServlet JavaDoc {
55
56     /**
57      * logger
58      */

59     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
60
61     /**
62      * Initializes the servlet.
63      * @param config ServletConfig
64      * @throws ServletException if super.init(config); fails
65      */

66     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
67         super.init(config);
68     }
69
70     /**
71      * Destroys the servlet.
72      */

73     public void destroy() {
74
75     }
76
77     /**
78      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
79      * methods.
80      *
81      * @param request servlet request
82      * @param response servlet response
83      *
84      * @throws ServletException default servlet exception thrown
85      * @throws IOException default servlet exception thrown
86      */

87     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
88             IOException JavaDoc {
89
90         response.setContentType("text/html");
91         PrintWriter JavaDoc out = response.getWriter();
92         // output your page here
93
out.println("<html>");
94         out.println("<head>");
95         out.println("<title>Google WebService Response</title>");
96         out.println("<style type=\"text/css\"> @import \"style/ow_jonas.css\"; </style>");
97         out.println("</head>");
98         out.println("<body class=\"bodywelcome\">");
99
100         out.println("<div class=\"logos\">");
101         out.println("<a HREF=\"http://jakarta.apache.org\"><img title=\"Jakarta Tomcat\" alt=\"Jakarta Tomcat\" SRC=\"images/tomcat.gif\" /></a>");
102         out.println("<a HREF=\"http://jetty.mortbay.org/jetty/\"><img title=\"Mortbay Jetty\" alt=\"Mortbay Jetty\" SRC=\"images/jetty.gif\" /></a>");
103         out.println("<a HREF=\"http://jonas.objectweb.org\"><img title=\"JOnAS WebSite\" alt=\"JOnAS WebSite\" SRC=\"images/ow_jonas_logo.gif\" /></a>");
104         out.println("</div>");
105
106         // Get and Display results
107
try {
108             // Get the Context
109
Context JavaDoc ctx = new InitialContext JavaDoc();
110
111             String JavaDoc key = (String JavaDoc) ctx.lookup("java:comp/env/key");
112
113             // Lookup the binded service
114
Object JavaDoc s = ctx.lookup("java:comp/env/service/google");
115             GoogleSearchService google = (GoogleSearchService) s;
116
117             // get the Port object
118
GoogleSearchPort search = google.getGoogleSearchPort();
119
120             // execute the query
121
GoogleSearchResult result = search.doGoogleSearch(key, request.getParameter("search"), 0, // first
122
// index
123
10, // maxresult
124
false, //filter
125
null, //restrict
126
false, // safesearch
127
null, //lr
128
null, //ie
129
null //oe
130
);
131             
132             out.println("<div class=\"titlepage\">Results</div>");
133             out.println("<div class=\"links\">");
134
135             // Display the results
136
ResultElement[] elements = result.getResultElements();
137             for (int i = 0; i < elements.length; i++) {
138                 out.println("<i><a HREF=\"" + elements[i].getURL() + "\">" + elements[i].getTitle() + "</a></i><br/>");
139                 out.println(elements[i].getSummary() + "<br/><br/>");
140             }
141             out.println("</div>");
142
143         } catch (NamingException JavaDoc ne) {
144             out.println("<div class=\"titlepage\">Error when looking for service-ref</div>");
145             ne.printStackTrace(out);
146         } catch (ServiceException JavaDoc se) {
147             out.println("<div class=\"titlepage\">Error when performign service-ref</div>");
148             se.printStackTrace(out);
149         }
150
151         out.println("</body>");
152         out.println("</html>");
153         out.close();
154     }
155
156     /**
157      * Handles the HTTP <code>GET</code> method.
158      *
159      * @param request servlet request
160      * @param response servlet response
161      *
162      * @throws ServletException default servlet exception thrown
163      * @throws IOException default servlet exception thrown
164      */

165     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
166         processRequest(request, response);
167     }
168
169     /**
170      * Handles the HTTP <code>POST</code> method.
171      *
172      * @param request servlet request
173      * @param response servlet response
174      *
175      * @throws ServletException default servlet exception thrown
176      * @throws IOException default servlet exception thrown
177      */

178     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
179             IOException JavaDoc {
180         processRequest(request, response);
181     }
182
183     /**
184      * @return Returns a short description of the servlet.
185      */

186     public String JavaDoc getServletInfo() {
187         return "Servlet which is client of the WebServices API of Google";
188     }
189
190 }
Popular Tags