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.http;
018
019 import java.io.IOException;
020 import java.io.OutputStream;
021 import java.net.URI;
022 import java.util.Iterator;
023 import java.util.Map;
024 import java.util.Set;
025 import java.util.concurrent.ConcurrentHashMap;
026
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.http.HttpServletResponse;
029
030
031 import org.mortbay.jetty.MimeTypes;
032 import org.mortbay.util.ByteArrayISO8859Writer;
033 import org.mortbay.util.StringUtil;
034
035 public class ManagedContextManager implements ContextManager {
036
037 private HttpConfiguration configuration;
038 private Map managedContexts;
039
040 public void init() throws Exception {
041 if (configuration == null) {
042 configuration = new HttpConfiguration();
043 }
044 managedContexts = new ConcurrentHashMap();
045 }
046
047 public void shutDown() throws Exception {
048 stop();
049 }
050
051 public void start() throws Exception {
052 }
053
054 public void stop() throws Exception {
055 }
056
057 public synchronized Object createContext(String strUrl, HttpProcessor processor) throws Exception {
058 URI uri = new URI(strUrl);
059 String path = uri.getPath();
060 if (!path.startsWith("/")) {
061 path = path + "/";
062 }
063 if (!path.endsWith("/")) {
064 path = path + "/";
065 }
066 managedContexts.put(path, processor);
067 return path;
068 }
069
070 public synchronized void remove(Object context) throws Exception {
071 managedContexts.remove(context);
072 }
073
074 public HttpConfiguration getConfiguration() {
075 return configuration;
076 }
077
078 public void setConfiguration(HttpConfiguration configuration) {
079 this.configuration = configuration;
080 }
081
082 public HttpProcessor getMainProcessor() {
083 if (!configuration.isManaged()) {
084 throw new IllegalStateException("ServerManager is not managed");
085 }
086 return new MainProcessor();
087 }
088
089 protected class MainProcessor implements HttpProcessor {
090 private String mapping;
091
092 public MainProcessor() {
093 this.mapping = configuration.getMapping();
094 }
095
096 public String getAuthMethod() {
097 return null;
098 }
099
100 public SslParameters getSsl() {
101 return null;
102 }
103
104 public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
105 String uri = request.getRequestURI();
106 if ("/".equals(uri) && "GET".equals(request.getMethod())) {
107 displayServices(request, response);
108 return;
109 }
110 Set urls = managedContexts.keySet();
111 for (Iterator iter = urls.iterator(); iter.hasNext();) {
112 String url = (String) iter.next();
113 if (uri.startsWith(request.getContextPath() + mapping + url)) {
114 HttpProcessor proc = (HttpProcessor) managedContexts.get(url);
115 proc.process(request, response);
116 return;
117 }
118 }
119 displayServices(request, response);
120 }
121
122 public void displayServices(HttpServletRequest request, HttpServletResponse response) throws IOException {
123 response.setStatus(404);
124 response.setContentType(MimeTypes.TEXT_HTML);
125
126 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
127
128 String uri = request.getRequestURI();
129 uri = StringUtil.replace(uri, "<", "<");
130 uri = StringUtil.replace(uri, ">", ">");
131
132 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
133 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
134 writer.write("No service matched or handled this request.<BR>");
135 writer.write("Known services are: <ul>");
136
137 Set servers = ManagedContextManager.this.managedContexts.keySet();
138 for (Iterator iter = servers.iterator(); iter.hasNext();) {
139 String context = (String) iter.next();
140 if (!context.endsWith("/")) {
141 context += "/";
142 }
143 String protocol = request.isSecure() ? "https" : "http";
144 context = protocol + "://" + request.getLocalName() + ":" + request.getLocalPort()
145 + request.getContextPath() + mapping + context;
146 writer.write("<li><a href=\"");
147 writer.write(context);
148 writer.write("?wsdl\">");
149 writer.write(context);
150 writer.write("</a></li>\n");
151 }
152
153 for (int i = 0; i < 10; i++) {
154 writer.write("\n<!-- Padding for IE -->");
155 }
156
157 writer.write("\n</BODY>\n</HTML>\n");
158 writer.flush();
159 response.setContentLength(writer.size());
160 OutputStream out = response.getOutputStream();
161 writer.writeTo(out);
162 out.close();
163 }
164 }
165
166 }