001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.url;
021:
022: import java.io.IOException;
023: import java.net.URL;
024:
025: import javax.portlet.PortletConfig;
026: import javax.portlet.PortletException;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029: import javax.portlet.UnavailableException;
030:
031: import com.nabhinc.core.MimeTypes;
032: import com.nabhinc.portlet.base.BasePortlet;
033: import com.nabhinc.util.IOUtil;
034:
035: /**
036: *
037: *
038: * @author Padmanabh Dabke
039: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public class URLPortlet extends BasePortlet {
042:
043: public static final String URL = "URL";
044: public static final String HTML_URL = "htmlURL";
045: public static final String WML_URL = "wmlURL";
046: public static final String XHTML_URL = "xhtmlURL";
047:
048: /**
049: * HTML URL
050: */
051: private String fpHTMLURL = null;
052:
053: /**
054: * Cached HTML content
055: */
056: private String fpHTMLCache = null;
057:
058: /**
059: * WML URL
060: */
061: private String fpWMLURL = null;
062:
063: /**
064: * Cached WML content
065: */
066: private String fpWMLCache = null;
067:
068: /**
069: * XHTML MP URL.
070: */
071: private String fpXHTMLURL = null;
072:
073: /**
074: * Cached XHTML MP content
075: */
076: private String fpXHTMLCache = null;
077:
078: /**
079: * File modification time when last checked
080: */
081: private long fpHTMLModify = -1;
082:
083: /**
084: * File modification time when last checked
085: */
086: private long fpWMLModify = 0;
087:
088: /**
089: * File modification time when last checked
090: */
091: private long fpXHTMLModify = 0;
092:
093: /**
094: * Do nothing constructor.
095: */
096: public URLPortlet() {
097: }
098:
099: /**
100: * Sets URLs that deliver HTML, WML, and XHTML MP content. You
101: * can either specify one URL that delivers content compliant with all
102: * formats via "URL" parameter , or you can specify three different urls
103: * using parameters: htmlURL, wmlURL, xhtmlMPURL.
104: */
105: public void init(PortletConfig config) throws PortletException {
106: super .init(config);
107:
108: fpHTMLURL = config.getInitParameter(URL);
109:
110: String temp = config.getInitParameter(HTML_URL);
111: if (temp != null)
112: fpHTMLURL = temp;
113:
114: temp = config.getInitParameter(WML_URL);
115: if (temp != null)
116: fpWMLURL = temp;
117:
118: temp = config.getInitParameter(XHTML_URL);
119: if (temp != null)
120: fpXHTMLURL = temp;
121:
122: if (fpHTMLURL == null) {
123: error("init", "URL not set.");
124: throw new UnavailableException("URL not set.");
125: }
126:
127: if (fpWMLURL == null)
128: fpWMLURL = fpHTMLURL;
129: if (fpXHTMLURL == null)
130: fpXHTMLURL = fpHTMLURL;
131:
132: }
133:
134: public void doView(RenderRequest request, RenderResponse response)
135: throws PortletException, IOException {
136:
137: String mimeType = request.getResponseContentType();
138:
139: if (mimeType == null || MimeTypes.HTML.equals(mimeType)) {
140: setHTMLContent();
141: response.getWriter().write(fpHTMLCache);
142: } else if (MimeTypes.WML.equals(mimeType)) {
143: setWMLContent();
144: response.getWriter().write(fpWMLCache);
145: } else if (MimeTypes.XHTML_MP.equals(mimeType)) {
146: setXHTMLContent();
147: response.getWriter().write(fpXHTMLCache);
148: } else {
149: throw new PortletException("Mime type " + mimeType
150: + " is not supported.");
151: }
152:
153: }
154:
155: /**
156: * Write the contents of this portlet to the output stream.
157: */
158: private void setHTMLContent() throws PortletException, IOException {
159:
160: URL url = null;
161: try {
162: url = new URL(fpHTMLURL);
163: long lastModified = url.openConnection().getLastModified();
164: if (lastModified == 0 || lastModified != fpHTMLModify) {
165: fpHTMLModify = lastModified;
166: fpHTMLCache = IOUtil.getURLContentAsString(fpHTMLURL);
167: }
168:
169: } catch (Exception ex) {
170: fpHTMLCache = "Invalid URL";
171: error("doView", "Invalid URL: " + fpHTMLURL);
172: throw new UnavailableException("Unable to find URL: "
173: + fpHTMLURL, 0);
174: }
175:
176: }
177:
178: /**
179: * Write the contents of this portlet to the output stream.
180: */
181: private void setWMLContent() throws PortletException, IOException {
182:
183: URL url = null;
184: try {
185: url = new URL(fpWMLURL);
186: long lastModified = url.openConnection().getLastModified();
187: if (lastModified != fpWMLModify) {
188: fpWMLModify = lastModified;
189: fpWMLCache = IOUtil.getURLContentAsString(fpWMLURL);
190: }
191:
192: } catch (Exception ex) {
193: fpWMLCache = "Invalid URL";
194: error("doView", "Invalid URL: " + fpWMLURL);
195: throw new UnavailableException("Unable to find URL: "
196: + fpWMLURL, 0);
197: }
198:
199: }
200:
201: /**
202: * Write the contents of this portlet to the output stream.
203: */
204: private void setXHTMLContent() throws PortletException, IOException {
205:
206: URL url = null;
207: try {
208: url = new URL(fpXHTMLURL);
209: long lastModified = url.openConnection().getLastModified();
210: if (lastModified != fpXHTMLModify) {
211: fpXHTMLModify = lastModified;
212: fpXHTMLCache = IOUtil.getURLContentAsString(fpXHTMLURL);
213: }
214:
215: } catch (Exception ex) {
216: fpXHTMLCache = "Invalid URL";
217: error("doView", "Invalid URL: " + fpXHTMLURL);
218: throw new UnavailableException("Unable to find URL: "
219: + fpXHTMLURL, 0);
220: }
221:
222: }
223:
224: }
|