This project has retired. For details please refer to its
Attic page.
RestDocsServlet xref
1 package org.apache.archiva.web.docs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
40
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
61 return;
62 }
63
64 String startPath = StringUtils.substringBefore( path, "/" );
65
66
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
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 }