forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPorts.java
More file actions
141 lines (110 loc) · 4.47 KB
/
Copy pathPorts.java
File metadata and controls
141 lines (110 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.github.dockerjava.api.model;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.builder.EqualsBuilder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.NullNode;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonDeserialize(using = Ports.Deserializer.class)
@JsonSerialize(using = Ports.Serializer.class)
public class Ports {
private final Map<ExposedPort, Binding> ports = new HashMap<ExposedPort, Binding>();
public Ports() { }
public Ports(ExposedPort exposedPort, Binding host) {
bind(exposedPort, host);
}
public void bind(ExposedPort exposedPort, Binding host) {
ports.put(exposedPort, host);
}
@Override
public String toString(){
return ports.toString();
}
public Map<ExposedPort, Binding> getBindings(){
return ports;
}
public static Binding Binding(String hostIp, int hostPort) {
return new Binding(hostIp, hostPort);
}
public static Binding Binding(int hostPort) {
return new Binding(hostPort);
}
public static class Binding {
private final String hostIp;
private final int hostPort;
public Binding(String hostIp, int hostPort) {
this.hostIp = hostIp;
this.hostPort = hostPort;
}
public Binding(int hostPort) {
this("", hostPort);
}
public String getHostIp() {
return hostIp;
}
public int getHostPort() {
return hostPort;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Binding) {
Binding other = (Binding) obj;
return new EqualsBuilder()
.append(hostIp, other.getHostIp())
.append(hostPort, other.getHostPort()).isEquals();
} else
return super.equals(obj);
}
}
public static class Deserializer extends JsonDeserializer<Ports> {
@Override
public Ports deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
Ports out = new Ports();
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) {
Map.Entry<String, JsonNode> field = it.next();
if (!field.getValue().equals(NullNode.getInstance())) {
String hostIp = field.getValue().get(0).get("HostIp").textValue();
int hostPort = field.getValue().get(0).get("HostPort").asInt();
out.bind(ExposedPort.parse(field.getKey()), new Binding(hostIp, hostPort));
}
}
return out;
}
}
public static class Serializer extends JsonSerializer<Ports> {
@Override
public void serialize(Ports portBindings, JsonGenerator jsonGen,
SerializerProvider serProvider) throws IOException, JsonProcessingException {
jsonGen.writeStartObject();
for(Entry<ExposedPort, Binding> entry : portBindings.getBindings().entrySet()){
jsonGen.writeFieldName(entry.getKey().getPort() + "/" + entry.getKey().getScheme());
jsonGen.writeStartArray();
jsonGen.writeStartObject();
jsonGen.writeStringField("HostIp", entry.getValue().getHostIp());
jsonGen.writeStringField("HostPort", "" + entry.getValue().getHostPort());
jsonGen.writeEndObject();
jsonGen.writeEndArray();
}
jsonGen.writeEndObject();
}
}
}