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