View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.services;
20  
21  import fr.cnes.doi.logging.business.JsonMessage;
22  import fr.cnes.doi.settings.Consts;
23  import fr.cnes.doi.settings.DoiSettings;
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.restlet.Application;
29  import org.restlet.Request;
30  import org.restlet.Response;
31  import org.restlet.data.LocalReference;
32  import org.restlet.data.MediaType;
33  import org.restlet.data.Status;
34  import org.restlet.ext.freemarker.TemplateRepresentation;
35  import org.restlet.representation.Representation;
36  import org.restlet.resource.ClientResource;
37  import org.restlet.service.StatusService;
38  
39  /**
40   * Provides a specific error page, which is sent in the HTTP response.
41   *
42   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
43   */
44  public class CnesStatusService extends StatusService {
45  
46      /**
47       * Logger.
48       */
49      private static final Logger LOG = LogManager.getLogger(CnesStatusService.class.getName());
50      /**
51       * Configuration file.
52       */
53      private final DoiSettings settings;
54  
55      /**
56       * Creates a specific error page.
57       */
58      public CnesStatusService() {
59          super();
60          this.settings = DoiSettings.getInstance();
61      }
62  
63      /**
64       * Returns the representation of the status page.
65       *
66       * @param status Status to send
67       * @param request request to server
68       * @param response response from the server
69       * @return the representation of the error page
70       */
71      @Override
72      public Representation getRepresentation(final Status status,
73              final Request request,
74              final Response response) {
75          final Map<String, String> dataModel = createDataModel(response);
76          final Representation mailFtl = new ClientResource(LocalReference.createClapReference(
77                  "class/CnesStatus.ftl")).get();
78          return new TemplateRepresentation(mailFtl, dataModel, MediaType.TEXT_HTML);
79      }
80  
81      /**
82       * Creates a data model. The data model is used to replace values in the
83       * template CnesStatus.ftl.
84       *
85       * @param response Response from server
86       * @return the data model
87       */
88      private Map<String, String> createDataModel(final Response response) {
89          LOG.traceEntry(new JsonMessage(response));
90          final Map<String, String> dataModel = new ConcurrentHashMap<>();
91          dataModel.put("applicationName", Application.getCurrent().getName());
92          dataModel.put("statusCode", String.valueOf(response.getStatus().getCode()));
93          dataModel.put("statusName", response.getStatus().getReasonPhrase());
94          dataModel.put("statusDescription", response.getStatus().getDescription());
95          dataModel.put("logo", "/resources/images/Cnes-logo.png");
96          dataModel.put("contactAdmin", settings.getString(Consts.SERVER_CONTACT_ADMIN, ""));
97          return LOG.traceExit(dataModel);
98      }
99  }