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.lang.StringUtils;
22 import org.jsoup.Jsoup;
23 import org.jsoup.nodes.Document;
24 import org.jsoup.nodes.Element;
25 import org.jsoup.select.Elements;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Iterator;
38 * @author Olivier Lamy
41 public class RestDocsServlet
44 private Logger logger = LoggerFactory.getLogger( getClass() );
47 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
48 throws ServletException, IOException
51 logger.debug( "docs request to path: {}", req.getPathInfo() );
53 String path = StringUtils.removeStart( req.getPathInfo(), "/" );
54 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
56 String startPath = StringUtils.substringBefore( path, "/" );
58 // replace all links !!
59 Document document = Jsoup.parse( is, "UTF-8", "" );
61 Element body = document.body().child( 0 );
63 Elements links = body.select( "a[href]" );
65 for ( Iterator<Element> elementIterator = links.iterator(); elementIterator.hasNext(); )
67 Element link = elementIterator.next();
68 link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) );
71 Elements codes = body.select( "code" );
73 for ( Iterator<Element> elementIterator = codes.iterator(); elementIterator.hasNext(); )
75 Element code = elementIterator.next();
76 code.attr( "class", code.attr( "class" ) + " nice-code" );
79 //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5
81 Elements headers = body.select( "h1" );
83 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); )
85 Element header = elementIterator.next();
86 header.tagName( "h3" );
89 headers = body.select( "h2" );
91 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); )
93 Element header = elementIterator.next();
94 header.tagName( "h4" );
97 headers = body.select( "h3" );
99 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); )
101 Element header = elementIterator.next();
102 header.tagName( "h5" );
105 Document res = new Document( "" );
106 res.appendChild( body.select( "div[id=main]" ).first() );
108 resp.getOutputStream().write( res.outerHtml().getBytes() );