1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.util.ContentTypes;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.journal.model.JournalTemplate;
30  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
31  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
32  import com.liferay.portlet.journal.util.JournalUtil;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  
35  import java.util.Map;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.apache.struts.action.Action;
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Raymond Augé
50   *
51   */
52  public class GetTemplateAction extends Action {
53  
54      public ActionForward execute(
55              ActionMapping mapping, ActionForm form, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          try {
60              long groupId = ParamUtil.getLong(request, "groupId");
61              String templateId = getTemplateId(request);
62  
63              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64                  WebKeys.THEME_DISPLAY);
65  
66              Map<String, String> tokens = JournalUtil.getTokens(
67                  groupId, themeDisplay);
68  
69              tokens.put("template_id", templateId);
70  
71              String languageId = LanguageUtil.getLanguageId(request);
72  
73              boolean transform = ParamUtil.get(request, "transform", true);
74  
75              JournalTemplate template =
76                  JournalTemplateLocalServiceUtil.getTemplate(
77                      groupId, templateId);
78  
79              String script = JournalUtil.getTemplateScript(
80                  template, tokens, languageId, transform);
81  
82              String extension = JournalTemplateImpl.LANG_TYPE_VM;
83  
84              if (template.getLangType() != null) {
85                  extension = template.getLangType();
86              }
87  
88              String fileName = null;
89              byte[] bytes = script.getBytes();
90  
91              String contentType = ContentTypes.TEXT_PLAIN_UTF8;
92  
93              if (Validator.equals(
94                      extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
95  
96                  contentType = ContentTypes.TEXT_CSS_UTF8;
97              }
98              else if (Validator.equals(
99                      extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
100 
101                 contentType = ContentTypes.TEXT_XML_UTF8;
102             }
103 
104             ServletResponseUtil.sendFile(
105                 response, fileName, bytes, contentType);
106 
107             return null;
108         }
109         catch (Exception e) {
110             PortalUtil.sendError(e, request, response);
111 
112             return null;
113         }
114     }
115 
116     protected String getTemplateId(HttpServletRequest request) {
117         String templateId = ParamUtil.getString(request, "templateId");
118 
119         // Backwards compatibility
120 
121         if (Validator.isNull(templateId)) {
122             templateId = ParamUtil.getString(request, "template_id");
123         }
124 
125         return templateId;
126     }
127 
128 }