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.io.InputStream;
22  import java.nio.charset.StandardCharsets;
23  import java.util.List;
24  import org.apache.commons.io.IOUtils;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.syncope.common.lib.to.JobTO;
27  import org.apache.syncope.common.lib.to.MailTemplateTO;
28  import org.apache.syncope.common.lib.to.NotificationTO;
29  import org.apache.syncope.common.lib.types.JobAction;
30  import org.apache.syncope.common.lib.types.MailTemplateFormat;
31  import org.apache.syncope.common.rest.api.service.MailTemplateService;
32  import org.apache.syncope.common.rest.api.service.NotificationService;
33  
34  public class NotificationRestClient extends BaseRestClient {
35  
36      private static final long serialVersionUID = 6328933265096511690L;
37  
38      public List<NotificationTO> list() {
39          return getService(NotificationService.class).list();
40      }
41  
42      public JobTO getJob() {
43          return getService(NotificationService.class).getJob();
44      }
45  
46      public void actionJob(final JobAction jobAction) {
47          getService(NotificationService.class).actionJob(jobAction);
48      }
49  
50      public NotificationTO read(final String key) {
51          return getService(NotificationService.class).read(key);
52      }
53  
54      public void create(final NotificationTO notificationTO) {
55          getService(NotificationService.class).create(notificationTO);
56      }
57  
58      public void update(final NotificationTO notificationTO) {
59          getService(NotificationService.class).update(notificationTO);
60      }
61  
62      public void delete(final String key) {
63          getService(NotificationService.class).delete(key);
64      }
65  
66      public List<MailTemplateTO> listTemplates() {
67          return getService(MailTemplateService.class).list();
68      }
69  
70      public void createTemplate(final MailTemplateTO mailTemplateTO) {
71          getService(MailTemplateService.class).create(mailTemplateTO);
72      }
73  
74      public void deleteTemplate(final String key) {
75          getService(MailTemplateService.class).delete(key);
76      }
77  
78      public MailTemplateTO readTemplate(final String key) {
79          return getService(MailTemplateService.class).read(key);
80      }
81  
82      public String readTemplateFormat(final String key, final MailTemplateFormat format) {
83          try {
84              return IOUtils.toString(InputStream.class.cast(
85                      getService(MailTemplateService.class).getFormat(key, format).getEntity()), StandardCharsets.UTF_8);
86          } catch (Exception e) {
87              LOG.error("Error retrieving mail template {} as {}", key, format, e);
88              return StringUtils.EMPTY;
89          }
90      }
91  
92      public void updateTemplateFormat(final String key, final String content, final MailTemplateFormat format) {
93          getService(MailTemplateService.class).setFormat(
94                  key, format, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
95      }
96  }