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: */package org.apache.geronimo.console;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.commons.fileupload.portlet.PortletFileUpload;
021: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
022: import org.apache.commons.fileupload.FileItem;
023: import org.apache.commons.fileupload.FileUploadException;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028: import javax.portlet.RenderRequest;
029: import javax.portlet.RenderResponse;
030: import javax.portlet.WindowState;
031: import javax.portlet.PortletConfig;
032: import javax.portlet.PortletRequest;
033: import java.util.Map;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.io.IOException;
038:
039: /**
040: * A base class for porlets consisting on multiple JSPs with before and after
041: * actions (e.g. for load and validation/save) and the ability for an "after"
042: * action to set the next page to load.
043: *
044: * @version $Rev: 612052 $ $Date: 2008-01-15 01:10:00 -0800 (Tue, 15 Jan 2008) $
045: */
046: public abstract class MultiPagePortlet extends BasePortlet {
047: private final static Log log = LogFactory
048: .getLog(MultiPagePortlet.class);
049: protected static final String MODE_KEY = "mode";
050: protected final Map<String, MultiPageAbstractHandler> helpers = new HashMap<String, MultiPageAbstractHandler>();
051:
052: public void destroy() {
053: for (MultiPageAbstractHandler handler : helpers.values()) {
054: handler.destroy();
055: }
056: helpers.clear();
057: super .destroy();
058: }
059:
060: public void processAction(ActionRequest actionRequest,
061: ActionResponse actionResponse) throws PortletException,
062: IOException {
063: String mode = null;
064: Map<String, FileItem> files = null;
065: Map<String, String> fields = null;
066: if (actionRequest.getContentType() != null
067: && actionRequest.getContentType().startsWith(
068: "multipart/form-data")) {
069: files = new HashMap<String, FileItem>();
070: fields = new HashMap<String, String>();
071: PortletFileUpload request = new PortletFileUpload(
072: new DiskFileItemFactory());
073: try {
074: List<FileItem> items = request
075: .parseRequest(actionRequest);
076: for (FileItem item : items) {
077: if (item.isFormField()) {
078: if (item.getFieldName().equals(MODE_KEY)) {
079: mode = item.getString();
080: }
081: fields.put(item.getFieldName(), item
082: .getString());
083: } else {
084: files.put(item.getFieldName(), item);
085: }
086: }
087: } catch (FileUploadException e) {
088: log
089: .error(
090: "Unable to process form including a file upload",
091: e);
092: }
093: } else {
094: mode = actionRequest.getParameter(MODE_KEY);
095: }
096: MultiPageModel model = getModel(actionRequest);
097: while (true) {
098: if (mode == null) {
099: break;
100: }
101: int pos = mode.lastIndexOf('-');
102: if (pos == -1) { // Assume it's a render request
103: break;
104: } else {
105: String type = mode.substring(pos + 1);
106: mode = mode.substring(0, pos);
107: MultiPageAbstractHandler handler = helpers.get(mode);
108: if (handler == null) {
109: log.error("No handler for action mode '" + mode
110: + "'");
111: break;
112: }
113: if (files == null) {
114: handler.getUploadFields().clear();
115: handler.getUploadFiles().clear();
116: } else {
117: handler.getUploadFields().putAll(fields);
118: handler.getUploadFiles().putAll(files);
119: }
120: log.debug("Using action handler '"
121: + handler.getClass().getName() + "'");
122: if (type.equals("before")) {
123: mode = handler.actionBeforeView(actionRequest,
124: actionResponse, model);
125: } else if (type.equals("after")) {
126: mode = handler.actionAfterView(actionRequest,
127: actionResponse, model);
128: } else {
129: log.error("Unrecognized portlet action '" + mode
130: + "'");
131: mode = null;
132: }
133: }
134: }
135: if (mode != null) {
136: actionResponse.setRenderParameter(MODE_KEY, mode);
137: }
138: if (model != null) {
139: model.save(actionResponse, actionRequest
140: .getPortletSession(true));
141: }
142: }
143:
144: protected void doView(RenderRequest renderRequest,
145: RenderResponse renderResponse) throws IOException,
146: PortletException {
147: if (WindowState.MINIMIZED
148: .equals(renderRequest.getWindowState())) {
149: return;
150: }
151: String mode = renderRequest.getParameter(MODE_KEY);
152: MultiPageModel model = getModel(renderRequest);
153: if (mode == null || mode.equals("")) {
154: mode = getDefaultMode();
155: }
156: MultiPageAbstractHandler handler = helpers.get(mode);
157: try {
158: if (handler == null) {
159: log.error("No handler for render mode '" + mode + "'");
160: } else {
161: log.debug("Using render handler '"
162: + handler.getClass().getName() + "'");
163: handler
164: .renderView(renderRequest, renderResponse,
165: model);
166: }
167: } catch (Throwable e) {
168: log.error("Unable to render portlet", e);
169: }
170: renderRequest.setAttribute(getModelJSPVariableName(), model);
171: if (handler != null) {
172: handler.getView().include(renderRequest, renderResponse);
173: }
174: }
175:
176: protected void addHelper(MultiPageAbstractHandler handler,
177: PortletConfig config) throws PortletException {
178: handler.init(config);
179: helpers.put(handler.getMode(), handler);
180: }
181:
182: protected String getDefaultMode() {
183: if (helpers.containsKey("index"))
184: return "index";
185: if (helpers.containsKey("list"))
186: return "list";
187: return null;
188: }
189:
190: protected abstract String getModelJSPVariableName();
191:
192: protected abstract MultiPageModel getModel(PortletRequest request);
193: }
|