URL sanitization.
authorTomas Zezula <tzezula@netbeans.org>
Thu, 06 Feb 2014 10:06:55 +0100
changeset 18161d7ba8f46af7c
parent 18160 3c8086f816d3
child 18162 4586a9fb7ed8
URL sanitization.
dew4nb/src/org/netbeans/modules/dew4nb/services/project/RedirectURLDisplayer.java
     1.1 --- a/dew4nb/src/org/netbeans/modules/dew4nb/services/project/RedirectURLDisplayer.java	Wed Feb 05 16:49:50 2014 +0100
     1.2 +++ b/dew4nb/src/org/netbeans/modules/dew4nb/services/project/RedirectURLDisplayer.java	Thu Feb 06 10:06:55 2014 +0100
     1.3 @@ -42,9 +42,13 @@
     1.4  
     1.5  package org.netbeans.modules.dew4nb.services.project;
     1.6  
     1.7 +import java.net.InetAddress;
     1.8 +import java.net.MalformedURLException;
     1.9  import java.net.URL;
    1.10 +import java.net.UnknownHostException;
    1.11  import org.netbeans.api.annotations.common.NonNull;
    1.12  import org.openide.awt.HtmlBrowser;
    1.13 +import org.openide.util.Parameters;
    1.14  import org.openide.util.lookup.ServiceProvider;
    1.15  
    1.16  /**
    1.17 @@ -54,11 +58,41 @@
    1.18  @ServiceProvider(service = HtmlBrowser.URLDisplayer.class, position = 1)
    1.19  public class RedirectURLDisplayer extends HtmlBrowser.URLDisplayer {
    1.20  
    1.21 +    private static final String LOCALHOST = "localhost";   //NOI18N
    1.22 +
    1.23      @Override
    1.24 -    public void showURL(@NonNull final URL url) {
    1.25 +    public void showURL(@NonNull URL url) {
    1.26 +        Parameters.notNull("url", url); //NOI18N
    1.27 +        final String host = url.getHost();
    1.28 +        if (isLocal(host)) {
    1.29 +            try {
    1.30 +                url = new URL(
    1.31 +                    url.getProtocol(),
    1.32 +                    java.net.InetAddress.getLocalHost().getHostName(),
    1.33 +                    url.getPort(),
    1.34 +                    url.getPath());
    1.35 +            } catch (MalformedURLException | UnknownHostException e) {
    1.36 +                //pass with original URL
    1.37 +            }
    1.38 +        }
    1.39          IORedirectProvider.showUrl(url);
    1.40      }
    1.41  
    1.42 +    private static boolean isLocal(String hostName) {
    1.43 +        if (LOCALHOST.equalsIgnoreCase(hostName)) {
    1.44 +            return true;
    1.45 +        }
    1.46 +        try {
    1.47 +            final InetAddress addr = InetAddress.getByName(LOCALHOST);
    1.48 +            if (addr.getHostAddress().equals(hostName)) {
    1.49 +                return true;
    1.50 +            }
    1.51 +        } catch (UnknownHostException ex) {
    1.52 +            //pass
    1.53 +        }
    1.54 +        return false;
    1.55 +    }
    1.56 +
    1.57   
    1.58  
    1.59  }