Author: chris.laprun(a)jboss.com
Date: 2008-01-29 19:12:37 -0500 (Tue, 29 Jan 2008)
New Revision: 9641
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleClippingPortlet.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleWeatherClippingPortlet.java
Log:
- Added simple map and weather portlets using Google search results and clipping to be
used as samples. Need to package them and make them use public render params.
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleClippingPortlet.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleClippingPortlet.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleClippingPortlet.java 2008-01-30
00:12:37 UTC (rev 9641)
@@ -0,0 +1,170 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.simple.samples;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletPreferences;
+import javax.portlet.PortletURL;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * A simple portlet using Google's search services to extract interesting information
(weather, map, ...) from first
+ * result.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public abstract class GoogleClippingPortlet extends GenericPortlet
+{
+ protected static final String A = "<a";
+ private static final String A_TARGET_BLANK = "<a
target='_blank'";
+ private static final String ZIP = "zip";
+ private static final String SAN_FRAN = "94102";
+ private static final String GOOGLE = "http://www.google.com/search?q=";
+
+ @Override
+ protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
+ {
+ String zip = renderRequest.getParameter(ZIP);
+ if (zip == null)
+ {
+ zip = renderRequest.getPreferences().getValue(ZIP, SAN_FRAN);
+ }
+
+ String query = getQueryString(zip);
+
+ URL url = new URL(query);
+
+ URLConnection connection = url.openConnection();
+ connection.setRequestProperty("User-Agent", "Mozilla/5.0");
+
+ InputStream in = new BufferedInputStream(connection.getInputStream());
+ StringBuilder tmp = new StringBuilder(8192);
+ byte[] buffer = new byte[4096];
+ while (true)
+ {
+ int i = in.read(buffer);
+ if (i == 0)
+ {
+ continue;
+ }
+ if (i == -1)
+ {
+ break;
+ }
+ tmp.append(new String(buffer, "UTF-8"), 0, i);
+ }
+
+ String html = tmp.toString();
+ String beg = "<div class=e>";
+ String end = "</table>";
+ int begIndex = html.indexOf(beg);
+ if (begIndex != -1)
+ {
+ // extract table containing specific first result
+ int endIndex = html.indexOf(end, begIndex);
+ html = html.substring(begIndex + beg.length(), endIndex + end.length());
+
+ html = postProcessHTML(html);
+
+ renderResponse.setContentType("text/html");
+ PrintWriter printWriter = renderResponse.getWriter();
+ printWriter.print(html);
+
+ PortletURL edit = renderResponse.createRenderURL();
+ edit.setPortletMode(PortletMode.EDIT);
+ printWriter.print("<p><a href='" + edit +
"'>Change location</a></p>");
+ }
+ }
+
+ @Override
+ protected void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
+ {
+ renderResponse.setContentType("text/html");
+ renderResponse.getWriter().print(
+ "<div align='center'>\n" +
+ " <br/>\n" +
+ " <font class='portlet-font'>Change
Location:</font>\n" +
+ "\n" +
+ " <form method='post' action='" +
renderResponse.createActionURL() + "'\n" +
+ " <font class='portlet-font'>Zip
Code:</font><br/>\n" +
+ " <input class='portlet-form-input-field'
type='text' value='' size='12' name='zip'>\n" +
+ " <br/>\n" +
+ " <input class='portlet-form-input-field'
type='submit' name='submit' value='submit'>\n" +
+ " </form>\n" +
+ "</div>");
+ }
+
+ @Override
+ public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
throws PortletException, IOException
+ {
+ String zip = actionRequest.getParameter(ZIP);
+
+ if (null != zip)
+ {
+ PortletPreferences prefs = actionRequest.getPreferences();
+ prefs.setValue(ZIP, zip);
+ prefs.store();
+ }
+
+ // set zip as render parameter
+ actionResponse.setRenderParameter(ZIP, zip);
+
+ // request view
+ actionResponse.setPortletMode(PortletMode.VIEW);
+ }
+
+ protected String postProcessHTML(String html)
+ {
+ String tmp = new String(html);
+
+ // links should open in new windows
+ tmp = tmp.replaceAll(A, A_TARGET_BLANK);
+
+ // src attributes should be absolute
+ tmp = tmp.replaceAll("src=/", "src=http://google.com/");
+ tmp = tmp.replaceAll("src=\"/",
"src=\"http://google.com/");
+
+ // forms should open in new windows and have an absolute action URL
+ tmp = tmp.replaceAll("action=\"/", "target='_blank'
action=\"http://google.com/");
+
+ return tmp;
+ }
+
+ protected String getQueryString(String zip)
+ {
+ return GOOGLE + zip;
+ }
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleClippingPortlet.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleWeatherClippingPortlet.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleWeatherClippingPortlet.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleWeatherClippingPortlet.java 2008-01-30
00:12:37 UTC (rev 9641)
@@ -0,0 +1,57 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+
+package org.jboss.portal.simple.samples;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class GoogleWeatherClippingPortlet extends GoogleClippingPortlet
+{
+ private final static String WEATHER_URL =
"http://www.google.com/search?q=weather+";
+ private static final String A_END = "</a";
+
+ protected String getQueryString(String zip)
+ {
+ return WEATHER_URL + zip;
+ }
+
+ protected String postProcessHTML(String html)
+ {
+ int begIndex;
+ int endIndex;
+
+ String tmp = new String(html);
+
+ // remove add to iGoogle link
+ begIndex = html.indexOf(A);
+ if (begIndex != -1)
+ {
+ endIndex = html.indexOf(A_END, begIndex);
+ tmp = tmp.substring(0, begIndex) + html.substring(endIndex);
+ }
+
+ return tmp;
+ }
+}
Property changes on:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/simple/samples/GoogleWeatherClippingPortlet.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native