001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journalcontent.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.PortletRequestModel;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.PrefsParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.journal.model.JournalArticleDisplay;
027    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
028    
029    import java.io.OutputStream;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletPreferences;
035    import javax.portlet.ResourceRequest;
036    import javax.portlet.ResourceResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Raymond Aug??
043     */
044    public class WebContentAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    PortletPreferences portletPreferences = actionRequest.getPreferences();
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    long groupId = PrefsParamUtil.getLong(
059                            portletPreferences, actionRequest, "groupId");
060                    String articleId = PrefsParamUtil.getString(
061                            portletPreferences, actionRequest, "articleId");
062                    String ddmTemplateKey = PrefsParamUtil.getString(
063                            portletPreferences, actionRequest, "ddmTemplateKey");
064    
065                    String viewMode = ParamUtil.getString(actionRequest, "viewMode");
066                    String languageId = LanguageUtil.getLanguageId(actionRequest);
067                    int page = ParamUtil.getInteger(actionRequest, "page", 1);
068    
069                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
070                            JournalContentUtil.getDisplay(
071                                    groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
072                                    new PortletRequestModel(actionRequest, actionResponse),
073                                    themeDisplay);
074                    }
075            }
076    
077            @Override
078            public void serveResource(
079                            ActionMapping actionMapping, ActionForm actionForm,
080                            PortletConfig portletConfig, ResourceRequest resourceRequest,
081                            ResourceResponse resourceResponse)
082                    throws Exception {
083    
084                    String contentType = ParamUtil.getString(
085                            resourceRequest, "contentType");
086    
087                    if (Validator.isNotNull(contentType)) {
088                            resourceResponse.setContentType(contentType);
089                    }
090    
091                    if (resourceRequest.getResourceID() != null) {
092                            super.serveResource(
093                                    actionMapping, actionForm, portletConfig, resourceRequest,
094                                    resourceResponse);
095                    }
096                    else {
097                            PortletPreferences portletPreferences =
098                                    resourceRequest.getPreferences();
099    
100                            ThemeDisplay themeDisplay =
101                                    (ThemeDisplay)resourceRequest.getAttribute(
102                                            WebKeys.THEME_DISPLAY);
103    
104                            long groupId = PrefsParamUtil.getLong(
105                                    portletPreferences, resourceRequest, "groupId");
106                            String articleId = PrefsParamUtil.getString(
107                                    portletPreferences, resourceRequest, "articleId");
108                            String ddmTemplateKey = PrefsParamUtil.getString(
109                                    portletPreferences, resourceRequest, "ddmTemplateKey");
110    
111                            String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
112                            String languageId = LanguageUtil.getLanguageId(resourceRequest);
113                            int page = ParamUtil.getInteger(resourceRequest, "page", 1);
114    
115                            JournalArticleDisplay articleDisplay = null;
116    
117                            if ((groupId > 0) && Validator.isNotNull(articleId)) {
118                                    articleDisplay = JournalContentUtil.getDisplay(
119                                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
120                                            page,
121                                            new PortletRequestModel(resourceRequest, resourceResponse),
122                                            themeDisplay);
123                            }
124    
125                            if (articleDisplay != null) {
126                                    try (OutputStream os =
127                                                    resourceResponse.getPortletOutputStream()) {
128    
129                                            String content = articleDisplay.getContent();
130    
131                                            byte[] bytes = content.getBytes(StringPool.UTF8);
132    
133                                            os.write(bytes);
134                                    }
135                            }
136                    }
137            }
138    
139    }