1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.enduser.rest;
20
21 import java.net.URI;
22 import javax.ws.rs.core.HttpHeaders;
23 import org.apache.cxf.jaxrs.client.WebClient;
24 import org.apache.syncope.client.enduser.SyncopeEnduserSession;
25 import org.apache.syncope.client.lib.SyncopeClient;
26 import org.apache.syncope.client.ui.commons.Constants;
27 import org.apache.syncope.client.ui.commons.rest.RestClient;
28 import org.apache.syncope.common.lib.search.OrderByClauseBuilder;
29 import org.apache.syncope.common.lib.types.ExecStatus;
30 import org.apache.syncope.common.rest.api.RESTHeaders;
31 import org.apache.syncope.common.rest.api.service.JAXRSService;
32 import org.apache.syncope.common.rest.api.service.SyncopeService;
33 import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public abstract class BaseRestClient implements RestClient {
38
39 private static final long serialVersionUID = 1523999867826481989L;
40
41 protected static final Logger LOG = LoggerFactory.getLogger(BaseRestClient.class);
42
43 public SyncopeService getSyncopeService() {
44 return getService(SyncopeService.class);
45 }
46
47 protected <T> T getService(final Class<T> serviceClass) {
48 return SyncopeEnduserSession.get().getService(serviceClass);
49 }
50
51 protected <T> T getService(final String etag, final Class<T> serviceClass) {
52 return SyncopeEnduserSession.get().getService(etag, serviceClass);
53 }
54
55 protected <T> void resetClient(final Class<T> serviceClass) {
56 SyncopeEnduserSession.get().resetClient(serviceClass);
57 }
58
59 public String toOrderBy(final SortParam<String> sort) {
60 OrderByClauseBuilder builder = SyncopeClient.getOrderByClauseBuilder();
61
62 String property = sort.getProperty();
63 if (property.indexOf('#') != -1) {
64 property = property.substring(property.indexOf('#') + 1);
65 }
66
67 if (sort.isAscending()) {
68 builder.asc(property);
69 } else {
70 builder.desc(property);
71 }
72
73 return builder.build();
74 }
75
76 protected <E extends JAXRSService, T> T getObject(
77 final E service, final URI location, final Class<T> resultClass) {
78
79 WebClient webClient = WebClient.fromClient(WebClient.client(service));
80 webClient.accept(SyncopeEnduserSession.get().getMediaType()).to(location.toASCIIString(), false);
81 return webClient.
82 header(RESTHeaders.DOMAIN, SyncopeEnduserSession.get().getDomain()).
83 header(HttpHeaders.AUTHORIZATION, "Bearer " + SyncopeEnduserSession.get().getJWT()).
84 get(resultClass);
85 }
86
87 protected String getStatus(final int httpStatus) {
88 ExecStatus execStatus = ExecStatus.fromHttpStatus(httpStatus);
89 return execStatus == null
90 ? Constants.UNKNOWN
91 : execStatus.name();
92 }
93 }