Author: julien(a)jboss.com
Date: 2008-07-27 08:38:45 -0400 (Sun, 27 Jul 2008)
New Revision: 11593
Modified:
branches/JBoss_Portal_Branch_2_7/core-samples/src/main/org/jboss/portal/core/samples/weather/WeatherPortlet.java
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/jsp/weather/edit.jsp
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/portlet.xml
Log:
rewrote a part of the weathe portlet so that
- it does not use a mix of render parameter and preferences for zip state
- does not store the entire feed URL in the preferences but rather the zip
- display the current zip when the preferences is edited
Modified:
branches/JBoss_Portal_Branch_2_7/core-samples/src/main/org/jboss/portal/core/samples/weather/WeatherPortlet.java
===================================================================
---
branches/JBoss_Portal_Branch_2_7/core-samples/src/main/org/jboss/portal/core/samples/weather/WeatherPortlet.java 2008-07-26
08:48:26 UTC (rev 11592)
+++
branches/JBoss_Portal_Branch_2_7/core-samples/src/main/org/jboss/portal/core/samples/weather/WeatherPortlet.java 2008-07-27
12:38:45 UTC (rev 11593)
@@ -24,6 +24,7 @@
import org.apache.log4j.Logger;
import org.jboss.portal.common.net.URLTools;
+import org.jboss.portal.common.io.IOTools;
import org.w3c.dom.Document;
import javax.portlet.ActionRequest;
@@ -58,7 +59,7 @@
public class WeatherPortlet extends GenericPortlet
{
/** Logger */
- public static Logger log = Logger.getLogger(WeatherPortlet.class);
+ public static final Logger log = Logger.getLogger(WeatherPortlet.class);
/** Edit page */
private static final String JSP_EDIT = "/WEB-INF/jsp/weather/edit.jsp";
@@ -66,22 +67,27 @@
/** Help page */
private static final String JSP_HELP = "/WEB-INF/jsp/weather/help.jsp";
+ /** RSS 2.0 XSL page for output */
+ private static final String RSS_XSL = "/WEB-INF/xsl/weather/Rss.xsl";
+
+ /** . */
+ private static final String E_XSL_UNREADABLE = "Unable to prepare XSL
files.";
+
+ /** . */
+ private static final String E_UNREADABLE = "Unable to read XML Source.";
+
+ /** . */
+ private static final String E_UNPARSEABLE = "Unable to parse XML Source.";
+
/** Default zip set in init param. */
private String DEFAULT_ZIP;
- /** RSS 2.0 XSL page for output */
- private static final String RSS_XSL = "/WEB-INF/xsl/weather/Rss.xsl";
-
/** Compiled Template for output */
private Templates template_2_0;
/** Base URL of Weather RSS */
private String RSS_URL_PREFIX;
- private static final String E_XSL_UNREADABLE = "Unable to prepare XSL
files.";
- private static final String E_UNREADABLE = "Unable to read XML Source.";
- private static final String E_UNPARSEABLE = "Unable to parse XML Source.";
-
/** Compile the XSL File. */
public void init()
{
@@ -91,7 +97,6 @@
StreamSource xslSource = new StreamSource(inputstream);
TransformerFactory tFactory = TransformerFactory.newInstance();
this.template_2_0 = tFactory.newTemplates(xslSource);
-
this.DEFAULT_ZIP = getInitParameter("default_zipcode");
this.RSS_URL_PREFIX = getInitParameter("base_url");
}
@@ -103,23 +108,21 @@
protected void doView(RenderRequest request, RenderResponse response) throws
IOException, PortletException
{
- response.setProperty("expiration-cache",
request.getPreferences().getValue("expires", "180"));
+// response.setProperty("expiration-cache",
request.getPreferences().getValue("expires", "180"));
InputStream xmlInputStream = null;
InputStream xslInputStream = null;
- String newZip = null;
+ //
try
{
- newZip = request.getParameter("newzip");
- response.setContentType("text/html");
+ String zip = getZip(request);
- if (null == newZip)
- {
- newZip = request.getPreferences().getValue("RssXml", RSS_URL_PREFIX
+ DEFAULT_ZIP);
- }
+ //
+ String feedURL = buildFeedURL(zip);
- xmlInputStream = URLTools.getContentAsInputStream(new URL(newZip), 5000, 5000);
+ //
+ xmlInputStream = URLTools.getContentAsInputStream(new URL(feedURL), 5000,
5000);
if (xmlInputStream == null)
{
log.error(E_UNREADABLE);
@@ -129,9 +132,10 @@
// Ready the parsers
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- Transformer transformer = this.template_2_0.newTransformer();
- Source xmlSource = null;
+ //
+ Transformer transformer;
+ Source xmlSource;
try
{
Document document = docBuilder.parse(xmlInputStream);
@@ -158,15 +162,8 @@
}
finally // close all streams
{
- if (xmlInputStream != null)
- {
- xmlInputStream.close();
- }
-
- if (xslInputStream != null)
- {
- xslInputStream.close();
- }
+ IOTools.safeClose(xmlInputStream);
+ IOTools.safeClose(xslInputStream);
}
}
@@ -181,6 +178,7 @@
protected void doEdit(RenderRequest request, RenderResponse response) throws
IOException, PortletException
{
response.setContentType("text/html");
+ request.setAttribute("zip", getZip(request));
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher(JSP_EDIT);
dispatcher.include(request, response);
}
@@ -208,7 +206,7 @@
PortletPreferences prefs = request.getPreferences();
try
{
- prefs.setValue("RssXml", RSS_URL_PREFIX + newZip);
+ prefs.setValue("zip", newZip);
prefs.store();
}
catch (Exception e)
@@ -219,10 +217,17 @@
// TODO: error checking for URL + NewZIP
- //store as the selected xml so that it shows up selected as displays
- response.setRenderParameter("newzip", RSS_URL_PREFIX + newZip);
-
//set the portlet mode back to view
response.setPortletMode(PortletMode.VIEW);
}
+
+ private String getZip(RenderRequest request)
+ {
+ return request.getPreferences().getValue("zip", DEFAULT_ZIP);
+ }
+
+ private String buildFeedURL(String zip)
+ {
+ return RSS_URL_PREFIX + zip;
+ }
}
\ No newline at end of file
Modified:
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/jsp/weather/edit.jsp
===================================================================
---
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/jsp/weather/edit.jsp 2008-07-26
08:48:26 UTC (rev 11592)
+++
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/jsp/weather/edit.jsp 2008-07-27
12:38:45 UTC (rev 11593)
@@ -31,9 +31,10 @@
<font class="portlet-font">Change Weather Location:</font>
<form method="post" action="
<portlet:actionURL></portlet:actionURL>">
- <font class="portlet-font">Zip Code:</font><br/>
- <input class="portlet-form-input-field" type="text"
value="" size="12" name="newzip">
- <br/>
+ <div class="portlet-font">Zip Code:</div>
+ <div>
+ <input class="portlet-form-input-field" type="text"
value="<%= renderRequest.getAttribute("zip") %>"
size="12" name="newzip">
+ </div>
<input class="portlet-form-input-field" type="submit"
name="submit" value="submit">
</form>
</div>
\ No newline at end of file
Modified:
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/portlet.xml
===================================================================
---
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/portlet.xml 2008-07-26
08:48:26 UTC (rev 11592)
+++
branches/JBoss_Portal_Branch_2_7/core-samples/src/resources/portal-weather-samples-war/WEB-INF/portlet.xml 2008-07-27
12:38:45 UTC (rev 11593)
@@ -44,7 +44,7 @@
<name>base_url</name>
<
value>http://xml.weather.yahoo.com/forecastrss?p=</value>
</init-param>
- <expiration-cache>180</expiration-cache>
+ <!--<expiration-cache>180</expiration-cache>-->
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
@@ -66,8 +66,8 @@
<value>180</value>
</preference>
<preference>
- <name>RssXml</name>
- <
value>http://xml.weather.yahoo.com/forecastrss?p=33145</value>
+ <name>zip</name>
+ <value>33145</value>
<read-only>false</read-only>
</preference>
</portlet-preferences>