1 package org.apache.archiva.webapp.ui.services.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( "onclick", "loadRestDocs('" + startPath + "\',\'"+ "rest-docs/" + startPath + "/" + link.attr( "href" ) + "\');" );
69 link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) );
73 Elements codes = body.select( "code" );
75 for ( Iterator<Element> elementIterator = codes.iterator(); elementIterator.hasNext(); )
77 Element code = elementIterator.next();
78 code.attr( "class", code.attr( "class" ) + " nice-code" );
81 //res.appendChild( body.child( 1 ) );
83 Document res = new Document( "" );
84 res.appendChild( body.select( "div[id=main]" ).first() );
86 resp.getOutputStream().write( res.outerHtml().getBytes() );
88 //IOUtils.copy( is, resp.getOutputStream() );
89 //super.doGet( req, resp );