From c7f0b5fc6a3c66e2d7de7b37f0ed4983b4a16006 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 7 May 2009 16:20:10 +0000 Subject: [PATCH 1/1] Make attachments more robusts git-svn-id: https://svn.argeo.org/slc/trunk@2424 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../mvc/attachment/GetAttachmentHandler.java | 18 +++++++ .../slc/core/attachment/SimpleAttachment.java | 2 +- .../slc/core/execution/ExecutionScope.java | 54 +++++++++---------- .../META-INF/spring/common.xml | 3 ++ .../META-INF/spring/osgi.xml | 13 +++-- 5 files changed, 58 insertions(+), 32 deletions(-) diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/attachment/GetAttachmentHandler.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/attachment/GetAttachmentHandler.java index 77a21ae31..b5ac9b5e6 100644 --- a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/attachment/GetAttachmentHandler.java +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/attachment/GetAttachmentHandler.java @@ -13,12 +13,30 @@ import org.springframework.web.HttpRequestHandler; /** Returns one single result. */ public class GetAttachmentHandler implements HttpRequestHandler { + protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download"; + private AttachmentsStorage attachmentsStorage; public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uuid = request.getParameter("uuid"); String contentType = request.getParameter("contentType"); + String name = request.getParameter("name"); + if (contentType == null || "".equals(contentType.trim())) { + contentType = FORCE_DOWNLOAD; + } + + if (contentType.trim().equals(FORCE_DOWNLOAD)) { + if (name != null) { + contentType = contentType + ";name=\"" + name + "\""; + response.setHeader("Content-Disposition", + "attachment; filename=\"" + name + "\""); + } + response.setHeader("Expires", "0"); + response.setHeader("Cache-Control", "no-cache, must-revalidate"); + response.setHeader("Pragma", "no-cache"); + } + SimpleAttachment resourceDescriptor = new SimpleAttachment(); resourceDescriptor.setUuid(uuid); resourceDescriptor.setContentType(contentType); diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/SimpleAttachment.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/SimpleAttachment.java index 23dad53f1..1c1f13eb9 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/SimpleAttachment.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/SimpleAttachment.java @@ -5,7 +5,7 @@ import java.util.UUID; public class SimpleAttachment implements Attachment { private String uuid = UUID.randomUUID().toString(); private String name; - private String contentType; + private String contentType = ""; public String getUuid() { return uuid; diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java index 05a60b73b..36d951093 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java @@ -13,50 +13,51 @@ import org.springframework.beans.factory.config.Scope; public class ExecutionScope implements Scope { private final static Log log = LogFactory.getLog(ExecutionScope.class); - private final ThreadLocal executionContext - = new ThreadLocal(); - + private final ThreadLocal executionContext = new ThreadLocal(); + public final ThreadLocal executionContextBeanName = new ThreadLocal(); - + public Object get(String name, ObjectFactory objectFactory) { if (log.isTraceEnabled()) log.trace("Getting scoped bean " + name); - + // check if an execution context is defined for this thread - if(executionContext.get() == null) { + if (executionContext.get() == null) { // if not, we expect objectFactory to produce an ExecutionContext Object obj = objectFactory.getObject(); - if(obj instanceof ExecutionContext) { + if (obj instanceof ExecutionContext) { // store the ExecutionContext in the ThreadLocal - executionContext.set((ExecutionContext)obj); + executionContext.set((ExecutionContext) obj); executionContextBeanName.set(name); return obj; + } else { + throw new SlcException( + "Expected an ExecutionContext, got an object of class " + + obj.getClass() + + " for bean " + + name + + ": make sure that you have porperly set scope=\"execution\" where required"); } - else { - throw new SlcException("Expected an ExecutionContext, got an object of class " - + obj.getClass() + " for bean " + name); - } } - - if(name.equals(executionContextBeanName.get())) { + + if (name.equals(executionContextBeanName.get())) { return executionContext.get(); - } - else { - // see if the executionContext already knows the object + } else { + // see if the executionContext already knows the object Object obj = executionContext.get().findScopedObject(name); - if(obj == null) { + if (obj == null) { obj = objectFactory.getObject(); - if(!(obj instanceof ExecutionContext)) { + if (!(obj instanceof ExecutionContext)) { executionContext.get().addScopedObject(name, obj); - } - else { - throw new SlcException("Only one ExecutionContext can be defined per Thread"); + } else { + throw new SlcException( + "Only one ExecutionContext can be defined per Thread"); } } return obj; } - + // if (ExecutionContext.getScopedObjects().containsKey(name)) { // // returns cached instance // Object obj = ExecutionContext.getScopedObjects().get(name); @@ -74,18 +75,17 @@ public class ExecutionScope implements Scope { } public String getConversationId() { - + return executionContext.get().getUuid(); } - + public Boolean hasExecutionContext() { return executionContext.get() != null; } - public void registerDestructionCallback(String name, Runnable callback) { // TODO: implement it - //throw new UnsupportedOperationException(); + // throw new UnsupportedOperationException(); } public Object remove(String name) { diff --git a/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/common.xml b/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/common.xml index 6e170a134..5dfab09ad 100644 --- a/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/common.xml +++ b/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/common.xml @@ -13,4 +13,7 @@ + + + \ No newline at end of file diff --git a/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/osgi.xml b/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/osgi.xml index 7d6a13d25..0460d7e07 100644 --- a/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/osgi.xml +++ b/server/org.argeo.slc.siteserver/bundles/org.argeo.slc.agent/META-INF/spring/osgi.xml @@ -6,8 +6,16 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> - + + + + + + + @@ -18,9 +26,6 @@ - - - -- 2.39.2