/*
* The contents of this file are subject to the Open Software License
* Version 3.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.opensource.org/licenses/osl-3.0.txt
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*/
package org.mulgara.server;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* A servlet for listing web services.
*
* @created Sep 6, 2008
* @author Paula Gearon
* @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
* @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
*/
public class ServiceListingServlet extends HttpServlet {
/** Autogenerated serialization ID */
private static final long serialVersionUID = -5568145649330453029L;
/** A map of service names to paths. */
private final Map<String,String> servicePaths;
/** The default path to use when not using this class. */
private final String defaultPath;
/**
* Creates the servlet for listing the given paths.
* @param servicePaths The names of all the services.
* @param defaultPath The default path to use when not using this servlet.
*/
public ServiceListingServlet(Map<String,String> servicePaths, String defaultPath) throws IOException {
this.servicePaths = servicePaths;
this.defaultPath = defaultPath;
}
/**
* Respond to a request for the servlet.
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String path = req.getPathInfo();
if (path.equals("/")) {
// file request
resp.setContentType("text/html");
resp.setHeader("pragma", "no-cache");
PrintWriter out = resp.getWriter();
printPage(out);
out.close();
} else {
resp.sendRedirect(defaultPath);
}
}
/**
* Provide a description for the servlet.
* @see javax.servlet.GenericServlet#getServletInfo()
*/
public String getServletInfo() {
return "Service List";
}
/**
* Outputs the list of service names and the paths to go to.
* @param out Where to send the page.
*/
private void printPage(PrintWriter out) {
out.println("<!DOCTYPE html PUBLIC \"-//W3C//Dtd XHTML 1.0 Transitional//EN\" \"http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
out.println("<title>Mulgara Web Services</title>");
out.println("</head>");
out.println("</body>");
out.println("<h2>Available Services</h2>");
out.println("<ul>");
for (Map.Entry<String,String> service: servicePaths.entrySet()) {
out.println("<li><a href=\"" + service.getValue() + "\">" + service.getKey() + "</a></li>");
}
out.println("</ul>");
out.println("</body>");
out.println("</html>");
}
}