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 {
47      private Server proxyServer;
48  
49      private ServerConnector serverConnector;
50  
51      /**
52       * @param proxyServlet the wanted auth proxy servlet
53       */
54      public 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      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          // 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     /**
109      * @throws Exception if any
110      */
111     public void stop() throws Exception {
112         if (proxyServer != null) {
113             proxyServer.stop();
114         }
115         proxyServer = null;
116     }
117 
118     /**
119      * A proxy servlet with authentication support.
120      */
121     static class AuthAsyncProxyServlet extends AsyncProxyServlet {
122         private Map<String, String> authentications;
123 
124         private long sleepTime = 0;
125 
126         /**
127          * Constructor for non authentication servlet.
128          */
129         public AuthAsyncProxyServlet() {
130             super();
131         }
132 
133         /**
134          * Constructor for authentication servlet.
135          *
136          * @param authentications a map of user/password
137          */
138         public AuthAsyncProxyServlet(Map<String, String> authentications) {
139             this();
140 
141             this.authentications = authentications;
142         }
143 
144         /**
145          * Constructor for authentication servlet.
146          *
147          * @param authentications a map of user/password
148          * @param sleepTime a positive time to sleep the service thread (for timeout)
149          */
150         public AuthAsyncProxyServlet(Map<String, String> authentications, long sleepTime) {
151             this();
152             this.authentications = authentications;
153             this.sleepTime = sleepTime;
154         }
155 
156         /** {@inheritDoc} */
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                             // nop
181                         }
182                     }
183                     String authPass = this.authentications.get(user);
184                     if (password.equals(authPass)) {
185                         // could throw exceptions...
186                         super.service(req, res);
187                         return;
188                     }
189                 }
190 
191                 // Proxy-Authenticate Basic realm="CCProxy Authorization"
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 }