protected void renderExclusive(
      HttpServletRequest request, HttpServletResponse response, ThemeDisplay themeDisplay)
      throws Exception {

    RenderRequestImpl renderRequestImpl =
        (RenderRequestImpl) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

    RenderResponseImpl renderResponseImpl =
        (RenderResponseImpl) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);

    StringServletResponse stringResponse =
        (StringServletResponse) renderRequestImpl.getAttribute(WebKeys.STRING_SERVLET_RESPONSE);

    if (stringResponse == null) {
      stringResponse = (StringServletResponse) renderResponseImpl.getHttpServletResponse();

      Portlet portlet = processPortletRequest(request, response, PortletRequest.RENDER_PHASE);

      InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(portlet, null);

      invokerPortlet.render(renderRequestImpl, renderResponseImpl);

      if (Validator.isNull(stringResponse.getString())) {
        stringResponse.setString(null);
      }
    }

    renderResponseImpl.transferHeaders(response);

    if (stringResponse.isCalledGetOutputStream()) {
      UnsyncByteArrayOutputStream ubaos = stringResponse.getUnsyncByteArrayOutputStream();

      InputStream is = new UnsyncByteArrayInputStream(ubaos.unsafeGetByteArray(), 0, ubaos.size());

      ServletResponseUtil.sendFile(
          request,
          response,
          renderResponseImpl.getResourceName(),
          is,
          renderResponseImpl.getContentType());
    } else if (stringResponse.isCalledGetWriter()) {
      byte[] content = stringResponse.getString().getBytes(StringPool.UTF8);

      ServletResponseUtil.sendFile(
          request,
          response,
          renderResponseImpl.getResourceName(),
          content,
          renderResponseImpl.getContentType());
    }

    renderRequestImpl.cleanUp();
  }
