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