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.resource.admin;
20  
21  import fr.cnes.doi.application.AdminApplication;
22  import fr.cnes.doi.resource.AbstractResource;
23  import fr.cnes.doi.settings.Consts;
24  import fr.cnes.doi.settings.DoiSettings;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  import org.apache.logging.log4j.Logger;
28  import org.restlet.data.LocalReference;
29  import org.restlet.data.MediaType;
30  import org.restlet.data.Method;
31  import org.restlet.data.Status;
32  import org.restlet.ext.freemarker.TemplateRepresentation;
33  import org.restlet.ext.wadl.MethodInfo;
34  import org.restlet.representation.Representation;
35  import org.restlet.resource.ClientResource;
36  import org.restlet.resource.Get;
37  import org.restlet.resource.ResourceException;
38  
39  /**
40   * Retrieves configuration file.
41   *
42   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
43   */
44  public class ConfigIhmResource extends AbstractResource {
45  
46      /**
47       * Logger.
48       */
49      private volatile Logger LOG;
50  
51      /**
52       * {@inheritDoc }
53       */
54      @Override
55      protected void doInit() throws ResourceException {
56          super.doInit();
57          final AdminApplication app = (AdminApplication) getApplication();
58          LOG = app.getLog();
59          LOG.traceEntry();
60          setDescription("This resources handles GUI configuration file.");
61          LOG.traceExit();
62      }
63  
64      /**
65       * Creates a data model. The data model is used to replace values in the
66       * template ihm_config.ftl.
67       *
68       * @return the data model
69       */
70      private Map<String, String> createDataModel() {
71          LOG.traceEntry();
72          final Map<String, String> dataModel = new ConcurrentHashMap<>();
73          dataModel.put("doi_prefix", DoiSettings.getInstance().getString(Consts.INIST_DOI));
74          return LOG.traceExit(dataModel);
75      }
76  
77      /**
78       * Returns the configuration file.
79       *
80       * @return the configuration file
81       */
82      @Get
83      public Representation getConfig() {
84          LOG.traceEntry();
85          final Map<String, String> dataModel = createDataModel();
86          final Representation configFtl = new ClientResource(LocalReference.createClapReference(
87                  "class/ihm_config.ftl")).get();
88          return LOG.traceExit(new TemplateRepresentation(configFtl, dataModel,
89                  MediaType.APPLICATION_JAVASCRIPT));
90      }
91  
92      /**
93       * {@inheritDoc }
94       */
95      @Override
96      protected void describeGet(final MethodInfo info) {
97          info.setName(Method.GET);
98          info.setDocumentation("Returns the GUI configuration");
99          addResponseDocToMethod(info, createResponseDoc(
100                 Status.SUCCESS_OK, "Operation successful",
101                 stringRepresentation())
102         );
103     }
104 
105 }