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