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 java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.restlet.data.MediaType;
26  import org.restlet.data.Method;
27  import org.restlet.data.Status;
28  import org.restlet.ext.wadl.DocumentationInfo;
29  import org.restlet.ext.wadl.MethodInfo;
30  import org.restlet.ext.wadl.RepresentationInfo;
31  import org.restlet.resource.Get;
32  import org.restlet.resource.ResourceException;
33  
34  import fr.cnes.doi.application.AdminApplication;
35  import fr.cnes.doi.resource.AbstractResource;
36  import fr.cnes.doi.utils.spec.Requirement;
37  
38  /**
39   * Provides a unique identifier to the project. This identifier is used as part
40   * of the DOI name.
41   *
42   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
43   */
44  public class SuffixProjectsDoisResource extends AbstractResource {
45  
46      /**
47       * Logger.
48       */
49      private volatile Logger LOG;
50  
51      /**
52       * Suffix of the project.
53       */
54      private volatile String suffixProject;
55  
56      /**
57       * Set-up method that can be overridden in order to initialize the state of
58       * the resource.
59       *
60       * Init by getting the DOI name in the template URL.
61       *
62       * @throws ResourceException - if a problem happens
63       */
64      @Override
65      protected void doInit() throws ResourceException {
66          super.doInit();
67          final AdminApplication app = (AdminApplication) getApplication();
68          LOG = app.getLog();
69          LOG.traceEntry();
70          setDescription("This resource handles the project suffix in the DOI name");
71          this.suffixProject = getResourcePath().replace(
72                  AdminApplication.ADMIN_URI + AdminApplication.SUFFIX_PROJECT_URI + "/", "");
73          final int startIndex = this.suffixProject.indexOf(AdminApplication.DOIS_URI);
74          this.suffixProject = startIndex == -1 ? null : this.suffixProject.substring(0, startIndex);
75          LOG.debug(this.suffixProject);
76  
77          LOG.traceExit();
78      }
79  
80      //TODO requirement
81      /**
82       * Returns the list of dois from the project suffix sent in url.
83       *
84       * @return the list of dois
85       */
86      @Requirement(reqId = Requirement.DOI_SRV_140, reqName = Requirement.DOI_SRV_140_NAME)
87      @Get
88      public List<String> getProjectsNameAsJson() {
89          LOG.traceEntry();
90  //        checkInput(this.suffixProject);
91  
92          List<String> response = new ArrayList<>();
93  
94          try {
95              response = (((AdminApplication)getApplication()).getDois(suffixProject));
96          } catch (Exception ex) {
97              LOG.error("Error in SuffixProjectsDoisResource while searching for dois in project {}",
98                      this.suffixProject, ex);
99          }
100         return LOG.traceExit(response);
101     }
102 
103     /**
104      * projects representation
105      *
106      * @return Wadl representation for projects
107      */
108     @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
109     private RepresentationInfo projectsRepresentation() {
110         final RepresentationInfo repInfo = new RepresentationInfo();
111         repInfo.setMediaType(MediaType.APPLICATION_XML);
112         final DocumentationInfo docInfo = new DocumentationInfo();
113         docInfo.setTitle("Projects Representation");
114         docInfo.setTextContent("The representation contains informations about all projects.");
115         repInfo.setDocumentation(docInfo);
116         return repInfo;
117     }
118 
119     //TODO describres get method
120     /**
121      * Describes a GET method.
122      *
123      * @param info method information
124      */
125     @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
126     @Override
127     protected void describeGet(final MethodInfo info) {
128         info.setName(Method.GET);
129         info.setDocumentation("Get information about the created projects");
130         addResponseDocToMethod(info, createResponseDoc(
131                 Status.SUCCESS_OK,
132                 "Operation successful",
133                 projectsRepresentation()
134         ));
135     }
136 
137 }