Permalink
Browse files

#7 openBrowserAt allows to specify a custom url when opening the brow…

…ser.
  • Loading branch information...
1 parent 7d91dbe commit d9dd60d3e4679f39695aa24a0c77ae3f973c19c2 @snicoll snicoll committed Apr 28, 2012
@@ -181,7 +181,7 @@ private void doStart() throws LifecycleException {
logger.info("Application has been deployed to [" + getConfig().getDeployUrl() + "]");
if (config.shouldOpenBrowser()) {
- BrowserUtils.openBrowser(getConfig().getDeployUrl());
+ BrowserUtils.openBrowser(getConfig().getOpenBrowserUrl());
}
if (isWaiting()) {
tomcat.getServer().await();
@@ -51,6 +51,9 @@
private static final Logger logger = LoggerFactory.getLogger(EmbedVaadinConfig.class);
+ private static final int PORT_AUTO = 0;
+
+
public static final String CONFIG_LOCATION = "/embed-vaadin.properties";
/**
@@ -60,9 +63,9 @@
/**
* The default HTTP port to use if none is set. By default, an available port
- * is taken. Holds an integer
+ * is taken. Holds an integer.
*/
- public static final int DEFAULT_PORT = 0;
+ public static final int DEFAULT_PORT = PORT_AUTO;
/**
* The key defining the context path to use.
@@ -80,7 +83,7 @@
public static final String KEY_CONTEXT_ROOT_DIR = "context.rootDir";
/**
- * The key defining if the thread that started the server should be blocked. Holds a boolean
+ * The key defining if the thread that started the server should be blocked. Holds a boolean.
*/
public static final String KEY_WAITING = "server.await";
@@ -114,6 +117,11 @@
*/
public static final boolean DEFAULT_OPEN_BROWSER = false;
+ /**
+ * The key defining if the browser should be opened at a custom url. Holds a String which could be
+ * either a relative path, an absolute path or a full url.
+ */
+ public static final String KEY_CUSTOM_BROWSER_URL = "browser.customUrl";
private int port;
private String contextPath;
@@ -124,6 +132,7 @@
private boolean productionMode;
private boolean openBrowser;
+ private String customBrowserUrl;
/**
* Creates a new instance using the configuration in the given {@link Properties}
@@ -146,6 +155,7 @@ public EmbedVaadinConfig(Properties properties) {
productionMode = helper.getBooleanProperty(KEY_PRODUCTION_MODE, DEFAULT_PRODUCTION_MODE);
openBrowser = helper.getBooleanProperty(KEY_OPEN_BROWSER, DEFAULT_OPEN_BROWSER);
+ customBrowserUrl = properties.getProperty(KEY_CUSTOM_BROWSER_URL);
logger.debug("Using " + this);
@@ -166,6 +176,7 @@ protected EmbedVaadinConfig(EmbedVaadinConfig clone) {
this.widgetSet = clone.widgetSet;
this.productionMode = clone.productionMode;
this.openBrowser = clone.openBrowser;
+ this.customBrowserUrl = clone.customBrowserUrl;
}
/**
@@ -263,6 +274,16 @@ public boolean shouldOpenBrowser() {
}
/**
+ * Returns any customization made to the url to use to open the browser or
+ * <tt>null</tt> if the default url should be used.
+ *
+ * @return a custom browser url or <tt>null</tt> if none is specified
+ */
+ public String getCustomBrowserUrl() {
+ return customBrowserUrl;
+ }
+
+ /**
* Returns the full url of the application, according to the port and
* context path.
* <p/>
@@ -273,21 +294,31 @@ public boolean shouldOpenBrowser() {
* @return the url of the web application
*/
public String getDeployUrl() {
- final StringBuilder sb = new StringBuilder();
-
- sb.append("http://localhost:");
- final int httpPort = getPort();
- if (httpPort == 0) {
- sb.append("[auto]");
+ if (getContextPath().isEmpty()) {
+ return buildUrl(getPort(), "/");
} else {
- sb.append(httpPort);
+ return buildUrl(getPort(), getContextPath());
}
- if (getContextPath().isEmpty()) {
- sb.append("/");
+ }
+
+ /**
+ * Returns the url to use when the browser opens, according to both the deploy url
+ * and a custom browser url, if set.
+ *
+ * @return the url to use when to open the browser
+ */
+ public String getOpenBrowserUrl() {
+ final String customBrowserUrl = getCustomBrowserUrl();
+ final String deployUrl = getDeployUrl();
+ if (customBrowserUrl == null) {
+ return deployUrl;
+ } else if (customBrowserUrl.startsWith("http://")) {
+ return customBrowserUrl;
+ } else if (customBrowserUrl.startsWith("/")) {
+ return buildUrl(getPort(), customBrowserUrl);
} else {
- sb.append(getContextPath());
+ return deployUrl + customBrowserUrl;
}
- return sb.toString();
}
void setPort(int port) {
@@ -318,6 +349,10 @@ void setOpenBrowser(boolean openBrowser) {
this.openBrowser = openBrowser;
}
+ final void setCustomBrowserUrl(String customBrowserUrl) {
+ this.customBrowserUrl = customBrowserUrl;
+ }
+
private void validate() {
if (!contextRootDirectory.exists()) {
throw new IllegalStateException("Cannot find file [" + contextRootDirectory.getAbsolutePath() + "]. "
@@ -368,6 +403,30 @@ static String cleanContextPath(String contextPath) {
}
}
+ /**
+ * Build a url for the specified port and context.
+ * <p/>
+ * If the <tt>httpPort</tt> is equal to {@link #PORT_AUTO}, the
+ * returned url is not used as is because no http port has been
+ * allocated yet.
+ *
+ * @param httpPort the http port to use
+ * @param context the context of the url
+ * @return a <tt>localhost</tt> url for the specified port and context
+ */
+ static String buildUrl(int httpPort, String context) {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("http://localhost:");
+ if (httpPort == PORT_AUTO) {
+ sb.append("[auto]");
+ } else {
+ sb.append(httpPort);
+ }
+ sb.append(context);
+ return sb.toString();
+ }
+
private static Properties loadProperties(String path, boolean failIfNotFound) {
try {
final Properties properties = new Properties();
@@ -189,6 +189,34 @@ public B openBrowser(boolean open) {
}
/**
+ * Opens the browser at a custom location. The <tt>reference</tt> parameter can customize the
+ * url to use to open the browser in the following ways:
+ * <ul>
+ * <li>As a <tt>full</tt> URL starting with <em>http://</em> - the url is used as is</li>
+ * <li>As a relative reference, not starting with <em>/</em> - added to the standard deploy url</li>
+ * <li>As an absolute reference, starting with <em>/</em> - specify the context of the url</li>
+ * </ul>
+ * <p/>
+ * For instance, consider the scenario when the application is being deployed on port 7080 with
+ * the <em>my-app</em> context (i.e. <tt>http://localhost:7080/my-app</tt>), the following apply:
+ * <ul>
+ * <li><tt>http://localhost:7080/another-app</tt> just use that url as is to open the browser</li>
+ * <li><tt>?debug#anchor</tt> would use <tt>http://localhost:7080/my-app?debug#anchor</tt></li>
+ * <li><tt>/another-app/foo</tt> would use <tt>http://localhost:7080/another-app/foo</tt></li>
+ * </ul>
+ * Note that this automatically enables the browser if that was not set before so there is no
+ * need to call {@link #openBrowser(boolean)}.
+ *
+ * @param reference a custom url, a relative reference or an absolute reference
+ * @return this
+ */
+ public B openBrowserAt(String reference) {
+ openBrowser(true);
+ getConfig().setCustomBrowserUrl(reference);
+ return self();
+ }
+
+ /**
* Builds an {@link EmbedVaadinServer} and starts it immediately.
*
* @return the created server
@@ -48,9 +48,14 @@ protected void assertVaadinConfig(EmbedVaadinConfig config, String widgetSet, bo
assertEquals("Wrong production mode", productionMode, config.isProductionMode());
}
- protected void assertBrowserConfig(EmbedVaadinConfig config, boolean openBrowser) {
+ protected void assertBrowserConfig(EmbedVaadinConfig config, boolean openBrowser, String customBrowserUrl) {
assertConfigIsNotNull(config);
assertEquals("Wrong openBrowser flag", openBrowser, config.shouldOpenBrowser());
+ assertEquals("Wrong custom browser url", customBrowserUrl, config.getCustomBrowserUrl());
+ }
+
+ protected void assertOpenBrowserUrl(EmbedVaadinConfig config, String openBrowserUrl) {
+ assertEquals("Wrong open browser url", openBrowserUrl, config.getOpenBrowserUrl());
}
@@ -33,7 +33,8 @@ public void createDefaultInstance() {
EmbedVaadinConfig.DEFAULT_WAITING);
assertDeployUrl(config, "http://localhost:[auto]/");
assertVaadinConfig(config, null, EmbedVaadinConfig.DEFAULT_PRODUCTION_MODE);
- assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER);
+ assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER, null);
+ assertOpenBrowserUrl(config, "http://localhost:[auto]/");
}
@Test(expected = IllegalStateException.class)
@@ -48,7 +49,8 @@ public void load() {
assertServerConfig(config, 12345, "/foo", false);
assertDeployUrl(config, "http://localhost:12345/foo");
assertVaadinConfig(config, "com.bsb.foo.MyWidgetSet", true);
- assertBrowserConfig(config, true);
+ assertBrowserConfig(config, true, "/foo/bar");
+ assertOpenBrowserUrl(config, "http://localhost:12345/foo/bar");
}
@Test(expected = IllegalStateException.class)
@@ -69,12 +71,13 @@ public void cloneConfig() {
config.setProductionMode(true);
config.setWaiting(true);
config.setOpenBrowser(true);
+ config.setCustomBrowserUrl("?debug");
// Now validate the clone has not changed
assertServerConfig(clone, EmbedVaadinConfig.DEFAULT_PORT, EmbedVaadinConfig.DEFAULT_CONTEXT_PATH,
EmbedVaadinConfig.DEFAULT_WAITING);
assertVaadinConfig(clone, null, EmbedVaadinConfig.DEFAULT_PRODUCTION_MODE);
- assertBrowserConfig(clone, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER);
+ assertBrowserConfig(clone, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER, null);
}
@Test
@@ -89,5 +92,27 @@ public void loadCleansRootContextPath() {
assertEquals("Context path '/' should have been detected as root context", "", config.getContextPath());
}
+ @Test
+ public void openBrowserToUrl() {
+ final EmbedVaadinConfig config = EmbedVaadinConfig.defaultConfig();
+ config.setCustomBrowserUrl("http://www.google.com");
+ assertOpenBrowserUrl(config, "http://www.google.com");
+ }
+
+ @Test
+ public void openBrowserToRelativeRef() {
+ final EmbedVaadinConfig config = EmbedVaadinConfig.defaultConfig();
+ config.setPort(8080);
+ config.setCustomBrowserUrl("?debug");
+ assertOpenBrowserUrl(config, "http://localhost:8080/?debug");
+ }
+
+ @Test
+ public void openBrowserToAbsoluteRef() {
+ final EmbedVaadinConfig config = EmbedVaadinConfig.defaultConfig();
+ config.setCustomBrowserUrl("/bar?debug");
+ assertOpenBrowserUrl(config, "http://localhost:[auto]/bar?debug");
+ }
+
}
@@ -58,7 +58,24 @@ public void startWithCustomWidgetSetAndCustomPort() {
checkVaadinIsDeployed(18002, "");
server.stop();
+ }
+
+ @Test
+ public void startWithCustomBrowserUrl() {
+ final EmbedVaadinServer server = EmbedVaadin.forComponent(new Button("Hello"))
+ .openBrowserAt("?debug").openBrowser(false).wait(false).build();
+
+ assertOpenBrowserUrl(server.getConfig(), "http://localhost:[auto]/?debug");
+
+ server.start();
+ // Once the server has started, the port should be set in the config
+ assertTrue("A port should have been allocated automatically", server.getConfig().getPort() != 0);
+ assertOpenBrowserUrl(server.getConfig(), "http://localhost:" + server.getConfig().getPort() + "/?debug");
+
+ checkVaadinIsDeployed(server.getConfig().getPort(), "");
+
+ server.stop();
}
}
@@ -34,7 +34,7 @@ public void createDefaultInstance() {
assertComponentConfig(config, EmbedComponentConfig.DEFAULT_THEME,
EmbedComponentConfig.DEFAULT_DEVELOPMENT_HEADER);
assertVaadinConfig(config, null, EmbedComponentConfig.DEFAULT_PRODUCTION_MODE);
- assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER);
+ assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER, null);
}
@Test
@@ -44,7 +44,7 @@ public void load() {
assertServerConfig(config, 12345, "/foo", false);
assertVaadinConfig(config, "com.bsb.foo.MyWidgetSet", true);
assertComponentConfig(config, "myTheme", false);
- assertBrowserConfig(config, true);
+ assertBrowserConfig(config, true, "/foo/bar");
}
@Test
@@ -16,6 +16,7 @@
package com.bsb.common.vaadin.embed.support;
import com.bsb.common.vaadin.embed.AbstractEmbedTest;
+import com.bsb.common.vaadin.embed.EmbedVaadinServer;
import com.bsb.common.vaadin.embed.component.EmbedComponentConfig;
import com.bsb.common.vaadin.embed.component.EmbedVaadinComponent;
import com.google.common.io.Files;
@@ -154,6 +155,15 @@ public void withOpenBrowser() {
}
@Test
+ public void withOpenBrowserAtEnablesOpenBrowser() {
+ final EmbedVaadinComponent embed = EmbedVaadin.forComponent(component).openBrowserAt("?debug");
+ final EmbedVaadinServer server = embed.build();
+ assertEquals("was not detected as expected", "?debug", server.getConfig().getCustomBrowserUrl());
+ assertTrue("openBrowserAt should enable openBrowser flag automatically",
+ server.getConfig().shouldOpenBrowser());
+ }
+
+ @Test
public void withCustomConfig() {
final EmbedVaadinComponent embed = EmbedVaadin.forComponent(component)
.withConfigPath("/config/simple-embed-vaadin.properties");
@@ -10,5 +10,6 @@ vaadin.widgetSet=com.bsb.foo.MyWidgetSet
vaadin.productionMode=true
open.browser=true
+browser.customUrl=/foo/bar
development.header=false

0 comments on commit d9dd60d

Please sign in to comment.