1 package org.apache.archiva.web.docs;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
39 * @author Olivier Lamy
42 public class RestDocsServlet
45 private Logger logger = LoggerFactory.getLogger( getClass() );
48 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
49 throws ServletException, IOException
52 logger.debug( "docs request to path: {}", req.getPathInfo() );
54 String path = StringUtils.removeStart( req.getPathInfo(), "/" );
55 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
57 if ( StringUtils.endsWith( path, ".xsd" ) )
59 StringEscapeUtils.escapeXml( resp.getWriter(), IOUtils.toString( is ) );
60 //IOUtils.copy( is, resp.getOutputStream() );
64 String startPath = StringUtils.substringBefore( path, "/" );
66 // replace all links !!
67 Document document = Jsoup.parse( is, "UTF-8", "" );
69 Element body = document.body().child( 0 );
71 Elements links = body.select( "a[href]" );
73 for ( Element link : links ) {
74 link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) );
77 Elements codes = body.select( "code" );
79 for ( Element code : codes ) {
80 code.attr( "class", code.attr( "class" ) + " nice-code" );
83 //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5
85 Elements headers = body.select( "h1" );
87 for ( Element header : headers ) {
88 header.tagName( "h3" );
91 headers = body.select( "h2" );
93 for ( Element header : headers ) {
94 header.tagName( "h4" );
97 headers = body.select( "h3" );
99 for ( Element header : headers ) {
100 header.tagName( "h5" );
103 Document res = new Document( "" );
104 res.appendChild( body.select( "div[id=main]" ).first() );
106 resp.getOutputStream().write( res.outerHtml().getBytes() );