1 package org.ofbiz.content.data; 2 3 import java.io.IOException ; 4 import java.io.OutputStream ; 5 import java.util.HashMap ; 6 import java.util.List ; 7 import java.util.Locale ; 8 import java.util.Map ; 9 10 import javax.servlet.ServletContext ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 import javax.servlet.http.HttpSession ; 14 15 import org.ofbiz.base.util.Debug; 16 import org.ofbiz.base.util.GeneralException; 17 import org.ofbiz.base.util.UtilHttp; 18 import org.ofbiz.base.util.UtilMisc; 19 import org.ofbiz.base.util.UtilProperties; 20 import org.ofbiz.base.util.UtilValidate; 21 import org.ofbiz.entity.GenericDelegator; 22 import org.ofbiz.entity.GenericEntityException; 23 import org.ofbiz.entity.GenericPK; 24 import org.ofbiz.entity.GenericValue; 25 import org.ofbiz.service.GenericServiceException; 26 import org.ofbiz.service.LocalDispatcher; 27 28 37 public class DataEvents { 38 39 public static final String module = DataEvents.class.getName(); 40 public static final String err_resource = "ContentErrorUiLabel"; 41 42 public static String uploadImage(HttpServletRequest request, HttpServletResponse response) { 43 return DataResourceWorker.uploadAndStoreImage(request, "dataResourceId", "imageData"); 44 } 45 46 47 public static String serveImage(HttpServletRequest request, HttpServletResponse response) { 48 HttpSession session = request.getSession(); 49 ServletContext application = session.getServletContext(); 50 51 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 52 Map parameters = UtilHttp.getParameterMap(request); 53 54 Debug.log("Img UserAgent - " + request.getHeader("User-Agent"), module); 55 56 String dataResourceId = (String ) parameters.get("imgId"); 57 if (UtilValidate.isEmpty(dataResourceId)) { 58 String errorMsg = "Error getting image record from db: " + " dataResourceId is empty"; 59 Debug.logError(errorMsg, module); 60 request.setAttribute("_ERROR_MESSAGE_", errorMsg); 61 return "error"; 62 } 63 64 try { 65 GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); 66 if (!"Y".equals(dataResource.getString("isPublic"))) { 67 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 69 if (userLogin == null) { 70 String errorMsg = "You must be logged in to download the Data Resource with ID [" + dataResourceId + "]"; 71 Debug.logError(errorMsg, module); 72 request.setAttribute("_ERROR_MESSAGE_", errorMsg); 73 return "error"; 74 } 75 76 List contentAndRoleList = delegator.findByAnd("ContentAndRole", 79 UtilMisc.toMap("partyId", userLogin.get("partyId"), "dataResourceId", dataResourceId)); 80 if (contentAndRoleList.size() == 0) { 81 String errorMsg = "You do not have permission to download the Data Resource with ID [" + dataResourceId + "], ie you are not associated with it."; 82 Debug.logError(errorMsg, module); 83 request.setAttribute("_ERROR_MESSAGE_", errorMsg); 84 return "error"; 85 } 86 } 87 88 String mimeType = DataResourceWorker.getMimeType(dataResource); 89 91 String userAgent = request.getHeader("User-Agent"); 93 if (userAgent.indexOf("MSIE") > -1) { 94 Debug.log("Found MSIE changing mime type from - " + mimeType, module); 95 mimeType = "application/octet-stream"; 96 } 97 98 if (mimeType != null) { 99 response.setContentType(mimeType); 100 } 101 OutputStream os = response.getOutputStream(); 102 DataResourceWorker.streamDataResource(os, delegator, dataResourceId, "", application.getInitParameter("webSiteId"), UtilHttp.getLocale(request), application.getRealPath("/")); 103 os.flush(); 104 } catch (GenericEntityException e) { 105 String errMsg = "Error downloading digital product content: " + e.toString(); 106 Debug.logError(e, errMsg, module); 107 request.setAttribute("_ERROR_MESSAGE_", errMsg); 108 return "error"; 109 } catch (GeneralException e) { 110 String errMsg = "Error downloading digital product content: " + e.toString(); 111 Debug.logError(e, errMsg, module); 112 request.setAttribute("_ERROR_MESSAGE_", errMsg); 113 return "error"; 114 } catch (IOException e) { 115 String errMsg = "Error downloading digital product content: " + e.toString(); 116 Debug.logError(e, errMsg, module); 117 request.setAttribute("_ERROR_MESSAGE_", errMsg); 118 return "error"; 119 } 120 121 return "success"; 122 } 123 124 125 128 public static String persistDataResource(HttpServletRequest request, HttpServletResponse response) { 129 Map result = null; 130 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 131 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 132 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 133 Map paramMap = UtilHttp.getParameterMap(request); 134 String dataResourceId = (String )paramMap.get("dataResourceId"); 135 GenericValue dataResource = delegator.makeValue("DataResource", null); 136 dataResource.setPKFields(paramMap); 137 dataResource.setNonPKFields(paramMap); 138 Map serviceInMap = new HashMap (dataResource); 139 serviceInMap.put("userLogin", userLogin); 140 String mode = (String )paramMap.get("mode"); 141 Locale locale = UtilHttp.getLocale(request); 142 143 if (mode != null && mode.equals("UPDATE")) { 144 try { 145 result = dispatcher.runSync("updateDataResource", serviceInMap); 146 } catch (GenericServiceException e) { 147 String errMsg = UtilProperties.getMessage(DataEvents.err_resource, "dataEvents.error_call_update_service", locale); 148 String errorMsg = "Error calling the updateDataResource service." + e.toString(); 149 Debug.logError(e, errorMsg, module); 150 request.setAttribute("_ERROR_MESSAGE_", errMsg + e.toString()); 151 return "error"; 152 } 153 } else { 154 mode = "CREATE"; 155 try { 156 result = dispatcher.runSync("createDataResource", serviceInMap); 157 } catch (GenericServiceException e) { 158 String errMsg = UtilProperties.getMessage(DataEvents.err_resource, "dataEvents.error_call_create_service", locale); 159 String errorMsg = "Error calling the createDataResource service." + e.toString(); 160 Debug.logError(e, errorMsg, module); 161 request.setAttribute("_ERROR_MESSAGE_", errMsg + e.toString()); 162 return "error"; 163 } 164 dataResourceId = (String )result.get("dataResourceId"); 165 dataResource.set("dataResourceId", dataResourceId); 166 } 167 168 169 GenericPK pk = dataResource.getPrimaryKey(); 171 HttpSession session = request.getSession(); 172 174 String returnStr = "success"; 175 if (mode.equals("CREATE") ) { 176 request.setAttribute("dataResourceId", result.get("dataResourceId") ); 178 String dataResourceTypeId = (String )serviceInMap.get("dataResourceTypeId"); 179 if (dataResourceTypeId != null) { 180 if (dataResourceTypeId.equals("ELECTRONIC_TEXT") 181 || dataResourceTypeId.equals("IMAGE_OBJECT") ) { 182 returnStr = dataResourceTypeId; 183 } 184 } 185 } 186 187 return returnStr; 188 } 189 } 190 | Popular Tags |