KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > JavadocRedirectServlet


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.doc;
30
31 import com.caucho.server.webapp.WebApp;
32 import com.caucho.server.webapp.WebAppController;
33 import com.caucho.util.L10N;
34 import com.caucho.util.URLUtil;
35
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.http.HttpServlet JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.PrintWriter JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  *
48  * Find a local javadoc and use it, or present option to use javadoc from
49  * Caucho website.
50  */

51 public class JavadocRedirectServlet extends HttpServlet JavaDoc {
52   static protected final Logger JavaDoc log =
53     Logger.getLogger(JavadocRedirectServlet.class.getName());
54   static final L10N L = new L10N(JavadocRedirectServlet.class);
55
56   public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57     throws ServletException JavaDoc, IOException JavaDoc
58   {
59     String JavaDoc query = request.getParameter("query");
60     if (query == null || query.length() == 0)
61        query = request.getPathInfo();
62     if (query == null)
63       query = "";
64
65     query = URLUtil.encodeURL(query);
66
67     // see if the resin-javadoc web-app is available
68
WebApp app = (WebApp) getServletContext();
69
70     WebApp japp = null;
71
72     ArrayList JavaDoc appControllers = app.getParent().getWebAppList();
73
74     for (int i = 0; i < appControllers.size(); i++)
75     {
76       WebAppController appController = (WebAppController) appControllers.get(i);
77
78       String JavaDoc contextPath = appController.getContextPath();
79
80       if (contextPath.startsWith("/resin-javadoc")) {
81         japp = appController.getWebApp();
82         break;
83       }
84     }
85
86     if (japp != null) {
87       String JavaDoc href = japp.getContextPath() + "?query=" + query;
88       if (log.isLoggable(Level.FINER))
89         log.finer(L.l("javadoc redirect to {0}",href));
90       response.sendRedirect(response.encodeRedirectURL(href));
91     }
92     else {
93       if (log.isLoggable(Level.FINER))
94         log.finer(L.l("javadoc no local javadoc"));
95       String JavaDoc href = "http://www.caucho.com/resin-javadoc/?query=" + query;
96
97       PrintWriter JavaDoc out = response.getWriter();
98       out.println("<html>");
99       out.println("<head><title>Resin&#174; Javadoc Not Found</title></head>");
100       out.println("<body>");
101       out.println("<h1 style='background: #ccddff'>Resin&#174; Javadoc Not Found</h1>");
102       out.println("A local copy of the Resin javadoc could not be found.");
103       out.println("<ul>");
104       out.println("<li><a HREF='" + href + "'>use Caucho website</a>");
105       out.println("</ul>");
106
107       out.println("</body>");
108       out.println("</html>");
109       out.close();
110     }
111   }
112
113 }
114
115
Popular Tags