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.io.IOException;
22  import java.io.InputStream;
23  import java.net.URI;
24  import java.nio.charset.StandardCharsets;
25  import java.util.List;
26  import javax.ws.rs.InternalServerErrorException;
27  import javax.ws.rs.core.MediaType;
28  import javax.ws.rs.core.Response;
29  import javax.ws.rs.core.StreamingOutput;
30  import org.apache.commons.io.IOUtils;
31  import org.apache.syncope.common.lib.to.RoleTO;
32  import org.apache.syncope.common.rest.api.RESTHeaders;
33  import org.apache.syncope.common.rest.api.service.RoleService;
34  import org.apache.syncope.core.logic.RoleLogic;
35  import org.springframework.stereotype.Service;
36  
37  @Service
38  public class RoleServiceImpl extends AbstractService implements RoleService {
39  
40      protected final RoleLogic logic;
41  
42      public RoleServiceImpl(final RoleLogic logic) {
43          this.logic = logic;
44      }
45  
46      @Override
47      public List<RoleTO> list() {
48          return logic.list();
49      }
50  
51      @Override
52      public RoleTO read(final String key) {
53          return logic.read(key);
54      }
55  
56      @Override
57      public Response create(final RoleTO roleTO) {
58          RoleTO created = logic.create(roleTO);
59          URI location = uriInfo.getAbsolutePathBuilder().path(created.getKey()).build();
60          return Response.created(location).
61                  header(RESTHeaders.RESOURCE_KEY, created.getKey()).
62                  build();
63      }
64  
65      @Override
66      public void update(final RoleTO roleTO) {
67          logic.update(roleTO);
68      }
69  
70      @Override
71      public void delete(final String key) {
72          logic.delete(key);
73      }
74  
75      @Override
76      public Response getAnyLayout(final String key) {
77          String template = logic.getAnyLayout(key);
78          StreamingOutput sout = (os) -> os.write(template.getBytes());
79  
80          return Response.ok(sout).
81                  type(MediaType.APPLICATION_JSON_TYPE).
82                  build();
83      }
84  
85      @Override
86      public void setAnyLayout(final String key, final InputStream anyLayoutIn) {
87          try {
88              logic.setAnyLayout(key, IOUtils.toString(anyLayoutIn, StandardCharsets.UTF_8.name()));
89          } catch (final IOException e) {
90              LOG.error("While setting console layout info for role {}", key, e);
91              throw new InternalServerErrorException("Could not read entity", e);
92          }
93      }
94  
95      @Override
96      public void removeAnyLayout(final String key) {
97          logic.setAnyLayout(key, null);
98      }
99  }