13

If I have a relative path to a static asset (flash/blah.swf), what is the best way to programmatically convert this to an absolute URL (http://localhost/app/flash/blah.swf)? Or what is the best way to get the base URL of the Wicket application? I've tried using RequestUtils.toAbsolutePath but it doesn't seem to work reliably and is frequently throwing exceptions. This needs to work on all servers the app is deployed to.

Gilean
  • 14,708
  • 10
  • 45
  • 52

5 Answers5

13

For Wicket 6 it is

String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse("my-relative-url.html"));
Manuel Manhart
  • 4,819
  • 3
  • 24
  • 28
  • 4
    And base URL is `String baseUrl = RequestCycle.get().getUrlRenderer().getBaseUrl().toString()` – Brad M Oct 22 '14 at 20:53
8
RequestUtils.toAbsolutePath(RequestCycle.get().getRequest().
  getRelativePathPrefixToWicketHandler());

worked for me.

Pops
  • 30,199
  • 37
  • 136
  • 151
Richard Noble
  • 213
  • 3
  • 6
1

For Wicket 1.5 there is information here

Don Roby
  • 40,677
  • 6
  • 91
  • 113
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
0

org.apache.wicket.protocol.http.servlet.ServletWebRequest has a method getRelativePathPrefixToContextRoot() (actually defined as abstract in a superclass).

A standard idiom for using it is

RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot() + location;
Don Roby
  • 40,677
  • 6
  • 91
  • 113
0

I ended up using something like this after adding a base_url property to my MyApplication class that extends the wicket application.

MyApplication app = (MyApplication)getApplication();
String appBaseUrl = app.getBaseUrl(); 
if (StringUtils.isEmpty(appBaseUrl)) { 
    appBaseUrl = RequestUtils.toAbsolutePath(urlFor(app.getHomePage(), new PageParameters()).toString());
    app.setBaseUrl(appBaseUrl); 
} 

// Add base URL to <script wicket:id="base_url"></script> to use with Flash
add(new Label("base_url", "base_url = \"" + appBaseUrl + "\";").setEscapeModelStrings(false)); 
Gilean
  • 14,708
  • 10
  • 45
  • 52