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.journal.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.journal.model.JournalArticle;
023    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
024    import com.liferay.util.servlet.ServletResponseUtil;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.struts.action.Action;
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Raymond Augé
036     * @author Brian Wing Shun Chan
037     */
038    public class GetLatestArticleContentAction extends Action {
039    
040            public ActionForward execute(
041                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
042                            HttpServletResponse response)
043                    throws Exception {
044    
045                    try {
046                            long groupId = ParamUtil.getLong(request, "groupId");
047                            String articleId = ParamUtil.getString(request, "articleId");
048    
049                            String languageId = LanguageUtil.getLanguageId(request);
050    
051                            JournalArticle article =
052                                    JournalArticleLocalServiceUtil.getLatestArticle(
053                                            groupId, articleId, WorkflowConstants.STATUS_APPROVED);
054    
055                            String fileName = "content.xml";
056                            byte[] bytes = article.getContentByLocale(languageId).getBytes();
057    
058                            ServletResponseUtil.sendFile(
059                                    request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
060    
061                            return null;
062                    }
063                    catch (Exception e) {
064                            PortalUtil.sendError(e, request, response);
065    
066                            return null;
067                    }
068            }
069    
070    }