Now that alone might not have been so tough, but then I also had to change the DNS entries. So my mapping table was like:
If you wanted to only redirect some traffic, you would need to change the code to NOT go to the defaulturl or install this in a specific application context container.
# properties defaulturl=http://ldapwiki.willeke.com/ oldbaseurl1=http://willeke.com:9080/wikildap/ newbaseurl1=http://ldapwiki.willeke.com/ oldbaseurl2=http://willeke.com:9080/wikiwilleke/ newbaseurl2=http://wiki.willeke.com/ oldbaseurl3=http://willeke.com:9080/classof1968/ newbaseurl3=http://classof1968.willeke.com/ oldbaseurl4=http://willeke.com:9080/wikiwillekegenealogy/ newbaseurl4=http://genealogy.willeke.com/ oldbaseurl5=http://www.willeke.com:9080/wikildap/ newbaseurl5=http://ldapwiki.willeke.com/ oldbaseurl6=http://www.willeke.com:9080/wikiwilleke/ newbaseurl6=http://wiki.willeke.com/ oldbaseurl7=http://www.willeke.com:9080/classof1968/ newbaseurl7=http://classof1968.willeke.com/ oldbaseurl8=http://www.willeke.com:9080/wikiwillekegenealogy/ newbaseurl9=http://genealogy.willeke.com/
The "defaulturl" was used for anything that did not map.
A typical URL might be:
http://willeke.com:9080/wikildap/Wiki.jsp?page=UsefullCommandsForEdirectoryThis URL would have to end up like:
http://ldapwiki.willeke.com/Wiki.jsp?page=UsefullCommandsForEdirectory
The oldbaseURLs were put in a HashMap as the key to the newBaseURLs.!!!The Actual Java Code I am sure there are some Java gurus that would cringe at this but it appears to work.
package com.willeke.web;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Redirect extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 5503481944024425059L;
private static Logger log = Logger.getLogger(Redirect.class.getName());
private static final ResourceBundle bundle = ResourceBundle.getBundle("Redirect");
HashMap<String, String> urlList = new HashMap<String, String>();
private static String DEFAULTSITE = bundle.getString("defaulturl");
/**
* Constructor of the object.
*/
public Redirect()
{
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy()
{
log.info("destroy");
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("received a Get");
// inCommingUrl="http://willeke.com:9080/wikildap/Wiki.jsp?page=JimWillekeResume";
String site = parseURL(request);
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", site);
response.setContentType("text/html");
Enumeration enumer = request.getAttributeNames();
String query = request.getQueryString();
while (enumer.hasMoreElements())
{
String name = (String) enumer.nextElement();
String value = (String) request.getAttribute(name);
response.addHeader(name, value);
}
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// InputStream is = request.getInputStream();
log.info("received a post");
// inCommingUrl="http://willeke.com:9080/wikildap/Wiki.jsp?page=JimWillekeResume";
String site = parseURL(request);
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", site);
response.setContentType("text/html");
Enumeration enumer = request.getAttributeNames();
while (enumer.hasMoreElements())
{
String name = (String) enumer.nextElement();
String value = (String) request.getAttribute(name);
response.addHeader(name, value);
}
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
private String parseURL(HttpServletRequest request)
{
String requestURL = request.getRequestURL().toString();
String query = request.getQueryString();
log.info("reqURL = " + request.getRequestURL());
String site = DEFAULTSITE;
Set<String> keyset = urlList.keySet();
Iterator<String> it = keyset.iterator();
while (it.hasNext())
{
String testUrl = (String) it.next();
if (requestURL.startsWith(testUrl))
{
String prefix = (String) urlList.get(testUrl);
String postfix = request.getRequestURL().substring(testUrl.length());
if (postfix.length() > 0)
{
site = prefix + postfix;
} else
{
site = prefix;
}
if (!(query == null))
{
if (query.length() > 0)
{
log.info("queryString: " + query);
site = site + "?" + query;
}
}
}
}
log.info("Sent to : " + site);
return site;
}
/**
* Returns information about the servlet, such as author, version, and
* copyright.
*
* @return String information about this servlet
*/
public String getServletInfo()
{
return "This is my redirect servlet";
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException
{
log.info("Starting INIT");
/**
* http://willeke.com:9080/wikildap/ - http://ldapwiki.willeke.com/
* http://willeke.com:9080/wikiwilleke/ - http://wiki.willeke.com/
* http://willeke.com:9080/classof1968/ -
* http://classof1968.willeke.com/
* http://willeke.com:9080/wikiwillekegenealogy/ -->>
* http://genealogy.willeke.com/
*/
// HashMap urlList = new HashMap();
bundle.getString("defaulturl");
for (int i = 1; i < 10; i++)
{
String oldBaseUrl = "oldbaseurl" + i;
String newBaseUrl = "newbaseurl" + i;
try
{
String oldone = bundle.getString(oldBaseUrl);
String newone = bundle.getString(newBaseUrl);
log.info("Adding OLD URL: " + oldone);
log.info(" mapping to New URL: " + newone);
urlList.put(oldone, newone);
} catch (RuntimeException e)
{
break;
}
}
log.info("Leaving INIT");
// Put your code here
}
}