1 25 package org.objectweb.wssample.servlets.wsclient; 26 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.xml.rpc.ServiceException ; 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 54 public class WsClientServlet extends HttpServlet { 55 56 59 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 60 61 66 public void init(ServletConfig config) throws ServletException { 67 super.init(config); 68 } 69 70 73 public void destroy() { 74 75 } 76 77 87 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException , 88 IOException { 89 90 response.setContentType("text/html"); 91 PrintWriter out = response.getWriter(); 92 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 try { 108 Context ctx = new InitialContext (); 110 111 String key = (String ) ctx.lookup("java:comp/env/key"); 112 113 Object s = ctx.lookup("java:comp/env/service/google"); 115 GoogleSearchService google = (GoogleSearchService) s; 116 117 GoogleSearchPort search = google.getGoogleSearchPort(); 119 120 GoogleSearchResult result = search.doGoogleSearch(key, request.getParameter("search"), 0, 10, false, null, false, null, null, null ); 131 132 out.println("<div class=\"titlepage\">Results</div>"); 133 out.println("<div class=\"links\">"); 134 135 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 ne) { 144 out.println("<div class=\"titlepage\">Error when looking for service-ref</div>"); 145 ne.printStackTrace(out); 146 } catch (ServiceException 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 165 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 166 processRequest(request, response); 167 } 168 169 178 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , 179 IOException { 180 processRequest(request, response); 181 } 182 183 186 public String getServletInfo() { 187 return "Servlet which is client of the WebServices API of Google"; 188 } 189 190 } | Popular Tags |