Use ExposedPort.toString() in serialization by albers · Pull Request #75 · docker-java/docker-java
Expand Up
@@ -18,20 +18,38 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.dockerjava.api.model.Ports.Binding;
/** * 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. <code>tcp</code>. * It can be published by Docker by {@link Ports#bind(ExposedPort, Binding) binding} * it to a host port, represented by a {@link Binding}. */ @JsonDeserialize(using = ExposedPort.Deserializer.class) @JsonSerialize(using = ExposedPort.Serializer.class) public class ExposedPort {
private String scheme; private final String scheme;
private int port; private final int port;
/** * Creates an {@link ExposedPort} for the given parameters. * * @param scheme the {@link #getScheme() scheme}, <code>tcp</code> or * <code>udp</code> * @param port the {@link #getPort() port number} */ public ExposedPort(String scheme, int port) { this.scheme = scheme; this.port = port; }
/** * @return the scheme (IP protocol), <code>tcp</code> or <code>udp</code> */ public String getScheme() { return scheme; } Expand All @@ -40,27 +58,50 @@ public int getPort() { return port; }
/** * Creates an {@link ExposedPort} for the TCP scheme. * This is a shortcut for <code>new ExposedPort("tcp", port)</code> */ public static ExposedPort tcp(int port) { return new ExposedPort("tcp", port); }
/** * Creates an {@link ExposedPort} for the UDP scheme. * This is a shortcut for <code>new ExposedPort("udp", port)</code> */ public static ExposedPort udp(int port) { return new ExposedPort("udp", port); }
public static ExposedPort parse(String serialized) { /** * Parses a textual port specification (as used by the Docker CLI) to an * {@link ExposedPort}. * * @param serialized the specification, e.g. <code>80/tcp</code> * @return an {@link ExposedPort} matching the specification * @throws IllegalArgumentException if the specification cannot be parsed */ 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; } catch (Exception e) { throw new RuntimeException("Error parsing ExposedPort '" + serialized + "'"); throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'"); } }
/** * Returns a string representation of this {@link ExposedPort} suitable * for inclusion in a JSON message. * The format is <code>port/scheme</code>, like the argument in {@link #parse(String)}. * * @return a string representation of this {@link ExposedPort} */ @Override public String toString() { return getPort() + "/" + getScheme(); return port + "/" + scheme; }
@Override Expand Down
import com.github.dockerjava.api.model.Ports.Binding;
/** * 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. <code>tcp</code>. * It can be published by Docker by {@link Ports#bind(ExposedPort, Binding) binding} * it to a host port, represented by a {@link Binding}. */ @JsonDeserialize(using = ExposedPort.Deserializer.class) @JsonSerialize(using = ExposedPort.Serializer.class) public class ExposedPort {
private String scheme; private final String scheme;
private int port; private final int port;
/** * Creates an {@link ExposedPort} for the given parameters. * * @param scheme the {@link #getScheme() scheme}, <code>tcp</code> or * <code>udp</code> * @param port the {@link #getPort() port number} */ public ExposedPort(String scheme, int port) { this.scheme = scheme; this.port = port; }
/** * @return the scheme (IP protocol), <code>tcp</code> or <code>udp</code> */ public String getScheme() { return scheme; } Expand All @@ -40,27 +58,50 @@ public int getPort() { return port; }
/** * Creates an {@link ExposedPort} for the TCP scheme. * This is a shortcut for <code>new ExposedPort("tcp", port)</code> */ public static ExposedPort tcp(int port) { return new ExposedPort("tcp", port); }
/** * Creates an {@link ExposedPort} for the UDP scheme. * This is a shortcut for <code>new ExposedPort("udp", port)</code> */ public static ExposedPort udp(int port) { return new ExposedPort("udp", port); }
public static ExposedPort parse(String serialized) { /** * Parses a textual port specification (as used by the Docker CLI) to an * {@link ExposedPort}. * * @param serialized the specification, e.g. <code>80/tcp</code> * @return an {@link ExposedPort} matching the specification * @throws IllegalArgumentException if the specification cannot be parsed */ 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; } catch (Exception e) { throw new RuntimeException("Error parsing ExposedPort '" + serialized + "'"); throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'"); } }
/** * Returns a string representation of this {@link ExposedPort} suitable * for inclusion in a JSON message. * The format is <code>port/scheme</code>, like the argument in {@link #parse(String)}. * * @return a string representation of this {@link ExposedPort} */ @Override public String toString() { return getPort() + "/" + getScheme(); return port + "/" + scheme; }
@Override Expand Down