package ch.rasc.embeddedtc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Host;

public class ListContextsServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private final Context parentContext;

	public ListContextsServlet(Context parentContext) {
		this.parentContext = parentContext;
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		StringBuilder sb = new StringBuilder(300);
		sb.append("<html>");
		sb.append("<head>");
		sb.append("<title>Error 404 - Not Found</title>");
		sb.append("</head>");
		sb.append("<body>");
		sb.append("<h2>Error 404 - Not Found.</h2>");

		sb.append("No context on this server matched or handled this request.");
		sb.append("<br>Contexts known to this server are: ");

		Host host = (Host) parentContext.getParent();

		sb.append("<ul>");
		Container[] contexts = host.findChildren();
		for (int i = 0; i < contexts.length; i++) {
			Context context = (Context) contexts[i];
			if (context != null) {
				if (!context.getPath().equals("") && context.getState().isAvailable()) {

					sb.append("<li>");
					sb.append("<a href=\"");
					sb.append(context.getPath());
					sb.append("\">");
					sb.append(context.getPath());
					sb.append("</a>");
					sb.append("</li>");
				}
			}
		}
		sb.append("</ul>");
		sb.append("</body>");
		sb.append("</html>");

		resp.setStatus(404);
		resp.getOutputStream().write(sb.toString().getBytes());
		resp.flushBuffer();
	}
}

@@ -1,8 +1,632 @@
+/**%0A * Copyright 2012-2013 Ralph Schaer %3Cralphschaer@gmail.com%3E%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 

Add missing license header