KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > data > DataEvents


1 package org.ofbiz.content.data;
2
3 import java.io.IOException JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.servlet.ServletContext JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import javax.servlet.http.HttpSession JavaDoc;
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 /**
29  * DataEvents Class
30  *
31  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
32  * @version $Rev: 6995 $
33  * @since 3.0
34  *
35  *
36  */

37 public class DataEvents {
38
39     public static final String JavaDoc module = DataEvents.class.getName();
40     public static final String JavaDoc err_resource = "ContentErrorUiLabel";
41
42     public static String JavaDoc uploadImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
43         return DataResourceWorker.uploadAndStoreImage(request, "dataResourceId", "imageData");
44     }
45
46     /** Streams ImageDataResource data to the output. */
47     public static String JavaDoc serveImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
48         HttpSession JavaDoc session = request.getSession();
49         ServletContext JavaDoc application = session.getServletContext();
50
51         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
52         Map JavaDoc parameters = UtilHttp.getParameterMap(request);
53         
54         Debug.log("Img UserAgent - " + request.getHeader("User-Agent"), module);
55
56         String JavaDoc dataResourceId = (String JavaDoc) parameters.get("imgId");
57         if (UtilValidate.isEmpty(dataResourceId)) {
58             String JavaDoc 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                 // now require login...
68
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
69                 if (userLogin == null) {
70                     String JavaDoc 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                 // make sure the logged in user can download this content; otherwise is a pretty big security hole for DataResource records...
77
// TODO: should we restrict the roleTypeId?
78
List JavaDoc contentAndRoleList = delegator.findByAnd("ContentAndRole",
79                         UtilMisc.toMap("partyId", userLogin.get("partyId"), "dataResourceId", dataResourceId));
80                 if (contentAndRoleList.size() == 0) {
81                     String JavaDoc 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 JavaDoc mimeType = DataResourceWorker.getMimeType(dataResource);
89             //if (Debug.infoOn()) Debug.logInfo("in serveImage, imageType:" + imageType, module);
90

91             // hack for IE and mime types
92
String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
115             String JavaDoc 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     /** Dual create and edit event.
126      * Needed to make permission criteria available to services.
127      */

128     public static String JavaDoc persistDataResource(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
129         Map JavaDoc 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 JavaDoc paramMap = UtilHttp.getParameterMap(request);
134         String JavaDoc dataResourceId = (String JavaDoc)paramMap.get("dataResourceId");
135         GenericValue dataResource = delegator.makeValue("DataResource", null);
136         dataResource.setPKFields(paramMap);
137         dataResource.setNonPKFields(paramMap);
138         Map JavaDoc serviceInMap = new HashMap JavaDoc(dataResource);
139         serviceInMap.put("userLogin", userLogin);
140         String JavaDoc mode = (String JavaDoc)paramMap.get("mode");
141         Locale JavaDoc 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 JavaDoc errMsg = UtilProperties.getMessage(DataEvents.err_resource, "dataEvents.error_call_update_service", locale);
148                 String JavaDoc 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 JavaDoc errMsg = UtilProperties.getMessage(DataEvents.err_resource, "dataEvents.error_call_create_service", locale);
159                 String JavaDoc 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 JavaDoc)result.get("dataResourceId");
165             dataResource.set("dataResourceId", dataResourceId);
166         }
167         
168    
169         // Save the primary key so that it can be used in a "quick pick" list later
170
GenericPK pk = dataResource.getPrimaryKey();
171         HttpSession JavaDoc session = request.getSession();
172         //ContentManagementWorker.mruAdd(session, pk);
173

174         String JavaDoc returnStr = "success";
175         if (mode.equals("CREATE") ) {
176             // Set up return message to guide selection of follow on view
177
request.setAttribute("dataResourceId", result.get("dataResourceId") );
178             String JavaDoc dataResourceTypeId = (String JavaDoc)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