KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > view > SimpleContentViewHandler


1 /*
2  * $Id: FopPdfViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.content.view;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.UtilHttp;
43 import org.ofbiz.base.util.UtilValidate;
44 import org.ofbiz.base.util.UtilXml;
45 import org.ofbiz.base.util.UtilMisc;
46 import org.ofbiz.entity.util.ByteWrapper;
47 import org.ofbiz.entity.GenericDelegator;
48 import org.ofbiz.entity.GenericValue;
49 import org.ofbiz.entity.GenericEntityException;
50 import org.ofbiz.content.data.DataResourceWorker;
51 import org.ofbiz.webapp.control.RequestHandlerException;
52 import org.ofbiz.webapp.view.ViewHandler;
53 import org.ofbiz.webapp.view.ViewHandlerException;
54 import org.ofbiz.base.util.GeneralException;
55
56 /**
57  * Uses XSL-FO formatted templates to generate PDF views
58  * This handler will use JPublish to generate the XSL-FO
59  *
60  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
61  * @version $Rev: 5462 $
62  * @since 3.0
63  */

64 public class SimpleContentViewHandler implements ViewHandler {
65     
66     public static final String JavaDoc module = SimpleContentViewHandler.class.getName();
67     protected ServletContext JavaDoc servletContext = null;
68     
69     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
70         this.servletContext = context;
71     }
72     /**
73      * @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
74      */

75     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
76         
77         String JavaDoc contentId = request.getParameter("contentId");
78         String JavaDoc rootContentId = request.getParameter("rootContentId");
79         String JavaDoc dataResourceId = request.getParameter("dataResourceId");
80         String JavaDoc contentRevisionSeqId = request.getParameter("contentRevisionSeqId");
81         String JavaDoc mimeTypeId = request.getParameter("mimeTypeId");
82         ByteWrapper byteWrapper = null;
83         Locale JavaDoc locale = UtilHttp.getLocale(request);
84         String JavaDoc rootDir = null;
85         String JavaDoc webSiteId = null;
86         String JavaDoc https = null;
87         
88         if (UtilValidate.isEmpty(rootDir)) {
89             rootDir = servletContext.getRealPath("/");
90         }
91         if (UtilValidate.isEmpty(webSiteId)) {
92             webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
93         }
94         if (UtilValidate.isEmpty(https)) {
95             https = (String JavaDoc) servletContext.getAttribute("https");
96         }
97         try {
98             Debug.logInfo("SCVH(0a)- dataResourceId:" + dataResourceId, module);
99             GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
100             if (UtilValidate.isEmpty(dataResourceId)) {
101                 if (UtilValidate.isEmpty(contentRevisionSeqId)) {
102                    GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId));
103                    dataResourceId = content.getString("dataResourceId");
104                    Debug.logInfo("SCVH(0b)- dataResourceId:" + dataResourceId, module);
105                 } else {
106                    GenericValue contentRevisionItem = delegator.findByPrimaryKeyCache("ContentRevisionItem", UtilMisc.toMap("contentId", rootContentId, "itemContentId", contentId, "contentRevisionSeqId", contentRevisionSeqId));
107                    if (contentRevisionItem == null) {
108                        throw new ViewHandlerException("ContentRevisionItem record not found for contentId=" + rootContentId
109                                                       + ", contentRevisionSeqId=" + contentRevisionSeqId + ", itemContentId=" + contentId);
110                    }
111                    Debug.logInfo("SCVH(1)- contentRevisionItem:" + contentRevisionItem, module);
112                    Debug.logInfo("SCVH(2)-contentId=" + rootContentId
113                            + ", contentRevisionSeqId=" + contentRevisionSeqId + ", itemContentId=" + contentId, module);
114                    dataResourceId = contentRevisionItem.getString("newDataResourceId");
115                    Debug.logInfo("SCVH(3)- dataResourceId:" + dataResourceId, module);
116                 }
117             }
118             GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
119             byteWrapper = DataResourceWorker.getContentAsByteWrapper(delegator, dataResourceId, https, webSiteId, locale, rootDir);
120             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(byteWrapper.getBytes());
121             // hack for IE and mime types
122
//String userAgent = request.getHeader("User-Agent");
123
//if (userAgent.indexOf("MSIE") > -1) {
124
// Debug.log("Found MSIE changing mime type from - " + mimeTypeId, module);
125
// mimeTypeId = "application/octet-stream";
126
//}
127
// setup chararcter encoding and content type
128
String JavaDoc charset = dataResource.getString("characterSetId");
129             mimeTypeId = dataResource.getString("mimeTypeId");
130             if (UtilValidate.isEmpty(charset)) {
131                 charset = servletContext.getInitParameter("charset");
132             }
133             if (UtilValidate.isEmpty(charset)) {
134                 charset = "ISO-8859-1";
135             }
136
137             // setup content type
138
String JavaDoc contentType2 = UtilValidate.isNotEmpty(mimeTypeId) ? mimeTypeId + "; charset=" +charset : contentType;
139
140             UtilHttp.streamContentToBrowser(response, bais, byteWrapper.getLength(), contentType2);
141         } catch(GenericEntityException e) {
142             throw new ViewHandlerException(e.getMessage());
143         } catch(IOException JavaDoc e) {
144             throw new ViewHandlerException(e.getMessage());
145         } catch(GeneralException e) {
146             throw new ViewHandlerException(e.getMessage());
147         }
148      }
149 }
150
Popular Tags