1 17 18 19 20 package org.apache.lenya.cms.cocoon.acting; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.TransformerException ; 35 36 import org.apache.avalon.excalibur.io.FileUtil; 37 import org.apache.avalon.framework.parameters.Parameters; 38 import org.apache.cocoon.acting.AbstractConfigurableAction; 39 import org.apache.cocoon.environment.ObjectModelHelper; 40 import org.apache.cocoon.environment.Redirector; 41 import org.apache.cocoon.environment.Request; 42 import org.apache.cocoon.environment.SourceResolver; 43 import org.apache.cocoon.servlet.multipart.Part; 44 import org.apache.lenya.cms.publication.Document; 45 import org.apache.lenya.cms.publication.PageEnvelope; 46 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 47 import org.apache.lenya.cms.publication.ResourcesManager; 48 import org.apache.lenya.xml.DocumentHelper; 49 import org.apache.lenya.xml.NamespaceHelper; 50 import org.w3c.dom.Element ; 51 52 59 public class UploadAction extends AbstractConfigurableAction { 60 61 private Document document; 62 private PageEnvelope pageEnvelope; 63 64 public static final String UPLOADASSET_PARAM_NAME = "properties.asset.data"; 65 public static final String UPLOADASSET_PARAM_PREFIX = "properties.asset."; 66 67 public static final String UPLOADASSET_RETURN_FILESIZE = "file-size"; 68 public static final String UPLOADASSET_RETURN_MIMETYPE = "mime-type"; 69 70 public static final String CONTENT_PREFIX = "content"; 71 72 public static final String FILE_NAME_REGEXP = "[-a-zA-Z0-9_.]+"; 73 74 public static final String [] DUBLIN_CORE_PARAMETERS = { "title", "creator", "subject", 76 "description", "publisher", "contributor", "date", "type", "format", "identifier", 77 "source", "language", "relation", "coverage", "rights" }; 78 79 94 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, 95 Parameters parameters) throws Exception { 96 97 Map results = new HashMap (); 98 Request request = ObjectModelHelper.getRequest(objectModel); 99 pageEnvelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 100 document = pageEnvelope.getDocument(); 101 102 File assetFile; 103 104 logRequestParameters(request); 105 106 String uploadType = request.getParameter("uploadtype"); 108 109 if (uploadType == null) { 111 uploadType = "asset"; 112 } 113 114 Map dublinCoreParams = getDublinCoreParameters(request); 115 116 Part part = (Part) request.get(UPLOADASSET_PARAM_NAME); 118 119 String fileName = part.getFileName(); 120 if (!fileName.matches(FILE_NAME_REGEXP) || FileUtil.getExtension(fileName).equals("")) { 121 getLogger().warn("The filename [" + fileName + "] is not valid for an asset."); 124 return null; 125 } 126 fileName = fileName.replace(' ', '_'); 128 String mimeType = part.getMimeType(); 129 int fileSize = part.getSize(); 130 131 results.put(UPLOADASSET_RETURN_MIMETYPE, mimeType); 132 results.put(UPLOADASSET_RETURN_FILESIZE, new Integer (fileSize)); 133 134 dublinCoreParams.put("format", mimeType); 135 dublinCoreParams.put("extent", Integer.toString(fileSize)); 136 137 if (uploadType.equals("asset")) { 138 ResourcesManager resourcesMgr = new ResourcesManager(document); 139 assetFile = new File (resourcesMgr.getPath(), fileName); 140 141 if (!resourcesMgr.getPath().exists()) { 142 resourcesMgr.getPath().mkdirs(); 143 } 144 145 File metaDataFile = new File (resourcesMgr.getPath(), fileName + ".meta"); 148 createMetaData(metaDataFile, dublinCoreParams); 149 150 } 151 else { 153 assetFile = new File (document.getFile().getParent(), fileName); 154 getLogger().debug("assetFile: " + assetFile); 155 } 156 157 saveAsset(assetFile, part); 158 159 return Collections.unmodifiableMap(results); 160 } 161 162 169 protected void saveAsset(File assetFile, Part part) throws Exception { 170 if (!assetFile.exists()) { 171 boolean created = assetFile.createNewFile(); 172 if (!created) { 173 throw new RuntimeException ("The file [" + assetFile + "] could not be created."); 174 } 175 } 176 177 byte[] buf = new byte[4096]; 178 FileOutputStream out = new FileOutputStream (assetFile); 179 try { 180 InputStream in = part.getInputStream(); 181 int read = in.read(buf); 182 183 while (read > 0) { 184 out.write(buf, 0, read); 185 read = in.read(buf); 186 } 187 } finally { 188 out.close(); 189 } 190 } 191 192 196 protected void logRequestParameters(Request request) { 197 for (Enumeration myenum = request.getParameterNames(); myenum.hasMoreElements();) { 198 String param = (String ) myenum.nextElement(); 199 getLogger().debug( 200 param + ": " + request.getParameter(param) + " [" + request.get(param) + "]"); 201 } 202 } 203 204 210 protected Map getDublinCoreParameters(Request request) { 211 HashMap dublinCoreParams = new HashMap (); 212 213 for (int i = 0; i < DUBLIN_CORE_PARAMETERS.length; i++) { 214 String paramName = DUBLIN_CORE_PARAMETERS[i]; 215 String paramValue = request.getParameter(UPLOADASSET_PARAM_PREFIX + paramName); 216 217 if (paramValue == null) { 218 paramValue = ""; 219 } 220 221 dublinCoreParams.put(paramName, paramValue); 222 } 223 224 Iterator iter = dublinCoreParams.keySet().iterator(); 225 while (iter.hasNext()) { 226 String paramName = (String ) iter.next(); 227 getLogger().debug(paramName + ": " + dublinCoreParams.get(paramName)); 228 } 229 230 return dublinCoreParams; 231 } 232 233 243 protected void createMetaData(File metaDataFile, Map dublinCoreParams) 244 throws TransformerConfigurationException , TransformerException , IOException , 245 ParserConfigurationException { 246 247 assert (metaDataFile.getParentFile().exists()); 248 249 NamespaceHelper helper = new NamespaceHelper("http://purl.org/dc/elements/1.1/", "dc", 250 "metadata"); 251 252 Element root = helper.getDocument().getDocumentElement(); 253 254 Iterator iter = dublinCoreParams.keySet().iterator(); 255 256 while (iter.hasNext()) { 257 String tagName = (String ) iter.next(); 258 String tagValue = (String ) dublinCoreParams.get(tagName); 259 root.appendChild(helper.createElement(tagName, tagValue)); 260 } 261 262 DocumentHelper.writeDocument(helper.getDocument(), metaDataFile); 263 } 264 } | Popular Tags |