-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHttpServerHandlerBinder.java
More file actions
94 lines (80 loc) · 3.79 KB
/
HttpServerHandlerBinder.java
File metadata and controls
94 lines (80 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nesscomputing.httpserver;
import java.lang.annotation.Annotation;
import javax.servlet.Servlet;
import com.google.inject.Binder;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.name.Names;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.HandlerWrapper;
/**
* Bind additional {@link Handler} elements into the http server.
*/
public final class HttpServerHandlerBinder
{
public static final String HANDLER_NAME = "_handlers";
public static final Annotation HANDLER_NAMED = Names.named(HANDLER_NAME);
public static final String LOGGING_NAME = "_logging";
public static final Annotation LOGGING_NAMED = Names.named(LOGGING_NAME);
public static final String SECURITY_NAME = "_security";
public static final Annotation SECURITY_NAMED = Names.named(SECURITY_NAME);
public static final String CATCHALL_NAME = "_catchall";
public static final Annotation CATCHALL_NAMED = Names.named(CATCHALL_NAME);
private HttpServerHandlerBinder()
{
}
/**
* Bind a new handler into the jetty service. These handlers are bound before the servlet handler and can handle parts
* of the URI space before a servlet sees it.
*
* Do not use this method to bind logging handlers as they would be called before any functionality has been executed and
* logging information would be incomplete. Use {@link HttpServerHandlerBinder#bindLoggingHandler(Binder) instead.
*/
public static LinkedBindingBuilder<Handler> bindHandler(final Binder binder)
{
final Multibinder<Handler> handlers = Multibinder.newSetBinder(binder, Handler.class, HANDLER_NAMED);
return handlers.addBinding();
}
/**
* Bind a new handler into the jetty service. These handlers are bound after the servlet handler and will not see any requests before they
* have been handled. This is intended to bind logging, statistics etc. which want to operate on the results of a request to the server.
*
* Any handler intended to manage content or a part of the URI space should use {@link HttpServerHandlerBinder#bindHandler(Binder)}.
*/
public static LinkedBindingBuilder<Handler> bindLoggingHandler(final Binder binder)
{
final Multibinder<Handler> handlers = Multibinder.newSetBinder(binder, Handler.class, LOGGING_NAMED);
return handlers.addBinding();
}
/**
* Bind a delegating security handler. Any request will be routed through this handler and can be allowed or denied by this handler. If no handler
* is bound, requests are forwarded to the internal handler collection.
*/
public static LinkedBindingBuilder<HandlerWrapper> bindSecurityHandler(final Binder binder)
{
return binder.bind(HandlerWrapper.class).annotatedWith(SECURITY_NAMED);
}
/**
* Configure the "get all requests" servlet that backs the {@link GuiceFilter}. This servlet should log all requests as legal request should never
* hit it.
*/
public static LinkedBindingBuilder<Servlet> bindCatchallServlet(final Binder binder)
{
return binder.bind (Servlet.class).annotatedWith(CATCHALL_NAMED);
}
}