1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.trendmicro.grid.acl.l0;
18
19 import com.trendmicro.grid.acl.client.AbstractClient;
20 import com.trendmicro.grid.acl.l0.datatypes.NameList;
21 import com.trendmicro.grid.acl.l0.datatypes.UsageStatistics;
22 import com.trendmicro.grid.acl.l0.datatypes.UsageStatisticsCollection;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Service;
25
26 import javax.annotation.PostConstruct;
27 import javax.annotation.Resource;
28 import javax.ws.rs.*;
29 import java.util.Collection;
30 import java.util.LinkedHashSet;
31 import java.util.Set;
32
33
34
35
36
37
38
39 @Service
40 @Path("/internal/server-statistics")
41 @Produces({"application/xml", "application/json"})
42 public class ServerStatisticsRestService implements Level0RestService {
43
44 @Resource
45 InternalCacheControlService cacheControlService;
46 @Resource
47 ServerStatisticsService localStatisticsService;
48 @Autowired(required = false)
49 Collection<AbstractClient> soapClients;
50
51 AbstractClient<ServerStatisticsService> statisticsServiceClient;
52
53 @PostConstruct
54 @SuppressWarnings("unchecked")
55 void selectServiceClient() {
56 if (soapClients != null) {
57 Class<ServerStatisticsService> eif = ServerStatisticsService.class;
58 for (AbstractClient soapClient : soapClients)
59 if (eif.equals(soapClient.getEndpointInterface()))
60 statisticsServiceClient = (AbstractClient<ServerStatisticsService>) soapClient;
61
62 if (statisticsServiceClient == null) {
63 throw new IllegalStateException("No soap client was found that satisfies " +
64 "the endpoint interface " + eif);
65 }
66 }
67 }
68
69 ServerStatisticsService selectStatisticsService(String nodeName) {
70 RestUtil.assertIsNotCrossSiteScriptingVulnerable(nodeName);
71
72 if (nodeName == null || nodeName.isEmpty() || statisticsServiceClient == null ||
73 "localhost".equalsIgnoreCase(nodeName) || nodeName.startsWith("127.0"))
74 return localStatisticsService;
75 else {
76 if (extractNetworkAddresses(
77 cacheControlService.getLocalClusterNodeAddressListInternal()).contains(nodeName))
78 return localStatisticsService;
79
80 Set<String> validNodeNames = extractNetworkAddresses(
81 cacheControlService.getCacheClusterNodeAddressListInternal());
82 if (!validNodeNames.contains(nodeName)) {
83 throw new IllegalArgumentException("Specified a node name of '" + nodeName +
84 "' that is not contained within " + validNodeNames);
85 }
86
87 return statisticsServiceClient.getRemotePort(nodeName);
88 }
89 }
90
91 Set<String> extractNetworkAddresses(Collection<String> sourceAddresses) {
92 Set<String> addresses = new LinkedHashSet<String>(sourceAddresses.size());
93 for (String sourceAddress : sourceAddresses) {
94 int idx = sourceAddress.indexOf('@');
95 if (idx == -1)
96 continue;
97 addresses.add(sourceAddress.substring(idx + 1));
98 }
99 return addresses;
100 }
101
102
103
104
105
106
107 @GET
108 @Path("/nodeNames")
109 public NameList getNodeNames() {
110 Set<String> addresses = extractNetworkAddresses(cacheControlService.getCacheClusterNodeAddressListInternal());
111 return new NameList(addresses);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125 @GET
126 @Path("/overallUsageStatistics/{nodeName}")
127 public UsageStatistics getOverallUsageStatistics(
128 @DefaultValue("localhost") @PathParam("nodeName") String nodeName) {
129 return selectStatisticsService(nodeName).getOverallUsageStatistics();
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 @GET
144 @Path("/currentUsageStatistics/{nodeName}")
145 public UsageStatistics getCurrentUsageStatistics(
146 @DefaultValue("localhost") @PathParam("nodeName") String nodeName) {
147 return selectStatisticsService(nodeName).getCurrentUsageStatistics();
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 @GET
162 @Path("/collectedUsageStatistics/{nodeName}")
163 public UsageStatisticsCollection getCollectedUsageStatistics(
164 @DefaultValue("localhost") @PathParam("nodeName") String nodeName) {
165 return selectStatisticsService(nodeName).getCollectedUsageStatistics();
166 }
167 }