Skip to content

Commit

Permalink
#7 openBrowserAt allows to specify a custom url when opening the brow…
Browse files Browse the repository at this point in the history
…ser.
  • Loading branch information
snicoll committed Apr 28, 2012
1 parent 7d91dbe commit d9dd60d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 21 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void doStart() throws LifecycleException {


logger.info("Application has been deployed to [" + getConfig().getDeployUrl() + "]"); logger.info("Application has been deployed to [" + getConfig().getDeployUrl() + "]");
if (config.shouldOpenBrowser()) { if (config.shouldOpenBrowser()) {
BrowserUtils.openBrowser(getConfig().getDeployUrl()); BrowserUtils.openBrowser(getConfig().getOpenBrowserUrl());
} }
if (isWaiting()) { if (isWaiting()) {
tomcat.getServer().await(); tomcat.getServer().await();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class EmbedVaadinConfig implements Serializable {


private static final Logger logger = LoggerFactory.getLogger(EmbedVaadinConfig.class); 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"; public static final String CONFIG_LOCATION = "/embed-vaadin.properties";


/** /**
Expand All @@ -60,9 +63,9 @@ public class EmbedVaadinConfig implements Serializable {


/** /**
* The default HTTP port to use if none is set. By default, an available port * 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. * The key defining the context path to use.
Expand All @@ -80,7 +83,7 @@ public class EmbedVaadinConfig implements Serializable {
public static final String KEY_CONTEXT_ROOT_DIR = "context.rootDir"; 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"; public static final String KEY_WAITING = "server.await";


Expand Down Expand Up @@ -114,6 +117,11 @@ public class EmbedVaadinConfig implements Serializable {
*/ */
public static final boolean DEFAULT_OPEN_BROWSER = false; 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 int port;
private String contextPath; private String contextPath;
Expand All @@ -124,6 +132,7 @@ public class EmbedVaadinConfig implements Serializable {
private boolean productionMode; private boolean productionMode;


private boolean openBrowser; private boolean openBrowser;
private String customBrowserUrl;


/** /**
* Creates a new instance using the configuration in the given {@link Properties} * Creates a new instance using the configuration in the given {@link Properties}
Expand All @@ -146,6 +155,7 @@ public EmbedVaadinConfig(Properties properties) {
productionMode = helper.getBooleanProperty(KEY_PRODUCTION_MODE, DEFAULT_PRODUCTION_MODE); productionMode = helper.getBooleanProperty(KEY_PRODUCTION_MODE, DEFAULT_PRODUCTION_MODE);


openBrowser = helper.getBooleanProperty(KEY_OPEN_BROWSER, DEFAULT_OPEN_BROWSER); openBrowser = helper.getBooleanProperty(KEY_OPEN_BROWSER, DEFAULT_OPEN_BROWSER);
customBrowserUrl = properties.getProperty(KEY_CUSTOM_BROWSER_URL);


logger.debug("Using " + this); logger.debug("Using " + this);


Expand All @@ -166,6 +176,7 @@ protected EmbedVaadinConfig(EmbedVaadinConfig clone) {
this.widgetSet = clone.widgetSet; this.widgetSet = clone.widgetSet;
this.productionMode = clone.productionMode; this.productionMode = clone.productionMode;
this.openBrowser = clone.openBrowser; this.openBrowser = clone.openBrowser;
this.customBrowserUrl = clone.customBrowserUrl;
} }


/** /**
Expand Down Expand Up @@ -262,6 +273,16 @@ public boolean shouldOpenBrowser() {
return openBrowser; return openBrowser;
} }


/**
* 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 * Returns the full url of the application, according to the port and
* context path. * context path.
Expand All @@ -273,21 +294,31 @@ public boolean shouldOpenBrowser() {
* @return the url of the web application * @return the url of the web application
*/ */
public String getDeployUrl() { public String getDeployUrl() {
final StringBuilder sb = new StringBuilder(); if (getContextPath().isEmpty()) {

return buildUrl(getPort(), "/");
sb.append("http://localhost:");
final int httpPort = getPort();
if (httpPort == 0) {
sb.append("[auto]");
} else { } 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 { } else {
sb.append(getContextPath()); return deployUrl + customBrowserUrl;
} }
return sb.toString();
} }


void setPort(int port) { void setPort(int port) {
Expand Down Expand Up @@ -318,6 +349,10 @@ void setOpenBrowser(boolean openBrowser) {
this.openBrowser = openBrowser; this.openBrowser = openBrowser;
} }


final void setCustomBrowserUrl(String customBrowserUrl) {
this.customBrowserUrl = customBrowserUrl;
}

private void validate() { private void validate() {
if (!contextRootDirectory.exists()) { if (!contextRootDirectory.exists()) {
throw new IllegalStateException("Cannot find file [" + contextRootDirectory.getAbsolutePath() + "]. " throw new IllegalStateException("Cannot find file [" + contextRootDirectory.getAbsolutePath() + "]. "
Expand Down Expand Up @@ -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) { private static Properties loadProperties(String path, boolean failIfNotFound) {
try { try {
final Properties properties = new Properties(); final Properties properties = new Properties();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -188,6 +188,34 @@ public B openBrowser(boolean open) {
return self(); return self();
} }


/**
* 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. * Builds an {@link EmbedVaadinServer} and starts it immediately.
* *
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ protected void assertVaadinConfig(EmbedVaadinConfig config, String widgetSet, bo
assertEquals("Wrong production mode", productionMode, config.isProductionMode()); 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); assertConfigIsNotNull(config);
assertEquals("Wrong openBrowser flag", openBrowser, config.shouldOpenBrowser()); 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());
} }




Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void createDefaultInstance() {
EmbedVaadinConfig.DEFAULT_WAITING); EmbedVaadinConfig.DEFAULT_WAITING);
assertDeployUrl(config, "http://localhost:[auto]/"); assertDeployUrl(config, "http://localhost:[auto]/");
assertVaadinConfig(config, null, EmbedVaadinConfig.DEFAULT_PRODUCTION_MODE); 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) @Test(expected = IllegalStateException.class)
Expand All @@ -48,7 +49,8 @@ public void load() {
assertServerConfig(config, 12345, "/foo", false); assertServerConfig(config, 12345, "/foo", false);
assertDeployUrl(config, "http://localhost:12345/foo"); assertDeployUrl(config, "http://localhost:12345/foo");
assertVaadinConfig(config, "com.bsb.foo.MyWidgetSet", true); 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) @Test(expected = IllegalStateException.class)
Expand All @@ -69,12 +71,13 @@ public void cloneConfig() {
config.setProductionMode(true); config.setProductionMode(true);
config.setWaiting(true); config.setWaiting(true);
config.setOpenBrowser(true); config.setOpenBrowser(true);
config.setCustomBrowserUrl("?debug");


// Now validate the clone has not changed // Now validate the clone has not changed
assertServerConfig(clone, EmbedVaadinConfig.DEFAULT_PORT, EmbedVaadinConfig.DEFAULT_CONTEXT_PATH, assertServerConfig(clone, EmbedVaadinConfig.DEFAULT_PORT, EmbedVaadinConfig.DEFAULT_CONTEXT_PATH,
EmbedVaadinConfig.DEFAULT_WAITING); EmbedVaadinConfig.DEFAULT_WAITING);
assertVaadinConfig(clone, null, EmbedVaadinConfig.DEFAULT_PRODUCTION_MODE); assertVaadinConfig(clone, null, EmbedVaadinConfig.DEFAULT_PRODUCTION_MODE);
assertBrowserConfig(clone, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER); assertBrowserConfig(clone, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER, null);
} }


@Test @Test
Expand All @@ -89,5 +92,27 @@ public void loadCleansRootContextPath() {
assertEquals("Context path '/' should have been detected as root context", "", config.getContextPath()); 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");
}

} }


Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,7 +58,24 @@ public void startWithCustomWidgetSetAndCustomPort() {
checkVaadinIsDeployed(18002, ""); checkVaadinIsDeployed(18002, "");


server.stop(); 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();
} }


} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void createDefaultInstance() {
assertComponentConfig(config, EmbedComponentConfig.DEFAULT_THEME, assertComponentConfig(config, EmbedComponentConfig.DEFAULT_THEME,
EmbedComponentConfig.DEFAULT_DEVELOPMENT_HEADER); EmbedComponentConfig.DEFAULT_DEVELOPMENT_HEADER);
assertVaadinConfig(config, null, EmbedComponentConfig.DEFAULT_PRODUCTION_MODE); assertVaadinConfig(config, null, EmbedComponentConfig.DEFAULT_PRODUCTION_MODE);
assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER); assertBrowserConfig(config, EmbedVaadinConfig.DEFAULT_OPEN_BROWSER, null);
} }


@Test @Test
Expand All @@ -44,7 +44,7 @@ public void load() {
assertServerConfig(config, 12345, "/foo", false); assertServerConfig(config, 12345, "/foo", false);
assertVaadinConfig(config, "com.bsb.foo.MyWidgetSet", true); assertVaadinConfig(config, "com.bsb.foo.MyWidgetSet", true);
assertComponentConfig(config, "myTheme", false); assertComponentConfig(config, "myTheme", false);
assertBrowserConfig(config, true); assertBrowserConfig(config, true, "/foo/bar");
} }


@Test @Test
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.bsb.common.vaadin.embed.support; package com.bsb.common.vaadin.embed.support;


import com.bsb.common.vaadin.embed.AbstractEmbedTest; 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.EmbedComponentConfig;
import com.bsb.common.vaadin.embed.component.EmbedVaadinComponent; import com.bsb.common.vaadin.embed.component.EmbedVaadinComponent;
import com.google.common.io.Files; import com.google.common.io.Files;
Expand Down Expand Up @@ -153,6 +154,15 @@ public void withOpenBrowser() {
assertEquals("was not detected as expected", true, embed.build().getConfig().shouldOpenBrowser()); assertEquals("was not detected as expected", true, embed.build().getConfig().shouldOpenBrowser());
} }


@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 @Test
public void withCustomConfig() { public void withCustomConfig() {
final EmbedVaadinComponent embed = EmbedVaadin.forComponent(component) final EmbedVaadinComponent embed = EmbedVaadin.forComponent(component)
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ vaadin.widgetSet=com.bsb.foo.MyWidgetSet
vaadin.productionMode=true vaadin.productionMode=true


open.browser=true open.browser=true
browser.customUrl=/foo/bar


development.header=false development.header=false

0 comments on commit d9dd60d

Please sign in to comment.