Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console » [javadoc | source]
    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.console;
   18   
   19   import java.io.IOException;
   20   import java.util.HashMap;
   21   import java.util.List;
   22   import java.util.Map;
   23   
   24   import javax.portlet.ActionRequest;
   25   import javax.portlet.ActionResponse;
   26   import javax.portlet.PortletConfig;
   27   import javax.portlet.PortletException;
   28   import javax.portlet.PortletRequest;
   29   import javax.portlet.RenderRequest;
   30   import javax.portlet.RenderResponse;
   31   import javax.portlet.WindowState;
   32   
   33   import org.apache.commons.fileupload.FileItem;
   34   import org.apache.commons.fileupload.FileUploadException;
   35   import org.apache.commons.fileupload.disk.DiskFileItemFactory;
   36   import org.apache.commons.fileupload.portlet.PortletFileUpload;
   37   import org.slf4j.Logger;
   38   import org.slf4j.LoggerFactory;
   39   
   40   /**
   41    * A base class for porlets consisting on multiple JSPs with before and after
   42    * actions (e.g. for load and validation/save) and the ability for an "after"
   43    * action to set the next page to load.
   44    *
   45    * @version $Rev: 690859 $ $Date: 2008-08-31 23:30:25 -0700 (Sun, 31 Aug 2008) $
   46    */
   47   public abstract class MultiPagePortlet extends BasePortlet {
   48       private static final Logger log = LoggerFactory.getLogger(MultiPagePortlet.class);
   49       protected static final String MODE_KEY = "mode";
   50       protected final Map<String, MultiPageAbstractHandler> helpers = new HashMap<String, MultiPageAbstractHandler>();
   51   
   52       public void destroy() {
   53           for (MultiPageAbstractHandler handler : helpers.values()) {
   54               handler.destroy();
   55           }
   56           helpers.clear();
   57           super.destroy();
   58       }
   59   
   60       public void processAction(ActionRequest actionRequest,
   61                                 ActionResponse actionResponse) throws PortletException, IOException {
   62           String mode = null;
   63           Map<String, FileItem> files = null;
   64           Map<String, String> fields = null;
   65           if(actionRequest.getContentType() != null && actionRequest.getContentType().startsWith("multipart/form-data")) {
   66               files = new HashMap<String, FileItem>();
   67               fields = new HashMap<String, String>();
   68               PortletFileUpload request = new PortletFileUpload(new DiskFileItemFactory());
   69               try {
   70                   List<FileItem> items = request.parseRequest(actionRequest);
   71                   for (FileItem item : items) {
   72                       if (item.isFormField()) {
   73                           if (item.getFieldName().equals(MODE_KEY)) {
   74                               mode = item.getString();
   75                           }
   76                           fields.put(item.getFieldName(), item.getString());
   77                       } else {
   78                           files.put(item.getFieldName(), item);
   79                       }
   80                   }
   81               } catch (FileUploadException e) {
   82                   log.error("Unable to process form including a file upload", e);
   83               }
   84           } else {
   85               mode = actionRequest.getParameter(MODE_KEY);
   86           }
   87           MultiPageModel model = getModel(actionRequest);
   88           while(true) {
   89               if(mode == null) {
   90                   break;
   91               }
   92               int pos = mode.lastIndexOf('-');
   93               if(pos == -1) { // Assume it's a render request
   94                   break;
   95               } else {
   96                   String type = mode.substring(pos+1);
   97                   mode = mode.substring(0, pos);
   98                   MultiPageAbstractHandler handler = helpers.get(mode);
   99                   if(handler == null) {
  100                       log.error("No handler for action mode '"+mode+"'");
  101                       break;
  102                   }
  103                   if(files == null) {
  104                       handler.getUploadFields().clear();
  105                       handler.getUploadFiles().clear();
  106                   } else {
  107                       handler.getUploadFields().putAll(fields);
  108                       handler.getUploadFiles().putAll(files);
  109                   }
  110                   log.debug("Using action handler '"+handler.getClass().getName()+"'");
  111                   if(type.equals("before")) {
  112                       mode = handler.actionBeforeView(actionRequest, actionResponse, model);
  113                   } else if(type.equals("after")) {
  114                       mode = handler.actionAfterView(actionRequest, actionResponse, model);
  115                   } else {
  116                       log.error("Unrecognized portlet action '"+mode+"'");
  117                       mode = null;
  118                   }
  119               }
  120           }
  121           if(mode != null) {
  122               actionResponse.setRenderParameter(MODE_KEY, mode);
  123           }
  124           if(model != null) {
  125               model.save(actionResponse, actionRequest.getPortletSession(true));
  126           }
  127       }
  128   
  129       protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
  130           if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
  131               return;
  132           }
  133           String mode = renderRequest.getParameter(MODE_KEY);
  134           MultiPageModel model = getModel(renderRequest);
  135           if(mode == null || mode.equals("")) {
  136               mode = getDefaultMode();
  137           }
  138           MultiPageAbstractHandler handler = helpers.get(mode);
  139           try {
  140               if(handler == null) {
  141                   log.error("No handler for render mode '"+mode+"'");
  142               } else {
  143                   log.debug("Using render handler '"+handler.getClass().getName()+"'");
  144                   handler.renderView(renderRequest, renderResponse, model);
  145               }
  146           } catch (Throwable e) {
  147               log.error("Unable to render portlet", e);
  148           }
  149           renderRequest.setAttribute(getModelJSPVariableName(), model);
  150           if(handler != null) {
  151               handler.getView().include(renderRequest, renderResponse);
  152           }
  153       }
  154   
  155       protected void addHelper(MultiPageAbstractHandler handler, PortletConfig config) throws PortletException {
  156           handler.init(config);
  157           helpers.put(handler.getMode(), handler);
  158       }
  159   
  160       protected String getDefaultMode() {
  161           if(helpers.containsKey("index")) return "index";
  162           if(helpers.containsKey("list")) return "list";
  163           return null;
  164       }
  165   
  166       protected abstract String getModelJSPVariableName();
  167   
  168       protected abstract MultiPageModel getModel(PortletRequest request);
  169   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console » [javadoc | source]