Ejemplo n.º 2
0
 protected void destroyPortlets(List<Portlet> portlets) throws Exception {
   for (Portlet portlet : portlets) {
     PortletInstanceFactoryUtil.destroy(portlet);
   }
 }
  protected Portlet processPortletRequest(
      HttpServletRequest request, HttpServletResponse response, String lifecycle) throws Exception {

    HttpSession session = request.getSession();

    long companyId = PortalUtil.getCompanyId(request);
    User user = PortalUtil.getUser(request);
    Layout layout = (Layout) request.getAttribute(WebKeys.LAYOUT);

    String portletId = ParamUtil.getString(request, "p_p_id");

    if (Validator.isNull(portletId)) {
      return null;
    }

    Portlet portlet = PortletLocalServiceUtil.getPortletById(companyId, portletId);

    if (portlet == null) {
      return null;
    }

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    themeDisplay.setScopeGroupId(PortalUtil.getScopeGroupId(request, portletId));

    ServletContext servletContext = (ServletContext) request.getAttribute(WebKeys.CTX);

    InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(portlet, servletContext);

    if (user != null) {
      InvokerPortletImpl.clearResponse(
          session, layout.getPrimaryKey(), portletId, LanguageUtil.getLanguageId(request));
    }

    PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);
    PortletContext portletContext = portletConfig.getPortletContext();

    WindowState windowState =
        WindowStateFactory.getWindowState(ParamUtil.getString(request, "p_p_state"));

    if (layout.isTypeControlPanel()
        && ((windowState == null)
            || windowState.equals(WindowState.NORMAL)
            || (Validator.isNull(windowState.toString())))) {

      windowState = WindowState.MAXIMIZED;
    }

    PortletMode portletMode =
        PortletModeFactory.getPortletMode(ParamUtil.getString(request, "p_p_mode"));

    PortletPreferencesIds portletPreferencesIds =
        PortletPreferencesFactoryUtil.getPortletPreferencesIds(request, portletId);

    PortletPreferences portletPreferences =
        PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds);

    processPublicRenderParameters(request, layout, portlet);

    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
      String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

      if (_log.isDebugEnabled()) {
        _log.debug("Content type " + contentType);
      }

      UploadServletRequest uploadRequest = null;

      try {
        if ((contentType != null) && (contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA))) {

          PortletConfigImpl invokerPortletConfigImpl =
              (PortletConfigImpl) invokerPortlet.getPortletConfig();

          if (invokerPortlet.isStrutsPortlet()
              || ((invokerPortletConfigImpl != null) && (!invokerPortletConfigImpl.isWARFile()))) {

            uploadRequest = new UploadServletRequestImpl(request);

            request = uploadRequest;
          }
        }

        if (PropsValues.AUTH_TOKEN_CHECK_ENABLED && invokerPortlet.isCheckAuthToken()) {

          AuthTokenUtil.check(request);
        }

        ActionRequestImpl actionRequestImpl =
            ActionRequestFactory.create(
                request,
                portlet,
                invokerPortlet,
                portletContext,
                windowState,
                portletMode,
                portletPreferences,
                layout.getPlid());

        ActionResponseImpl actionResponseImpl =
            ActionResponseFactory.create(
                actionRequestImpl, response, portletId, user, layout, windowState, portletMode);

        actionRequestImpl.defineObjects(portletConfig, actionResponseImpl);

        ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequestImpl);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);

        invokerPortlet.processAction(actionRequestImpl, actionResponseImpl);

        actionResponseImpl.transferHeaders(response);

        RenderParametersPool.put(
            request, layout.getPlid(), portletId, actionResponseImpl.getRenderParameterMap());

        List<LayoutTypePortlet> layoutTypePortlets = null;

        if (!actionResponseImpl.getEvents().isEmpty()) {
          if (PropsValues.PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET) {
            layoutTypePortlets =
                getLayoutTypePortlets(layout.getGroupId(), layout.isPrivateLayout());
          } else {
            if (layout.isTypePortlet()) {
              LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

              layoutTypePortlets = new ArrayList<LayoutTypePortlet>();

              layoutTypePortlets.add(layoutTypePortlet);
            }
          }

          processEvents(actionRequestImpl, actionResponseImpl, layoutTypePortlets);

          actionRequestImpl.defineObjects(portletConfig, actionResponseImpl);
        }
      } finally {
        if (uploadRequest != null) {
          uploadRequest.cleanUp();
        }

        ServiceContextThreadLocal.popServiceContext();
      }
    } else if (lifecycle.equals(PortletRequest.RENDER_PHASE)
        || lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {

      PortalUtil.updateWindowState(portletId, user, layout, windowState, request);

      PortalUtil.updatePortletMode(portletId, user, layout, portletMode, request);
    }

    if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
      PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

      String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portletId);

      portletDisplay.setId(portletId);
      portletDisplay.setRootPortletId(portlet.getRootPortletId());
      portletDisplay.setInstanceId(portlet.getInstanceId());
      portletDisplay.setResourcePK(portletPrimaryKey);
      portletDisplay.setPortletName(portletConfig.getPortletName());
      portletDisplay.setNamespace(PortalUtil.getPortletNamespace(portletId));

      ResourceRequestImpl resourceRequestImpl =
          ResourceRequestFactory.create(
              request,
              portlet,
              invokerPortlet,
              portletContext,
              windowState,
              portletMode,
              portletPreferences,
              layout.getPlid());

      ResourceResponseImpl resourceResponseImpl =
          ResourceResponseFactory.create(resourceRequestImpl, response, portletId, companyId);

      resourceRequestImpl.defineObjects(portletConfig, resourceResponseImpl);

      try {
        ServiceContext serviceContext = ServiceContextFactory.getInstance(resourceRequestImpl);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);

        invokerPortlet.serveResource(resourceRequestImpl, resourceResponseImpl);
      } finally {
        ServiceContextThreadLocal.popServiceContext();
      }
    }

    return portlet;
  }
  protected void processEvent(
      PortletRequestImpl portletRequestImpl,
      StateAwareResponseImpl stateAwareResponseImpl,
      List<LayoutTypePortlet> layoutTypePortlets,
      LayoutTypePortlet layoutTypePortlet,
      Portlet portlet,
      Event event)
      throws Exception {

    HttpServletRequest request = portletRequestImpl.getHttpServletRequest();
    HttpServletResponse response = stateAwareResponseImpl.getHttpServletResponse();
    HttpSession session = request.getSession();

    String portletId = portlet.getPortletId();

    ServletContext servletContext = (ServletContext) request.getAttribute(WebKeys.CTX);

    InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(portlet, servletContext);

    PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);
    PortletContext portletContext = portletConfig.getPortletContext();

    WindowState windowState = null;

    if (layoutTypePortlet.hasStateMaxPortletId(portletId)) {
      windowState = WindowState.MAXIMIZED;
    } else if (layoutTypePortlet.hasStateMinPortletId(portletId)) {
      windowState = WindowState.MINIMIZED;
    } else {
      windowState = WindowState.NORMAL;
    }

    PortletMode portletMode = null;

    if (layoutTypePortlet.hasModeAboutPortletId(portletId)) {
      portletMode = LiferayPortletMode.ABOUT;
    } else if (layoutTypePortlet.hasModeConfigPortletId(portletId)) {
      portletMode = LiferayPortletMode.CONFIG;
    } else if (layoutTypePortlet.hasModeEditPortletId(portletId)) {
      portletMode = PortletMode.EDIT;
    } else if (layoutTypePortlet.hasModeEditDefaultsPortletId(portletId)) {
      portletMode = LiferayPortletMode.EDIT_DEFAULTS;
    } else if (layoutTypePortlet.hasModeEditGuestPortletId(portletId)) {
      portletMode = LiferayPortletMode.EDIT_GUEST;
    } else if (layoutTypePortlet.hasModeHelpPortletId(portletId)) {
      portletMode = PortletMode.HELP;
    } else if (layoutTypePortlet.hasModePreviewPortletId(portletId)) {
      portletMode = LiferayPortletMode.PREVIEW;
    } else if (layoutTypePortlet.hasModePrintPortletId(portletId)) {
      portletMode = LiferayPortletMode.PRINT;
    } else {
      portletMode = PortletMode.VIEW;
    }

    User user = stateAwareResponseImpl.getUser();
    Layout layout = stateAwareResponseImpl.getLayout();

    PortletPreferences portletPreferences = portletRequestImpl.getPreferencesImpl();

    EventRequestImpl eventRequestImpl =
        EventRequestFactory.create(
            request,
            portlet,
            invokerPortlet,
            portletContext,
            windowState,
            portletMode,
            portletPreferences,
            layoutTypePortlet.getLayout().getPlid());

    eventRequestImpl.setEvent(serializeEvent(event, invokerPortlet.getPortletClassLoader()));

    EventResponseImpl eventResponseImpl =
        EventResponseFactory.create(eventRequestImpl, response, portletId, user, layout);

    eventRequestImpl.defineObjects(portletConfig, eventResponseImpl);

    try {
      try {
        InvokerPortletImpl.clearResponse(
            session,
            layout.getPrimaryKey(),
            portletId,
            LanguageUtil.getLanguageId(eventRequestImpl));

        invokerPortlet.processEvent(eventRequestImpl, eventResponseImpl);

        if (eventResponseImpl.isCalledSetRenderParameter()) {
          Map<String, String[]> renderParameterMap = new HashMap<String, String[]>();

          MapUtil.copy(eventResponseImpl.getRenderParameterMap(), renderParameterMap);

          RenderParametersPool.put(request, layout.getPlid(), portletId, renderParameterMap);
        }
      } catch (UnavailableException ue) {
        throw ue;
      }

      processEvents(eventRequestImpl, eventResponseImpl, layoutTypePortlets);
    } finally {
      eventRequestImpl.cleanUp();
    }
  }