Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:org.brunocvcunha.taskerbox.core.http.TaskerboxHttpBox.java

/**
 * Getting Singleton/*from  w  ww.  j a v  a2  s  .  com*/
 *
 * @return
 * @throws IOException
 */
public static synchronized TaskerboxHttpBox getInstance() throws IOException {
    if (instance == null) {

        log.info("Creating new HttpClient...");

        Properties prop = new Properties();

        File configDir = TaskerboxConfigurationUtils.getConfigurationDir();

        String hostName = InetAddress.getLocalHost().getHostName();
        File hostFile = new File(configDir, "taskerbox-" + hostName + ".properties");
        if (hostFile.exists()) {
            prop.load(new FileInputStream(hostFile));
        }

        log.info("HTTP Using Proxy? " + prop.getProperty("proxy"));

        instance = new TaskerboxHttpBox();
        if (isTrue(prop.getProperty("proxy"))) {
            instance.setUseProxy(true);
            instance.setProxyHost(prop.getProperty("proxy.host"));
            instance.setProxyPort(Integer.valueOf(prop.getProperty("proxy.port")));
            instance.setAuthProxy(isTrue(prop.getProperty("proxy.auth")));

            instance.setProxySocks(isTrue(prop.getProperty("proxy.socks")));
            instance.setUseNtlm(isTrue(prop.getProperty("http.use.ntlm")));
            instance.setSocksHost(prop.getProperty("proxy.socks.host"));
            instance.setSocksPort(Integer.valueOf(prop.getProperty("proxy.socks.port")));
            instance.setNtlmProxy(isTrue(prop.getProperty("proxy.ntlm")));
            instance.setProxyDomain(prop.getProperty("proxy.domain"));
            instance.setProxyUser(prop.getProperty("proxy.user"));
            instance.setProxyPassword(prop.getProperty("proxy.password"));
            instance.setProxyWorkstation(prop.getProperty("proxy.workstation"));
        }
        instance.setup();
    }
    return instance;
}

From source file:io.fabric8.kubernetes.pipeline.KubernetesPipelineTest.java

private void configureCloud(JenkinsRuleNonLocalhost r) throws Exception {
    // Slaves running in Kubernetes (minikube) need to connect to this server, so localhost does not work
    URL url = r.getURL();/*from   ww  w  . j  a va  2 s .  c  o m*/
    URL nonLocalhostUrl = new URL(url.getProtocol(), InetAddress.getLocalHost().getHostAddress(), url.getPort(),
            url.getFile());
    JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString());

    r.jenkins.clouds.add(cloud);
}

From source file:com.homeadvisor.kafdrop.config.ServiceDiscoveryConfiguration.java

public Map<String, Object> serviceDetails(Integer serverPort) {
    Map<String, Object> details = new LinkedHashMap<>();

    Optional.ofNullable(infoEndpoint.invoke()).ifPresent(
            infoMap -> Optional.ofNullable((Map<String, Object>) infoMap.get("build")).ifPresent(buildInfo -> {
                details.put("serviceName", buildInfo.get("artifact"));
                details.put("serviceDescription", buildInfo.get("description"));
                details.put("serviceVersion", buildInfo.get("version"));
            }));//w  w  w.  j  ava 2s. com

    final String name = (String) details.getOrDefault("serviceName", "kafdrop");

    String host = null;
    try {
        host = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        host = "<unknown>";
    }

    details.put("id", Stream.of(name, host, UUID.randomUUID().toString()).collect(Collectors.joining("_")));
    details.put("name", name);
    details.put("host", host);
    details.put("jmxPort", JmxUtils.getJmxPort(environment));
    details.put("jmxHealthMBean", jmxDomain + ":name=" + healthCheckBeanName() + ",type="
            + ClassUtils.getShortName(HealthCheckConfiguration.HealthCheck.class));
    details.put("port", serverPort);

    return details;
}

From source file:org.opcfoundation.ua.application.Application.java

public Application() {
    // Create application name
    String publicHostname = "";
    try {//from w  w w.ja  va  2s.  c  om
        publicHostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
    }

    applicationDescription.setApplicationUri("urn:" + publicHostname + ":" + UUID.randomUUID());

    getOpctcpSettings().setCertificateValidator(CertificateValidator.ALLOW_ALL);
    getHttpsSettings().setCertificateValidator(CertificateValidator.ALLOW_ALL);
    getHttpsSettings().setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}

From source file:com.floragunn.searchguard.config.EvaluatorTest.java

