1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.rest;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.apache.cxf.jaxrs.client.WebClient;
32 import org.apache.syncope.client.lib.WebClientBuilder;
33 import org.apache.syncope.common.lib.to.ConnObject;
34 import org.apache.syncope.common.lib.to.PagedConnObjectResult;
35 import org.apache.syncope.common.lib.to.ResourceTO;
36 import org.apache.syncope.common.rest.api.RESTHeaders;
37 import org.apache.syncope.common.rest.api.beans.ConnObjectTOQuery;
38 import org.apache.syncope.common.rest.api.service.ResourceService;
39 import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
40
41
42
43
44 public class ResourceRestClient extends BaseRestClient {
45
46 private static final long serialVersionUID = -6898907679835668987L;
47
48 public boolean check(final String coreAddress, final String domain, final String jwt, final String key)
49 throws IOException {
50
51 WebClient client = WebClientBuilder.build(coreAddress).
52 path("resources").
53 accept(MediaType.APPLICATION_JSON_TYPE).
54 type(MediaType.APPLICATION_JSON_TYPE).
55 header(RESTHeaders.DOMAIN, domain).
56 authorization("Bearer " + jwt);
57 Response response = client.path(key).get();
58 if (response.getStatus() == Response.Status.OK.getStatusCode()) {
59 response = client.back(false).path("check").
60 post(IOUtils.toString((InputStream) response.getEntity(), StandardCharsets.UTF_8));
61 return response.getStatus() == Response.Status.NO_CONTENT.getStatusCode();
62 }
63 return false;
64 }
65
66 public Pair<Boolean, String> check(final ResourceTO resourceTO) {
67 boolean check = false;
68 String errorMessage = null;
69 try {
70 getService(ResourceService.class).check(resourceTO);
71 check = true;
72 } catch (Exception e) {
73 LOG.error("Connector not found {}", resourceTO.getConnector(), e);
74 errorMessage = e.getMessage();
75 }
76
77 return Pair.of(check, errorMessage);
78 }
79
80 public ConnObject readConnObject(final String resource, final String anyTypeKey, final String anyKey) {
81 return getService(ResourceService.class).readConnObject(resource, anyTypeKey, anyKey);
82 }
83
84 public String getConnObjectKeyValue(final String resource, final String anyTypeKey, final String anyKey) {
85 try {
86 Response response = getService(ResourceService.class).getConnObjectKeyValue(resource, anyTypeKey, anyKey);
87 if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
88 return response.getHeaderString(RESTHeaders.CONNOBJECT_KEY);
89 }
90 } catch (Exception e) {
91 LOG.debug("Error fetching connector object key", e);
92 }
93 LOG.error("Unable to determine connector object key value for resource {}, {} and {}",
94 resource, anyTypeKey, anyKey);
95 return null;
96 }
97
98 public Pair<String, List<ConnObject>> searchConnObjects(
99 final String resource,
100 final String anyTypeKey,
101 final ConnObjectTOQuery.Builder queryBuilder,
102 final SortParam<String> sortParam) {
103
104 final List<ConnObject> result = new ArrayList<>();
105 String nextPageResultCookie = null;
106
107 PagedConnObjectResult list;
108 try {
109 if (sortParam != null) {
110 queryBuilder.orderBy(toOrderBy(sortParam));
111 }
112 list = getService(ResourceService.class).searchConnObjects(resource, anyTypeKey, queryBuilder.build());
113 result.addAll(list.getResult());
114 nextPageResultCookie = list.getPagedResultsCookie();
115 } catch (Exception e) {
116 LOG.error("While listing objects on {} for any type {}", resource, anyTypeKey, e);
117 }
118
119 return Pair.of(nextPageResultCookie, result);
120 }
121
122 public ResourceTO read(final String name) {
123 return getService(ResourceService.class).read(name);
124 }
125
126 public List<ResourceTO> list() {
127 List<ResourceTO> resources = List.of();
128 try {
129 resources = getService(ResourceService.class).list();
130 resources.sort(Comparator.comparing(ResourceTO::getKey));
131 } catch (Exception e) {
132 LOG.error("Could not fetch the Resource list", e);
133 }
134
135 return resources;
136 }
137
138 public ResourceTO create(final ResourceTO resourceTO) {
139 ResourceService service = getService(ResourceService.class);
140 Response response = service.create(resourceTO);
141 return getObject(service, response.getLocation(), ResourceTO.class);
142 }
143
144 public void update(final ResourceTO resourceTO) {
145 getService(ResourceService.class).update(resourceTO);
146 }
147
148 public void delete(final String name) {
149 getService(ResourceService.class).delete(name);
150 }
151
152 public void setLatestSyncToken(final String key, final String anyType) {
153 getService(ResourceService.class).setLatestSyncToken(key, anyType);
154 }
155
156 public void removeSyncToken(final String key, final String anyType) {
157 getService(ResourceService.class).removeSyncToken(key, anyType);
158 }
159 }