From: <plu...@us...> - 2003-11-04 04:10:49
|
Update of /cvsroot/portlet-opensrc/portlet-opensrc/jsr168/webcontent/googlesearch/com/plumtree/portlet/portlets In directory sc8-pr-cvs1:/tmp/cvs-serv14881/jsr168/webcontent/googlesearch/com/plumtree/portlet/portlets Added Files: GooglePortlet.java Log Message: new samples for google search and rss --- NEW FILE: GooglePortlet.java --- /* Copyright (c) 2003, Plumtree Software All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions is met: Neither the name of Plumtree Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.plumtree.portlet.portlets; import java.io.IOException; import java.io.PrintWriter; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.PortletMode; import javax.portlet.PortletSecurityException; import javax.portlet.PortletURL; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.UnavailableException; import javax.portlet.WindowState; import com.google.soap.search.GoogleSearch; import com.google.soap.search.GoogleSearchResult; import com.google.soap.search.GoogleSearchResultElement; /** * Portlet Demonstrating Google Search */ public class GooglePortlet extends GenericPortlet { /** Search Query */ private static final String SEARCH_QUERY = "searchQuery"; /** Start result- i.e. where to start */ private static final String START_RESULT = "startResult"; /** Key for google search key */ public static final String KEY = "KEY"; /**default content type*/ public static final String CONTENT_TYPE_HTML = "text/html;charset=UTF-8"; /** * @see javax.servlet.portlet.Portlet#processAction(javax.servlet.portlet.ActionRequest, * javax.servlet.portlet.ActionResponse) */ public void processAction( ActionRequest request, ActionResponse response) throws UnavailableException, PortletSecurityException, PortletException, IOException { String searchQuery = request.getParameter(GooglePortlet.SEARCH_QUERY); if (null != searchQuery) { response.setRenderParameter( GooglePortlet.SEARCH_QUERY, searchQuery); } String startResult = request.getParameter(GooglePortlet.START_RESULT); if (null != startResult) { response.setRenderParameter( GooglePortlet.START_RESULT, startResult); } } /** * Displays a generic help message and a back button * @see javax.servlet.portlet.GenericPortlet#doHelp(javax.servlet.portlet. * RenderRequest,javax.servlet.portlet.RenderRequest) */ protected void doHelp( RenderRequest request, RenderResponse response) throws UnavailableException, PortletSecurityException, PortletException, IOException { response.setContentType(CONTENT_TYPE_HTML); PrintWriter out = response.getWriter(); out.println("<table>"); out.println("<tr>"); out.println("<td>"); PortletURL renderURL = response.createRenderURL(); renderURL.setPortletMode(PortletMode.VIEW); out.println( "The Google Portlet searches Google, using the specified query.<BR/> The number of results returned at a time is ten- this is a limitation of the Google API<BR/> To change the key, or adust safe search, contact your administrator.<BR/> <a href=\"" + renderURL.toString() + "\">back</a>"); out.println("</td>"); out.println("</tr>"); out.println("</form>"); out.println("</table>"); } /** * @see javax.servlet.portlet.GenericPortlet#doView(javax.servlet.portlet.RenderRequest,javax.servlet.portlet.RenderRequest) */ protected void doView( RenderRequest request, RenderResponse response) throws UnavailableException, PortletSecurityException, PortletException, IOException { try { String searchQuery = request.getParameter(GooglePortlet.SEARCH_QUERY); String startResult = request.getParameter(GooglePortlet.START_RESULT); int totalResults = 0; response.setContentType(CONTENT_TYPE_HTML); PrintWriter out = response.getWriter(); String key = this.getInitParameter("key"); if ((null == key) || key.equals("")) { out.println( "Key for search is not available. Check with your administrator to obtain the Google API Key"); return; } //js function to open windows StringBuffer buff = new StringBuffer(200); buff.append( "<SCRIPT language=\"Javascript\">function openGoogleWin(href){"); buff.append("var newWin = window.open(href);"); buff.append("}</SCRIPT>"); out.print(buff.toString()); //make a button to go into max mode //see if we want to use slightly different code or richer content? WindowState state = request.getWindowState(); WindowState otherState = null; if (state.equals(WindowState.MAXIMIZED)) { otherState = WindowState.NORMAL; } else { otherState = WindowState.MAXIMIZED; } PortletURL otherUrl = response.createRenderURL(); if (!state.equals(WindowState.MAXIMIZED)) { otherUrl.setWindowState(otherState); out.println( "<a href=\"" + otherUrl.toString() + "\">" + otherState.toString() + "</a>"); } GoogleSearchResult result = null; GoogleSearchResultElement[] elements = null; if (null != searchQuery) { GoogleSearch googleSearch = new GoogleSearch(); googleSearch.setKey(key); googleSearch.setQueryString(searchQuery); if (null != startResult) { int start = Integer.parseInt(startResult); googleSearch.setStartResult(start); } result = googleSearch.doSearch(); elements = result.getResultElements(); } PortletURL actionURL = response.createActionURL(); actionURL.setWindowState(state); out.println("<table>"); out.println( "<form name=\"inputForm\" target=\"_self\" method=\"POST\" action=\"" + actionURL.toString() + "\">"); out.println("<tr>"); out.println("<td>"); PortletURL renderLink = response.createRenderURL(); renderLink.setPortletMode(PortletMode.HELP); out.println("<a href=\"" + renderLink + "\">help</a>"); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>"); out.println(" "); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>"); out.println( "<input type=\"text\" name=\"" + GooglePortlet.SEARCH_QUERY + "\" value=\"\">"); out.println( "<input name=\"inputSubmit\" type=\"submit\" value=\"submit\">"); out.println("</td>"); out.println("</tr>"); out.println("</form>"); out.println("</table>"); if (null != searchQuery) { out.println("<table>"); out.println("<tr>"); out.println("<td>"); totalResults = result.getEstimatedTotalResultsCount(); out.println("# of results is " + totalResults); out.println("</td>"); out.println("</tr>"); for (int i = 0; i < elements.length; i++) { GoogleSearchResultElement element = elements[i]; out.println("<tr>"); out.println("<td>"); out.println( "<a href=\"javascript:openGoogleWin('" + element.getURL() + "');\">" + element.getTitle() + "</a>"); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>"); out.println(element.getSummary()); out.println("</td>"); out.println("</tr>"); } out.println("<tr>"); out.println("<td>"); out.println(" "); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>"); out.println(" "); out.println("</td>"); out.println("</tr>"); if (totalResults > 0) { printLinks( response, out, totalResults, result.getStartIndex(), searchQuery, state); } out.println("</table>"); } } catch (Exception e) { throw new PortletException(e); } } /** * Prints the links 1...10 at the bottom of the portlet * * @param response RenderResponse * @param out PrintWriter * @param results total number of results * @param currentPos which page we are on * @param searchQuery query * @param windowState current window state */ private void printLinks( RenderResponse response, PrintWriter out, int results, int currentPos, String searchQuery, WindowState windowState) { int total = 10; total = ((results > 100) ? total : (results / 10)); if (currentPos != 1) { currentPos = (currentPos - 1) / 10; } out.println("<tr>"); out.println("<td>"); for (int i = 1; i <= total; i++) { if (i == currentPos) { out.print(i); } else { PortletURL actionURL = response.createActionURL(); try { actionURL.setWindowState(windowState); } catch (Exception e) { //ignore } actionURL.setParameter( GooglePortlet.SEARCH_QUERY, searchQuery); actionURL.setParameter( GooglePortlet.START_RESULT, "" + (i * 10)); out.print("<a href=\""); out.print(actionURL.toString()); out.print("\">"); out.print(i); out.println("</a>"); } out.print(" "); } out.println("</td>"); out.println("</tr>"); } } |