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 {
47 private Server proxyServer;
48
49 private ServerConnector serverConnector;
50
51
52
53
54 public ProxyServer(AuthAsyncProxyServlet proxyServlet) {
55 this(null, 0, proxyServlet);
56 }
57
58
59
60
61
62
63 public 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
109
110
111 public void stop() throws Exception {
112 if (proxyServer != null) {
113 proxyServer.stop();
114 }
115 proxyServer = null;
116 }
117
118
119
120
121 static class AuthAsyncProxyServlet extends AsyncProxyServlet {
122 private Map<String, String> authentications;
123
124 private long sleepTime = 0;
125
126
127
128
129 public AuthAsyncProxyServlet() {
130 super();
131 }
132
133
134
135
136
137
138 public AuthAsyncProxyServlet(Map<String, String> authentications) {
139 this();
140
141 this.authentications = authentications;
142 }
143
144
145
146
147
148
149
150 public AuthAsyncProxyServlet(Map<String, String> authentications, long sleepTime) {
151 this();
152 this.authentications = authentications;
153 this.sleepTime = sleepTime;
154 }
155
156
157 @Override
158 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
159 final HttpServletRequest request = (HttpServletRequest) req;
160 final HttpServletResponse response = (HttpServletResponse) res;
161
162 if (this.authentications != null && !this.authentications.isEmpty()) {
163 String proxyAuthorization = request.getHeader("Proxy-Authorization");
164 if (proxyAuthorization != null && proxyAuthorization.startsWith("Basic ")) {
165 String proxyAuth = proxyAuthorization.substring("Basic ".length());
166 String authorization = new String(Base64.getDecoder().decode(proxyAuth), StandardCharsets.UTF_8);
167
168 String[] authTokens = authorization.split(":");
169 String user = authTokens[0];
170 String password = authTokens[1];
171
172 if (this.authentications.get(user) == null) {
173 throw new IllegalArgumentException(user + " not found in the map!");
174 }
175
176 if (sleepTime > 0) {
177 try {
178 Thread.sleep(sleepTime);
179 } catch (InterruptedException e) {
180
181 }
182 }
183 String authPass = this.authentications.get(user);
184 if (password.equals(authPass)) {
185
186 super.service(req, res);
187 return;
188 }
189 }
190
191
192 response.addHeader("Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"");
193 response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
194 return;
195 }
196
197 super.service(req, res);
198 }
199 }
200 }