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.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.cxf.Bus;
031    import org.apache.cxf.endpoint.Server;
032    import org.apache.cxf.endpoint.ServerRegistry;
033    import org.apache.cxf.transport.http_jetty.JettyHTTPDestination;
034    import org.eclipse.jetty.http.HttpMethods;
035    import org.eclipse.jetty.http.MimeTypes;
036    import org.eclipse.jetty.server.HttpConnection;
037    import org.eclipse.jetty.server.Request;
038    import org.eclipse.jetty.server.handler.AbstractHandler;
039    import org.eclipse.jetty.util.ByteArrayISO8859Writer;
040    import org.eclipse.jetty.util.StringUtil;
041    
042    
043    public class ListServiceHandler extends AbstractHandler {
044    
045    
046        private Map<String, Bus> allBuses;
047        
048        public ListServiceHandler(Map<String, Bus> allBuses) {
049            this.allBuses = allBuses;
050        }
051        
052        public void handle(String target, HttpServletRequest request,
053                HttpServletResponse response, int dispatch) throws IOException,
054                ServletException {
055            if (response.isCommitted()
056                    || HttpConnection.getCurrentConnection().getRequest()
057                            .isHandled()) {
058                return;
059            }
060    
061            String method = request.getMethod();
062    
063            if (!method.equals(HttpMethods.GET)
064                    || !request.getRequestURI().equals("/")) {
065                return;
066            }
067    
068            response.setStatus(404);
069            response.setContentType(MimeTypes.TEXT_HTML);
070    
071            ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
072    
073            String uri = request.getRequestURI();
074            uri = StringUtil.replace(uri, "<", "&lt;");
075            uri = StringUtil.replace(uri, ">", "&gt;");
076    
077            writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
078            writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
079            writer.write("No service matched or handled this request.<BR>");
080            writer.write("Known services on cxf bc component are: <ul>");
081    
082            List<Server> servers = new ArrayList<Server>();
083            for (Bus bus : allBuses.values()) {
084                ServerRegistry serverRegistry = bus.getExtension(ServerRegistry.class);
085                servers.addAll(serverRegistry.getServers());
086            }
087            for (Iterator<Server> iter = servers.iterator(); iter.hasNext();) {
088                Server server = (Server) iter.next();
089                JettyHTTPDestination jhd = (JettyHTTPDestination)server.getDestination();
090                String address = jhd.getAddress().getAddress().getValue();
091                writer.write("<li><a href=\"");
092                writer.write(address);
093                writer.write("?wsdl\">");
094                writer.write(address);
095                writer.write("</a></li>\n");
096            }
097            
098            for (int i = 0; i < 10; i++) {
099                writer.write("\n<!-- Padding for IE                  -->");
100            }
101    
102            writer.write("\n</BODY>\n</HTML>\n");
103            writer.flush();
104            response.setContentLength(writer.size());
105            OutputStream out = response.getOutputStream();
106            writer.writeTo(out);
107            out.close();
108        }
109    
110        public void handle(String target, Request baseRequest, HttpServletRequest request,
111                           HttpServletResponse response) throws IOException, ServletException {
112            handle(target, request, response, 0);
113                    
114        }
115    
116    }