Author: chris.laprun(a)jboss.com
Date: 2008-08-06 09:41:24 -0400 (Wed, 06 Aug 2008)
New Revision: 11668
Modified:
modules/portlet/trunk/samples/src/main/artifacts/google-portlet-war/WEB-INF/portlet.xml
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java
Log:
- Make Google portlets more configurable so that we don't need to re-compile them each
time Google changes its code.
- Added extra information in error message.
- Portlets don't check for alternate version of query string anymore.
Modified:
modules/portlet/trunk/samples/src/main/artifacts/google-portlet-war/WEB-INF/portlet.xml
===================================================================
---
modules/portlet/trunk/samples/src/main/artifacts/google-portlet-war/WEB-INF/portlet.xml 2008-08-05
18:57:49 UTC (rev 11667)
+++
modules/portlet/trunk/samples/src/main/artifacts/google-portlet-war/WEB-INF/portlet.xml 2008-08-06
13:41:24 UTC (rev 11668)
@@ -41,11 +41,27 @@
<keywords>sample,map,google</keywords>
</portlet-info>
<portlet-preferences>
+ <!-- Default zip code -->
<preference>
<name>zipcode</name>
<value>94102</value>
<read-only>false</read-only>
</preference>
+ <preference>
+ <!-- Query used to retrieve data -->
+ <name>query</name>
+
<
value>http://www.google.com/search?gl=US&hl=en&q=</...
+ <read-only>false</read-only>
+ </preference>
+ <preference>
+ <!--
+ String used to identify beginning of interesting data: the portlet will
extract the first table it
+ finds after this String.
+ -->
+ <name>beginningString</name>
+ <value><![CDATA[<div id=res]]></value>
+ <read-only>false</read-only>
+ </preference>
</portlet-preferences>
<supported-public-render-parameter>zipcode</supported-public-render-parameter>
</portlet>
@@ -66,11 +82,27 @@
<keywords>sample,weather,google</keywords>
</portlet-info>
<portlet-preferences>
+ <!-- Default zip code -->
<preference>
<name>zipcode</name>
- <value>80201</value>
+ <value>94102</value>
<read-only>false</read-only>
</preference>
+ <preference>
+ <!-- Query used to retrieve data -->
+ <name>query</name>
+
<
value>http://www.google.com/search?gl=US&hl=en&q=weath...
+ <read-only>false</read-only>
+ </preference>
+ <preference>
+ <!--
+ String used to identify beginning of interesting data: the portlet will
extract the first table it
+ finds after this String.
+ -->
+ <name>beginningString</name>
+ <value><![CDATA[<div id=res]]></value>
+ <read-only>false</read-only>
+ </preference>
</portlet-preferences>
<supported-public-render-parameter>zipcode</supported-public-render-parameter>
</portlet>
@@ -79,11 +111,5 @@
<identifier>zipcode</identifier>
<qname
xmlns:g='urn:jboss:portal:simple:google'>g:zipcode</qname>
</public-render-parameter>
-
- <public-render-parameter>
- <identifier>zipcode</identifier>
- <qname
xmlns:g='urn:jboss:portal:simple:google'>g:zipcode</qname>
- </public-render-parameter>
-
</portlet-app>
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java 2008-08-05
18:57:49 UTC (rev 11667)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java 2008-08-06
13:41:24 UTC (rev 11668)
@@ -50,20 +50,27 @@
{
protected static final String A = "<a";
private static final String A_TARGET_BLANK = "<a
target='_blank'";
+
private static final String ZIP = "zipcode";
- private static final String SAN_FRAN = "94102";
+ private static final String BEGINNING_STRING = "beginningString";
+ private static final String QUERY = "query";
+
/**
* gl=US forces use of US google site, hl=en forces results to be in English so that
regardless of location the query
* should result in the expected result.
*/
- private static final String GOOGLE =
"http://www.google.com/search?gl=US&hl=en&q=";
+ private static final String DEFAULT_QUERY =
"http://www.google.com/search?gl=US&hl=en&q=";
+ private static final String DEFAULT_BEG = "<div
id=\"res\">";
+ private static final String DEFAULT_ZIP = "94102";
+ private static final String END_TABLE = "</table>";
+ private static final String BEG_TABLE = "<table";
@Override
protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
{
String zip = getZipCode(renderRequest);
- String query = getQueryString(zip);
+ String query = getQueryString(renderRequest, zip);
URL url = new URL(query);
@@ -74,25 +81,19 @@
String html = new String(getBytes(in, 16384), "UTF-8");
in.close();
- String beg = "<div id=\"res\"><div";
- String alternateBeg = "<div id=res><div";
- String end = "</table>";
+ String beg = getBegString(renderRequest);
int begIndex = html.indexOf(beg);
if (begIndex != -1)
{
- html = process(html, beg, end, begIndex);
+ html = process(html, begIndex + beg.length());
}
else
{
- begIndex = html.indexOf(alternateBeg);
- if (begIndex != -1)
- {
- html = process(html, alternateBeg, end, begIndex);
- }
- else
- {
- html = "<font class='portlet-font'>Couldn't retrieve
result from Google. Sorry! :(</font>";
- }
+ html = "<p class='portlet-font'>Couldn't retrieve result
from Google. Check that '"
+ + beg.replaceAll("<", "<") + "'
(value of the <code>beginningString</code> portlet preference) is "
+ + "contained in the result of issued query ('" + query
+ + "', based on the value of the <code>query</code>
portlet preference). You can adjust these values in "
+ + "<code>portlet.xml</code> to tweak this portlet
output...</p>";
}
renderResponse.setContentType("text/html");
@@ -100,14 +101,17 @@
printWriter.print(html);
}
- private String process(String html, String beg, String end, int begIndex) throws
IOException
+ private String getBegString(RenderRequest renderRequest)
{
+ return renderRequest.getPreferences().getValue(BEGINNING_STRING, DEFAULT_BEG);
+ }
+
+ private String process(String html, int begIndex) throws IOException
+ {
// extract table containing specific first result
- int endIndex = html.indexOf(end, begIndex);
- html = html.substring(begIndex + beg.length(), endIndex + end.length());
-
- html = html.substring(html.indexOf(">") + 1);
-
+ int tableIndex = html.indexOf(BEG_TABLE, begIndex);
+ int endIndex = html.indexOf(END_TABLE, tableIndex);
+ html = html.substring(tableIndex, endIndex + END_TABLE.length());
html = postProcessHTML(html);
return html;
@@ -118,7 +122,7 @@
String zip = renderRequest.getParameter(ZIP);
if (zip == null)
{
- zip = renderRequest.getPreferences().getValue(ZIP, SAN_FRAN);
+ zip = renderRequest.getPreferences().getValue(ZIP, DEFAULT_ZIP);
}
return zip;
}
@@ -128,17 +132,17 @@
{
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>");
+ "<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
@@ -175,9 +179,9 @@
return html;
}
- protected String getQueryString(String zip)
+ protected String getQueryString(RenderRequest renderRequest, String zip)
{
- return GOOGLE + zip;
+ return renderRequest.getPreferences().getValue(QUERY, DEFAULT_QUERY) + zip;
}
private byte[] getBytes(InputStream in, int bufferSize) throws IOException,
IllegalArgumentException
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java 2008-08-05
18:57:49 UTC (rev 11667)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java 2008-08-06
13:41:24 UTC (rev 11668)
@@ -29,14 +29,8 @@
*/
public class GoogleWeatherClippingPortlet extends GoogleClippingPortlet
{
- private final static String WEATHER_URL =
"http://www.google.com/search?gl=US&hl=en&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;