KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > ManagedContextManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.mortbay.jetty.MimeTypes;
32 import org.mortbay.util.ByteArrayISO8859Writer;
33 import org.mortbay.util.StringUtil;
34
35 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
36
37 public class ManagedContextManager implements ContextManager {
38
39     private static final Log logger = LogFactory.getLog(ManagedContextManager.class);
40     
41     private HttpConfiguration configuration;
42     private Map JavaDoc managedContexts;
43     
44     public void init() throws Exception JavaDoc {
45         if (configuration == null) {
46             configuration = new HttpConfiguration();
47         }
48         managedContexts = new ConcurrentHashMap();
49     }
50
51     public void shutDown() throws Exception JavaDoc {
52         stop();
53     }
54
55     public void start() throws Exception JavaDoc {
56     }
57
58     public void stop() throws Exception JavaDoc {
59     }
60     
61     public synchronized Object JavaDoc createContext(String JavaDoc strUrl,
62                                              HttpProcessor processor) throws Exception JavaDoc {
63         URI JavaDoc uri = new URI JavaDoc(strUrl);
64         String JavaDoc path = uri.getPath();
65         if (!path.startsWith("/")) {
66             path = path + "/";
67         }
68         if (!path.endsWith("/")) {
69             path = path + "/";
70         }
71         managedContexts.put(path, processor);
72         return path;
73     }
74     
75     public synchronized void remove(Object JavaDoc context) throws Exception JavaDoc {
76         managedContexts.remove(context);
77     }
78
79     public HttpConfiguration getConfiguration() {
80         return configuration;
81     }
82
83     public void setConfiguration(HttpConfiguration configuration) {
84         this.configuration = configuration;
85     }
86
87     public HttpProcessor getMainProcessor() {
88         if (!configuration.isManaged()) {
89             throw new IllegalStateException JavaDoc("ServerManager is not managed");
90         }
91         return new MainProcessor();
92     }
93     
94     protected class MainProcessor implements HttpProcessor {
95         private String JavaDoc mapping;
96         public MainProcessor() {
97             this.mapping = configuration.getMapping();
98         }
99         public String JavaDoc getAuthMethod() {
100             return null;
101         }
102         public SslParameters getSsl() {
103             return null;
104         }
105         public void process(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
106             String JavaDoc uri = request.getRequestURI();
107             if ("/".equals(uri) && "GET".equals(request.getMethod())) {
108                 displayServices(request, response);
109                 return;
110             }
111             Set JavaDoc urls = managedContexts.keySet();
112             for (Iterator JavaDoc iter = urls.iterator(); iter.hasNext();) {
113                 String JavaDoc url = (String JavaDoc) iter.next();
114                 if (uri.startsWith(request.getContextPath() + mapping + url)) {
115                     HttpProcessor proc = (HttpProcessor) managedContexts.get(url);
116                     proc.process(request, response);
117                     return;
118                 }
119             }
120             displayServices(request, response);
121         }
122         public void displayServices(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
123             response.setStatus(404);
124             response.setContentType(MimeTypes.TEXT_HTML);
125             
126             ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
127
128             String JavaDoc uri = request.getRequestURI();
129             uri = StringUtil.replace(uri, "<", "&lt;");
130             uri = StringUtil.replace(uri, ">", "&gt;");
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 JavaDoc servers = ManagedContextManager.this.managedContexts.keySet();
138             for (Iterator JavaDoc iter = servers.iterator(); iter.hasNext();) {
139                 String JavaDoc context = (String JavaDoc) iter.next();
140                 if (!context.endsWith("/")) {
141                     context += "/";
142                 }
143                 String JavaDoc protocol = request.isSecure() ? "https" : "http";
144                 context = protocol + "://" + request.getLocalName() + ":" + request.getLocalPort() + request.getContextPath() + mapping + context;
145                 writer.write("<li><a HREF=\"");
146                 writer.write(context);
147                 writer.write("?wsdl\">");
148                 writer.write(context);
149                 writer.write("</a></li>\n");
150             }
151             
152             for (int i=0; i < 10; i++) {
153                 writer.write("\n<!-- Padding for IE -->");
154             }
155             
156             writer.write("\n</BODY>\n</HTML>\n");
157             writer.flush();
158             response.setContentLength(writer.size());
159             OutputStream JavaDoc out = response.getOutputStream();
160             writer.writeTo(out);
161             out.close();
162         }
163     }
164
165 }
166
Popular Tags