001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.test;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.util.Map;
022: import java.util.Iterator;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.Portlet;
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletMode;
030: import javax.portlet.PortletURL;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033: import javax.portlet.WindowState;
034: import javax.portlet.PortletPreferences;
035:
036: /**
037: * This is a very simple test portlet
038: *
039: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
040: *
041: * @version CVS $Id: TestPortlet.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class TestPortlet implements Portlet {
044:
045: /* (non-Javadoc)
046: * @see javax.portlet.Portlet#destroy()
047: */
048: public void destroy() {
049: // nothing to do
050: }
051:
052: /* (non-Javadoc)
053: * @see javax.portlet.Portlet#init(javax.portlet.PortletConfig)
054: */
055: public void init(PortletConfig config) throws PortletException {
056: // nothing to do
057: }
058:
059: /* (non-Javadoc)
060: * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
061: */
062: public void processAction(ActionRequest req, ActionResponse res)
063: throws PortletException, IOException {
064:
065: PortletPreferences prefs = req.getPreferences();
066: String key = req.getParameter("name");
067: String value = req.getParameter("value");
068: if (key != null && value != null) {
069: prefs.setValue(key, value);
070: try {
071: prefs.store();
072: } catch (Exception e) {
073: e.printStackTrace();
074: }
075: }
076: // nothing to do
077: }
078:
079: /* (non-Javadoc)
080: * @see javax.portlet.Portlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
081: */
082: public void render(RenderRequest req, RenderResponse res)
083: throws PortletException, IOException {
084: res.setContentType("text/html");
085: Writer writer = res.getWriter();
086: writer.write("<h1>Simple TestPortlet</h1>\n");
087: writer.write("<p>This is a Cocoon test portlet.</p>\n");
088: writer.write("<p>I'm running in: "
089: + req.getPortalContext().getPortalInfo() + "</p>");
090: writer.write("<p>Current portlet mode: " + req.getPortletMode()
091: + "</p>");
092: writer.write("<p>Current window state: " + req.getWindowState()
093: + "</p>");
094: writer.write("<p>Portlet Preferences:</p>");
095: PortletPreferences prefs = req.getPreferences();
096: Map map = prefs.getMap();
097: for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
098: Map.Entry me = (Map.Entry) iter.next();
099: String key = (String) me.getKey();
100: String[] values = (String[]) me.getValue();
101: if (values.length == 1) {
102: writer.write(" Key: " + key + " Value: " + values[0]
103: + "<br />");
104: } else if (values.length > 1) {
105: writer.write(" Key: " + key + " Value: " + values[0]
106: + "<br />");
107: for (int i = 1; i < values.length; ++i) {
108: writer.write(" ");
109: for (int j = 0; j < key.length(); ++j) {
110: writer.write(" ");
111: }
112: writer.write(" Value: " + values[i] + "<br />");
113: }
114: } else {
115: writer.write(" Key: " + key + " Value: <br />");
116: }
117: }
118:
119: PortletURL url;
120: url = res.createActionURL();
121: url.setPortletMode(PortletMode.EDIT);
122:
123: writer.write("<form method=\"POST\" action=\"" + url.toString()
124: + "\"><br />");
125: writer.write("Update Preferences: <br />");
126: writer
127: .write(" Key: <input type=\"text\" name=\"name\" size=\"16\" maxlength=\"16\">");
128: writer
129: .write(" Value: <input type=\"text\" name=\"value\" size=\"32\" maxlength=\"32\">");
130: writer
131: .write("<br /><input type=\"submit\" value=\"Update Preferences\"/>");
132: writer.write("</form>");
133: writer.write("<table><tr><td>Change Portlet Mode:</td>");
134:
135: url = res.createRenderURL();
136: url.setPortletMode(PortletMode.EDIT);
137: this .writeLink(writer, url, "Edit");
138:
139: url = res.createRenderURL();
140: url.setPortletMode(PortletMode.HELP);
141: this .writeLink(writer, url, "Help");
142:
143: url = res.createRenderURL();
144: url.setPortletMode(PortletMode.VIEW);
145: this .writeLink(writer, url, "View");
146:
147: writer.write("</tr><tr><td>Change Window Mode:</td>");
148: url = res.createRenderURL();
149: url.setWindowState(WindowState.MINIMIZED);
150: this .writeLink(writer, url, "Minimized");
151:
152: url = res.createRenderURL();
153: url.setWindowState(WindowState.NORMAL);
154: this .writeLink(writer, url, "Normal");
155:
156: url = res.createRenderURL();
157: url.setWindowState(WindowState.MAXIMIZED);
158: this .writeLink(writer, url, "Maximized");
159: writer.write("</tr></table>");
160: }
161:
162: protected void writeLink(Writer writer, PortletURL url, String text)
163: throws IOException {
164: writer.write("<td><a href=\"");
165: writer.write(url.toString());
166: writer.write("\">");
167: writer.write(text);
168: writer.write("</a></td>");
169: }
170: }
|