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