1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.javadoc;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.ServletRequest;
23 import javax.servlet.ServletResponse;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import java.io.IOException;
28 import java.net.InetAddress;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Base64;
31 import java.util.Map;
32
33 import org.eclipse.jetty.proxy.AsyncProxyServlet;
34 import org.eclipse.jetty.proxy.ConnectHandler;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.server.ServerConnector;
37 import org.eclipse.jetty.servlet.ServletContextHandler;
38 import org.eclipse.jetty.servlet.ServletHolder;
39
40
41
42
43
44
45
46 class ProxyServer implements AutoCloseable {
47 private Server proxyServer;
48
49 private ServerConnector serverConnector;
50
51
52
53
54 ProxyServer(AuthAsyncProxyServlet proxyServlet) {
55 this(null, 0, proxyServlet);
56 }
57
58
59
60
61
62
63 ProxyServer(String hostName, int port, AuthAsyncProxyServlet proxyServlet) {
64 proxyServer = new Server();
65
66 serverConnector = new ServerConnector(proxyServer);
67 serverConnector.setHost(InetAddress.getLoopbackAddress().getHostName());
68 serverConnector.setReuseAddress(true);
69 serverConnector.setPort(0);
70
71 proxyServer.addConnector(serverConnector);
72
73
74 ConnectHandler proxy = new ConnectHandler();
75 proxyServer.setHandler(proxy);
76
77
78 ServletContextHandler context = new ServletContextHandler(proxy, "/", true, false);
79 ServletHolder appServletHolder = new ServletHolder(proxyServlet);
80 context.addServlet(appServletHolder, "/*");
81 }
82
83
84
85
86 public String getHostName() {
87 return serverConnector.getHost() == null
88 ? InetAddress.getLoopbackAddress().getHostName()
89 : serverConnector.getHost();
90 }
91
92
93
94
95 public int getPort() {
96 return serverConnector.getLocalPort();
97 }
98
99
100
101
102 public void start() throws Exception {
103 if (proxyServer != null) {
104 proxyServer.start();
105 }
106 }
107
108 @Override
109 public void close() throws Exception {
110 this.stop();
111 }
112
113
114
115
116 public void stop() throws Exception {
117 if (proxyServer != null) {
118 proxyServer.stop();
119 }
120 proxyServer = null;
121 }
122
123
124
125
126 static class AuthAsyncProxyServlet extends AsyncProxyServlet {
127 private Map<String, String> authentications;
128
129 private long sleepTime = 0;
130
131
132
133
134 AuthAsyncProxyServlet() {
135 super();
136 }
137
138
139
140
141
142
143 AuthAsyncProxyServlet(Map<String, String> authentications) {
144 this();
145
146 this.authentications = authentications;
147 }
148
149
150
151
152
153
154
155 AuthAsyncProxyServlet(Map<String, String> authentications, long sleepTime) {
156 this();
157 this.authentications = authentications;
158 this.sleepTime = sleepTime;
159 }
160
161
162 @Override
163 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
164 final HttpServletRequest request = (HttpServletRequest) req;
165 final HttpServletResponse response = (HttpServletResponse) res;
166
167 if (this.authentications != null && !this.authentications.isEmpty()) {
168 String proxyAuthorization = request.getHeader("Proxy-Authorization");
169 if (proxyAuthorization != null && proxyAuthorization.startsWith("Basic ")) {
170 String proxyAuth = proxyAuthorization.substring("Basic ".length());
171 String authorization = new String(Base64.getDecoder().decode(proxyAuth), StandardCharsets.UTF_8);
172
173 String[] authTokens = authorization.split(":");
174 String user = authTokens[0];
175 String password = authTokens[1];
176
177 if (this.authentications.get(user) == null) {
178 throw new IllegalArgumentException(user + " not found in the map!");
179 }
180
181 if (sleepTime > 0) {
182 try {
183 Thread.sleep(sleepTime);
184 } catch (InterruptedException e) {
185
186 }
187 }
188 String authPass = this.authentications.get(user);
189 if (password.equals(authPass)) {
190
191 super.service(req, res);
192 return;
193 }
194 }
195
196
197 response.addHeader("Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"");
198 response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
199 return;
200 }
201
202 super.service(req, res);
203 }
204 }
205 }