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