001    /**
002     * Copyright (c) 2000-2013 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.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.journal.model.JournalArticleDisplay;
026    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
027    import com.liferay.util.portlet.PortletRequestUtil;
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 = ParamUtil.getLong(actionRequest, "groupId");
059    
060                    if (groupId < 1) {
061                            groupId = GetterUtil.getLong(
062                                    portletPreferences.getValue("groupId", null));
063                    }
064    
065                    String articleId = ParamUtil.getString(actionRequest, "articleId");
066                    String ddmTemplateKey = ParamUtil.getString(
067                            actionRequest, "ddmTemplateKey");
068    
069                    if (Validator.isNull(articleId)) {
070                            articleId = GetterUtil.getString(
071                                    portletPreferences.getValue("articleId", null));
072                            ddmTemplateKey = GetterUtil.getString(
073                                    portletPreferences.getValue("ddmTemplateKey", null));
074                    }
075    
076                    String viewMode = ParamUtil.getString(actionRequest, "viewMode");
077                    String languageId = LanguageUtil.getLanguageId(actionRequest);
078                    int page = ParamUtil.getInteger(actionRequest, "page", 1);
079    
080                    String xmlRequest = PortletRequestUtil.toXML(
081                            actionRequest, actionResponse);
082    
083                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
084                            JournalContentUtil.getDisplay(
085                                    groupId, articleId, ddmTemplateKey, viewMode, languageId,
086                                    themeDisplay, page, xmlRequest);
087                    }
088            }
089    
090            @Override
091            public void serveResource(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, ResourceRequest resourceRequest,
094                            ResourceResponse resourceResponse)
095                    throws Exception {
096    
097                    String contentType = ParamUtil.getString(
098                            resourceRequest, "contentType");
099    
100                    if (Validator.isNotNull(contentType)) {
101                            resourceResponse.setContentType(contentType);
102                    }
103    
104                    if (resourceRequest.getResourceID() != null) {
105                            super.serveResource(
106                                    actionMapping, actionForm, portletConfig, resourceRequest,
107                                    resourceResponse);
108                    }
109                    else {
110                            PortletPreferences portletPreferences =
111                                    resourceRequest.getPreferences();
112    
113                            ThemeDisplay themeDisplay =
114                                    (ThemeDisplay)resourceRequest.getAttribute(
115                                            WebKeys.THEME_DISPLAY);
116    
117                            long groupId = ParamUtil.getLong(resourceRequest, "groupId");
118    
119                            if (groupId < 1) {
120                                    groupId = GetterUtil.getLong(
121                                            portletPreferences.getValue("groupId", null));
122                            }
123    
124                            String articleId = ParamUtil.getString(
125                                    resourceRequest, "articleId");
126                            String ddmTemplateKey = ParamUtil.getString(
127                                    resourceRequest, "ddmTemplateKey");
128    
129                            if (Validator.isNull(articleId)) {
130                                    articleId = GetterUtil.getString(
131                                            portletPreferences.getValue("articleId", null));
132                                    ddmTemplateKey = GetterUtil.getString(
133                                            portletPreferences.getValue("ddmTemplateKey", null));
134                            }
135    
136                            String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
137                            String languageId = LanguageUtil.getLanguageId(resourceRequest);
138                            int page = ParamUtil.getInteger(resourceRequest, "page", 1);
139                            String xmlRequest = PortletRequestUtil.toXML(
140                                    resourceRequest, resourceResponse);
141    
142                            JournalArticleDisplay articleDisplay = null;
143    
144                            if ((groupId > 0) && Validator.isNotNull(articleId)) {
145                                    articleDisplay = JournalContentUtil.getDisplay(
146                                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
147                                            themeDisplay, page, xmlRequest);
148                            }
149    
150                            if (articleDisplay != null) {
151                                    OutputStream os = resourceResponse.getPortletOutputStream();
152    
153                                    try {
154                                            String content = articleDisplay.getContent();
155    
156                                            byte[] bytes = content.getBytes(StringPool.UTF8);
157    
158                                            os.write(bytes);
159                                    }
160                                    finally {
161                                            os.close();
162                                    }
163                            }
164                    }
165            }
166    
167    }