1 64 package com.jcorporate.expresso.services.controller.dbmaint; 65 66 import com.jcorporate.expresso.core.controller.ControllerException; 67 import com.jcorporate.expresso.core.controller.ControllerRequest; 68 import com.jcorporate.expresso.core.controller.ControllerResponse; 69 import com.jcorporate.expresso.core.controller.ErrorCollection; 70 import com.jcorporate.expresso.core.controller.NonHandleableException; 71 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 72 import com.jcorporate.expresso.core.dataobjects.DataObject; 73 import com.jcorporate.expresso.core.dataobjects.NestableDataObject; 74 import com.jcorporate.expresso.core.dataobjects.Securable; 75 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCDataObject; 76 import com.jcorporate.expresso.core.dataobjects.jdbc.LobField; 77 import com.jcorporate.expresso.core.db.DBException; 78 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 79 import com.jcorporate.expresso.core.misc.StringUtil; 80 import com.jcorporate.expresso.core.security.User; 81 import com.jcorporate.expresso.services.dbobj.MediaDBObject; 82 import com.jcorporate.expresso.services.dbobj.MimeTypes; 83 import com.jcorporate.expresso.services.dbobj.Setup; 84 import org.apache.log4j.Logger; 85 86 import javax.servlet.ServletOutputStream ; 87 import javax.servlet.ServletResponse ; 88 import javax.servlet.http.HttpServletResponse ; 89 import java.io.InputStream ; 90 91 92 101 102 public class ViewBlob extends DynamicCmd { 103 104 107 private static Logger log = Logger.getLogger(ViewBlob.class); 108 109 112 public ViewBlob() { 113 super("ViewBlob", "Binary File Contents"); 114 this.addRequiredParameter("fieldName"); 115 } 116 117 123 public void run(ControllerRequest newRequest, ControllerResponse newResponse) 124 throws com.jcorporate.expresso.core.controller.NonHandleableException, 125 com.jcorporate.expresso.core.controller.ControllerException { 126 super.run(newRequest, newResponse); 127 128 DataObject curDBObj = null; 129 try { 130 curDBObj = this.retrieveMyDBObject(); 131 132 133 139 if (curDBObj instanceof NestableDataObject) { 140 if (!(((NestableDataObject) curDBObj).getNestedFromFieldName(newRequest.getParameter("fieldName")) instanceof JDBCDataObject)) { 141 ErrorCollection ee = new ErrorCollection(); 142 ee.addError("Blob viewing is only possible under nested JDBC dataobjects."); 143 newResponse.saveErrors(ee); 144 this.transition("Update", newRequest, newResponse); 145 return; 146 } 147 } else if (!(curDBObj instanceof JDBCDataObject)) { 148 ErrorCollection ee = new ErrorCollection(); 149 ee.addError("Blob viewing is only possible under JDBC dataobjects."); 150 newResponse.saveErrors(ee); 151 this.transition("Update", newRequest, newResponse); 152 return; 153 } 154 } catch (DBException ex) { 155 ErrorCollection ee = new ErrorCollection(); 156 ee.addError(ex); 157 newResponse.saveErrors(ee); 158 this.transition("Update", newRequest, newResponse); 159 return; 160 } 161 162 if (!(newRequest instanceof ServletControllerRequest)) { 163 throw new ControllerException("This state can only be" + 164 " run inside an HTTP Servlet Environment"); 165 } 166 167 try { 168 if (curDBObj instanceof Securable) { 170 ((Securable) curDBObj).isAllowed("S"); 171 } else { 172 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT 173 || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) { 174 } else { 176 String allowInsecure = Setup.getValue(newRequest.getDataContext(), 177 com.jcorporate.expresso.core.ExpressoSchema.class.getName(), 178 "insecureDBMaint"); 179 if (!(StringUtil.toBoolean(allowInsecure))) { 180 throw new SecurityException ("Access to unsecured Objects not allowed"); 181 } 182 } 183 } 184 } catch (DBException ex) { 185 throw new ControllerException("Not allowed.", ex); 186 } 187 188 189 String fieldName = newRequest.getParameter("fieldName"); 193 194 195 DataObject curDataObjectToUse = curDBObj; 196 if (curDataObjectToUse instanceof NestableDataObject) { 197 String fullFieldName = fieldName; 198 fieldName = ((NestableDataObject) curDataObjectToUse).getFieldFromNestedName(fieldName); 199 curDataObjectToUse = ((NestableDataObject) curDataObjectToUse).getNestedFromFieldName(fullFieldName); 200 } 201 202 203 if (curDataObjectToUse instanceof MediaDBObject) { 207 sendMediaObjectField(newRequest, newResponse, 208 (MediaDBObject) curDataObjectToUse, fieldName); 209 return; 210 } else { 211 sendBlobField(newRequest, newResponse, 212 (JDBCDataObject) curDataObjectToUse, fieldName); 213 return; 214 } 215 216 217 } 218 219 229 protected void sendBlobField(ControllerRequest newRequest, ControllerResponse newResponse, 230 JDBCDataObject dbobj, String fieldName) throws ControllerException, 231 NonHandleableException { 232 ServletControllerRequest request = (ServletControllerRequest) newRequest; 233 ServletResponse sResponse = request.getServletResponse(); 234 235 236 LobField lf = new LobField(); 237 try { 238 lf.setCriteria(dbobj); 239 240 InputStream theBlob = lf.getBlobStream(fieldName); 241 if (theBlob == null) { 242 newResponse.addError("No Data Saved For This Record"); 243 this.transition("Update", newRequest, newResponse); 244 lf.close(); 245 return; 246 } 247 248 String fileName = (String ) dbobj.getDataField(fieldName).getAttribute("fileName"); 249 if (fileName == null || fileName.length() == 0) { 250 fileName = fieldName; 251 } 252 253 ((HttpServletResponse ) sResponse).setHeader("Content-Disposition", 254 "inline;filename=" + fileName); 255 newResponse.setCustomResponse(true); 256 257 String mimeType = (String ) dbobj.getDataField(fieldName).getAttribute("mimeType"); 258 if (mimeType == null || mimeType.length() == 0) { 259 sResponse.setContentType("application/x-unknown"); 260 } else { 261 sResponse.setContentType(mimeType); 262 } 263 264 sendStream(theBlob, sResponse); 265 } catch (DBException ex) { 266 throw new ControllerException("Error getting BLOB field: ", ex); 267 } catch (java.io.IOException ex) { 268 log.error("I/O Error transferring BLOB", ex); 269 } finally { 270 lf.close(); 271 } 272 } 273 274 285 protected void sendMediaObjectField(ControllerRequest newRequest, 286 ControllerResponse newResponse, 287 MediaDBObject dbobj, 288 String fieldName) throws ControllerException, NonHandleableException { 289 ServletControllerRequest request = (ServletControllerRequest) newRequest; 290 ServletResponse sResponse = request.getServletResponse(); 291 try { 292 293 InputStream theBlob = dbobj.retrieveBlob(fieldName); 297 if (theBlob == null) { 298 dbobj.release(); 299 newResponse.addError("No Data Saved For This Record"); 300 this.transition("Update", newRequest, newResponse); 301 return; 302 } 303 304 String fileName = dbobj.getField(fieldName + MediaDBObject.FLD_FILE_SUFFIX); 305 306 newResponse.setCustomResponse(true); 307 308 if (fileName != null && fileName.length() > 0) { 312 ((HttpServletResponse ) sResponse).setHeader("Content-Disposition", 313 "inline;filename=" + fileName); 314 } 315 316 int mimeNumber = dbobj.getFieldInt(fieldName + MediaDBObject.FLD_MIME_SUFFIX); 320 321 if (mimeNumber <= 0) { 322 sResponse.setContentType("application/x-unknown"); 323 } else { 324 try { 325 MimeTypes contentType = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT); 326 contentType.setField("MimeNumber", mimeNumber); 327 contentType.retrieve(); 328 sResponse.setContentType(contentType.getField("MimeType")); 329 } catch (DBException ex) { 330 sResponse.setContentType("application/x-unknown"); 331 } 332 } 333 334 335 int tempLength = dbobj.getFieldInt(fieldName + MediaDBObject.FLD_SIZE_SUFFIX); 341 342 if (tempLength > 0) { 345 sResponse.setContentLength(tempLength); 346 } 347 348 sendStream(theBlob, sResponse); 349 350 351 } catch (DBException ex) { 352 throw new ControllerException("Error getting BLOB field: ", ex); 353 } catch (java.io.IOException ex) { 354 log.error("I/O Error transferring BLOB", ex); 355 } finally { 356 dbobj.release(); 357 } 358 359 } 360 361 369 protected void sendStream(InputStream is, ServletResponse response) 370 throws java.io.IOException { 371 ServletOutputStream out = response.getOutputStream(); 375 byte[] buf = new byte[4096]; int bytesRead; 377 378 while ((bytesRead = is.read(buf)) != -1) { 379 out.write(buf, 0, bytesRead); 380 } 381 382 try { 383 out.flush(); 384 } catch (java.io.IOException ex) { 385 log.error("I/O Error flushing BLOB stream", ex); 386 } 387 388 try { 389 out.close(); 390 } catch (java.io.IOException ex) { 391 log.error("I/O Error closing BLOB output stream", ex); 392 } 393 394 } 395 } | Popular Tags |