001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.cxfbc;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import javax.servlet.ServletException;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.cxf.endpoint.Server;
029    import org.apache.cxf.endpoint.ServerRegistry;
030    import org.apache.cxf.transport.http_jetty.JettyHTTPDestination;
031    import org.mortbay.jetty.HttpConnection;
032    import org.mortbay.jetty.HttpMethods;
033    import org.mortbay.jetty.MimeTypes;
034    import org.mortbay.jetty.handler.AbstractHandler;
035    import org.mortbay.util.ByteArrayISO8859Writer;
036    import org.mortbay.util.StringUtil;
037    
038    public class ListServiceHandler extends AbstractHandler {
039    
040    
041        private ServerRegistry serverRegistry;
042        
043        public ListServiceHandler(ServerRegistry serRegistry) {
044            this.serverRegistry = serRegistry;
045        }
046        
047        public void handle(String target, HttpServletRequest request,
048                HttpServletResponse response, int dispatch) throws IOException,
049                ServletException {
050            if (response.isCommitted()
051                    || HttpConnection.getCurrentConnection().getRequest()
052                            .isHandled()) {
053                return;
054            }
055    
056            String method = request.getMethod();
057    
058            if (!method.equals(HttpMethods.GET)
059                    || !request.getRequestURI().equals("/")) {
060                return;
061            }
062    
063            response.setStatus(404);
064            response.setContentType(MimeTypes.TEXT_HTML);
065    
066            ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
067    
068            String uri = request.getRequestURI();
069            uri = StringUtil.replace(uri, "<", "&lt;");
070            uri = StringUtil.replace(uri, ">", "&gt;");
071    
072            writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
073            writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
074            writer.write("No service matched or handled this request.<BR>");
075            writer.write("Known services on cxf bc component are: <ul>");
076    
077            List<Server> servers = serverRegistry.getServers();
078            for (Iterator iter = servers.iterator(); iter.hasNext();) {
079                Server server = (Server) iter.next();
080                JettyHTTPDestination jhd = (JettyHTTPDestination)server.getDestination();
081                String address = jhd.getAddress().getAddress().getValue();
082                writer.write("<li><a href=\"");
083                writer.write(address);
084                writer.write("?wsdl\">");
085                writer.write(address);
086                writer.write("</a></li>\n");
087            }
088            
089            for (int i = 0; i < 10; i++) {
090                writer.write("\n<!-- Padding for IE                  -->");
091            }
092    
093            writer.write("\n</BODY>\n</HTML>\n");
094            writer.flush();
095            response.setContentLength(writer.size());
096            OutputStream out = response.getOutputStream();
097            writer.writeTo(out);
098            out.close();
099        }
100    
101    }