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.Arrays;
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.ext.wadl.ParameterStyle;
31  import org.restlet.resource.Get;
32  import org.restlet.resource.ResourceException;
33  
34  /**
35   * Formats a citation. CrossRef, DataCite and mEDRA support formatted citations
36   * via the text/bibliography content type. These are the output of the Citation
37   * Style Language processor, citeproc-js. The content type can take two
38   * additional parameters to customise its response format.
39   * <p>
40   * "\"style\" can be chosen from the list of style names found in the CSL style
41   * repository. Many styles are supported, including common styles such as apa
42   * and harvard3
43   *
44   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
45   */
46  public class FormatCitationResource extends BaseCitationResource {
47  
48      /**
49       * The digital object identifier to format.
50       */
51      private volatile String doiName;
52  
53      /**
54       * The style of formatting.
55       */
56      private volatile String style;
57  
58      /**
59       * The language to format.
60       */
61      private volatile String language;
62  
63      /**
64       * Init by getting doi, lang and style values.
65       *
66       * @throws ResourceException - if a problem happens
67       */
68      @Override
69      protected void doInit() throws ResourceException {
70          super.doInit();
71          LOG.traceEntry();
72          this.doiName = getQueryValue(DOI_PARAMETER);
73          this.language = getQueryValue(LANG_PARAMETER);
74          this.style = getQueryValue(STYLE_PARAMETER);
75  
76          LOG.debug("DOI Parameter : " + this.doiName);
77          LOG.debug("LANGUAGE Parameter : " + this.language);
78          LOG.debug("STYLE Parameter : " + this.language);
79  
80          final StringBuilder description = new StringBuilder();
81          description.append("CrossRef, DataCite and mEDRA support formatted "
82                  + "citations via the text/bibliography content type. These are "
83                  + "the output of the Citation Style Language processor, "
84                  + "citeproc-js. The content type can take two additional "
85                  + "parameters to customise its response format.");
86          description.append("\"style\" can be chosen from the list of style names"
87                  + " found in the CSL style repository. Many styles are supported,"
88                  + " including common styles such as apa and harvard3");
89          setDescription(description.toString());
90          LOG.traceExit();
91      }
92  
93      /**
94       * Returns the formatted citation.
95       *
96       * @return the formatted citation
97       * @throws ResourceException - if a problem happens when requesting Cross
98       * Cite or if {@link #DOI_PARAMETER} and {@link #LANG_PARAMETER} and
99       * {@link #STYLE_PARAMETER} are not set
100      */
101     @Requirement(reqId = Requirement.DOI_SRV_120, reqName = Requirement.DOI_SRV_120_NAME)
102     @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
103     @Get
104     public String getFormat() throws ResourceException {
105         LOG.traceEntry();
106         try {
107             checkInputs();
108             final String result = this.getApp().getClient().getFormat(
109                     this.doiName, this.style, this.language
110             );
111             return LOG.traceExit(result);
112         } catch (ClientCrossCiteException ex) {
113             ((AbstractApplication) getApplication()).sendAlertWhenDataCiteFailed(ex);
114             throw LOG.throwing(Level.ERROR, new ResourceException(ex.getStatus(), ex.
115                     getDetailMessage(), ex));
116         }
117     }
118 
119     /**
120      * Checks input parameters
121      *
122      * @throws ResourceException - if {@link #DOI_PARAMETER} and
123      * {@link #LANG_PARAMETER} and {@link #STYLE_PARAMETER} are not set
124      */
125     private void checkInputs() throws ResourceException {
126         LOG.traceEntry();
127         final StringBuilder errorMsg = new StringBuilder();
128         if (this.doiName == null || this.doiName.isEmpty()) {
129             errorMsg.append(DOI_PARAMETER).append(" value is not set.");
130         }
131         if (this.language == null || this.language.isEmpty()) {
132             errorMsg.append(LANG_PARAMETER).append(" value is not set.");
133         }
134         if (this.style == null || this.style.isEmpty()) {
135             errorMsg.append(STYLE_PARAMETER).append(" value is not set.");
136         }
137         if (errorMsg.length() == 0) {
138             LOG.debug("The parameters are valid");
139         } else {
140             throw LOG.throwing(Level.ERROR, new ResourceException(
141                     Status.CLIENT_ERROR_BAD_REQUEST, errorMsg.toString()));
142         }
143         LOG.traceExit();
144     }
145 
146     /**
147      * Describes the Get Method.
148      *
149      * @param info Wadl description
150      */
151     @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
152     @Override
153     protected final void describeGet(final MethodInfo info) {
154         info.setName(Method.GET);
155         info.setDocumentation("Select Formatting Style");
156         addRequestDocToMethod(info, Arrays.asList(
157                 createQueryParamDoc(DOI_PARAMETER, ParameterStyle.QUERY,
158                         "doi to format for the citation", true, "xs:string"),
159                 createQueryParamDoc(LANG_PARAMETER, ParameterStyle.QUERY,
160                         "language for the citation formating", true, "xs:string"),
161                 createQueryParamDoc(STYLE_PARAMETER, ParameterStyle.QUERY,
162                         "style fot the citation formating", true, "xs:string")
163         ));
164         addResponseDocToMethod(info, createResponseDoc(
165                 Status.SUCCESS_OK,
166                 "Operation successful",
167                 listRepresentation("Format representation", MediaType.TEXT_PLAIN,
168                         "The formatted citation"))
169         );
170         addResponseDocToMethod(info, createResponseDoc(
171                 Status.CLIENT_ERROR_NOT_FOUND,
172                 "DOI not found",
173                 listRepresentation("Error representation",
174                         MediaType.TEXT_HTML, "Error"))
175         );
176         addResponseDocToMethod(info, createResponseDoc(
177                 Status.CLIENT_ERROR_BAD_REQUEST,
178                 "Wrong input parameters",
179                 listRepresentation("Error representation",
180                         MediaType.TEXT_HTML, "Error"))
181         );
182     }
183 
184 }