11package com .github .dockerjava .api .model ;
22
3- import static org .apache .commons .lang .StringUtils .isEmpty ;
4-
5- import java .io .IOException ;
6- import java .util .HashMap ;
7- import java .util .Iterator ;
8- import java .util .Map ;
9- import java .util .Map .Entry ;
10-
11- import org .apache .commons .lang .ArrayUtils ;
12- import org .apache .commons .lang .builder .EqualsBuilder ;
13-
143import com .fasterxml .jackson .core .JsonGenerator ;
154import com .fasterxml .jackson .core .JsonParser ;
165import com .fasterxml .jackson .core .JsonProcessingException ;
2312import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
2413import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
2514import com .fasterxml .jackson .databind .node .NullNode ;
15+ import org .apache .commons .lang .ArrayUtils ;
16+ import org .apache .commons .lang .builder .EqualsBuilder ;
17+
18+ import java .io .IOException ;
19+ import java .util .HashMap ;
20+ import java .util .Iterator ;
21+ import java .util .Map ;
22+ import java .util .Map .Entry ;
23+
24+ import static org .apache .commons .lang .StringUtils .isEmpty ;
2625
2726/**
2827 * A container for port bindings, made available as a {@link Map} via its {@link #getBindings()} method.
@@ -111,15 +110,15 @@ public Map<ExposedPort, Binding[]> getBindings() {
111110 /**
112111 * Creates a {@link Binding} for the given IP address and port number.
113112 */
114- public static Binding binding (String hostIp , Integer hostPort ) {
113+ public static Binding binding (String hostIp , String hostPort ) {
115114 return new Binding (hostIp , hostPort );
116115 }
117116
118117 /**
119118 * Creates a {@link Binding} for the given port number, leaving the IP address undefined.
120119 */
121- public static Binding binding (Integer hostPort ) {
122- return new Binding (hostPort );
120+ public static Binding binding (String hostPort ) {
121+ return new Binding (null , hostPort );
123122 }
124123
125124 /**
@@ -132,38 +131,38 @@ public static Binding binding(Integer hostPort) {
132131 */
133132 public static class Binding {
134133
135- private final String hostIp ;
136-
137- private final Integer hostPort ;
138-
139134 /**
140- * Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort() port number}.
135+ * Creates a {@link Binding} for the given {@link #getHostPort() port number or range}, leaving the {@link #getHostIp() IP address}
136+ * undefined.
141137 *
142138 * @see Ports#bind(ExposedPort, Binding)
143139 * @see ExposedPort
144140 */
145- public Binding (String hostIp , Integer hostPort ) {
146- this .hostIp = isEmpty (hostIp ) ? null : hostIp ;
147- this .hostPort = hostPort ;
141+ public static Binding forPort (String port ) {
142+ return new Binding (null , port );
148143 }
149144
150145 /**
151- * Creates a {@link Binding} for the given {@link #getHostPort () port number }, leaving the {@link #getHostIp () IP address }
146+ * Creates a {@link Binding} for the given {@link #getHostIp () IP address }, leaving the {@link #getHostPort () port number }
152147 * undefined.
153- *
154- * @see Ports#bind(ExposedPort, Binding)
155- * @see ExposedPort
156148 */
157- public Binding ( Integer hostPort ) {
158- this ( null , hostPort );
149+ public static Binding forIp ( String hostIp ) {
150+ return new Binding ( hostIp , null );
159151 }
160152
153+ private final String hostIp ;
154+
155+ private final String hostPort ;
156+
161157 /**
162- * Creates a {@link Binding} for the given {@link #getHostIp() IP address}, leaving the {@link #getHostPort() port number}
163- * undefined.
158+ * Creates a {@link Binding} for the given {@link #getHostIp() IP address} and {@link #getHostPort() port number}.
159+ *
160+ * @see Ports#bind(ExposedPort, Binding)
161+ * @see ExposedPort
164162 */
165- public Binding (String hostIp ) {
166- this (hostIp , null );
163+ public Binding (String hostIp , String hostPort ) {
164+ this .hostIp = isEmpty (hostIp ) ? null : hostIp ;
165+ this .hostPort = hostPort ;
167166 }
168167
169168 /**
@@ -184,7 +183,7 @@ public String getHostIp() {
184183 /**
185184 * @return the port number on the Docker host. May be <code>null</code>, in which case Docker will dynamically assign a port.
186185 */
187- public Integer getHostPort () {
186+ public String getHostPort () {
188187 return hostPort ;
189188 }
190189
@@ -208,10 +207,10 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
208207 String [] parts = serialized .split (":" );
209208 switch (parts .length ) {
210209 case 2 : {
211- return new Binding (parts [0 ], Integer . valueOf ( parts [1 ]) );
210+ return new Binding (parts [0 ], parts [1 ]);
212211 }
213212 case 1 : {
214- return parts [0 ].contains ("." ) ? new Binding (parts [0 ]) : new Binding ( Integer . valueOf (parts [0 ]) );
213+ return parts [0 ].contains ("." ) ? Binding . forIp (parts [0 ]) : Binding . forPort (parts [0 ]);
215214 }
216215 default : {
217216 throw new IllegalArgumentException ();
@@ -231,7 +230,7 @@ public static Binding parse(String serialized) throws IllegalArgumentException {
231230 @ Override
232231 public String toString () {
233232 if (isEmpty (hostIp )) {
234- return Integer . toString ( hostPort ) ;
233+ return hostPort ;
235234 } else if (hostPort == null ) {
236235 return hostIp ;
237236 } else {
@@ -270,7 +269,7 @@ public Ports deserialize(JsonParser jsonParser, DeserializationContext deseriali
270269 JsonNode bindingNode = bindingsArray .get (i );
271270 if (!bindingNode .equals (NullNode .getInstance ())) {
272271 String hostIp = bindingNode .get ("HostIp" ).textValue ();
273- int hostPort = bindingNode .get ("HostPort" ).asInt ();
272+ String hostPort = bindingNode .get ("HostPort" ).textValue ();
274273 out .bind (ExposedPort .parse (portNode .getKey ()), new Binding (hostIp , hostPort ));
275274 }
276275 }
@@ -294,8 +293,7 @@ public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvi
294293 for (Binding binding : entry .getValue ()) {
295294 jsonGen .writeStartObject ();
296295 jsonGen .writeStringField ("HostIp" , binding .getHostIp () == null ? "" : binding .getHostIp ());
297- jsonGen .writeStringField ("HostPort" , binding .getHostPort () == null ? "" : binding .getHostPort ()
298- .toString ());
296+ jsonGen .writeStringField ("HostPort" , binding .getHostPort () == null ? "" : binding .getHostPort ());
299297 jsonGen .writeEndObject ();
300298 }
301299 jsonGen .writeEndArray ();
0 commit comments