From: Leif Åstrand Date: Tue, 4 Sep 2012 13:59:24 +0000 (+0300) Subject: Move getBaseDirectory() to DeploymentConfiguration (#9402) X-Git-Tag: 7.0.0.beta1~184^2~9 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b0b1a13247bf931eb7f33ff6d0a88fc1833144e9;p=vaadin-framework.git Move getBaseDirectory() to DeploymentConfiguration (#9402) --- diff --git a/server/src/com/vaadin/server/ApplicationContext.java b/server/src/com/vaadin/server/ApplicationContext.java index 2c09ed3857..85a77241d9 100644 --- a/server/src/com/vaadin/server/ApplicationContext.java +++ b/server/src/com/vaadin/server/ApplicationContext.java @@ -15,7 +15,6 @@ */ package com.vaadin.server; -import java.io.File; import java.io.Serializable; import java.util.Collection; import java.util.Collections; @@ -148,20 +147,6 @@ public abstract class ApplicationContext implements HttpSessionBindingListener, return Logger.getLogger(ApplicationContext.class.getName()); } - /** - * Returns application context base directory. - * - * Typically an application is deployed in a such way that is has an - * application directory. For web applications this directory is the root - * directory of the web applications. In some cases applications might not - * have an application directory (for example web applications running - * inside a war). - * - * @return The application base directory or null if the application has no - * base directory. - */ - public abstract File getBaseDirectory(); - /** * Gets the session to which this application context is currently * associated. diff --git a/server/src/com/vaadin/server/DeploymentConfiguration.java b/server/src/com/vaadin/server/DeploymentConfiguration.java index 1f202eb923..0a70ef09b3 100644 --- a/server/src/com/vaadin/server/DeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DeploymentConfiguration.java @@ -16,6 +16,7 @@ package com.vaadin.server; +import java.io.File; import java.io.Serializable; import java.util.Iterator; @@ -123,4 +124,18 @@ public interface DeploymentConfiguration extends Serializable { * @return the system messages object */ public SystemMessages getSystemMessages(); + + /** + * Returns application context base directory. + * + * Typically an application is deployed in a such way that is has an + * application directory. For web applications this directory is the root + * directory of the web applications. In some cases applications might not + * have an application directory (for example web applications running + * inside a war). + * + * @return The application base directory or null if the application has no + * base directory. + */ + public File getBaseDirectory(); } diff --git a/server/src/com/vaadin/server/PortletApplicationContext2.java b/server/src/com/vaadin/server/PortletApplicationContext2.java index 6a12eafc47..f157dc9ae6 100644 --- a/server/src/com/vaadin/server/PortletApplicationContext2.java +++ b/server/src/com/vaadin/server/PortletApplicationContext2.java @@ -15,15 +15,11 @@ */ package com.vaadin.server; -import java.io.File; import java.io.Serializable; -import java.net.URL; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; @@ -69,28 +65,6 @@ public class PortletApplicationContext2 extends ApplicationContext { private final Map sharedParameterActionNameMap = new HashMap(); private final Map sharedParameterActionValueMap = new HashMap(); - @Override - public File getBaseDirectory() { - PortletSession session = getPortletSession(); - String resultPath = session.getPortletContext().getRealPath("/"); - if (resultPath != null) { - return new File(resultPath); - } else { - try { - final URL url = session.getPortletContext().getResource("/"); - return new File(url.getFile()); - } catch (final Exception e) { - // FIXME: Handle exception - getLogger() - .log(Level.INFO, - "Cannot access base directory, possible security issue " - + "with Application Server or Servlet Container", - e); - } - } - return null; - } - protected PortletCommunicationManager getApplicationManager( Application application) { PortletCommunicationManager mgr = (PortletCommunicationManager) applicationToAjaxAppMgrMap @@ -403,8 +377,4 @@ public class PortletApplicationContext2 extends ApplicationContext { "Portlet mode can only be changed from a portlet request"); } } - - private Logger getLogger() { - return Logger.getLogger(PortletApplicationContext2.class.getName()); - } } diff --git a/server/src/com/vaadin/server/ServletApplicationContext.java b/server/src/com/vaadin/server/ServletApplicationContext.java index 4184b68de4..910051a26b 100644 --- a/server/src/com/vaadin/server/ServletApplicationContext.java +++ b/server/src/com/vaadin/server/ServletApplicationContext.java @@ -16,7 +16,6 @@ package com.vaadin.server; -import java.io.File; import java.util.Enumeration; import java.util.HashMap; @@ -98,21 +97,6 @@ public class ServletApplicationContext extends ApplicationContext { setSession(new WrappedHttpSession(newSession)); } - /** - * Gets the application context base directory. - * - * @see com.vaadin.server.ApplicationContext#getBaseDirectory() - */ - @Override - public File getBaseDirectory() { - final String realPath = VaadinServlet.getResourcePath(getHttpSession() - .getServletContext(), "/"); - if (realPath == null) { - return null; - } - return new File(realPath); - } - /** * Gets the http-session application is running in. * diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 923fcd3b71..5fb2340bc8 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -16,6 +16,7 @@ package com.vaadin.server; import java.io.BufferedWriter; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -24,11 +25,13 @@ import java.io.PrintWriter; import java.io.Serializable; import java.lang.reflect.Method; import java.net.MalformedURLException; +import java.net.URL; import java.security.GeneralSecurityException; import java.util.Enumeration; import java.util.Locale; import java.util.Map; import java.util.Properties; +import java.util.logging.Level; import java.util.logging.Logger; import javax.portlet.ActionRequest; @@ -154,6 +157,29 @@ public class VaadinPortlet extends GenericPortlet implements Constants { public SystemMessages getSystemMessages() { return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; } + + @Override + public File getBaseDirectory() { + PortletContext context = getPortlet().getPortletContext(); + String resultPath = context.getRealPath("/"); + if (resultPath != null) { + return new File(resultPath); + } else { + try { + final URL url = context.getResource("/"); + return new File(url.getFile()); + } catch (final Exception e) { + // FIXME: Handle exception + getLogger() + .log(Level.INFO, + "Cannot access base directory, possible security issue " + + "with Application Server or Servlet Container", + e); + } + } + return null; + } + } public static class WrappedHttpAndPortletRequest extends diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 3cf2c645a0..62fb8ba71e 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -16,6 +16,7 @@ package com.vaadin.server; import java.io.BufferedWriter; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -140,6 +141,16 @@ public class VaadinServlet extends HttpServlet implements Constants { public SystemMessages getSystemMessages() { return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; } + + @Override + public File getBaseDirectory() { + final String realPath = VaadinServlet.getResourcePath( + servlet.getServletContext(), "/"); + if (realPath == null) { + return null; + } + return new File(realPath); + } } private static class AbstractApplicationServletWrapper implements Callback { diff --git a/uitest/src/com/vaadin/tests/resources/NonExistingFileResource.java b/uitest/src/com/vaadin/tests/resources/NonExistingFileResource.java index eb8559f746..45552df39c 100644 --- a/uitest/src/com/vaadin/tests/resources/NonExistingFileResource.java +++ b/uitest/src/com/vaadin/tests/resources/NonExistingFileResource.java @@ -3,9 +3,11 @@ package com.vaadin.tests.resources; import java.io.File; import com.vaadin.server.FileResource; +import com.vaadin.server.WrappedRequest; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.util.CurrentInstance; public class NonExistingFileResource extends TestBase { @@ -24,8 +26,10 @@ public class NonExistingFileResource extends TestBase { @Override public void buttonClick(ClickEvent event) { - FileResource res = new FileResource(new File(getContext() - .getBaseDirectory() + "/" + filename)); + FileResource res = new FileResource(new File(CurrentInstance + .get(WrappedRequest.class).getDeploymentConfiguration() + .getBaseDirectory() + + "/" + filename)); getMainWindow().open(res); } diff --git a/uitest/src/com/vaadin/tests/tickets/Ticket1975.java b/uitest/src/com/vaadin/tests/tickets/Ticket1975.java index e85fe294f2..4fa3470f0b 100644 --- a/uitest/src/com/vaadin/tests/tickets/Ticket1975.java +++ b/uitest/src/com/vaadin/tests/tickets/Ticket1975.java @@ -5,13 +5,14 @@ import java.io.File; import java.io.FileInputStream; import com.vaadin.Application; -import com.vaadin.server.ServletApplicationContext; +import com.vaadin.server.WrappedRequest; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.CustomLayout; import com.vaadin.ui.GridLayout; import com.vaadin.ui.UI.LegacyWindow; +import com.vaadin.util.CurrentInstance; public class Ticket1975 extends Application.LegacyApplication { @@ -33,7 +34,6 @@ public class Ticket1975 extends Application.LegacyApplication { try { cl1 = new CustomLayout(new ByteArrayInputStream(s.getBytes())); layout.addComponent(cl1); - ServletApplicationContext wc = ((ServletApplicationContext) getContext()); layout.addComponent(new Button("Disable/Enable", new ClickListener() { @@ -47,8 +47,10 @@ public class Ticket1975 extends Application.LegacyApplication { } })); - File f = new File(wc.getBaseDirectory().getAbsoluteFile() - + "/VAADIN/themes/" + getTheme() + File baseDir = CurrentInstance.get(WrappedRequest.class) + .getDeploymentConfiguration().getBaseDirectory() + .getAbsoluteFile(); + File f = new File(baseDir + "/VAADIN/themes/" + getTheme() + "/layouts/Ticket1975.html"); cl2 = new CustomLayout(new FileInputStream(f)); diff --git a/uitest/src/com/vaadin/tests/util/SampleDirectory.java b/uitest/src/com/vaadin/tests/util/SampleDirectory.java index e15f0ccfe3..7304f1cab8 100644 --- a/uitest/src/com/vaadin/tests/util/SampleDirectory.java +++ b/uitest/src/com/vaadin/tests/util/SampleDirectory.java @@ -20,10 +20,12 @@ import java.io.File; import com.vaadin.Application; import com.vaadin.server.SystemError; +import com.vaadin.server.WrappedRequest; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Label; import com.vaadin.ui.Panel; import com.vaadin.ui.UI; +import com.vaadin.util.CurrentInstance; /** * Provides sample directory based on application directory. If this fails then @@ -46,7 +48,8 @@ public class SampleDirectory { + "context base directory failed, " + "possible security constraint with Application " + "Server or Servlet Container.
"; - File file = application.getContext().getBaseDirectory(); + File file = CurrentInstance.get(WrappedRequest.class) + .getDeploymentConfiguration().getBaseDirectory(); if ((file == null) || (!file.canRead()) || (file.getAbsolutePath() == null)) { // cannot access example directory, possible security issue with