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.application;
20  
21  import fr.cnes.doi.client.ClientCrossCiteCitation;
22  import fr.cnes.doi.resource.citation.FormatCitationResource;
23  import fr.cnes.doi.resource.citation.LanguageCitationResource;
24  import fr.cnes.doi.resource.citation.StyleCitationResource;
25  import fr.cnes.doi.settings.Consts;
26  import fr.cnes.doi.utils.spec.Requirement;
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.apache.logging.log4j.ThreadContext;
30  import org.restlet.Request;
31  import org.restlet.Response;
32  import org.restlet.Restlet;
33  import org.restlet.data.ClientInfo;
34  import org.restlet.routing.Filter;
35  import org.restlet.routing.Router;
36  
37  /**
38   * Provides an application to get citation from a registered DOI. Books and
39   * journal articles have long benefited from an infrastructure that makes them
40   * easy to cite, a key element in the process of research and academic
41   * discourse. In this mind, a data should cited in just the same way. DataCite
42   * DOIs help further research and assures reliable, predictable, and unambiguous
43   * access to research data in order to:
44   * <ul>
45   * <li>support proper attribution and credit</li>
46   * <li>support collaboration and reuse of data</li>
47   * <li>enable reproducibility of findings</li>
48   * <li>foster faster and more efficient research progress, and</li>
49   * <li>provide the means to share data with future researchers</li>
50   * </ul>
51   * DataCite also looks to community practices that provide data citation
52   * guidance. The Joint Declaration of Data Citation Principles is a set of
53   * guiding principles for data within scholarly literature, another dataset, or
54   * any other research object (Data Citation Synthesis Group 2014).
55   * <p>
56   * The FAIR Guiding Principles provide a guideline for the those that want to
57   * enhance reuse of their data (Wilkinson 2016).
58   * <p>
59   * <b>Security</b><br>
60   * --------------<br>
61   * No authentication / no autorisation
62   * <p>
63   * <b>Routing</b><br>
64   * --------------<br>
65   * <br>
66   * root<br>
67   * |<br>
68   * |__ style<br>
69   * |__ language<br>
70   * |__ format<br>
71   *
72   * @see <a href="http://citation.crosscite.org/">Making a citation</a>
73   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
74   */
75  @Requirement(reqId = Requirement.DOI_SRV_100, reqName = Requirement.DOI_SRV_100_NAME)
76  @Requirement(reqId = Requirement.DOI_SRV_110, reqName = Requirement.DOI_SRV_110_NAME)
77  @Requirement(reqId = Requirement.DOI_SRV_120, reqName = Requirement.DOI_SRV_120_NAME)
78  @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
79  public final class DoiCrossCiteApplication extends AbstractApplication {
80  
81      /**
82       * URI {@value #STYLES_URI} to get the styles, which are used to format the
83       * citation.
84       */
85      public static final String STYLES_URI = "/style";
86      /**
87       * URI {@value #LANGUAGE_URI} to get the languages, which are used to format
88       * the citation.
89       */
90      public static final String LANGUAGE_URI = "/language";
91      /**
92       * URI {@value #FORMAT_URI} to retrieves the citation.
93       */
94      public static final String FORMAT_URI = "/format";
95      /**
96       * Application name.
97       */
98      public static final String NAME = "Cross Cite Application";
99  
100     /**
101      * Logger.
102      */
103     private static final Logger LOG = LogManager.getLogger(DoiCrossCiteApplication.class.getName());
104 
105     /**
106      * Client to query CrossCite.
107      */
108     private final ClientCrossCiteCitation client;
109 
110     /**
111      * Constructs the application.
112      */
113     public DoiCrossCiteApplication() {
114         super();
115         setName(NAME);
116         final StringBuilder description = new StringBuilder();
117         description.append("Books and journal articles have long benefited from "
118                 + "an infrastructure that makes them easy to cite, a key element"
119                 + " in the process of research and academic discourse. "
120                 + "We believe that you should cite data in just the same way "
121                 + "that you can cite other sources of information, "
122                 + "such as articles and books.");
123         description.append("DataCite DOIs help further research and assures "
124                 + "reliable, predictable, and unambiguous access to research "
125                 + "data in order to:");
126         description.append("<ul>");
127         description.append("<li>support proper attribution and credit</li>");
128         description.append("<li>support collaboration and reuse of data</li>");
129         description.append("<li>enable reproducibility of findings</li>");
130         description.append("<li>foster faster and more efficient research progress, and</li>");
131         description.append("<li>provide the means to share data with future researchers</li>");
132         description.append("</ul>");
133         description.append("DataCite also looks to community practices that provide"
134                 + " data citation guidance. The Joint Declaration of Data Citation"
135                 + " Principles is a set of guiding principles for data within "
136                 + "scholarly literature, another dataset, or any other research "
137                 + "object (Data Citation Synthesis Group 2014). The FAIR Guiding "
138                 + "Principles provide a guideline for the those that want to "
139                 + "enhance reuse of their data (Wilkinson 2016).");
140         setDescription(description.toString());
141         final String contextMode = this.getConfig().getString(Consts.CONTEXT_MODE);
142         this.client = new ClientCrossCiteCitation(
143                 ClientCrossCiteCitation.Context.valueOf(contextMode)
144         );
145     }
146 
147     /**
148      * Creates router the DOICrossCiteApplication. This routes routes the
149      * following resources:
150      * <ul>
151      * <li>{@link DoiCrossCiteApplication#STYLES_URI} to access to the different
152      * styles for a citation</li>
153      * <li>{@link DoiCrossCiteApplication#LANGUAGE_URI} to access to the
154      * different languages for a citation</li>
155      * <li>{@link DoiCrossCiteApplication#FORMAT_URI} to access to the formatted
156      * citation</li>
157      * </ul>
158      *
159      * @return router
160      */
161     @Override
162     public Restlet createInboundRoot() {
163         LOG.traceEntry();
164 
165         final Filter logContext = new Filter() {
166             /**
167              * Get the IP client from proxy (if there is one) and set it in
168              * the header.
169              * 
170              * @param request request
171              * @param response response
172              * @return 0
173              */            
174             @Override            
175             protected int beforeHandle(final Request request, final Response response) {
176                 final ClientInfo clientInfo = request.getClientInfo();
177                 final String ipAddress = request.getHeaders().getFirstValue(
178                         Consts.PROXIFIED_IP, clientInfo.getUpstreamAddress()
179                 );
180                 ThreadContext.put(Consts.LOG_IP_ADDRESS, ipAddress);
181                 return Filter.CONTINUE;
182             }
183         };          
184         
185         final Router router = new Router(getContext());
186         router.attach(STYLES_URI, StyleCitationResource.class);
187         router.attach(LANGUAGE_URI, LanguageCitationResource.class);
188         router.attach(FORMAT_URI, FormatCitationResource.class);
189         
190         logContext.setNext(router);
191 
192         return LOG.traceExit(logContext);
193     }
194 
195     /**
196      * Returns the client to query cross cite.
197      *
198      * @return the client
199      */
200     public ClientCrossCiteCitation getClient() {
201         return this.client;
202     }
203 
204     /**
205      * Returns the logger.
206      *
207      * @return the logger
208      */
209     @Override
210     public Logger getLog() {
211         return LOG;
212     }
213 
214 }