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.Response;
28  import javax.ws.rs.core.StreamingOutput;
29  import org.apache.commons.io.IOUtils;
30  import org.apache.syncope.common.lib.to.MailTemplateTO;
31  import org.apache.syncope.common.lib.types.MailTemplateFormat;
32  import org.apache.syncope.common.rest.api.RESTHeaders;
33  import org.apache.syncope.common.rest.api.service.MailTemplateService;
34  import org.apache.syncope.core.logic.MailTemplateLogic;
35  import org.springframework.stereotype.Service;
36  
37  @Service
38  public class MailTemplateServiceImpl extends AbstractService implements MailTemplateService {
39  
40      protected final MailTemplateLogic logic;
41  
42      public MailTemplateServiceImpl(final MailTemplateLogic logic) {
43          this.logic = logic;
44      }
45  
46      @Override
47      public Response create(final MailTemplateTO mailTemplateTO) {
48          MailTemplateTO created = logic.create(mailTemplateTO.getKey());
49          URI location = uriInfo.getAbsolutePathBuilder().path(created.getKey()).build();
50          return Response.created(location).
51                  header(RESTHeaders.RESOURCE_KEY, created.getKey()).
52                  build();
53      }
54  
55      @Override
56      public MailTemplateTO read(final String key) {
57          return logic.read(key);
58      }
59  
60      @Override
61      public List<MailTemplateTO> list() {
62          return logic.list();
63      }
64  
65      @Override
66      public void delete(final String key) {
67          logic.delete(key);
68      }
69  
70      @Override
71      public Response getFormat(final String key, final MailTemplateFormat format) {
72          String template = logic.getFormat(key, format);
73          StreamingOutput sout = (os) -> os.write(template.getBytes());
74  
75          return Response.ok(sout).
76                  type(format.getMediaType()).
77                  build();
78      }
79  
80      @Override
81      public void setFormat(final String key, final MailTemplateFormat format, final InputStream templateIn) {
82          try {
83              logic.setFormat(key, format, IOUtils.toString(templateIn, StandardCharsets.UTF_8));
84          } catch (final IOException e) {
85              LOG.error("While setting format {} for mail template {}", format, key, e);
86              throw new InternalServerErrorException("Could not read entity", e);
87          }
88      }
89  
90      @Override
91      public void removeFormat(final String key, final MailTemplateFormat format) {
92          logic.setFormat(key, format, null);
93      }
94  }