001    /**
002     * Copyright (c) 2000-2010 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            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    PortletPreferences preferences = actionRequest.getPreferences();
052    
053                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
054                            WebKeys.THEME_DISPLAY);
055    
056                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
057    
058                    if (groupId < 1) {
059                            groupId = GetterUtil.getLong(
060                                    preferences.getValue("group-id", StringPool.BLANK));
061                    }
062    
063                    String articleId = ParamUtil.getString(actionRequest, "articleId");
064                    String templateId = ParamUtil.getString(actionRequest, "templateId");
065    
066                    if (Validator.isNull(articleId)) {
067                            articleId = GetterUtil.getString(
068                                    preferences.getValue("article-id", StringPool.BLANK));
069                            templateId = GetterUtil.getString(
070                                    preferences.getValue("template-id", StringPool.BLANK));
071                    }
072    
073                    String viewMode = ParamUtil.getString(actionRequest, "viewMode");
074                    String languageId = LanguageUtil.getLanguageId(actionRequest);
075                    int page = ParamUtil.getInteger(actionRequest, "page", 1);
076    
077                    String xmlRequest = PortletRequestUtil.toXML(
078                            actionRequest, actionResponse);
079    
080                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
081                            JournalContentUtil.getDisplay(
082                                    groupId, articleId, templateId, viewMode, languageId,
083                                    themeDisplay, page, xmlRequest);
084                    }
085            }
086    
087            public void serveResource(
088                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
089                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
090                    throws Exception {
091    
092                    String contentType = ParamUtil.getString(
093                            resourceRequest, "contentType");
094    
095                    if (Validator.isNotNull(contentType)) {
096                            resourceResponse.setContentType(contentType);
097                    }
098    
099                    if (resourceRequest.getResourceID() != null) {
100                            super.serveResource(
101                                    mapping, form, portletConfig, resourceRequest,
102                                    resourceResponse);
103                    }
104                    else {
105                            PortletPreferences preferences = resourceRequest.getPreferences();
106    
107                            ThemeDisplay themeDisplay =
108                                    (ThemeDisplay)resourceRequest.getAttribute(
109                                            WebKeys.THEME_DISPLAY);
110    
111                            long groupId = ParamUtil.getLong(resourceRequest, "groupId");
112    
113                            if (groupId < 1) {
114                                    groupId = GetterUtil.getLong(
115                                            preferences.getValue("group-id", StringPool.BLANK));
116                            }
117    
118                            String articleId = ParamUtil.getString(
119                                    resourceRequest, "articleId");
120                            String templateId = ParamUtil.getString(
121                                    resourceRequest, "templateId");
122    
123                            if (Validator.isNull(articleId)) {
124                                    articleId = GetterUtil.getString(
125                                            preferences.getValue("article-id", StringPool.BLANK));
126                                    templateId = GetterUtil.getString(
127                                            preferences.getValue("template-id", StringPool.BLANK));
128                            }
129    
130                            String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
131                            String languageId = LanguageUtil.getLanguageId(resourceRequest);
132                            int page = ParamUtil.getInteger(resourceRequest, "page", 1);
133                            String xmlRequest = PortletRequestUtil.toXML(
134                                    resourceRequest, resourceResponse);
135    
136                            JournalArticleDisplay articleDisplay = null;
137    
138                            if ((groupId > 0) && Validator.isNotNull(articleId)) {
139                                    articleDisplay = JournalContentUtil.getDisplay(
140                                            groupId, articleId, templateId, viewMode, languageId,
141                                            themeDisplay, page, xmlRequest);
142                            }
143    
144                            if (articleDisplay != null) {
145                                    OutputStream os = resourceResponse.getPortletOutputStream();
146    
147                                    try {
148                                            String content = articleDisplay.getContent();
149    
150                                            byte[] bytes = content.getBytes(StringPool.UTF8);
151    
152                                            os.write(bytes);
153                                    }
154                                    finally {
155                                            os.close();
156                                    }
157                            }
158                    }
159            }
160    
161    }