65cf89f5286d9bfba5604e5e27e1c015e8fb445c
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.gateway.authn.remote.web.client;
2
3 import lombok.extern.slf4j.Slf4j;
4
5 import org.apache.commons.lang3.StringUtils;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.stereotype.Component;
9 import org.springframework.web.client.RestTemplate;
10
11 import com.alibaba.fastjson.JSONObject;
12
13 @Slf4j
14 @Component
15 public class AuthnAccountRemoteRestTemplate {
16   
17   @Autowired
18   private RestTemplate authnAccountRestTemplate;
19   
20   @Value(value = "${sw-backend-base-api.uri}/v1/authn")
21   private String url;
22   
23   private JSONObject defaultErrorJson(Throwable cause) {
24     JSONObject error = new JSONObject();
25     
26     error.put("code", -1);
27     error.put("message", cause.getMessage());
28     error.put("error", cause.getMessage());
29     
30     return error;
31   }
32
33   public JSONObject account(String username) {
34     
35     try {
36       final String path = "/{username}/account";
37       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
38       log.debug(url);
39       
40       return authnAccountRestTemplate.getForObject(this.url + path, JSONObject.class);
41     } catch (Exception e) {
42       e.printStackTrace();
43       
44       return defaultErrorJson(e);
45     }
46   }
47
48   public JSONObject roles(String username) {
49
50     try {
51       final String path = "/{username}/roles";
52       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
53       log.debug(url);
54       
55       return authnAccountRestTemplate.getForObject(url, JSONObject.class);
56     } catch (Exception e) {
57       e.printStackTrace();
58       
59       return defaultErrorJson(e);
60     }
61   }
62
63   public JSONObject applications(String username, String applicationId) {
64
65     try {
66       final String path = "/{username}/applications";
67       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
68       log.debug(url);
69       
70       return authnAccountRestTemplate.getForObject(this.url + path, JSONObject.class);
71     } catch (Exception e) {
72       e.printStackTrace();
73       
74       return defaultErrorJson(e);
75     }
76   }
77
78   public JSONObject menus(String username, String applicationId) {
79
80     try {
81       final String path = "/{username}/menus";
82       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
83       log.debug(url);
84       
85       return authnAccountRestTemplate.getForObject(this.url + path, JSONObject.class);
86     } catch (Exception e) {
87       e.printStackTrace();
88       
89       return defaultErrorJson(e);
90     }
91   }
92
93   public JSONObject operations(String username, String applicationId) {
94
95     try {
96       final String path = "/{username}/operations";
97       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
98       log.debug(url);
99       
100       return authnAccountRestTemplate.getForObject(this.url + path, JSONObject.class);
101     } catch (Exception e) {
102       e.printStackTrace();
103       
104       return defaultErrorJson(e);
105     }
106   }
107
108   public JSONObject resources(String username, String applicationId) {
109
110     try {
111       final String path = "/{username}/resources";
112       final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
113       log.debug(url);
114       
115       return authnAccountRestTemplate.getForObject(this.url + path, JSONObject.class);
116     } catch (Exception e) {
117       e.printStackTrace();
118       
119       return defaultErrorJson(e);
120     }
121   }
122
123 }