Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT replacement for java.net.URL

Tags:

url

gwt

I have to replace the protocol part of a already existing url in GWT. The java.net package has a class which was build for exactly that purpose: URL. Sadly GWT does not emulate the java.net package.

How can I reassemble a url in GWT without creating my own parser? (I know about UrlBuilder, but UrlBuilder won't take an existing URL)

Example: I have a url in a string "http://myserver.com/somepath/file.html?param" and I want to replace the protocol part with "https".

like image 814
Eduard Wirch Avatar asked Nov 30 '10 08:11

Eduard Wirch


2 Answers

public void onModuleLoad() {
    Button btn = new Button("change protocol");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            UrlBuilder builder = Window.Location.createUrlBuilder().setProtocol("https");
            Window.Location.replace(builder.buildString());
        }
    });
    RootPanel.get().add(btn);
}
like image 105
z00bs Avatar answered Sep 19 '22 13:09

z00bs


It's ugly, but you can always create an anchor element and extract the parts from there.

AnchorElement a = Document.get().createAnchorElement();
a.setHref("http://test.com/somerandompath");
Window.alert(a.getPropertyString("protocol") + " " + a.getPropertyString("host")) + " " a.getPropertyString("pathname"));

a.removeFromParent();
like image 20
Blake Pettersson Avatar answered Sep 18 '22 13:09

Blake Pettersson