001 package 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 021 import org.apache.commons.io.IOUtils; 022 import org.apache.commons.lang.StringEscapeUtils; 023 import org.apache.commons.lang.StringUtils; 024 import org.jsoup.Jsoup; 025 import org.jsoup.nodes.Document; 026 import org.jsoup.nodes.Element; 027 import org.jsoup.select.Elements; 028 import org.slf4j.Logger; 029 import org.slf4j.LoggerFactory; 030 031 import javax.servlet.ServletException; 032 import javax.servlet.http.HttpServlet; 033 import javax.servlet.http.HttpServletRequest; 034 import javax.servlet.http.HttpServletResponse; 035 import java.io.IOException; 036 import java.io.InputStream; 037 import java.util.Iterator; 038 039 /** 040 * @author Olivier Lamy 041 * @since 1.4-M4 042 */ 043 public class RestDocsServlet 044 extends HttpServlet 045 { 046 private Logger logger = LoggerFactory.getLogger( getClass() ); 047 048 @Override 049 protected void doGet( HttpServletRequest req, HttpServletResponse resp ) 050 throws ServletException, IOException 051 { 052 053 logger.debug( "docs request to path: {}", req.getPathInfo() ); 054 055 String path = StringUtils.removeStart( req.getPathInfo(), "/" ); 056 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( path ); 057 058 if ( StringUtils.endsWith( path, ".xsd" ) ) 059 { 060 StringEscapeUtils.escapeXml( resp.getWriter(), IOUtils.toString( is ) ); 061 //IOUtils.copy( is, resp.getOutputStream() ); 062 return; 063 } 064 065 String startPath = StringUtils.substringBefore( path, "/" ); 066 067 // replace all links !! 068 Document document = Jsoup.parse( is, "UTF-8", "" ); 069 070 Element body = document.body().child( 0 ); 071 072 Elements links = body.select( "a[href]" ); 073 074 for ( Iterator<Element> elementIterator = links.iterator(); elementIterator.hasNext(); ) 075 { 076 Element link = elementIterator.next(); 077 link.attr( "href", "#" + startPath + "/" + link.attr( "href" ) ); 078 } 079 080 Elements codes = body.select( "code" ); 081 082 for ( Iterator<Element> elementIterator = codes.iterator(); elementIterator.hasNext(); ) 083 { 084 Element code = elementIterator.next(); 085 code.attr( "class", code.attr( "class" ) + " nice-code" ); 086 } 087 088 //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5 089 090 Elements headers = body.select( "h1" ); 091 092 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); ) 093 { 094 Element header = elementIterator.next(); 095 header.tagName( "h3" ); 096 } 097 098 headers = body.select( "h2" ); 099 100 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); ) 101 { 102 Element header = elementIterator.next(); 103 header.tagName( "h4" ); 104 } 105 106 headers = body.select( "h3" ); 107 108 for ( Iterator<Element> elementIterator = headers.iterator(); elementIterator.hasNext(); ) 109 { 110 Element header = elementIterator.next(); 111 header.tagName( "h5" ); 112 } 113 114 Document res = new Document( "" ); 115 res.appendChild( body.select( "div[id=main]" ).first() ); 116 117 resp.getOutputStream().write( res.outerHtml().getBytes() ); 118 119 } 120 }