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 styles for citation.
35   *
36   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
37   */
38  public class StyleCitationResource 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 style");
51          description.append("A \"style\" can be chosen from the list of style "
52                  + "names found in the CSL style repository.");
53          description.append("Many styles are supported, including common styles "
54                  + "such as apa and harvard3.");
55          setDescription(description.toString());
56          LOG.traceExit();
57      }
58  
59      /**
60       * Returns the styles as JSON array for a citation.
61       *
62       * @return the possibles styles for a citation
63       * @throws ResourceException - Will thrown an Exception when a problem
64       * happens during the request to Cross Cite
65       */
66      @Requirement(reqId = Requirement.DOI_SRV_100, reqName = Requirement.DOI_SRV_100_NAME)
67      @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
68      @Get("json|xml")
69      public List<String> getStyles() throws ResourceException {
70          LOG.traceEntry();
71          final List<String> result;
72          try {
73              result = this.getApp().getClient().getStyles();
74          } catch (ClientCrossCiteException ex) {
75              ((AbstractApplication) getApplication()).sendAlertWhenDataCiteFailed(ex);
76              throw LOG.throwing(Level.ERROR, new ResourceException(ex.getStatus(), ex.
77                      getDetailMessage(), ex));
78          }
79          return LOG.traceExit(result);
80      }
81  
82      /**
83       * Describes the Get Method.
84       *
85       * @param info Wadl description
86       */
87      @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
88      @Override
89      protected final void describeGet(final MethodInfo info) {
90          info.setName(Method.GET);
91          info.setDocumentation("Retrieves the list of supported styles");
92          addResponseDocToMethod(info, createResponseDoc(
93                  Status.SUCCESS_OK, "Operation successful",
94                  listRepresentation("Style representation", MediaType.TEXT_XML,
95                          "A List of String representing the possible styles"))
96          );
97          addResponseDocToMethod(info, createResponseDoc(
98                  Status.SUCCESS_OK, "Operation successful",
99                  listRepresentation("Style representation",
100                         MediaType.APPLICATION_JSON,
101                         "A JSON array representing the possible styles"))
102         );
103         addResponseDocToMethod(info, createResponseDoc(
104                 Status.SERVER_ERROR_INTERNAL, "server internal error, try later "
105                 + "and if problem persists please contact us")
106         );
107     }
108 }