001: /*
002: * Created on Mar 8, 2003
003: */
004: package net.sf.jportlet.portlet;
005:
006: import java.io.IOException;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010:
011: import javax.servlet.http.HttpServlet;
012:
013: /**
014: * The abstract <code>Portlet</code> is used by the portlet container to
015: * invoke the portlet.
016: *
017: * @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
018: */
019: public abstract class Portlet extends HttpServlet {
020: //~ Instance fields --------------------------------------------------------
021:
022: private PortletConfig _portletConfig;
023:
024: //~ Methods ----------------------------------------------------------------
025:
026: /**
027: * Returns the configuration of the portlet
028: * @return PortletConfig
029: */
030: public PortletConfig getPortletConfig() {
031: return _portletConfig;
032: }
033:
034: /**
035: * Returns the time the response of the Portlet object was last modified,
036: * in milliseconds since midnight January 1, 1970 GMT.
037: * If the time is unknown, this method returns a negative number (the default).
038: * <p>
039: * Portlets that can quickly determine their last modification time should
040: * override this method. This makes browser and proxy caches work more
041: * effectively, reducing the load on server and network resources
042: *
043: * @param request
044: * @return long
045: */
046: public abstract long getLastModified(PortletRequest request);
047:
048: /**
049: * Initialize the portlet
050: * @param config configuration of the portlet
051: * @throws PortletException if any error occurs
052: */
053: public void init(PortletConfig portletConfig)
054: throws PortletException {
055: _portletConfig = portletConfig;
056: }
057:
058: /**
059: * Called by the portlet container to ask the portlet to initialize a personalized user experience
060: *
061: * @param request
062: * @throws PortletException
063: */
064: public abstract void login(PortletRequest request)
065: throws PortletException;
066:
067: /**
068: * Called by the portlet container to indicate that a concrete portlet instance is being removed
069: *
070: * @param request
071: */
072: public abstract void logout(PortletRequest request)
073: throws PortletException;
074:
075: /**
076: * This function is invoke by the container in order to render the portlet
077: *
078: * @param request current request
079: * @param response current response
080: * @throws IOException if any IO error occurs
081: * @throws PortletException if any other error
082: */
083: public abstract void service(PortletRequest request,
084: PortletResponse response) throws PortletException,
085: IOException;
086:
087: //~ Inner Classes ----------------------------------------------------------
088:
089: /**
090: * The <code>Markup</code> class is a finite enumeration of the
091: * possible markup languages that a portlet can supports
092: */
093: public static class Markup {
094: //~ Static fields/initializers -----------------------------------------
095:
096: public static final Markup HTML = new Markup("html",
097: "text/html");
098: public static final Markup WML = new Markup("wml", "text/wml");
099:
100: //~ Instance fields ----------------------------------------------------
101:
102: private String _name;
103: private String _mimeType;
104:
105: //~ Constructors -------------------------------------------------------
106:
107: private Markup(String name, String mimeType) {
108: _name = name;
109: _mimeType = mimeType;
110: }
111:
112: //~ Methods ------------------------------------------------------------
113:
114: public static Portlet.Markup fromMimeType(String mimeType) {
115: if (HTML._mimeType.equalsIgnoreCase(mimeType)) {
116: return HTML;
117: } else if (WML._mimeType.equalsIgnoreCase(mimeType)) {
118: return WML;
119: } else {
120: return null;
121: }
122: }
123:
124: public static Portlet.Markup fromString(String str) {
125: if (HTML._name.equalsIgnoreCase(str)) {
126: return HTML;
127: } else if (WML._name.equalsIgnoreCase(str)) {
128: return WML;
129: } else {
130: return null;
131: }
132: }
133:
134: public String toString() {
135: return _name;
136: }
137:
138: public String getMimeType() {
139: return _mimeType;
140: }
141: }
142:
143: /**
144: * The <code>Mode</code> class is a finite enumeration of the
145: * possible modes that a portlet can assume
146: */
147: public static class Mode {
148: //~ Static fields/initializers -----------------------------------------
149:
150: public static final Mode CONFIGURE = new Mode("configure");
151: public static final Mode EDIT = new Mode("edit");
152: public static final Mode HELP = new Mode("help");
153: public static final Mode VIEW = new Mode("view");
154:
155: //~ Instance fields ----------------------------------------------------
156:
157: private String _name;
158:
159: //~ Constructors -------------------------------------------------------
160:
161: private Mode(String name) {
162: _name = name;
163: }
164:
165: //~ Methods ------------------------------------------------------------
166:
167: public String toString() {
168: return _name;
169: }
170:
171: public static Portlet.Mode fromString(String str) {
172: if (CONFIGURE._name.equals(str)) {
173: return CONFIGURE;
174: } else if (EDIT._name.equals(str)) {
175: return EDIT;
176: } else if (HELP._name.equals(str)) {
177: return HELP;
178: } else if (VIEW._name.equals(str)) {
179: return VIEW;
180: } else {
181: return null;
182: }
183: }
184:
185: public static Collection getAll() {
186: ArrayList all = new ArrayList();
187: all.add(CONFIGURE);
188: all.add(EDIT);
189: all.add(HELP);
190: all.add(VIEW);
191: return all;
192: }
193: }
194: }
|