| 1 |
|
package org.trails.component.blob; |
| 2 |
|
|
| 3 |
|
import java.io.IOException; |
| 4 |
|
import java.io.OutputStream; |
| 5 |
|
import java.util.HashMap; |
| 6 |
|
import java.util.Map; |
| 7 |
|
|
| 8 |
|
import ognl.Ognl; |
| 9 |
|
import ognl.OgnlException; |
| 10 |
|
|
| 11 |
|
import org.apache.commons.logging.Log; |
| 12 |
|
import org.apache.commons.logging.LogFactory; |
| 13 |
|
import org.apache.hivemind.util.Defense; |
| 14 |
|
import org.apache.tapestry.IRequestCycle; |
| 15 |
|
import org.apache.tapestry.engine.IEngineService; |
| 16 |
|
import org.apache.tapestry.engine.ILink; |
| 17 |
|
import org.apache.tapestry.error.RequestExceptionReporter; |
| 18 |
|
import org.apache.tapestry.services.LinkFactory; |
| 19 |
|
import org.apache.tapestry.util.ContentType; |
| 20 |
|
import org.apache.tapestry.web.WebResponse; |
| 21 |
|
import org.trails.descriptor.BlobDescriptorExtension; |
| 22 |
|
import org.trails.descriptor.DescriptorService; |
| 23 |
|
import org.trails.persistence.PersistenceService; |
| 24 |
|
import org.trails.component.Utils; |
| 25 |
|
|
| 26 |
0 |
public class BlobDownloadService implements IEngineService { |
| 27 |
|
|
| 28 |
0 |
private static final Log LOG = LogFactory.getLog(BlobDownloadService.class); |
| 29 |
|
|
| 30 |
|
public static final String SERVICE_NAME = "BlobService"; |
| 31 |
|
|
| 32 |
|
private RequestExceptionReporter _exceptionReporter; |
| 33 |
|
|
| 34 |
|
private LinkFactory _linkFactory; |
| 35 |
|
|
| 36 |
|
private WebResponse _response; |
| 37 |
|
|
| 38 |
|
private PersistenceService persistenceService; |
| 39 |
|
|
| 40 |
|
private DescriptorService descriptorService; |
| 41 |
|
|
| 42 |
|
public PersistenceService getPersistenceService() { |
| 43 |
0 |
return persistenceService; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
public void setPersistenceService(PersistenceService persistenceService) { |
| 47 |
0 |
this.persistenceService = persistenceService; |
| 48 |
0 |
} |
| 49 |
|
|
| 50 |
|
public DescriptorService getDescriptorService() { |
| 51 |
0 |
return descriptorService; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
public void setDescriptorService(DescriptorService descriptorService) { |
| 55 |
0 |
this.descriptorService = descriptorService; |
| 56 |
0 |
} |
| 57 |
|
|
| 58 |
|
private static final String BLOBID = "id"; |
| 59 |
|
|
| 60 |
|
private static final String ENTITY_NAME = "class"; |
| 61 |
|
|
| 62 |
|
private static final String BYTES_PROPERTY = "property"; |
| 63 |
|
|
| 64 |
|
private static final String CONTENT_TYPE = "contentType"; |
| 65 |
|
|
| 66 |
|
private static final String FILE_NAME = "fileName"; |
| 67 |
|
|
| 68 |
|
public ILink getLink(boolean post, Object parameter) { |
| 69 |
|
|
| 70 |
0 |
Defense.isAssignable(((Object[]) parameter)[0], TrailsBlobAsset.class, |
| 71 |
0 |
"parameter"); |
| 72 |
|
|
| 73 |
0 |
TrailsBlobAsset asset = (TrailsBlobAsset) ((Object[]) parameter)[0]; |
| 74 |
|
|
| 75 |
0 |
Map<String, String> parameters = new HashMap<String, String>(); |
| 76 |
|
|
| 77 |
0 |
parameters.put(BLOBID, String.valueOf(asset.getIdProperty())); |
| 78 |
0 |
parameters.put(ENTITY_NAME, asset.getEntityName()); |
| 79 |
0 |
parameters.put(BYTES_PROPERTY, asset.getBytesProperty()); |
| 80 |
|
|
| 81 |
0 |
if (asset.getFileName() != null) { |
| 82 |
0 |
parameters.put(FILE_NAME, asset.getFileName()); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
0 |
if (asset.getContentType() != null) { |
| 86 |
0 |
parameters.put(CONTENT_TYPE, asset.getContentType()); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
0 |
return _linkFactory.constructLink(this, false, parameters, true); |
| 90 |
|
|
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
public void service(IRequestCycle cycle) throws IOException { |
| 94 |
|
|
| 95 |
0 |
synchronized (cycle) { |
| 96 |
0 |
String blobID = cycle.getParameter(BLOBID); |
| 97 |
0 |
String entityName = cycle.getParameter(ENTITY_NAME); |
| 98 |
0 |
String bytesProp = cycle.getParameter(BYTES_PROPERTY); |
| 99 |
0 |
String fileName = cycle.getParameter(FILE_NAME); |
| 100 |
0 |
String contentType = cycle.getParameter(CONTENT_TYPE); |
| 101 |
|
|
| 102 |
|
try { |
| 103 |
0 |
BlobDescriptorExtension blobDescriptor = getDescriptorService() |
| 104 |
0 |
.getClassDescriptor(Utils.getClassForName(entityName)) |
| 105 |
0 |
.getPropertyDescriptor(bytesProp).getExtension( |
| 106 |
0 |
BlobDescriptorExtension.class); |
| 107 |
|
|
| 108 |
0 |
if (blobDescriptor != null && blobID != null |
| 109 |
0 |
&& !"".equals(blobID)) { |
| 110 |
0 |
Object model = getPersistenceService().getInstance( |
| 111 |
0 |
Utils.getClassForName(entityName), Integer.valueOf(blobID)); |
| 112 |
0 |
if (model != null) { |
| 113 |
0 |
byte[] bytes = new byte[0]; |
| 114 |
|
|
| 115 |
0 |
if (blobDescriptor.isBytes()) { |
| 116 |
0 |
if (fileName == null) { |
| 117 |
0 |
if (!"".equals(blobDescriptor.getFileName())) |
| 118 |
0 |
fileName = blobDescriptor.getFileName(); |
| 119 |
|
else |
| 120 |
0 |
fileName = ((ITrailsBlob) model) |
| 121 |
0 |
.getFileName(); |
| 122 |
|
} |
| 123 |
0 |
if (contentType == null) { |
| 124 |
0 |
if (!"".equals(blobDescriptor.getContentType())) |
| 125 |
0 |
contentType = blobDescriptor |
| 126 |
0 |
.getContentType(); |
| 127 |
|
else |
| 128 |
0 |
contentType = ((ITrailsBlob) model) |
| 129 |
0 |
.getContentType(); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
0 |
bytes = (byte[]) Ognl.getValue(bytesProp, model); |
| 133 |
0 |
} else if (blobDescriptor.isITrailsBlob()) { |
| 134 |
0 |
ITrailsBlob trailsBlob = (ITrailsBlob) Ognl |
| 135 |
0 |
.getValue(bytesProp, model); |
| 136 |
0 |
if (trailsBlob != null) { |
| 137 |
0 |
bytes = trailsBlob.getBytes(); |
| 138 |
0 |
contentType = !"".equals(blobDescriptor |
| 139 |
0 |
.getContentType()) ? blobDescriptor |
| 140 |
0 |
.getContentType() : trailsBlob |
| 141 |
0 |
.getContentType(); |
| 142 |
0 |
fileName = !"".equals(blobDescriptor |
| 143 |
0 |
.getFileName()) ? blobDescriptor |
| 144 |
0 |
.getFileName() : trailsBlob |
| 145 |
0 |
.getFileName(); |
| 146 |
|
} |
| 147 |
|
} |
| 148 |
|
|
| 149 |
0 |
if (bytes.length > 0) { |
| 150 |
0 |
_response.setHeader("Expires", "0"); |
| 151 |
0 |
_response |
| 152 |
0 |
.setHeader("Cache-Control", |
| 153 |
0 |
"must-revalidate, post-check=0,pre-check=0"); |
| 154 |
0 |
_response.setHeader("Pragma", "public"); |
| 155 |
0 |
_response.setHeader("Content-Disposition", |
| 156 |
0 |
blobDescriptor.getContentDisposition() |
| 157 |
0 |
.getValue() |
| 158 |
0 |
+ "; filename=" + fileName); |
| 159 |
0 |
_response.setContentLength(bytes.length); |
| 160 |
|
|
| 161 |
0 |
OutputStream output = _response |
| 162 |
0 |
.getOutputStream(new ContentType( |
| 163 |
0 |
contentType)); |
| 164 |
0 |
output.write(bytes); |
| 165 |
|
} else { |
| 166 |
0 |
String errorText = "BlobDownloadServcie: entityName->" + |
| 167 |
0 |
entityName + ", blobID ->" + |
| 168 |
0 |
blobID + " : has not been ingested yet"; LOG.info(errorText); |
| 169 |
|
|
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
} else { |
| 174 |
0 |
String errorText = "BlobDownloadServcie: entityName->" + |
| 175 |
0 |
entityName + ", blobID ->" + |
| 176 |
0 |
blobID + " : has not been ingested yet"; |
| 177 |
0 |
LOG.info(errorText); |
| 178 |
|
|
| 179 |
|
} |
| 180 |
|
|
| 181 |
0 |
} catch (OgnlException e) { |
| 182 |
0 |
e.printStackTrace(); |
| 183 |
|
} |
| 184 |
|
} |
| 185 |
0 |
return; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
public String getName() { |
| 189 |
0 |
return SERVICE_NAME; |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
public void setExceptionReporter(RequestExceptionReporter exceptionReporter) { |
| 193 |
0 |
_exceptionReporter = exceptionReporter; |
| 194 |
0 |
} |
| 195 |
|
|
| 196 |
|
public void setLinkFactory(LinkFactory linkFactory) { |
| 197 |
0 |
_linkFactory = linkFactory; |
| 198 |
0 |
} |
| 199 |
|
|
| 200 |
|
public void setResponse(WebResponse response) { |
| 201 |
0 |
_response = response; |
| 202 |
0 |
} |
| 203 |
|
} |