/* $Id: Enhancer.java,v 1.7 1999/03/17 19:03:03 remek Exp $ Revision: $Revision: 1.7 $ Updated: $Date: 1999/03/17 19:03:03 $ Updated by: $Author: remek $ Copyright 1999 Northeast Parallel Architectures Center, Syracuse University */ import java.io.*; import java.util.*; import java.net.*; import java.lang.*; import javax.servlet.*; import javax.servlet.http.*; public class Enhancer extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try{ URL targetURL; String temp = req.getParameter("url"); if(temp==null){ reportError("url parameter not specified!", res); return; } try{ targetURL = new URL(req.getParameter("url")); } catch (MalformedURLException e){ reportError("Malformed URL: " + e.toString(), res); return; } HttpURLConnection connectionURL; try{ connectionURL = (HttpURLConnection)targetURL.openConnection(); } catch(IOException e){ reportError(e.toString(), res); return; } String typeURL = connectionURL.getContentType(); if(typeURL==null){ reportError("Could not open the connection to specified URL!", res); return; } res.setContentType(typeURL); BufferedReader input; InputStream tempis = connectionURL.getInputStream(); if(tempis==null){ reportError("Empty input stream!", res); return; } input = new BufferedReader(new InputStreamReader(tempis)); if(typeURL.substring(0, 4).toUpperCase().compareTo("TEXT") != 0){ //unknown type content OutputStreamWriter out; out = new OutputStreamWriter(res.getOutputStream()); int c; while (-1 != ((c = input.read()))) out.write(c); out.close(); } else { //text/html PrintWriter out; out = res.getWriter(); String str = null; String wholeDoc = ""; try{ while (null != ((str = input.readLine()))) wholeDoc = wholeDoc + str + "\n"; } catch(Exception e){ reportError("Server closed connection. URL not found?", res); return; } String newDoc = processHead(wholeDoc, req); wholeDoc = processLinks(newDoc); out.println(wholeDoc); } input.close (); } catch(Exception e){ reportError("unexpected exception " + e.toString(), res); } } public String processHead(String page, HttpServletRequest req) { String result = new String(page); String baseURL = new String(req.getParameter("url")); String trailingSlash = "/"; if(baseURL.substring(baseURL.length()-4).toUpperCase().compareTo("HTML")==0 || baseURL.substring(baseURL.length()-3).toUpperCase().compareTo("HTM")==0) trailingSlash = ""; try{ int headStart = page.indexOf("
"); if(headStart==-1) headStart = page.indexOf(""); if(headStart!=-1) { result = page.substring(0, headStart + 6) + "