View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.rest;
20  
21  import java.net.URI;
22  import java.util.Optional;
23  import javax.ws.rs.core.HttpHeaders;
24  import org.apache.cxf.jaxrs.client.WebClient;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.client.lib.SyncopeClient;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.rest.RestClient;
29  import org.apache.syncope.common.lib.search.OrderByClauseBuilder;
30  import org.apache.syncope.common.lib.types.ExecStatus;
31  import org.apache.syncope.common.rest.api.RESTHeaders;
32  import org.apache.syncope.common.rest.api.service.JAXRSService;
33  import org.apache.syncope.common.rest.api.service.SyncopeService;
34  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  public abstract class BaseRestClient implements RestClient {
39  
40      private static final long serialVersionUID = 1523999867826481989L;
41  
42      protected static final Logger LOG = LoggerFactory.getLogger(BaseRestClient.class);
43  
44      public static String toOrderBy(final SortParam<String> sort) {
45          OrderByClauseBuilder builder = SyncopeClient.getOrderByClauseBuilder();
46  
47          String property = sort.getProperty();
48          if (property.indexOf('#') != -1) {
49              property = property.substring(property.indexOf('#') + 1);
50          }
51  
52          if (sort.isAscending()) {
53              builder.asc(property);
54          } else {
55              builder.desc(property);
56          }
57  
58          return builder.build();
59      }
60  
61      public SyncopeService getSyncopeService() {
62          return getService(SyncopeService.class);
63      }
64  
65      protected <T> T getService(final Class<T> serviceClass) {
66          return SyncopeConsoleSession.get().getService(serviceClass);
67      }
68  
69      protected <T> T getService(final String etag, final Class<T> serviceClass) {
70          return SyncopeConsoleSession.get().getService(etag, serviceClass);
71      }
72  
73      protected <T> void resetClient(final Class<T> serviceClass) {
74          SyncopeConsoleSession.get().resetClient(serviceClass);
75      }
76  
77      protected <E extends JAXRSService, T> T getObject(
78              final E service, final URI location, final Class<T> resultClass) {
79  
80          WebClient webClient = WebClient.fromClient(WebClient.client(service));
81          webClient.accept(SyncopeConsoleSession.get().getMediaType()).to(location.toASCIIString(), false);
82          return webClient.
83                  header(RESTHeaders.DOMAIN, SyncopeConsoleSession.get().getDomain()).
84                  header(HttpHeaders.AUTHORIZATION, "Bearer " + SyncopeConsoleSession.get().getJWT()).
85                  get(resultClass);
86      }
87  
88      protected String getStatus(final int httpStatus) {
89          ExecStatus execStatus = ExecStatus.fromHttpStatus(httpStatus);
90          return Optional.ofNullable(execStatus).map(Enum::name).orElse(Constants.UNKNOWN);
91      }
92  }