NetworkAssociationRestView.java
6.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package fi.codecrew.moya.rest;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import fi.codecrew.moya.beans.NetworkAssociationBeanLocal;
import fi.codecrew.moya.enums.NetworkAssociationStatus;
import fi.codecrew.moya.model.NetworkAssociation;
import fi.codecrew.moya.rest.pojo.NetworkAssociationActionPojo;
import fi.codecrew.moya.rest.pojo.NetworkAssociationInfoPojo;
import fi.codecrew.moya.rest.pojo.NetworkAssociationInfoResponseRoot;
import fi.codecrew.moya.rest.pojo.NetworkAssociationInfolistResponseRoot;
import fi.codecrew.moya.rest.pojo.NetworkAssociationResponseRoot;
@RequestScoped
@Path("/networkassociation")
public class NetworkAssociationRestView {
@EJB
private NetworkAssociationBeanLocal networkAssociationBean;
@POST
@Path("/auth")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationResponseRoot auth(
@FormParam("username") String username,
@FormParam("password") String password,
@FormParam("ip") String ip,
@FormParam("mac") String mac,
@FormParam("code") String code,
@FormParam("coderequired") Boolean codeRequired
) {
NetworkAssociationResponseRoot resp = new NetworkAssociationResponseRoot();
try {
NetworkAssociation na = networkAssociationBean.tryAssociate(
username, password, ip, mac, code, codeRequired);
if(na.getStatus().equals(NetworkAssociationStatus.ACTIVE))
resp.getAdditions().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
else
resp.getPendings().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
} catch(Exception e) {
resp.getResult().setResultCode(0);
if(e.getMessage() != null && e.getMessage() != "") {
resp.getResult().setMessage(e.getMessage());
} else {
resp.getResult().setMessage("UNKNOWN_ERROR");
}
}
return resp;
}
@POST
@Path("/codeauth")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationResponseRoot codeAuth(
@FormParam("usercode") String usercode,
@FormParam("ip") String ip,
@FormParam("mac") String mac,
@FormParam("code") String code,
@FormParam("coderequired") Boolean codeRequired
) {
NetworkAssociationResponseRoot resp = new NetworkAssociationResponseRoot();
try {
NetworkAssociation na = networkAssociationBean.tryAssociate(
usercode, ip, mac, code, codeRequired);
if(na.getStatus().equals(NetworkAssociationStatus.ACTIVE))
resp.getAdditions().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
else
resp.getPendings().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
} catch(Exception e) {
resp.getResult().setResultCode(0);
if(e.getMessage() != null && e.getMessage() != "") {
resp.getResult().setMessage(e.getMessage());
} else {
resp.getResult().setMessage("UNKNOWN_ERROR");
}
}
return resp;
}
@GET
@Path("/get_association_infos")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationInfolistResponseRoot getAssociationInfos() {
NetworkAssociationInfolistResponseRoot nairr = new NetworkAssociationInfolistResponseRoot();
for(NetworkAssociation na : networkAssociationBean.getActiveAssociations(false)) {
nairr.getAssociations().add(new NetworkAssociationInfoPojo(na));
}
return nairr;
}
@GET
@Path("/get_association_info_by_ip/{ipAddress}")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationInfoResponseRoot getAssociationInfos(
@PathParam("ipAddress") String ipAddress
) {
NetworkAssociation na = networkAssociationBean.getActiveAssociationByIP(ipAddress);
NetworkAssociationInfoResponseRoot nairr;
if(na != null) {
nairr = new NetworkAssociationInfoResponseRoot(
new NetworkAssociationInfoPojo(na)
);
} else {
nairr = new NetworkAssociationInfoResponseRoot();
}
return nairr;
}
@GET
@Path("/get_all/{activate}")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationResponseRoot getAll(
@PathParam("activate") Boolean activate
) {
NetworkAssociationResponseRoot resp = new NetworkAssociationResponseRoot();
try {
if(activate == null)
throw new Exception("INVALID_PARAMETER_FOR_ACTIVATE");
List<NetworkAssociation> activeAssociations = networkAssociationBean.getActiveAssociations(activate);
for(NetworkAssociation na : activeAssociations) {
resp.getAdditions().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
}
return resp;
} catch(Exception e) {
resp.getResult().setResultCode(0);
resp.getResult().setMessage(e.getMessage());
return resp;
}
}
@GET
@Path("/get_all/{horizon}/{activate}")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationResponseRoot getAllByHorizon(
@PathParam("activate") Boolean activate,
@PathParam("horizon") String horizon
) {
NetworkAssociationResponseRoot resp = new NetworkAssociationResponseRoot();
try {
if(activate == null)
throw new Exception("INVALID_PARAMETER_FOR_ACTIVATE");
List<NetworkAssociation> activeAssociations = networkAssociationBean.getActiveAssociationsByHorizon(activate, horizon);
for(NetworkAssociation na : activeAssociations) {
resp.getAdditions().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
}
return resp;
} catch(Exception e) {
resp.getResult().setResultCode(0);
resp.getResult().setMessage(e.getMessage());
return resp;
}
}
@POST
@Path("/get_status_by_ip_mac")
@Produces({ MediaType.APPLICATION_JSON })
public NetworkAssociationResponseRoot getChanges(
@FormParam("ip") String ip,
@FormParam("mac") String mac
) {
NetworkAssociationResponseRoot resp = new NetworkAssociationResponseRoot();
try {
for(NetworkAssociation na : networkAssociationBean.getStatusByIPAndMAC(ip, mac)) {
if(na.getStatus().equals(NetworkAssociationStatus.ACTIVE)) {
resp.getAdditions().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
} else if(na.getStatus().equals(NetworkAssociationStatus.PENDING)) {
resp.getPendings().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
} else if(na.getStatus().equals(NetworkAssociationStatus.EXPIRED)) {
resp.getRemovals().add(new NetworkAssociationActionPojo(na.getIP(), na.getMAC()));
}
}
return resp;
} catch(Exception e) {
resp.getResult().setResultCode(0);
resp.getResult().setMessage(e.getMessage());
return resp;
}
}
}