View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A Proxy server.
42   *
43   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
44   * @since 2.6
45   */
46  class ProxyServer implements AutoCloseable {
47      private Server proxyServer;
48  
49      private ServerConnector serverConnector;
50  
51      /**
52       * @param proxyServlet the wanted auth proxy servlet
53       */
54      ProxyServer(AuthAsyncProxyServlet proxyServlet) {
55          this(null, 0, proxyServlet);
56      }
57  
58      /**
59       * @param hostName the server name
60       * @param port the server port
61       * @param proxyServlet the wanted auth proxy servlet
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          // Setup proxy handler to handle CONNECT methods
74          ConnectHandler proxy = new ConnectHandler();
75          proxyServer.setHandler(proxy);
76  
77          // Setup proxy servlet
78          ServletContextHandler context = new ServletContextHandler(proxy, "/", true, false);
79          ServletHolder appServletHolder = new ServletHolder(proxyServlet);
80          context.addServlet(appServletHolder, "/*");
81      }
82  
83      /**
84       * @return the host name
85       */
86      public String getHostName() {
87          return serverConnector.getHost() == null
88                  ? InetAddress.getLoopbackAddress().getHostName()
89                  : serverConnector.getHost();
90      }
91  
92      /**
93       * @return the host port
94       */
95      public int getPort() {
96          return serverConnector.getLocalPort();
97      }
98  
99      /**
100      * @throws Exception if any
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      * @throws Exception if any
115      */
116     public void stop() throws Exception {
117         if (proxyServer != null) {
118             proxyServer.stop();
119         }
120         proxyServer = null;
121     }
122 
123     /**
124      * A proxy servlet with authentication support.
125      */
126     static class AuthAsyncProxyServlet extends AsyncProxyServlet {
127         private Map<String, String> authentications;
128 
129         private long sleepTime = 0;
130 
131         /**
132          * Constructor for non authentication servlet.
133          */
134         AuthAsyncProxyServlet() {
135             super();
136         }
137 
138         /**
139          * Constructor for authentication servlet.
140          *
141          * @param authentications a map of user/password
142          */
143         AuthAsyncProxyServlet(Map<String, String> authentications) {
144             this();
145 
146             this.authentications = authentications;
147         }
148 
149         /**
150          * Constructor for authentication servlet.
151          *
152          * @param authentications a map of user/password
153          * @param sleepTime a positive time to sleep the service thread (for timeout)
154          */
155         AuthAsyncProxyServlet(Map<String, String> authentications, long sleepTime) {
156             this();
157             this.authentications = authentications;
158             this.sleepTime = sleepTime;
159         }
160 
161         /** {@inheritDoc} */
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                             // nop
186                         }
187                     }
188                     String authPass = this.authentications.get(user);
189                     if (password.equals(authPass)) {
190                         // could throw exceptions...
191                         super.service(req, res);
192                         return;
193                     }
194                 }
195 
196                 // Proxy-Authenticate Basic realm="CCProxy Authorization"
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 }