import java.lang.invoke.MethodHandles;
import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
-import java.util.Vector;
import org.immutables.gson.Gson;
import org.immutables.value.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
-import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@Component("PolicyAgentApi")
}
@Override
- public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
- String url = baseUrl() + "/policy_schemas";
- ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
- if (!rsp.getStatusCode().is2xxSuccessful()) {
- return new ResponseEntity<>(rsp.getStatusCode());
- }
-
- PolicyTypes result = new PolicyTypes();
- JsonParser jsonParser = new JsonParser();
- JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
- for (JsonElement schema : schemas) {
- JsonObject schemaObj = schema.getAsJsonObject();
- String title = schemaObj.get("title").getAsString();
- String schemaAsStr = schemaObj.toString();
- PolicyType pt = new PolicyType(title, schemaAsStr);
- result.add(pt);
+ public ResponseEntity<String> getAllPolicyTypes() {
+ try {
+ String url = baseUrl() + "/policy_schemas";
+ ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
+ if (!rsp.getStatusCode().is2xxSuccessful()) {
+ return rsp;
+ }
+
+ PolicyTypes result = new PolicyTypes();
+ JsonParser jsonParser = new JsonParser();
+
+ JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
+ for (JsonElement schema : schemas) {
+ JsonObject schemaObj = schema.getAsJsonObject();
+ String title = schemaObj.get("title").getAsString();
+ String schemaAsStr = schemaObj.toString();
+ PolicyType pt = new PolicyType(title, schemaAsStr);
+ result.add(pt);
+ }
+ return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
+ } catch (Exception e) {
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
- return new ResponseEntity<>(result, rsp.getStatusCode());
}
@Override
- public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
+ public ResponseEntity<String> getPolicyInstancesForType(String type) {
String url = baseUrl() + "/policies?type={type}";
Map<String, ?> uriVariables = Map.of("type", type);
ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
if (!rsp.getStatusCode().is2xxSuccessful()) {
- return new ResponseEntity<>(rsp.getStatusCode());
+ return rsp;
}
- Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
- List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
-
- PolicyInstances result = new PolicyInstances();
- for (PolicyInfo p : rspParsed) {
- result.add(p);
+ try {
+ Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
+ List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
+ PolicyInstances result = new PolicyInstances();
+ for (PolicyInfo p : rspParsed) {
+ result.add(p);
+ }
+ return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
+ } catch (Exception e) {
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
- return new ResponseEntity<>(result, rsp.getStatusCode());
}
@Override
- public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
+ public ResponseEntity<Object> getPolicyInstance(String id) {
String url = baseUrl() + "/policy?instance={id}";
Map<String, ?> uriVariables = Map.of("id", id);
- return this.restTemplate.getForEntity(url, String.class, uriVariables);
+ return this.restTemplate.getForEntity(url, Object.class, uriVariables);
}
@Override
- public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
- throws RestClientException {
+ public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
+ String ric) {
String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
Map<String, ?> uriVariables = Map.of( //
"type", policyTypeIdString, //
"service", "dashboard");
try {
- this.restTemplate.put(url, json, uriVariables);
+ this.restTemplate.put(url, createJsonHttpEntity(json), uriVariables);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
- return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Override
- public void deletePolicy(String policyInstanceId) throws RestClientException {
+ public ResponseEntity<String> deletePolicy(String policyInstanceId) {
String url = baseUrl() + "/policy?instance={instance}";
Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
- this.restTemplate.delete(url, uriVariables);
+ try {
+ this.restTemplate.delete(url, uriVariables);
+ return new ResponseEntity<>(HttpStatus.OK);
+ } catch (Exception e) {
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
+ }
+
}
@Value.Immutable
@Gson.TypeAdapters
interface RicInfo {
- public String name();
+ public String ricName();
public Collection<String> nodeNames();
}
@Override
- public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
+ public ResponseEntity<String> getRicsSupportingType(String typeName) {
String url = baseUrl() + "/rics?policyType={typeName}";
Map<String, ?> uriVariables = Map.of("typeName", typeName);
String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
- Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
- List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
-
- Collection<String> result = new Vector<>(rspParsed.size());
- for (RicInfo ric : rspParsed) {
- result.add(ric.name());
+ try {
+ Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
+ List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
+ Collection<String> result = new ArrayList<>(rspParsed.size());
+ for (RicInfo ric : rspParsed) {
+ result.add(ric.ricName());
+ }
+ return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
+ } catch (Exception e) {
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
- return new ResponseEntity<>(result, HttpStatus.OK);
+ }
+
+ private HttpEntity<Object> createJsonHttpEntity(Object content) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ return new HttpEntity<>(content, headers);
}
}