001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: WsClientServlet.java 6004 2004-12-20 11:07:52Z 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:
039: import org.objectweb.jonas.common.Log;
040:
041: import org.objectweb.util.monolog.api.Logger;
042:
043: import org.objectweb.wssample.gen.google.GoogleSearchService;
044: import org.objectweb.wssample.gen.google.GoogleSearchPort;
045: import org.objectweb.wssample.gen.google.GoogleSearchResult;
046: import org.objectweb.wssample.gen.google.ResultElement;
047:
048: /**
049: * WebServices Client Servlet
050: *
051: * @author Guillaume Sauthier
052: */
053: public class WsClientServlet extends HttpServlet {
054:
055: /**
056: * logger
057: */
058: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
059:
060: /**
061: * Initializes the servlet.
062: * @param config ServletConfig
063: * @throws ServletException if super.init(config); fails
064: */
065: public void init(ServletConfig config) throws ServletException {
066: super .init(config);
067: }
068:
069: /**
070: * Destroys the servlet.
071: */
072: public void destroy() {
073:
074: }
075:
076: /**
077: * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
078: * methods.
079: *
080: * @param request servlet request
081: * @param response servlet response
082: *
083: * @throws ServletException default servlet exception thrown
084: * @throws IOException default servlet exception thrown
085: */
086: protected void processRequest(HttpServletRequest request,
087: HttpServletResponse response) throws ServletException,
088: IOException {
089:
090: response.setContentType("text/html");
091: PrintWriter out = response.getWriter();
092: // output your page here
093: out.println("<html>");
094: out.println("<head>");
095: out.println("<title>Google WebService Response</title>");
096: out
097: .println("<style type=\"text/css\"> @import \"style/ow_jonas.css\"; </style>");
098: out.println("</head>");
099: out.println("<body class=\"bodywelcome\">");
100:
101: out.println("<div class=\"logos\">");
102: out
103: .println("<a href=\"http://jakarta.apache.org\"><img title=\"Jakarta Tomcat\" alt=\"Jakarta Tomcat\" src=\"images/tomcat.gif\" /></a>");
104: out
105: .println("<a href=\"http://jetty.mortbay.org/jetty/\"><img title=\"Mortbay Jetty\" alt=\"Mortbay Jetty\" src=\"images/jetty.gif\" /></a>");
106: out
107: .println("<a href=\"http://jonas.objectweb.org\"><img title=\"JOnAS WebSite\" alt=\"JOnAS WebSite\" src=\"images/ow_jonas_logo.gif\" /></a>");
108: out.println("</div>");
109:
110: // Get and Display results
111: try {
112: // Get the Context
113: Context ctx = new InitialContext();
114:
115: String key = (String) ctx.lookup("java:comp/env/key");
116:
117: // Lookup the binded service
118: Object s = ctx.lookup("java:comp/env/service/google");
119: GoogleSearchService google = (GoogleSearchService) s;
120:
121: // get the Port object
122: GoogleSearchPort search = google.getGoogleSearchPort();
123:
124: // execute the query
125: GoogleSearchResult result = search.doGoogleSearch(key,
126: request.getParameter("search"), 0, // first
127: // index
128: 10, // maxresult
129: false, //filter
130: null, //restrict
131: false, // safesearch
132: null, //lr
133: null, //ie
134: null //oe
135: );
136:
137: out.println("<div class=\"titlepage\">Results</div>");
138: out.println("<div class=\"links\">");
139:
140: // Display the results
141: ResultElement[] elements = result.getResultElements();
142: for (int i = 0; i < elements.length; i++) {
143: out.println("<i><a href=\"" + elements[i].getURL()
144: + "\">" + elements[i].getTitle()
145: + "</a></i><br/>");
146: out.println(elements[i].getSummary() + "<br/><br/>");
147: }
148: out.println("</div>");
149:
150: } catch (NamingException ne) {
151: out
152: .println("<div class=\"titlepage\">Error when looking for service-ref</div>");
153: ne.printStackTrace(out);
154: } catch (ServiceException se) {
155: out
156: .println("<div class=\"titlepage\">Error when performign service-ref</div>");
157: se.printStackTrace(out);
158: }
159:
160: out.println("</body>");
161: out.println("</html>");
162: out.close();
163: }
164:
165: /**
166: * Handles the HTTP <code>GET</code> method.
167: *
168: * @param request servlet request
169: * @param response servlet response
170: *
171: * @throws ServletException default servlet exception thrown
172: * @throws IOException default servlet exception thrown
173: */
174: protected void doGet(HttpServletRequest request,
175: HttpServletResponse response) throws ServletException,
176: IOException {
177: processRequest(request, response);
178: }
179:
180: /**
181: * Handles the HTTP <code>POST</code> method.
182: *
183: * @param request servlet request
184: * @param response servlet response
185: *
186: * @throws ServletException default servlet exception thrown
187: * @throws IOException default servlet exception thrown
188: */
189: protected void doPost(HttpServletRequest request,
190: HttpServletResponse response) throws ServletException,
191: IOException {
192: processRequest(request, response);
193: }
194:
195: /**
196: * @return Returns a short description of the servlet.
197: */
198: public String getServletInfo() {
199: return "Servlet which is client of the WebServices API of Google";
200: }
201:
202: }
|