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.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  }