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.citation;
20  
21  import fr.cnes.doi.application.AbstractApplication;
22  import fr.cnes.doi.exception.ClientCrossCiteException;
23  import fr.cnes.doi.utils.spec.Requirement;
24  import java.util.List;
25  import org.apache.logging.log4j.Level;
26  import org.restlet.data.MediaType;
27  import org.restlet.data.Method;
28  import org.restlet.data.Status;
29  import org.restlet.ext.wadl.MethodInfo;
30  import org.restlet.resource.Get;
31  import org.restlet.resource.ResourceException;
32  
33  /**
34   * The supported languages for citation.
35   *
36   * @author Jean-christophe Malapert (jean-christophe.malapert@cnes.fr)
37   */
38  public class LanguageCitationResource extends BaseCitationResource {
39  
40      /**
41       * Init.
42       *
43       * @throws ResourceException - if a problem happens
44       */
45      @Override
46      protected void doInit() throws ResourceException {
47          super.doInit();
48          LOG.traceEntry();
49          final StringBuilder description = new StringBuilder();
50          description.append("Selects a Language and Country.");
51          description.append("The language is used to format the citation.");
52          setDescription(description.toString());
53          LOG.traceExit();
54      }
55  
56      /**
57       * Returns the languages as JSON to format the citation.
58       *
59       * @return the languages
60       * @throws ResourceException - Will thrown an Exception when a problem
61       * happens during the request to Cross Cite
62       */
63      @Requirement(reqId = Requirement.DOI_SRV_110, reqName = Requirement.DOI_SRV_110_NAME)
64      @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
65      @Get("json|xml")
66      public List<String> getLanguages() throws ResourceException {
67          LOG.traceEntry();
68          final List<String> result;
69          try {
70              result = this.getApp().getClient().getLanguages();
71          } catch (ClientCrossCiteException ex) {
72              ((AbstractApplication) getApplication()).sendAlertWhenDataCiteFailed(ex);
73              throw LOG.throwing(Level.ERROR, new ResourceException(ex.getStatus(), ex.
74                      getDetailMessage(), ex));
75          }
76          return LOG.traceExit(result);
77      }
78  
79      /**
80       * Describes the Get Method.
81       *
82       * @param info Wadl description
83       */
84      @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
85      @Override
86      protected final void describeGet(final MethodInfo info) {
87          info.setName(Method.GET);
88          info.setDocumentation("Retrieves the supported languages");
89          addResponseDocToMethod(info, createResponseDoc(
90                  Status.SUCCESS_OK, "Operation successful",
91                  listRepresentation("Language representation",
92                          MediaType.TEXT_XML,
93                          "A List of String representing the possible languages"))
94          );
95          addResponseDocToMethod(info, createResponseDoc(
96                  Status.SUCCESS_OK, "Operation successful",
97                  listRepresentation("Language representation",
98                          MediaType.APPLICATION_JSON,
99                          "A JSON array representing the possible languages"))
100         );
101         addResponseDocToMethod(info, createResponseDoc(
102                 Status.SERVER_ERROR_INTERNAL, "server internal error, "
103                 + "try later and if problem persists please contact us")
104         );
105     }
106 }