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