public void setValues(final UIPortlet uiPortlet) throws Exception {
    uiPortlet_ = uiPortlet;
    invokeGetBindingBean(uiPortlet_);
    String icon = uiPortlet.getIcon();

    if (icon == null || icon.length() < 0) {
      icon = "PortletIcon";
    }
    getChild(UIFormInputIconSelector.class).setSelectedIcon(icon);
    getChild(UIFormInputThemeSelector.class)
        .getChild(UIItemThemeSelector.class)
        .setSelectedTheme(uiPortlet.getSuitedTheme(null));
    if (hasEditMode()) {
      uiPortlet.setCurrentPortletMode(PortletMode.EDIT);
    } else {
      Map<String, String> portletPreferenceMaps = new HashMap<String, String>();
      org.gatein.pc.api.Portlet portlet = uiPortlet.getProducedOfferedPortlet();
      Set<String> keySet = portlet.getInfo().getPreferences().getKeys();

      for (String key : keySet) {
        PreferenceInfo preferenceInfo = portlet.getInfo().getPreferences().getPreference(key);
        if (!preferenceInfo.isReadOnly()) {
          String ppValue =
              (preferenceInfo.getDefaultValue().size() > 0)
                  ? preferenceInfo.getDefaultValue().get(0)
                  : "";
          portletPreferenceMaps.put(key, ppValue);
        }
      }

      Portlet pp = uiPortlet.getPreferences();
      if (pp != null) {
        for (Preference pref : pp) {
          if (!pref.isReadOnly()) {
            portletPreferenceMaps.put(
                pref.getName(), (pref.getValues().size() > 0) ? pref.getValues().get(0) : "");
          }
        }
      }

      if (portletPreferenceMaps.size() > 0) {
        Set<String> ppKeySet = portletPreferenceMaps.keySet();
        UIFormInputSet uiPortletPrefSet = getChildById(FIELD_PORTLET_PREF);
        uiPortletPrefSet.getChildren().clear();
        for (String ppKey : ppKeySet) {
          String ppValue = portletPreferenceMaps.get(ppKey);
          UIFormStringInput preferenceStringInput = new UIFormStringInput(ppKey, null, ppValue);
          preferenceStringInput.setLabel(ppKey);
          preferenceStringInput.addValidator(MandatoryValidator.class);
          uiPortletPrefSet.addUIFormInput(preferenceStringInput);
        }

        uiPortletPrefSet.setRendered(true);
        setSelectedTab(FIELD_PORTLET_PREF);
        return;
      }

      setSelectedTab("PortletSetting");
    }
  }
  public String getEditModeContent() {
    StringBuilder portletContent = new StringBuilder();
    try {
      PortalRequestContext prcontext =
          (PortalRequestContext) WebuiRequestContext.getCurrentInstance();
      prcontext.ignoreAJAXUpdateOnPortlets(true);
      StatefulPortletContext portletContext = uiPortlet_.getPortletContext();

      ExoPortletInvocationContext portletInvocationContext =
          new ExoPortletInvocationContext(prcontext, uiPortlet_);

      List<Cookie> requestCookies =
          new ArrayList<Cookie>(Arrays.asList(prcontext.getRequest().getCookies()));

      PortletInvocation portletInvocation = uiPortlet_.create(RenderInvocation.class, prcontext);
      RenderInvocation renderInvocation = (RenderInvocation) portletInvocation;
      // make sure we are in the EDIT mode, and not whatever the current portlet mode is for the
      // Portlet
      renderInvocation.setMode(Mode.create(PortletMode.EDIT.toString()));

      PortletInvocationResponse portletResponse = uiPortlet_.invoke(renderInvocation);
      portletContent.append(uiPortlet_.generateRenderMarkup(portletResponse, prcontext).toString());
    } catch (Throwable ex) {
      WebuiRequestContext webuiRequest = WebuiRequestContext.getCurrentInstance();
      portletContent.append(
          webuiRequest.getApplicationResourceBundle().getString("UIPortlet.message.RuntimeError"));
      log.error(
          "The portlet "
              + uiPortlet_.getName()
              + " could not be loaded. Check if properly deployed.",
          ExceptionUtil.getRootCause(ex));
    }
    return portletContent.toString();
  }
 /**
  * This method is used to set the next portlet mode if this one needs to be modified because of
  * the incoming request
  */
 public static void setNextMode(UIPortlet uiPortlet, PortletMode portletMode) {
   if (portletMode != null) {
     if (portletMode.equals(PortletMode.HELP)) {
       uiPortlet.setCurrentPortletMode(PortletMode.HELP);
     } else if (portletMode.equals(PortletMode.EDIT)) {
       uiPortlet.setCurrentPortletMode(PortletMode.EDIT);
     } else {
       uiPortlet.setCurrentPortletMode(PortletMode.VIEW);
     }
   }
 }
    public void execute(final Event<UIPortletForm> event) throws Exception {
      UIPortletForm uiPortletForm = event.getSource();
      UIPortlet uiPortlet = uiPortletForm.getUIPortlet();
      UIFormInputIconSelector uiIconSelector =
          uiPortletForm.getChild(UIFormInputIconSelector.class);
      uiPortletForm.invokeSetBindingBean(uiPortlet);
      if (uiIconSelector.getSelectedIcon().equals("Default")) {
        uiPortlet.setIcon("PortletIcon");
      } else {
        uiPortlet.setIcon(uiIconSelector.getSelectedIcon());
      }
      UIFormInputThemeSelector uiThemeSelector =
          uiPortletForm.getChild(UIFormInputThemeSelector.class);
      uiPortlet.putSuitedTheme(
          null, uiThemeSelector.getChild(UIItemThemeSelector.class).getSelectedTheme());
      uiPortletForm.savePreferences();
      UIMaskWorkspace uiMaskWorkspace = uiPortletForm.getParent();
      PortalRequestContext pcontext = (PortalRequestContext) event.getRequestContext();
      uiMaskWorkspace.createEvent("Close", Phase.DECODE, pcontext).broadcast();
      if (uiPortletForm.hasEditMode()) {
        uiPortlet.setCurrentPortletMode(PortletMode.VIEW);
      }

      String width = uiPortletForm.getUIStringInput("width").getValue();
      if (width == null || width.length() == 0) {
        uiPortlet.setWidth(null);
      } else {
        if (!width.endsWith("px")) {
          width.concat("px");
        }
        uiPortlet.setWidth(width);
      }

      String height = uiPortletForm.getUIStringInput("height").getValue();
      if (height == null || height.length() == 0) {
        uiPortlet.setHeight(null);
      } else {
        if (!height.endsWith("px")) {
          height.concat("px");
        }
        uiPortlet.setHeight(height);
      }

      pcontext
          .getJavascriptManager()
          .addJavascript("eXo.portal.PortalComposer.toggleSaveButton();");
      UIPortalApplication uiPortalApp = uiPortlet.getAncestorOfType(UIPortalApplication.class);
      UIWorkingWorkspace uiWorkingWS =
          uiPortalApp.getChildById(UIPortalApplication.UI_WORKING_WS_ID);
      pcontext.addUIComponentToUpdateByAjax(uiWorkingWS);
      pcontext.ignoreAJAXUpdateOnPortlets(true);
    }
 public void execute(final Event<UIPortletForm> event) throws Exception {
   UIPortletForm uiPortletForm = event.getSource();
   UIPortlet uiPortlet = uiPortletForm.getUIPortlet();
   if (uiPortletForm.hasEditMode()) {
     uiPortlet.setCurrentPortletMode(PortletMode.VIEW);
   }
   UIPortalApplication uiPortalApp = Util.getUIPortalApplication();
   PortalRequestContext pcontext = (PortalRequestContext) event.getRequestContext();
   // add by Pham Dinh Tan
   UIMaskWorkspace uiMaskWorkspace = uiPortalApp.getChildById(UIPortalApplication.UI_MASK_WS_ID);
   uiMaskWorkspace.broadcast(event, Phase.DECODE);
   pcontext.ignoreAJAXUpdateOnPortlets(true);
 }
    public void execute(Event<UIPortlet<S, C>> event) throws Exception {
      UIPortlet<S, C> uiPortlet = event.getSource();
      PortalRequestContext prcontext = (PortalRequestContext) event.getRequestContext();

      // set the public render parameters from the request before creating the invocation
      HttpServletRequest request = prcontext.getRequest();
      setupPublicRenderParams(uiPortlet, request.getParameterMap());

      // set the navigational state
      String navState =
          prcontext.getRequestParameter(ExoPortletInvocationContext.NAVIGATIONAL_STATE_PARAM_NAME);
      if (navState != null) {
        uiPortlet.setNavigationalState(ParametersStateString.create(navState));
      }

      //
      ActionInvocation actionInvocation = uiPortlet.create(ActionInvocation.class, prcontext);
      if (actionInvocation == null) {
        return;
      }
      //
      PortletInvocationResponse portletResponse = uiPortlet.invoke(actionInvocation);

      // deal with potential portlet context modifications
      ExoPortletInstanceContext instanceCtx =
          (ExoPortletInstanceContext) actionInvocation.getInstanceContext();
      if (instanceCtx.getModifiedContext() != null) {
        StatefulPortletContext<C> updatedCtx =
            (StatefulPortletContext<C>) instanceCtx.getModifiedContext();
        C portletState = uiPortlet.getModifiedState(updatedCtx);
        uiPortlet.update(portletState);
      } else {
        // todo: fix me as this shouldn't probably be done only for the WSRP case
        PortletContext clonedContext = instanceCtx.getClonedContext();
        if (clonedContext != null) {
          C state = uiPortlet.getClonedState(clonedContext);
          uiPortlet.update(state);
        }
      }

      if (portletResponse instanceof UpdateNavigationalStateResponse) {
        handleUpdateNavigationalStateResponse(
            (UpdateNavigationalStateResponse) portletResponse, uiPortlet, prcontext);
      } else if (portletResponse instanceof HTTPRedirectionResponse) {
        handleRedirectionResponse(
            (HTTPRedirectionResponse) portletResponse, prcontext.getResponse());
        prcontext.setResponseComplete(true);
      } else if (portletResponse instanceof ErrorResponse) {
        handleErrorResponse((ErrorResponse) portletResponse);
      } else if (portletResponse instanceof SecurityResponse) {
        handleSecurityResponse((SecurityResponse) portletResponse);
      } else {
        throw new Exception(
            "Unexpected response type ["
                + portletResponse
                + "]. Expected an UpdateNavigationResponse"
                + ", a HTTPRedirectionResponse or an ErrorResponse.");
      }
    }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();
      PortalRequestContext context = (PortalRequestContext) event.getRequestContext();
      List<UIPortlet> portletInstancesInPage = new ArrayList<UIPortlet>();
      //      UIPortalApplication uiPortal = uiPortlet.getAncestorOfType(UIPortalApplication.class);
      UIPortalApplication uiPortal = (UIPortalApplication) context.getUIApplication();
      uiPortal.findComponentOfType(portletInstancesInPage, UIPortlet.class);
      EventsWrapper eventsWrapper =
          (EventsWrapper) event.getRequestContext().getAttribute(PORTLET_EVENTS);
      List<javax.portlet.Event> events = eventsWrapper.getEvents();

      /*
       * Iterate over all the events that the processAction
       * has generated. Check among all the portlet instances deployed in the
       * page (usual layout or webos) which instance can be targeted by the
       * event and then process the event on the associated UIPortlet component
       */
      while (events.size() > 0) {
        javax.portlet.Event nativeEvent = events.remove(0);
        QName eventName = nativeEvent.getQName();
        for (Iterator<UIPortlet> iterator = portletInstancesInPage.iterator();
            iterator.hasNext(); ) {
          UIPortlet uiPortletInPage = iterator.next();
          if (uiPortletInPage.supportsProcessingEvent(eventName)
              && !eventsWrapper.isInvokedTooManyTimes(uiPortletInPage)) {
            List<javax.portlet.Event> newEvents = processEvent(uiPortletInPage, nativeEvent);
            eventsWrapper.increaseCounter(uiPortletInPage);
            if (context.useAjax()) {
              log.info(
                  "Events were generated inside the scope of an AJAX call, hence will only refresh the targeted portlets");
              event.getRequestContext().addUIComponentToUpdateByAjax(uiPortletInPage);
            } else {
              log.info(
                  "Events were generated outside the scope of an AJAX call, hence will make a full render of the page");
              context.ignoreAJAXUpdateOnPortlets(true);
            }
            if (newEvents != null && !newEvents.isEmpty()) {
              log.trace(
                  "The portlet: "
                      + uiPortletInPage.getPortletContext().getId()
                      + " processEvent() method has generated new events itself");
              events.addAll(newEvents);
            }
          }
        }
      }
    }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();
      uiPortlet.setNavigationalState(null);

      // set the public params
      HttpServletRequest request = event.getRequestContext().getRequest();
      setupPublicRenderParams(uiPortlet, request.getParameterMap());

      // set render params
      String navState =
          event
              .getRequestContext()
              .getRequestParameter(ExoPortletInvocationContext.NAVIGATIONAL_STATE_PARAM_NAME);
      if (navState != null) {
        uiPortlet.setNavigationalState(ParametersStateString.create(navState));
      }
    }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();

      String portletMode = null;

      Object changePortletModeAttribute =
          event.getRequestContext().getAttribute(CHANGE_PORTLET_MODE_EVENT);
      if (changePortletModeAttribute != null && changePortletModeAttribute instanceof String) {
        portletMode = (String) changePortletModeAttribute;
      }

      if (portletMode == null) {
        portletMode = event.getRequestContext().getRequestParameter(Constants.PORTAL_PORTLET_MODE);
      }
      if (portletMode == null) {
        portletMode = event.getRequestContext().getRequestParameter(UIComponent.OBJECTID);
      }
      if (portletMode == null) {
        portletMode = uiPortlet.getCurrentPortletMode().toString();
      }

      log.trace(
          "Change portlet mode of " + uiPortlet.getPortletContext().getId() + " to " + portletMode);
      if (portletMode.equals(PortletMode.HELP.toString())) {
        uiPortlet.setCurrentPortletMode(PortletMode.HELP);
      } else if (portletMode.equals(PortletMode.EDIT.toString())) {
        uiPortlet.setCurrentPortletMode(PortletMode.EDIT);
      } else if (portletMode.equals(PortletMode.VIEW.toString())) {
        uiPortlet.setCurrentPortletMode(PortletMode.VIEW);
      } else {
        PortletMode customMode = new PortletMode(portletMode);
        uiPortlet.setCurrentPortletMode(customMode);
      }
      event.getRequestContext().addUIComponentToUpdateByAjax(uiPortlet);
    }
 /**
  * This method is used to set the next portlet window state if this one needs to be modified
  * because of the incoming request
  */
 public static void setNextState(UIPortlet uiPortlet, WindowState state) {
   if (state != null) {
     UIPage uiPage = uiPortlet.getAncestorOfType(UIPage.class);
     if (WindowState.MAXIMIZED.equals(state)) {
       uiPortlet.setCurrentWindowState(WindowState.MAXIMIZED);
       if (uiPage != null) {
         uiPage.setMaximizedUIPortlet(uiPortlet);
       }
     } else if (WindowState.MINIMIZED.equals(state)) {
       uiPortlet.setCurrentWindowState(WindowState.MINIMIZED);
       if (uiPage != null) {
         uiPage.setMaximizedUIPortlet(null);
       }
     } else {
       uiPortlet.setCurrentWindowState(WindowState.NORMAL);
       if (uiPage != null) {
         uiPage.setMaximizedUIPortlet(null);
       }
     }
   }
 }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();

      UIPortalApplication uiPortalApp = uiPortlet.getAncestorOfType(UIPortalApplication.class);
      UIWorkingWorkspace uiWorkingWS =
          uiPortalApp.getChildById(UIPortalApplication.UI_WORKING_WS_ID);
      PortalRequestContext pcontext = (PortalRequestContext) event.getRequestContext();
      pcontext.addUIComponentToUpdateByAjax(uiWorkingWS);
      pcontext.ignoreAJAXUpdateOnPortlets(true);

      String windowState = null;

      Object changeWindowStateAttribute =
          event.getRequestContext().getAttribute(CHANGE_WINDOW_STATE_EVENT);
      if (changeWindowStateAttribute != null && changeWindowStateAttribute instanceof String) {
        windowState = (String) changeWindowStateAttribute;
      }

      if (windowState == null) {
        windowState = event.getRequestContext().getRequestParameter(Constants.PORTAL_WINDOW_STATE);
      }
      if (windowState == null) {
        if (event.getRequestContext().getRequestParameter(UIComponent.OBJECTID) != null) {
          windowState = event.getRequestContext().getRequestParameter(UIComponent.OBJECTID).trim();
        }
      }

      if (windowState == null) {
        windowState = uiPortlet.getCurrentWindowState().toString();
      }

      UIPage uiPage = uiPortlet.getAncestorOfType(UIPage.class);
      if (windowState.equals(WindowState.MAXIMIZED.toString())) {
        if (uiPage != null) {
          uiPage.normalizePortletWindowStates();
          uiPage.setMaximizedUIPortlet(uiPortlet);
        }
        uiPortlet.setCurrentWindowState(WindowState.MAXIMIZED);
        return;
      } else {
        if (windowState.equals(WindowState.MINIMIZED.toString())) {
          uiPortlet.setCurrentWindowState(WindowState.MINIMIZED);
        } else {
          uiPortlet.setCurrentWindowState(WindowState.NORMAL);
        }
        if (uiPage != null) {
          clearMaximizedUIComponent(uiPage, uiPortlet);
        }
      }
    }
  /**
   * This method is called by the process action and render action listeners, aka during the
   * processDecode() phase of our UI framework
   *
   * <p>It goes throughs all the request parameters and add to the public render parameters Map the
   * one that are supported by the targeted portlet
   */
  public static void setupPublicRenderParams(
      UIPortlet uiPortlet, Map<String, String[]> requestParams) {
    if (ParameterValidation.existsAndIsNotEmpty(requestParams)) {
      UIPortal uiPortal = Util.getUIPortal();
      Map<String, String[]> publicParams = uiPortal.getPublicParameters();

      for (String key : requestParams.keySet()) {
        String[] value = requestParams.get(key);
        if (uiPortlet.supportsPublicParam(key)) {
          if (value.length > 0) {
            publicParams.put(key, value);
          } else {
            publicParams.remove(key);
          }
        }
      }
    }
  }
  private void savePreferences() throws Exception {
    UIFormInputSet uiPortletPrefSet = getChildById(FIELD_PORTLET_PREF);
    List<UIFormStringInput> uiFormInputs = new ArrayList<UIFormStringInput>(3);
    uiPortletPrefSet.findComponentOfType(uiFormInputs, UIFormStringInput.class);
    if (uiFormInputs.size() < 1) {
      return;
    }

    PropertyChange[] propertyChanges = new PropertyChange[uiFormInputs.size()];

    for (int i = 0; i < uiFormInputs.size(); i++) {
      String name = uiFormInputs.get(i).getName();
      String value = uiFormInputs.get(i).getValue();
      propertyChanges[i] = PropertyChange.newUpdate(name, value);
    }

    // Now save it
    uiPortlet_.update(propertyChanges);
  }
  /**
   * This method is called when the javax.portlet.Event is supported by the current portlet stored
   * in the Portlet Caontainer
   *
   * <p>The processEvent() method can also generates IPC events and hence the portal itself will
   * call the ProcessEventsActionListener once again
   */
  public static <S, C extends Serializable, I> List<javax.portlet.Event> processEvent(
      UIPortlet<S, C> uiPortlet, javax.portlet.Event event) {
    log.trace("Process Event: " + event.getName() + " for portlet: " + uiPortlet.getState());
    try {
      PortalRequestContext context =
          (PortalRequestContext) WebuiRequestContext.getCurrentInstance();

      //
      EventInvocation eventInvocation = uiPortlet.create(EventInvocation.class, context);

      //
      eventInvocation.setName(event.getQName());
      eventInvocation.setPayload(event.getValue());

      //
      PortletInvocationResponse piResponse = uiPortlet.invoke(eventInvocation);

      //
      ExoPortletInstanceContext instanceCtx =
          (ExoPortletInstanceContext) eventInvocation.getInstanceContext();
      if (instanceCtx.getModifiedContext() != null) {
        StatefulPortletContext<C> updatedCtx =
            (StatefulPortletContext<C>) instanceCtx.getModifiedContext();
        C portletState = updatedCtx.getState();
        uiPortlet.update(portletState);
      }

      // todo: handle the error response better than this.
      if (!(piResponse instanceof UpdateNavigationalStateResponse)) {
        if (piResponse instanceof ErrorResponse) {
          ErrorResponse errorResponse = (ErrorResponse) piResponse;
          throw (Exception) errorResponse.getCause();
        } else {
          throw new Exception(
              "Unexpected response type ["
                  + piResponse
                  + "]. Expected a UpdateNavigationResponse or an ErrorResponse.");
        }
      }

      UpdateNavigationalStateResponse navResponse = (UpdateNavigationalStateResponse) piResponse;

      //

      /*
       * Update the portlet window state according to the action output
       * information
       *
       * If the current node is displaying a usual layout page, also tells the
       * page which portlet to render or not when the state is maximized
       */
      WindowState state = new WindowState(getWindowStateOrDefault(navResponse));
      setNextState(uiPortlet, state);

      // update the portlet with the next mode to display
      PortletMode mode = new PortletMode(getPortletModeOrDefault(navResponse));
      setNextMode(uiPortlet, mode);

      StateString navState = navResponse.getNavigationalState();
      if (navState != null) {
        uiPortlet.setNavigationalState(navResponse.getNavigationalState());
      }
      setupPublicRenderParams(uiPortlet, navResponse.getPublicNavigationalStateUpdates());

      // TODO: (mwringe) add this to the UpdateNavigationStateResponse.Event class instead of here
      class PortletEvent implements javax.portlet.Event {
        QName qName;

        Serializable value;

        public PortletEvent(QName qName, Serializable value) {
          this.qName = qName;
          this.value = value;
        }

        public String getName() {
          return qName.getLocalPart();
        }

        public QName getQName() {
          return qName;
        }

        public Serializable getValue() {
          return value;
        }
      }

      List<UpdateNavigationalStateResponse.Event> nsEvents = navResponse.getEvents();
      List<javax.portlet.Event> events = new ArrayList<javax.portlet.Event>(nsEvents.size());
      if (nsEvents != null && !nsEvents.isEmpty()) {
        for (UpdateNavigationalStateResponse.Event nsEvent : nsEvents) {
          javax.portlet.Event portletEvent =
              new PortletEvent(nsEvent.getName(), nsEvent.getPayload());
          events.add(portletEvent);
        }
      }

      return events;
    } catch (Exception e) {
      log.error("Problem while processesing event for the portlet: " + uiPortlet.getState(), e);
    }
    return null;
  }
 private static void clearMaximizedUIComponent(UIPage uiPage, UIPortlet uiPortlet) {
   if (uiPage.getMaximizedUIPortlet() != null
       && uiPage.getMaximizedUIPortlet().getId().equals(uiPortlet.getId())) {
     uiPage.setMaximizedUIPortlet(null);
   }
 }
    public void execute(Event<UIPortlet<S, C>> event) throws Exception {
      UIPortlet<S, C> uiPortlet = event.getSource();
      log.trace("Serve Resource for portlet: " + uiPortlet.getPortletContext());
      String resourceId = null;

      //
      PortalRequestContext context = (PortalRequestContext) event.getRequestContext();
      HttpServletResponse response = context.getResponse();

      //
      try {
        // Set the NavigationalState
        String navState =
            context.getRequestParameter(ExoPortletInvocationContext.NAVIGATIONAL_STATE_PARAM_NAME);
        if (navState != null) {
          uiPortlet.setNavigationalState(ParametersStateString.create(navState));
        }

        //
        ResourceInvocation resourceInvocation = uiPortlet.create(ResourceInvocation.class, context);

        // set the resourceId to be used in case of a problem
        resourceId = resourceInvocation.getResourceId();

        //
        PortletInvocationResponse portletResponse = uiPortlet.invoke(resourceInvocation);

        //
        int statusCode;
        MultiValuedPropertyMap<String> transportHeaders;
        String contentType;
        String charset;
        Object content;
        if (!(portletResponse instanceof ContentResponse)) {
          if (portletResponse instanceof ErrorResponse) {
            ErrorResponse errorResponse = (ErrorResponse) portletResponse;
            Throwable cause = errorResponse.getCause();
            if (cause != null) {
              log.trace("Got error response from portlet", cause);
            } else if (errorResponse.getMessage() != null) {
              log.trace("Got error response from portlet:" + errorResponse.getMessage());
            } else {
              log.trace("Got error response from portlet");
            }
          } else {
            log.trace(
                "Unexpected response type ["
                    + portletResponse
                    + "]. Expected a ContentResponse or an ErrorResponse.");
          }
          statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
          contentType = null;
          charset = null;
          transportHeaders = null;
          content = null;
        } else {
          //
          ContentResponse piResponse = (ContentResponse) portletResponse;
          ResponseProperties properties = piResponse.getProperties();
          transportHeaders = properties != null ? properties.getTransportHeaders() : null;

          // Look at status code if there is one and honour it
          String status =
              transportHeaders != null
                  ? transportHeaders.getValue(ResourceResponse.HTTP_STATUS_CODE)
                  : null;
          if (status != null) {
            try {
              statusCode = Integer.parseInt(status);
            } catch (NumberFormatException e) {
              statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            }
          } else {
            statusCode = HttpServletResponse.SC_OK;
          }

          //
          contentType = piResponse.getContentType();
          charset = piResponse.getEncoding();

          //
          log.trace(
              "Try to get a resource of type: "
                  + contentType
                  + " for the portlet: "
                  + uiPortlet.getPortletContext());
          if (piResponse.getChars() != null) {
            content = piResponse.getChars();
          } else if (piResponse.getBytes() != null) {
            content = piResponse.getBytes();
          } else {
            content = null;
          }
        }

        //
        response.setStatus(statusCode);

        // Set content type if any
        if (contentType != null) {
          response.setContentType(contentType);
        }

        // Set encoding
        if (charset != null) {
          response.setCharacterEncoding(charset);
        }

        // Send headers if any
        if (transportHeaders != null) {
          sendHeaders(transportHeaders, context);
        }

        // Send body if any
        if (content instanceof String) {
          context.getWriter().write((String) content);
        } else if (content instanceof byte[]) {
          byte[] bytes = (byte[]) content;
          response.setContentLength(bytes.length);
          OutputStream stream = response.getOutputStream();
          try {
            stream.write(bytes);
          } finally {
            Safe.close(stream);
          }
        }

        //
        response.flushBuffer();
      } catch (Exception e) {
        if (!e.getClass().toString().contains("ClientAbortException")) {
          log.error(
              "Problem while serving resource "
                  + (resourceId != null ? resourceId : "")
                  + " for the portlet: "
                  + uiPortlet.getPortletContext().getId(),
              e);
        }
      } finally {
        /** The resource method does not need to go through the render phase */
        event.getRequestContext().setResponseComplete(true);
      }
    }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();

      UIPortalApplication uiPortalApp = uiPortlet.getAncestorOfType(UIPortalApplication.class);
      UIWorkingWorkspace uiWorkingWS =
          uiPortalApp.getChildById(UIPortalApplication.UI_WORKING_WS_ID);
      PortalRequestContext pcontext = (PortalRequestContext) event.getRequestContext();
      pcontext.addUIComponentToUpdateByAjax(uiWorkingWS);
      pcontext.ignoreAJAXUpdateOnPortlets(true);

      String windowState = null;

      Object changeWindowStateAttribute =
          event.getRequestContext().getAttribute(CHANGE_WINDOW_STATE_EVENT);
      if (changeWindowStateAttribute != null && changeWindowStateAttribute instanceof String) {
        windowState = (String) changeWindowStateAttribute;
      }

      if (windowState == null) {
        windowState = event.getRequestContext().getRequestParameter(Constants.PORTAL_WINDOW_STATE);
      }
      if (windowState == null) {
        windowState = event.getRequestContext().getRequestParameter(UIComponent.OBJECTID).trim();
      }

      if (windowState == null) {
        windowState = uiPortlet.getCurrentWindowState().toString();
      }

      UIPageBody uiPageBody = uiPortlet.getAncestorOfType(UIPageBody.class);
      UIPage uiPage = uiPortlet.getAncestorOfType(UIPage.class);
      if (windowState.equals(WindowState.MAXIMIZED.toString())) {
        if (uiPageBody != null) {
          uiPortlet.setCurrentWindowState(WindowState.MAXIMIZED);
          // TODO dang.tung: we have to set maximized portlet for page because in ShowMaxWindow case
          // the PageBody isn't rendered
          //                reference: UIPortalLifecycle, UIPageLifecycle, renderChildren() in
          // UIPageBody
          // ---------------------------------------------------------
          if (uiPage != null && uiPage.isShowMaxWindow()) {
            uiPage.setMaximizedUIPortlet(uiPortlet);
          }
          // ---------------------------------------------------------
          uiPageBody.setMaximizedUIComponent(uiPortlet);
        } else {
          uiPortlet.setCurrentWindowState(WindowState.NORMAL);
        }
        return;
      }
      if (uiPageBody != null) {
        UIPortlet maxPortlet = (UIPortlet) uiPageBody.getMaximizedUIComponent();
        if (maxPortlet == uiPortlet) {
          uiPageBody.setMaximizedUIComponent(null);
        }
      }
      // TODO dang.tung: for ShowMaxWindow situation
      // ----------------------------------------------------------------
      if (uiPage != null) {
        UIPortlet maxPortlet = (UIPortlet) uiPage.getMaximizedUIPortlet();
        if (maxPortlet == uiPortlet) {
          uiPage.setMaximizedUIPortlet(null);
        }
      }
      // -----------------------------------------------------------------
      if (windowState.equals(WindowState.MINIMIZED.toString())) {
        uiPortlet.setCurrentWindowState(WindowState.MINIMIZED);
        return;
      }
      uiPortlet.setCurrentWindowState(WindowState.NORMAL);
    }
    private void handleUpdateNavigationalStateResponse(
        UpdateNavigationalStateResponse navStateResponse,
        UIPortlet<S, C> uiPortlet,
        PortalRequestContext prcontext)
        throws Exception {
      /*
       * Update the portlet window state according to the action output
       * information
       *
       * If the current node is displaying a usual layout page, also tells the
       * page which portlet to render or not when the state is maximized
       */
      // Note: we should only update the WindowState if the
      // UpdateNavigationalStateResponse.getWindowState is not null,
      // otherwise it means the WindowState has not changed and we should use the current value.
      if (navStateResponse.getWindowState() != null) {
        WindowState state = new WindowState(getWindowStateOrDefault(navStateResponse));
        setNextState(uiPortlet, state);
      }

      // update the portlet with the next mode to display
      // Note: we should only update the Mode if the UpdateNavigationalStateResponse.getMode is not
      // null,
      // otherwise it means the mode has not changed and we should use the current value.
      if (navStateResponse.getMode() != null) {
        PortletMode mode = new PortletMode(getPortletModeOrDefault(navStateResponse));
        setNextMode(uiPortlet, mode);
      }

      /*
       * Cache the render parameters in the UI portlet component to handle the
       * navigational state. Each time a portlet is rendered (except using
       * directly a RenderURL) those parameters are added to the portlet request
       * to preserve the portlet state among all the portal clicks
       */

      //
      StateString navigationalState = navStateResponse.getNavigationalState();
      if (navigationalState != null) {
        uiPortlet.setNavigationalState(navigationalState);
      }

      // update the public render parameters with the changes from the invocation
      setupPublicRenderParams(uiPortlet, navStateResponse.getPublicNavigationalStateUpdates());

      /*
       * Handle the events returned by the action output and broadcast a new UI
       * event to the ProcessEventsActionListener that will then target the
       * portlet container service directly
       */

      // TODO: (mwringe) add this to the UpdateNavigationStateResponse.Event class instead of here
      class PortletEvent implements javax.portlet.Event {
        QName qName;

        Serializable value;

        public PortletEvent(QName qName, Serializable value) {
          this.qName = qName;
          this.value = value;
        }

        public String getName() {
          return qName.getLocalPart();
        }

        public QName getQName() {
          return qName;
        }

        public Serializable getValue() {
          return value;
        }
      }

      List<UpdateNavigationalStateResponse.Event> nsEvents = navStateResponse.getEvents();
      List<javax.portlet.Event> events = new ArrayList<javax.portlet.Event>(nsEvents.size());
      if (nsEvents != null && !nsEvents.isEmpty()) {
        for (UpdateNavigationalStateResponse.Event nsEvent : nsEvents) {
          if (uiPortlet.supportsPublishingEvent(nsEvent.getName())) {
            javax.portlet.Event portletEvent =
                new PortletEvent(nsEvent.getName(), nsEvent.getPayload());
            events.add(portletEvent);
          }
        }
      }

      if (events != null) {
        prcontext.setAttribute(PORTLET_EVENTS, new EventsWrapper(events));
        uiPortlet.createEvent("ProcessEvents", Phase.PROCESS, prcontext).broadcast();
      }
    }
 public boolean hasEditMode() {
   return uiPortlet_.getSupportModes().contains("edit");
 }