1 25 package org.ofbiz.content.view; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 import java.io.StringWriter ; 31 import java.io.UnsupportedEncodingException ; 32 import java.io.Writer ; 33 import java.util.List ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 import java.util.Locale ; 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 64 public class SimpleContentViewHandler implements ViewHandler { 65 66 public static final String module = SimpleContentViewHandler.class.getName(); 67 protected ServletContext servletContext = null; 68 69 public void init(ServletContext context) throws ViewHandlerException { 70 this.servletContext = context; 71 } 72 75 public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { 76 77 String contentId = request.getParameter("contentId"); 78 String rootContentId = request.getParameter("rootContentId"); 79 String dataResourceId = request.getParameter("dataResourceId"); 80 String contentRevisionSeqId = request.getParameter("contentRevisionSeqId"); 81 String mimeTypeId = request.getParameter("mimeTypeId"); 82 ByteWrapper byteWrapper = null; 83 Locale locale = UtilHttp.getLocale(request); 84 String rootDir = null; 85 String webSiteId = null; 86 String https = null; 87 88 if (UtilValidate.isEmpty(rootDir)) { 89 rootDir = servletContext.getRealPath("/"); 90 } 91 if (UtilValidate.isEmpty(webSiteId)) { 92 webSiteId = (String ) servletContext.getAttribute("webSiteId"); 93 } 94 if (UtilValidate.isEmpty(https)) { 95 https = (String ) 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 bais = new ByteArrayInputStream (byteWrapper.getBytes()); 121 String 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 String 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 e) { 144 throw new ViewHandlerException(e.getMessage()); 145 } catch(GeneralException e) { 146 throw new ViewHandlerException(e.getMessage()); 147 } 148 } 149 } 150 | Popular Tags |