### Eclipse Workspace Patch 1.0
#P xwiki-web-standard
Index: src/main/webapp/WEB-INF/struts-config.xml
===================================================================
--- src/main/webapp/WEB-INF/struts-config.xml	(revision 29016)
+++ src/main/webapp/WEB-INF/struts-config.xml	(working copy)
@@ -257,6 +257,11 @@
           name="download"
           scope="request">
       </action>
+      <action path="/temp/"
+          type="com.xpn.xwiki.web.TempResourceAction"
+          name="temp"
+          scope="request">
+      </action>
       <action path="/downloadrev/"
               type="com.xpn.xwiki.web.DownloadRevAction"
               name="dowloadrev"
#P xwiki-core
Index: src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java
===================================================================
--- src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java	(revision 29015)
+++ src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java	(working copy)
@@ -137,6 +137,7 @@
             actionMap.put("jsx", "view");
             actionMap.put("ssx", "view");
             actionMap.put("tex", "view");
+            actionMap.put("temp", "view");
             actionMap.put("unknown", "view");
         }
 
Index: pom.xml
===================================================================
--- pom.xml	(revision 29015)
+++ pom.xml	(working copy)
@@ -257,6 +257,13 @@
       <groupId>commons-beanutils</groupId>
       <artifactId>commons-beanutils</artifactId>
     </dependency>
+    
+    <!-- For extracting metadata from various document types -->
+    <dependency>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-core</artifactId>
+      <version>0.7</version>
+    </dependency>
 
     <!-- Logging -->
     <dependency>
Index: src/main/java/com/xpn/xwiki/web/TempResourceAction.java
===================================================================
--- src/main/java/com/xpn/xwiki/web/TempResourceAction.java	(revision 0)
+++ src/main/java/com/xpn/xwiki/web/TempResourceAction.java	(revision 0)
@@ -0,0 +1,129 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package com.xpn.xwiki.web;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.tika.Tika;
+import org.apache.tika.mime.MimeTypes;
+import org.xwiki.container.Container;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+
+/**
+ * Action responsible for downloading temporary resources created by various modules.
+ * 
+ * @version $Id$
+ * @since 2.4M1
+ */
+public class TempResourceAction extends XWikiAction
+{   
+    /**
+     * URI pattern for this action.
+     */
+    public static final Pattern URI_PATTERN = Pattern.compile(".*/temp/([^/]*)/([^/]*)/([^/]*)/(.*)");
+    
+    /**
+     * Logging support.
+     */
+    private static final Log LOG = LogFactory.getLog(TempResourceAction.class);
+
+    /**
+     * Used for detecting mime-types of files.
+     */
+    private Tika tika = new Tika();
+
+    /**
+     * Used to resolve temporary working dir.
+     */
+    private Container container = Utils.getComponent(Container.class);
+
+    /**
+     * {@inheritDoc}
+     */
+    public String render(XWikiContext context) throws XWikiException
+    {
+        // User should have view right on the associated document.
+        if (!context.getWiki().checkAccess("view", context.getDoc(), context)) {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_ACCESS_DENIED,
+                "Access denied");
+        }
+        
+        XWikiRequest request = context.getRequest();
+        XWikiResponse response = context.getResponse();
+        String uri = request.getRequestURI();        
+
+        // Locate the temporary file.
+        File tempFile = getTempFile(uri);
+        if (null == tempFile) {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION,
+                "Invalid temporary resource URL");
+        }
+
+        // Write temporary file into response.
+        response.setDateHeader("Last-Modified", tempFile.lastModified());
+        String contentType = MimeTypes.OCTET_STREAM;
+        try {
+            contentType = tika.detect(tempFile);
+        } catch (IOException ex) {
+            LOG.warn(String.format("Unable to determine mime type for temporary resource [%s]", tempFile
+                .getAbsolutePath()), ex);
+        }
+        response.setContentType(contentType);
+        try {
+            response.setContentLength((int) tempFile.length());
+            IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream());
+        } catch (IOException e) {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e);
+        }
+        return null;
+    }
+
+    /**
+     * Returns the temporary file corresponding to the specified URI.
+     * 
+     * @param uri request URI.
+     * @return temporary file corresponding to the specified URI or null if no such file can be located.
+     */
+    private File getTempFile(String uri)
+    {
+        Matcher matcher = URI_PATTERN.matcher(uri);
+        File result = null;
+        if (matcher.find()) {
+            String space = matcher.group(1);
+            String page = matcher.group(2);
+            String module = matcher.group(3);
+            String filePath = matcher.group(4);
+            String path = String.format("temp/%s/%s/%s/%s", module, space, page, filePath);
+            result = new File(container.getApplicationContext().getTemporaryDirectory(), path);
+            result = result.exists() ? result : null;
+        }
+        return result;
+    }
+}
