This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.web.docs;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.commons.io.IOUtils;
022import org.apache.commons.lang.StringEscapeUtils;
023import org.apache.commons.lang.StringUtils;
024import org.jsoup.Jsoup;
025import org.jsoup.nodes.Document;
026import org.jsoup.nodes.Element;
027import org.jsoup.select.Elements;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import javax.servlet.ServletException;
032import javax.servlet.http.HttpServlet;
033import javax.servlet.http.HttpServletRequest;
034import javax.servlet.http.HttpServletResponse;
035import java.io.IOException;
036import java.io.InputStream;
037
038/**
039 * @author Olivier Lamy
040 * @since 1.4-M4
041 */
042public class RestDocsServlet
043    extends HttpServlet
044{
045    private Logger logger = LoggerFactory.getLogger( getClass() );
046
047    @Override
048    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
049        throws ServletException, IOException
050    {
051
052        logger.debug( "docs request to path: {}", req.getPathInfo() );
053
054        String path = StringUtils.removeStart( req.getPathInfo(), "/" );
055        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
056
057        if ( StringUtils.endsWith( path, ".xsd" ) )
058        {
059            StringEscapeUtils.escapeXml( resp.getWriter(), IOUtils.toString( is ) );
060            //IOUtils.copy( is, resp.getOutputStream() );
061            return;
062        }
063
064        String startPath = StringUtils.substringBefore( path, "/" );
065
066        // replace all links !!
067        Document document = Jsoup.parse( is, "UTF-8", "" );
068
069        Element body = document.body().child( 0 );
070
071        Elements links = body.select( "a[href]" );
072
073        for ( Element link : links ) {
074            link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) );
075        }
076        
077        Elements datalinks = body.select( "[data-href]" );
078
079        for ( Element link : datalinks ) {
080            link.attr( "data-href", "#" + startPath + "/" + link.attr( "data-href" ) );
081        }
082
083        Elements codes = body.select( "code" );
084
085        for ( Element code : codes ) {
086            code.attr( "class", code.attr( "class" ) + " nice-code" );
087        }
088
089        //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5
090
091        Elements headers = body.select( "h1" );
092
093        for ( Element header : headers ) {
094            header.tagName( "h3" );
095        }
096
097        headers = body.select( "h2" );
098
099        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}