17a3430d68e06a52c005ddbfdeb061ed92cba18d
[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 AuthnRemoteRestTemplate {
16   
17   @Autowired
18   private RestTemplate authnRestTemplate;
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 authnRestTemplate.getForObject(url, JSONObject.class);
41     } catch (Exception e) {
42       e.printStackTrace();
43       
44       return defaultErrorJson(e);
45     }
46   }
47
48   public JSONObject accountRoles(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 authnRestTemplate.getForObject(url, JSONObject.class);
56     } catch (Exception e) {
57       e.printStackTrace();
58       
59       return defaultErrorJson(e);
60     }
61   }
62
63   public JSONObject accountApplications(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 authnRestTemplate.getForObject(url, JSONObject.class);
71     } catch (Exception e) {
72       e.printStackTrace();
73       
74       return defaultErrorJson(e);
75     }
76   }
77
78   public JSONObject accountMenus(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 authnRestTemplate.getForObject(url, JSONObject.class);
86     } catch (Exception e) {
87       e.printStackTrace();
88       
89       return defaultErrorJson(e);
90     }
91   }
92
93   public JSONObject accountOperations(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 authnRestTemplate.getForObject(url, JSONObject.class);
101     } catch (Exception e) {
102       e.printStackTrace();
103       
104       return defaultErrorJson(e);
105     }
106   }
107
108   public JSONObject accountResources(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 authnRestTemplate.getForObject(url, JSONObject.class);
116     } catch (Exception e) {
117       e.printStackTrace();
118       
119       return defaultErrorJson(e);
120     }
121   }
122
123   public JSONObject resourceRoleSets() {
124
125     try {
126       final String path = "/resourceRoleSets";
127       final String url = this.url + path;
128       log.debug(url);
129       
130       return authnRestTemplate.getForObject(url, JSONObject.class);
131     } catch (Exception e) {
132       e.printStackTrace();
133       
134       return defaultErrorJson(e);
135     }
136   }
137
138
139   public JSONObject routes() {
140
141     try {
142       final String path = "/routes";
143       final String url = this.url + path;
144       log.debug(url);
145       
146       return authnRestTemplate.getForObject(url, JSONObject.class);
147     } catch (Exception e) {
148       e.printStackTrace();
149       
150       return defaultErrorJson(e);
151     }
152   }
153
154 }