1 package com.supwisdom.institute.backend.gateway.authn.remote.web.client;
3 import lombok.extern.slf4j.Slf4j;
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;
11 import com.alibaba.fastjson.JSONObject;
15 public class AuthnRemoteRestTemplate {
18 private RestTemplate authnRestTemplate;
20 @Value(value = "${sw-backend-base-api.uri}/v1/authn")
23 private JSONObject defaultErrorJson(Throwable cause) {
24 JSONObject error = new JSONObject();
26 error.put("code", -1);
27 error.put("message", cause.getMessage());
28 error.put("error", cause.getMessage());
33 public JSONObject account(String username) {
36 final String path = "/{username}/account";
37 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
40 return authnRestTemplate.getForObject(url, JSONObject.class);
41 } catch (Exception e) {
44 return defaultErrorJson(e);
48 public JSONObject accountRoles(String username) {
51 final String path = "/{username}/roles";
52 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
55 return authnRestTemplate.getForObject(url, JSONObject.class);
56 } catch (Exception e) {
59 return defaultErrorJson(e);
63 public JSONObject accountApplications(String username, String applicationId) {
66 final String path = "/{username}/applications";
67 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
70 return authnRestTemplate.getForObject(url, JSONObject.class);
71 } catch (Exception e) {
74 return defaultErrorJson(e);
78 public JSONObject accountMenus(String username, String applicationId) {
81 final String path = "/{username}/menus";
82 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
85 return authnRestTemplate.getForObject(url, JSONObject.class);
86 } catch (Exception e) {
89 return defaultErrorJson(e);
93 public JSONObject accountOperations(String username, String applicationId) {
96 final String path = "/{username}/operations";
97 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
100 return authnRestTemplate.getForObject(url, JSONObject.class);
101 } catch (Exception e) {
104 return defaultErrorJson(e);
108 public JSONObject accountResources(String username, String applicationId) {
111 final String path = "/{username}/resources";
112 final String url = this.url + StringUtils.replaceEach(path, new String[] {"{username}"}, new String[] {username});
115 return authnRestTemplate.getForObject(url, JSONObject.class);
116 } catch (Exception e) {
119 return defaultErrorJson(e);
123 public JSONObject resourceRoleSets() {
126 final String path = "/resourceRoleSets";
127 final String url = this.url + path;
130 return authnRestTemplate.getForObject(url, JSONObject.class);
131 } catch (Exception e) {
134 return defaultErrorJson(e);
139 public JSONObject routes() {
142 final String path = "/routes";
143 final String url = this.url + path;
146 return authnRestTemplate.getForObject(url, JSONObject.class);
147 } catch (Exception e) {
150 return defaultErrorJson(e);