This project has retired. For details please refer to its Attic page.
RestDocsServlet xref
View Javadoc
1   package org.apache.archiva.web.docs;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.commons.io.IOUtils;
22  import org.apache.commons.lang.StringEscapeUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.jsoup.Jsoup;
25  import org.jsoup.nodes.Document;
26  import org.jsoup.nodes.Element;
27  import org.jsoup.select.Elements;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.IOException;
36  import java.io.InputStream;
37  
38  /**
39   * @author Olivier Lamy
40   * @since 1.4-M4
41   */
42  public class RestDocsServlet
43      extends HttpServlet
44  {
45      private Logger logger = LoggerFactory.getLogger( getClass() );
46  
47      @Override
48      protected void doGet( HttpServletRequest req, HttpServletResponse resp )
49          throws ServletException, IOException
50      {
51  
52          logger.debug( "docs request to path: {}", req.getPathInfo() );
53  
54          String path = StringUtils.removeStart( req.getPathInfo(), "/" );
55          InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
56  
57          if ( StringUtils.endsWith( path, ".xsd" ) )
58          {
59              StringEscapeUtils.escapeXml( resp.getWriter(), IOUtils.toString( is ) );
60              //IOUtils.copy( is, resp.getOutputStream() );
61              return;
62          }
63  
64          String startPath = StringUtils.substringBefore( path, "/" );
65  
66          // replace all links !!
67          Document document = Jsoup.parse( is, "UTF-8", "" );
68  
69          Element body = document.body().child( 0 );
70  
71          Elements links = body.select( "a[href]" );
72  
73          for ( Element link : links ) {
74              link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) );
75          }
76          
77          Elements datalinks = body.select( "[data-href]" );
78  
79          for ( Element link : datalinks ) {
80              link.attr( "data-href", "#" + startPath + "/" + link.attr( "data-href" ) );
81          }
82  
83          Elements codes = body.select( "code" );
84  
85          for ( Element code : codes ) {
86              code.attr( "class", code.attr( "class" ) + " nice-code" );
87          }
88  
89          //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5
90  
91          Elements headers = body.select( "h1" );
92  
93          for ( Element header : headers ) {
94              header.tagName( "h3" );
95          }
96  
97          headers = body.select( "h2" );
98  
99          for ( Element header : headers ) {
100             header.tagName( "h4" );
101         }
102 
103         headers = body.select( "h3" );
104 
105         for ( Element header : headers ) {
106             header.tagName( "h5" );
107         }
108 
109         Document res = new Document( "" );
110         res.appendChild( body.select( "div[id=main]" ).first() );
111         
112         Elements scripts = body.select( "script" );
113         for ( Element script : scripts )
114         {
115              res.appendChild( script );
116         } 
117         resp.getOutputStream().write( res.outerHtml().getBytes() );
118 
119     }
120 }