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 org.restlet.ext.wadl;
20  
21  import fr.cnes.doi.utils.spec.Requirement;
22  import java.io.IOException;
23  import java.net.URL;
24  import java.util.logging.Level;
25  import org.restlet.Context;
26  import org.restlet.data.MediaType;
27  import org.restlet.engine.Engine;
28  import org.restlet.ext.xml.TransformRepresentation;
29  import org.restlet.ext.xml.XmlRepresentation;
30  import org.restlet.representation.EmptyRepresentation;
31  import org.restlet.representation.InputRepresentation;
32  import org.restlet.representation.Representation;
33  
34  /**
35   * Generates a WADL based on a patch of the Restlet XSLT.
36   *
37   * @author Jean-Christoph Malapert
38   */
39  @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
40  public class WadlCnesRepresentation extends WadlRepresentation {
41  
42      /**
43       * Constructs a new Wadl representation based on a new XSLT.
44       *
45       * @param application application
46       */
47      public WadlCnesRepresentation(final ApplicationInfo application) {
48          super(application);
49      }
50  
51      /**
52       * Returns the HTML representation of the WADL
53       *
54       * @return HTML representation of the WADL
55       */
56      @Override
57      public Representation getHtmlRepresentation() {
58          Representation representation;
59          final URL wadl2htmlXsltUrl = Engine
60                  .getResource("org/restlet/ext/wadl/wadlcnes2html.xslt");
61  
62          if (wadl2htmlXsltUrl == null) {
63              representation = new EmptyRepresentation();
64          } else {
65              try {
66                  setSaxSource(XmlRepresentation.getSaxSource(this));
67                  final InputRepresentation xslRep = new InputRepresentation(
68                          wadl2htmlXsltUrl.openStream(),
69                          MediaType.APPLICATION_W3C_XSLT);
70                  representation = new TransformRepresentation(
71                          Context.getCurrent(), this, xslRep);
72                  representation.setMediaType(MediaType.TEXT_HTML);
73              } catch (IOException e) {
74                  Context.getCurrentLogger().log(Level.WARNING,
75                          "Unable to generate the WADL HTML representation",
76                          e);
77                  representation = new EmptyRepresentation();
78              }
79          }
80  
81          return representation;
82      }
83  
84  }