@Test
public void testEval3() throws IOException, MalformedConfigurationException {
    final TokenEvaluator te = new TokenEvaluator(new BytesArray(loadFile("ac_rules_4.json")));

    Evaluator eval = te.getEvaluator(Lists.<String>newArrayList("public"), Lists.<String>newArrayList(),
            Lists.<String>newArrayList(), InetAddress.getLocalHost(), new User("test"));
    Assert.assertTrue(eval.getBypassAll());
    Assert.assertFalse(eval.getExecuteAll());
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("XX", "XX"));

    eval = te.getEvaluator(Lists.<String>newArrayList("eight"), Lists.<String>newArrayList(),
            Lists.<String>newArrayList(), InetAddress.getByName("8.8.8.8"), new User("test"));
    Assert.assertTrue(eval.getBypassAll());
    Assert.assertFalse(eval.getExecuteAll());
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("XX", "XX"));

    eval = te.getEvaluator(Lists.<String>newArrayList(), Lists.<String>newArrayList("internal"),
            Lists.<String>newArrayList(), InetAddress.getByName("127.0.0.1"), new User("test"));
    Assert.assertTrue(eval.getBypassAll());
    Assert.assertFalse(eval.getExecuteAll());
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("XX", "XX"));

    eval = te.getEvaluator(Lists.<String>newArrayList(), Lists.<String>newArrayList("internal"),
            Lists.<String>newArrayList("xxx"), InetAddress.getByName("8.8.8.8"), new User("test"));
    Assert.assertTrue(eval.getBypassAll());
    Assert.assertFalse(eval.getExecuteAll());
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("check", "1"));
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("XX", "XX"));
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("read", "only"));

    final User user = new User("jacksonm");
    user.addRole("ceo");

    eval = te.getEvaluator(Lists.<String>newArrayList(), Lists.<String>newArrayList("internal"),
            Lists.<String>newArrayList("xxx"), InetAddress.getByName("8.8.8.8"), user);
    Assert.assertTrue(eval.getBypassAll());
    Assert.assertFalse(eval.getExecuteAll());
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("check", "1"));
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("XX", "XX"));
    Assert.assertEquals(FilterAction.BYPASS, eval.evaluateFilter("read", "only"));
}

From source file:com.at.lic.LicenseControl.java

private String getMAC() throws Exception {
    String mac = "1:2:3:4:5:6:7:8"; // default mac address

    InetAddress addr = InetAddress.getLocalHost();
    NetworkInterface ni = NetworkInterface.getByInetAddress(addr);

    if (ni.isLoopback() || ni.isVirtual()) {
        ni = null;//from www . j  ava2s  .c  o  m
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            NetworkInterface aNI = (NetworkInterface) nis.nextElement();
            if (!aNI.isLoopback() && !aNI.isVirtual()) {
                ni = aNI;
                break;
            }
        }
    }

    if (ni != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        byte[] HAs = ni.getHardwareAddress();
        for (int i = 0; i < HAs.length; i++) {
            ps.format("%02X:", HAs[i]);
        }
        mac = baos.toString();
        if (mac.length() > 0) {
            mac = mac.replaceFirst(":$", "");
        }

        ps.close();
        baos.close();

    }

    return mac;
}

From source file:edu.berkeley.sparrow.daemon.nodemonitor.NodeMonitorThrift.java

/**
 * Initialize this thrift service./*from   w  w  w  . ja va2 s  . c om*/
 *
 * This spawns 2 multi-threaded thrift servers, one exposing the app-facing
 * agent service and the other exposing the internal-facing agent service,
 * and listens for requests to both servers. We require explicit specification of the
 * ports for these respective interfaces, since they cannot always be determined from
 * within this class under certain configurations (e.g. a config file specifies
 * multiple NodeMonitors).
 */
public void initialize(Configuration conf, int nmPort, int internalPort) throws IOException {
    nodeMonitor.initialize(conf, internalPort);

    // Setup application-facing agent service.
    NodeMonitorService.Processor<NodeMonitorService.Iface> processor = new NodeMonitorService.Processor<NodeMonitorService.Iface>(
            this);

    int threads = conf.getInt(SparrowConf.NM_THRIFT_THREADS, DEFAULT_NM_THRIFT_THREADS);
    TServers.launchThreadedThriftServer(nmPort, threads, processor);

    // Setup internal-facing agent service.
    InternalService.Processor<InternalService.Iface> internalProcessor = new InternalService.Processor<InternalService.Iface>(
            this);
    int internalThreads = conf.getInt(SparrowConf.INTERNAL_THRIFT_THREADS, DEFAULT_INTERNAL_THRIFT_THREADS);
    TServers.launchThreadedThriftServer(internalPort, internalThreads, internalProcessor);

    internalAddr = new InetSocketAddress(InetAddress.getLocalHost(), internalPort);
}

From source file:net.sf.j2ep.requesthandlers.RequestHandlerBase.java

/**
 * Will write the proxy specific headers such as Via and x-forwarded-for.
 * //from   w w w  .  j a  v  a  2s  .  c o m
 * @param method Method to write the headers to
 * @param request The incoming request, will need to get virtual host.
 * @throws HttpException 
 */
private void setProxySpecificHeaders(HttpMethod method, HttpServletRequest request) throws HttpException {
    String serverHostName = "jEasyExtensibleProxy";
    try {
        serverHostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        log.error("Couldn't get the hostname needed for headers x-forwarded-server and Via", e);
    }

    String originalVia = request.getHeader("via");
    StringBuffer via = new StringBuffer("");
    if (originalVia != null) {
        if (originalVia.indexOf(serverHostName) != -1) {
            log.error("This proxy has already handled the request, will abort.");
            throw new HttpException("Request has a cyclic dependency on this proxy.");
        }
        via.append(originalVia).append(", ");
    }
    via.append(request.getProtocol()).append(" ").append(serverHostName);

    method.setRequestHeader("via", via.toString());
    method.setRequestHeader("x-forwarded-for", request.getRemoteAddr());
    method.setRequestHeader("x-forwarded-host", request.getServerName());
    method.setRequestHeader("x-forwarded-server", serverHostName);

    method.setRequestHeader("accept-encoding", "");
}