Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/handler/DefaultHandler.java @ 807:947b11aa3157
remove jetty favicon
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 07 Sep 2016 23:26:12 -0600 |
parents | 3428c60d7cfc |
children | 8e9db0bbf4f9 |
comparison
equal
deleted
inserted
replaced
806:49cbf6ee529f | 807:947b11aa3157 |
---|---|
51 * | 51 * |
52 * @org.apache.xbean.XBean | 52 * @org.apache.xbean.XBean |
53 */ | 53 */ |
54 public class DefaultHandler extends AbstractHandler | 54 public class DefaultHandler extends AbstractHandler |
55 { | 55 { |
56 private static final Logger LOG = Log.getLogger(DefaultHandler.class); | 56 private static final Logger LOG = Log.getLogger(DefaultHandler.class); |
57 | 57 |
58 final long _faviconModified=(System.currentTimeMillis()/1000)*1000L; | 58 boolean _showContexts=true; |
59 byte[] _favicon; | 59 |
60 boolean _serveIcon=true; | 60 /* ------------------------------------------------------------ */ |
61 boolean _showContexts=true; | 61 /* |
62 | 62 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) |
63 public DefaultHandler() | 63 */ |
64 { | 64 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException |
65 try | 65 { |
66 { | 66 if (response.isCommitted() || baseRequest.isHandled()) |
67 URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico"); | 67 return; |
68 if (fav!=null) | 68 |
69 { | 69 baseRequest.setHandled(true); |
70 Resource r = Resource.newResource(fav); | 70 |
71 _favicon=IO.readBytes(r.getInputStream()); | 71 String method=request.getMethod(); |
72 } | 72 |
73 } | 73 if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/")) |
74 catch(Exception e) | 74 { |
75 { | 75 response.sendError(HttpServletResponse.SC_NOT_FOUND); |
76 LOG.warn(e); | 76 return; |
77 } | 77 } |
78 } | |
79 | |
80 /* ------------------------------------------------------------ */ | |
81 /* | |
82 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) | |
83 */ | |
84 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
85 { | |
86 if (response.isCommitted() || baseRequest.isHandled()) | |
87 return; | |
88 | |
89 baseRequest.setHandled(true); | |
90 | |
91 String method=request.getMethod(); | |
92 | 78 |
93 // little cheat for common request | 79 response.setStatus(HttpServletResponse.SC_NOT_FOUND); |
94 if (_serveIcon && _favicon!=null && method.equals(HttpMethods.GET) && request.getRequestURI().equals("/favicon.ico")) | 80 response.setContentType(MimeTypes.TEXT_HTML); |
95 { | 81 |
96 if (request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE)==_faviconModified) | 82 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500); |
97 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); | 83 |
98 else | 84 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found"); |
99 { | 85 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n"); |
100 response.setStatus(HttpServletResponse.SC_OK); | 86 writer.write("No context on this server matched or handled this request.<BR>"); |
101 response.setContentType("image/x-icon"); | 87 |
102 response.setContentLength(_favicon.length); | 88 if (_showContexts) |
103 response.setDateHeader(HttpHeaders.LAST_MODIFIED, _faviconModified); | 89 { |
104 response.setHeader(HttpHeaders.CACHE_CONTROL,"max-age=360000,public"); | 90 writer.write("Contexts known to this server are: <ul>"); |
105 response.getOutputStream().write(_favicon); | 91 |
106 } | 92 Server server = getServer(); |
107 return; | 93 Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class); |
108 } | 94 |
109 | 95 for (int i=0;handlers!=null && i<handlers.length;i++) |
110 | 96 { |
111 if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/")) | 97 ContextHandler context = (ContextHandler)handlers[i]; |
112 { | 98 if (context.isRunning()) |
113 response.sendError(HttpServletResponse.SC_NOT_FOUND); | 99 { |
114 return; | 100 writer.write("<li><a href=\""); |
115 } | 101 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) |
102 writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
103 writer.write(context.getContextPath()); | |
104 if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/")) | |
105 writer.write("/"); | |
106 writer.write("\">"); | |
107 writer.write(context.getContextPath()); | |
108 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) | |
109 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
110 writer.write(" ---> "); | |
111 writer.write(context.toString()); | |
112 writer.write("</a></li>\n"); | |
113 } | |
114 else | |
115 { | |
116 writer.write("<li>"); | |
117 writer.write(context.getContextPath()); | |
118 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) | |
119 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
120 writer.write(" ---> "); | |
121 writer.write(context.toString()); | |
122 if (context.isFailed()) | |
123 writer.write(" [failed]"); | |
124 if (context.isStopped()) | |
125 writer.write(" [stopped]"); | |
126 writer.write("</li>\n"); | |
127 } | |
128 } | |
129 } | |
130 | |
131 for (int i=0;i<10;i++) | |
132 writer.write("\n<!-- Padding for IE -->"); | |
133 | |
134 writer.write("\n</BODY>\n</HTML>\n"); | |
135 writer.flush(); | |
136 response.setContentLength(writer.size()); | |
137 OutputStream out=response.getOutputStream(); | |
138 writer.writeTo(out); | |
139 out.close(); | |
140 } | |
116 | 141 |
117 response.setStatus(HttpServletResponse.SC_NOT_FOUND); | 142 public boolean getShowContexts() |
118 response.setContentType(MimeTypes.TEXT_HTML); | 143 { |
119 | 144 return _showContexts; |
120 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500); | 145 } |
121 | |
122 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found"); | |
123 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n"); | |
124 writer.write("No context on this server matched or handled this request.<BR>"); | |
125 | |
126 if (_showContexts) | |
127 { | |
128 writer.write("Contexts known to this server are: <ul>"); | |
129 | |
130 Server server = getServer(); | |
131 Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class); | |
132 | |
133 for (int i=0;handlers!=null && i<handlers.length;i++) | |
134 { | |
135 ContextHandler context = (ContextHandler)handlers[i]; | |
136 if (context.isRunning()) | |
137 { | |
138 writer.write("<li><a href=\""); | |
139 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) | |
140 writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
141 writer.write(context.getContextPath()); | |
142 if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/")) | |
143 writer.write("/"); | |
144 writer.write("\">"); | |
145 writer.write(context.getContextPath()); | |
146 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) | |
147 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
148 writer.write(" ---> "); | |
149 writer.write(context.toString()); | |
150 writer.write("</a></li>\n"); | |
151 } | |
152 else | |
153 { | |
154 writer.write("<li>"); | |
155 writer.write(context.getContextPath()); | |
156 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) | |
157 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); | |
158 writer.write(" ---> "); | |
159 writer.write(context.toString()); | |
160 if (context.isFailed()) | |
161 writer.write(" [failed]"); | |
162 if (context.isStopped()) | |
163 writer.write(" [stopped]"); | |
164 writer.write("</li>\n"); | |
165 } | |
166 } | |
167 } | |
168 | |
169 for (int i=0;i<10;i++) | |
170 writer.write("\n<!-- Padding for IE -->"); | |
171 | |
172 writer.write("\n</BODY>\n</HTML>\n"); | |
173 writer.flush(); | |
174 response.setContentLength(writer.size()); | |
175 OutputStream out=response.getOutputStream(); | |
176 writer.writeTo(out); | |
177 out.close(); | |
178 } | |
179 | 146 |
180 /* ------------------------------------------------------------ */ | 147 public void setShowContexts(boolean show) |
181 /** | 148 { |
182 * @return Returns true if the handle can server the jetty favicon.ico | 149 _showContexts = show; |
183 */ | 150 } |
184 public boolean getServeIcon() | |
185 { | |
186 return _serveIcon; | |
187 } | |
188 | |
189 /* ------------------------------------------------------------ */ | |
190 /** | |
191 * @param serveIcon true if the handle can server the jetty favicon.ico | |
192 */ | |
193 public void setServeIcon(boolean serveIcon) | |
194 { | |
195 _serveIcon = serveIcon; | |
196 } | |
197 | |
198 public boolean getShowContexts() | |
199 { | |
200 return _showContexts; | |
201 } | |
202 | |
203 public void setShowContexts(boolean show) | |
204 { | |
205 _showContexts = show; | |
206 } | |
207 | 151 |
208 } | 152 } |