All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.nio.common.InetLocation Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.common;

import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.control.Either;
import io.vavr.control.Try;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Optional;

//@Accessors(fluent = true)
//@Builder(toBuilder = true)
public final class InetLocation {

    private final URL url;

    private final InetSocketAddress socketAddress;

    private final String protocol;

    private final String host;

    private final String path;

    private final String query;

    private final int port;

    private InetLocation(URL url, InetSocketAddress socketAddress, String protocol, String host, String path,
        String query, int port) {
        this.url = url;
        this.socketAddress = socketAddress;
        this.protocol = protocol;
        this.host = host;
        this.path = path;
        this.query = query;
        this.port = port;
    }

    public URL getUrl() {
        return url;
    }

    public InetSocketAddress getSocketAddress() {
        return socketAddress;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getHost() {
        return host;
    }

    public String getPath() {
        return path;
    }

    public String getQuery() {
        return query;
    }

    public int getPort() {
        return port;
    }

    public Optional address() {
        return Optional.of(socketAddress);
    }

    public InetLocationBuilder toBuilder() {
        return new InetLocationBuilder().host(host).path(path).port(port).protocol(protocol).query(query)
            .socketAddress(socketAddress).url(url);
    }

    @Override
    public String toString() {
        return "http://" + host + (port != -1 ? ":" + port : "") + path;
    }

    public static InetLocation fromSpec(final String spec) throws MalformedURLException, UnknownHostException {
        final InetLocationBuilder[] url = new InetLocationBuilder[1];
        tryUrlOrSocketAddress(spec).peekLeft(javaNetUrl -> {
            url[0] = InetLocation.builder()
                .protocol(javaNetUrl.getProtocol())
                .port(javaNetUrl.getPort() != -1 ? javaNetUrl.getPort() : NioConstants.DEFAULT_PORT)
                .host(javaNetUrl.getHost())
                .path(javaNetUrl.getPath())
                .query(javaNetUrl.getQuery())
                .url(javaNetUrl);
        }).peek(socketAddress -> {
            url[0] = InetLocation.builder()
                .protocol("udp")
                .port(socketAddress.getPort() != -1 ? socketAddress.getPort() : NioConstants.DEFAULT_PORT)
                .host(socketAddress.getHostName())
                .path("")
                .query(null)
                .url(null);
        });
        final InetLocation tempUrl = url[0].build();
        return tempUrl.toBuilder().socketAddress(new InetSocketAddress(InetAddress.getByName(tempUrl.getHost()), tempUrl.getPort()))
            .build(); // Shitty hack, boo Java
    }

    private static Either tryUrlOrSocketAddress(final String spec) throws MalformedURLException {
        final Optional> addressAndPortMaybe =
            Optional.ofNullable(spec)
                .map(s -> Tuple.of(
                    s.replaceAll(":\\d+", ""),
                    s.replaceAll("^((\\d+\\.)+\\d[.:]|(localhost:)|(\\S+:))", "")));
        final Optional> urlOrSocketAddressMaybe = addressAndPortMaybe
            .map(addressAndPort -> Try.of(() -> Either.left(new java.net.URL(spec)))
                .getOrElseTry(() -> Try.of(() -> Either.right(new InetSocketAddress(
                    InetAddress.getByName(addressAndPort._1()).getHostAddress(),
                    Integer.parseInt(addressAndPort._2())
                )))
                    .getOrElseThrow(() -> new MalformedURLException("Provided URL is neither a standard http or socket host address"))));
        return urlOrSocketAddressMaybe.orElseThrow(() -> new MalformedURLException("Provided URL was null"));
    }

    public static InetLocationBuilder builder() {
        return new InetLocationBuilder();
    }

    public static class InetLocationBuilder {

        private URL url;

        private InetSocketAddress socketAddress;

        private String protocol;

        private String host;

        private String path;

        private String query;

        private int port;

        private InetLocationBuilder() {
            super();
        }

        public InetLocationBuilder url(URL url) {
            this.url = url;
            return this;
        }

        public InetLocationBuilder socketAddress(InetSocketAddress socketAddress) {
            this.socketAddress = socketAddress;
            return this;
        }

        public InetLocationBuilder protocol(String protocol) {
            this.protocol = protocol;
            return this;
        }

        public InetLocationBuilder host(String host) {
            this.host = host;
            return this;
        }

        public InetLocationBuilder path(String path) {
            this.path = path;
            return this;
        }

        public InetLocationBuilder query(String query) {
            this.query = query;
            return this;
        }

        public InetLocationBuilder port(int port) {
            this.port = port;
            return this;
        }

        public InetLocation build() {
            return new InetLocation(url, socketAddress, protocol, host, path, query, port);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy