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.core.rest.cxf.service;
20  
21  import java.time.OffsetDateTime;
22  import java.time.format.DateTimeFormatter;
23  import java.util.List;
24  import javax.ws.rs.core.Response;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.common.lib.to.AccessTokenTO;
27  import org.apache.syncope.common.lib.to.PagedResult;
28  import org.apache.syncope.common.rest.api.RESTHeaders;
29  import org.apache.syncope.common.rest.api.beans.AccessTokenQuery;
30  import org.apache.syncope.common.rest.api.service.AccessTokenService;
31  import org.apache.syncope.core.logic.AccessTokenLogic;
32  import org.springframework.stereotype.Service;
33  
34  @Service
35  public class AccessTokenServiceImpl extends AbstractService implements AccessTokenService {
36  
37      protected final AccessTokenLogic logic;
38  
39      public AccessTokenServiceImpl(final AccessTokenLogic logic) {
40          this.logic = logic;
41      }
42  
43      @Override
44      public Response login() {
45          Pair<String, OffsetDateTime> login = logic.login();
46          return Response.noContent().
47                  header(RESTHeaders.TOKEN, login.getLeft()).
48                  header(RESTHeaders.TOKEN_EXPIRE, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(login.getRight())).
49                  build();
50      }
51  
52      @Override
53      public Response refresh() {
54          Pair<String, OffsetDateTime> refresh = logic.refresh();
55          return Response.noContent().
56                  header(RESTHeaders.TOKEN, refresh.getLeft()).
57                  header(RESTHeaders.TOKEN_EXPIRE, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(refresh.getRight())).
58                  build();
59      }
60  
61      @Override
62      public void logout() {
63          logic.logout();
64      }
65  
66      @Override
67      public PagedResult<AccessTokenTO> list(final AccessTokenQuery query) {
68          Pair<Integer, List<AccessTokenTO>> result = logic.list(
69                  query.getPage(),
70                  query.getSize(),
71                  getOrderByClauses(query.getOrderBy()));
72          return buildPagedResult(result.getRight(), query.getPage(), query.getSize(), result.getLeft());
73      }
74  
75      @Override
76      public void delete(final String key) {
77          logic.delete(key);
78      }
79  
80  }