diff --git a/src/main/java/com/github/dockerjava/api/model/ExposedPort.java b/src/main/java/com/github/dockerjava/api/model/ExposedPort.java index d085f6c4f..d19bfbf05 100644 --- a/src/main/java/com/github/dockerjava/api/model/ExposedPort.java +++ b/src/main/java/com/github/dockerjava/api/model/ExposedPort.java @@ -1,5 +1,8 @@ package com.github.dockerjava.api.model; +import static com.github.dockerjava.api.model.InternetProtocol.TCP; +import static com.github.dockerjava.api.model.InternetProtocol.UDP; + import java.io.IOException; import java.util.Map.Entry; @@ -22,8 +25,8 @@ /** * Represents a container port that Docker exposes to external clients. - * The port is defined by its {@link #getPort() port number} and a - * {@link #getScheme() scheme}, e.g. tcp. + * The port is defined by its {@link #getPort() port number} and an + * {@link InternetProtocol}. * It can be published by Docker by {@link Ports#bind(ExposedPort, Binding) binding} * it to a host port, represented by a {@link Binding}. */ @@ -31,47 +34,66 @@ @JsonSerialize(using = ExposedPort.Serializer.class) public class ExposedPort { - private final String scheme; - + private final InternetProtocol protocol; private final int port; + /** + * Creates an {@link ExposedPort} for the given parameters. + * + * @param port the {@link #getPort() port number} + * @param protocol the {@link InternetProtocol} + */ + public ExposedPort(int port, InternetProtocol protocol) { + this.port = port; + this.protocol = protocol; + } + /** * Creates an {@link ExposedPort} for the given parameters. * * @param scheme the {@link #getScheme() scheme}, tcp or * udp * @param port the {@link #getPort() port number} + * @deprecated use {@link #ExposedPort(int, InternetProtocol)} */ + @Deprecated public ExposedPort(String scheme, int port) { - this.scheme = scheme; - this.port = port; + this(port, InternetProtocol.valueOf(scheme)); } + /** @return the {@link InternetProtocol} */ + public InternetProtocol getProtocol() { + return protocol; + } + /** - * @return the scheme (IP protocol), tcp or udp + * @return the scheme (internet protocol), tcp or udp + * @deprecated use {@link #getProtocol()} */ + @Deprecated public String getScheme() { - return scheme; + return protocol.toString(); } + /** @return the port number */ public int getPort() { return port; } /** - * Creates an {@link ExposedPort} for the TCP scheme. - * This is a shortcut for new ExposedPort("tcp", port) + * Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}. + * This is a shortcut for new ExposedPort(port, {@link InternetProtocol#TCP}) */ public static ExposedPort tcp(int port) { - return new ExposedPort("tcp", port); + return new ExposedPort(port, TCP); } /** - * Creates an {@link ExposedPort} for the UDP scheme. - * This is a shortcut for new ExposedPort("udp", port) + * Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}. + * This is a shortcut for new ExposedPort(port, {@link InternetProtocol#UDP}) */ public static ExposedPort udp(int port) { - return new ExposedPort("udp", port); + return new ExposedPort(port, UDP); } /** @@ -85,8 +107,7 @@ public static ExposedPort udp(int port) { public static ExposedPort parse(String serialized) throws IllegalArgumentException { try { String[] parts = serialized.split("/"); - ExposedPort out = new ExposedPort(parts[1], Integer.valueOf(parts[0])); - return out; + return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1])); } catch (Exception e) { throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'"); } @@ -95,20 +116,20 @@ public static ExposedPort parse(String serialized) throws IllegalArgumentExcepti /** * Returns a string representation of this {@link ExposedPort} suitable * for inclusion in a JSON message. - * The format is port/scheme, like the argument in {@link #parse(String)}. + * The format is port/protocol, like the argument in {@link #parse(String)}. * * @return a string representation of this {@link ExposedPort} */ @Override public String toString() { - return port + "/" + scheme; + return port + "/" + protocol.toString(); } @Override public boolean equals(Object obj) { if (obj instanceof ExposedPort) { ExposedPort other = (ExposedPort) obj; - return new EqualsBuilder().append(scheme, other.getScheme()) + return new EqualsBuilder().append(protocol, other.getProtocol()) .append(port, other.getPort()).isEquals(); } else return super.equals(obj); @@ -116,7 +137,7 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return new HashCodeBuilder().append(scheme).append(port).toHashCode(); + return new HashCodeBuilder().append(protocol).append(port).toHashCode(); } public static class Deserializer extends JsonDeserializer { diff --git a/src/main/java/com/github/dockerjava/api/model/InternetProtocol.java b/src/main/java/com/github/dockerjava/api/model/InternetProtocol.java new file mode 100644 index 000000000..96c215245 --- /dev/null +++ b/src/main/java/com/github/dockerjava/api/model/InternetProtocol.java @@ -0,0 +1,49 @@ +package com.github.dockerjava.api.model; + + +/** + * The IP protocols supported by Docker. + * + * @see #TCP + * @see #UDP + */ +public enum InternetProtocol { + /** The Transmission Control Protocol */ + TCP, + + /** The User Datagram Protocol */ + UDP; + + /** + * The default {@link InternetProtocol}: {@link #TCP} + */ + public static final InternetProtocol DEFAULT = TCP; + + /** + * Returns a string representation of this {@link InternetProtocol} suitable + * for inclusion in a JSON message. + * The output is the lowercased name of the Protocol, e.g. tcp. + * + * @return a string representation of this {@link InternetProtocol} + */ + @Override + public String toString() { + return super.toString().toLowerCase(); + } + + /** + * Parses a string to an {@link InternetProtocol}. + * + * @param serialized the protocol, e.g. tcp or TCP + * @return an {@link InternetProtocol} described by the string + * @throws IllegalArgumentException if the argument cannot be parsed + */ + public static InternetProtocol parse(String serialized) throws IllegalArgumentException { + try { + return valueOf(serialized.toUpperCase()); + } catch (Exception e) { + throw new IllegalArgumentException("Error parsing Protocol '" + serialized + "'"); + } + } + +} diff --git a/src/test/java/com/github/dockerjava/api/model/InternetProtocolTest.java b/src/test/java/com/github/dockerjava/api/model/InternetProtocolTest.java new file mode 100644 index 000000000..ea0b20d73 --- /dev/null +++ b/src/test/java/com/github/dockerjava/api/model/InternetProtocolTest.java @@ -0,0 +1,42 @@ +package com.github.dockerjava.api.model; + +import static com.github.dockerjava.api.model.InternetProtocol.*; +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class InternetProtocolTest { + + @Test + public void defaultProtocol() { + assertEquals(InternetProtocol.DEFAULT, TCP); + } + + @Test + public void stringify() { + assertEquals(TCP.toString(), "tcp"); + } + + @Test + public void parseUpperCase() { + assertEquals(InternetProtocol.parse("TCP"), TCP); + } + + @Test + public void parseLowerCase() { + assertEquals(InternetProtocol.parse("tcp"), TCP); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Protocol.*") + public void parseInvalidInput() { + InternetProtocol.parse("xx"); + } + + @Test(expectedExceptions = IllegalArgumentException.class, + expectedExceptionsMessageRegExp = "Error parsing Protocol 'null'") + public void parseNull() { + InternetProtocol.parse(null); + } + +}