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