KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > servjumpers


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.servlet;
11
12 import java.io.IOException JavaDoc;
13
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.*;
16
17 import org.mmbase.module.builders.Jumpers;
18 import org.mmbase.util.logging.*;
19
20 /**
21  * Servjumpers filters all url's to see if it has a jumper (specified in the
22  * jumpers builder).
23  * If a jumper is found, it will redirect the jumper to the designation url.
24  *
25  * @application SCAN (depends from JamesServlet)
26  * @deprecated use JumpersFilter
27  * @author Daniel Ockeloen
28  * @version $Id: servjumpers.java,v 1.22 2004/10/01 08:41:49 pierre Exp $
29  * @see JumpersFilter
30  */

31 public class servjumpers extends JamesServlet {
32     private static final Logger log = Logging.getLoggerInstance(servjumpers.class);
33
34     public void init() throws ServletException JavaDoc {
35         super.init();
36         // Initializing log here because log4j has to be initialized first.
37
log.info("Init of servlet " + getServletConfig().getServletName() + ".");
38     }
39
40     /**
41      * Service call for filtering.
42      * Will be called by the server when a request is done
43      * by a user.
44      * Determines an url based on a jumper key, provided the original
45      * url (reclaimed from the request) is NOT a directory root and NOT a fully
46      * specified file.<br />
47      * So '/mypath/' or '/myfile.html' will not be filtered, but '/mypath' will.
48      * @param req The HTTP Servlet request
49      * @param res The HTTP Servlet response
50      */

51     public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException JavaDoc,IOException JavaDoc {
52         incRefCount(req); // increase reference count
53
try {
54             String JavaDoc url = null;
55             String JavaDoc tmpr = req.getRequestURI().substring(1);
56             if (tmpr.indexOf('.') == -1 && (!tmpr.endsWith("/"))) url = getUrl(tmpr);
57             if (url != null) {
58                 res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); // 301
59
res.setContentType("text/html");
60                 res.setHeader("Location", url);
61                 return;
62             }
63         }
64         finally { // decrease reference count always
65
decRefCount(req);
66         }
67     }
68
69     /**
70      * Retrieve an alternate url based on a jumper key.
71      * @param key the jumper key (original url specified)
72      * @return the alternate yurl, or <code>null</code> if no url was found.
73      * @since MMBase-1.7
74      */

75     protected String JavaDoc getUrl(String JavaDoc key) {
76         String JavaDoc url = null;
77         Jumpers bul = null;
78         if (mmbase != null) {
79             bul = (Jumpers) mmbase.getMMObject("jumpers");
80         }
81         if (bul != null) {
82             if (key.endsWith("/")) {
83                 url = bul.getJump(key.substring(0, key.length() - 1));
84             } else {
85                 url = bul.getJump(key);
86             }
87             if (url != null) return url;
88         } else {
89             log.error("servjumpers -> can't access NodeManager jumpers (check jumpers.xml)");
90         }
91         return null;
92     }
93 }
94
Popular Tags