View Javadoc
1   package org.apache.maven.plugins.site.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.*;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.io.IOUtils;
34  import org.eclipse.jetty.util.B64Code;
35  import org.eclipse.jetty.proxy.AsyncProxyServlet;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.apache.maven.plugins.site.deploy.HttpRequest;
39  
40  
41  /**
42   * @author Olivier Lamy
43   */
44  public class AuthAsyncProxyServlet
45      extends AsyncProxyServlet
46  {
47      private Map<String, String> authentications;
48  
49      private long sleepTime = 0;
50  
51      private Logger log = LoggerFactory.getLogger( getClass() );
52  
53      List<HttpRequest> httpRequests = new ArrayList<HttpRequest>();
54  
55      private File siteTargetPath;
56  
57      /**
58       * Constructor for non authentication servlet.
59       */
60      public AuthAsyncProxyServlet( File siteTargetPath )
61      {
62          super();
63          this.siteTargetPath = siteTargetPath;
64      }
65  
66      /**
67       * Constructor for authentication servlet.
68       * 
69       * @param authentications a map of user/password
70       */
71      public AuthAsyncProxyServlet( Map<String, String> authentications, File siteTargetPath )
72      {
73          this( siteTargetPath );
74  
75          this.authentications = authentications;
76      }
77  
78      /**
79       * Constructor for authentication servlet.
80       * 
81       * @param authentications a map of user/password
82       * @param sleepTime a positive time to sleep the service thread (for timeout)
83       */
84      public AuthAsyncProxyServlet( Map<String, String> authentications, long sleepTime, File siteTargetPath )
85      {
86          this( siteTargetPath );
87  
88          this.authentications = authentications;
89          this.sleepTime = sleepTime;
90      }
91  
92      /** {@inheritDoc} */
93      public void service( ServletRequest req, ServletResponse res )
94          throws ServletException, IOException
95      {
96          final HttpServletRequest request = (HttpServletRequest) req;
97          final HttpServletResponse response = (HttpServletResponse) res;
98  
99          log.info( "handle " );
100 
101         if ( this.authentications != null && !this.authentications.isEmpty() )
102         {
103             String proxyAuthorization = request.getHeader( "Proxy-Authorization" );
104             if ( proxyAuthorization != null && proxyAuthorization.startsWith( "Basic " ) )
105             {
106                 String proxyAuth = proxyAuthorization.substring( 6 );
107                 String authorization = new String(B64Code.decode( proxyAuth ));
108                 String[] authTokens = authorization.split( ":" );
109                 String user = authTokens[0];
110                 String password = authTokens[1];
111 
112                 if ( this.authentications.get( user ) == null )
113                 {
114                     throw new IllegalArgumentException( user + " not found in the map!" );
115                 }
116 
117                 if ( sleepTime > 0 )
118                 {
119                     try
120                     {
121                         Thread.sleep( sleepTime );
122                     }
123                     catch ( InterruptedException e )
124                     {
125                         // nop
126                     }
127                 }
128                 String authPass = this.authentications.get( user );
129                 if ( password.equals( authPass ) )
130                 {
131                     String targetPath = request.getServletPath();
132 
133                     HttpRequest rq = new HttpRequest();
134                     rq.method = request.getMethod();
135                     rq.path = targetPath;
136 
137                     @SuppressWarnings( "rawtypes" )
138                     Enumeration headerNames = request.getHeaderNames();
139                     while ( headerNames.hasMoreElements() )
140                     {
141                         String name = (String) headerNames.nextElement();
142                         rq.headers.put( name, request.getHeader( name ) );
143                     }
144 
145                     httpRequests.add( rq );
146 
147                     if ( request.getMethod().equalsIgnoreCase( "PUT" ) && targetPath != null )
148                     {
149                         File targetFile = new File( siteTargetPath, targetPath );
150                         log.info( "writing file " + targetFile.getPath() );
151                         FileUtils.writeByteArrayToFile( targetFile, IOUtils.toByteArray( request.getInputStream() ) );
152                     }
153 
154                     response.setStatus( HttpServletResponse.SC_OK );
155                     return;
156                 }
157             }
158 
159             // Proxy-Authenticate Basic realm="CCProxy Authorization"
160             response.addHeader( "Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"" );
161             response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
162             return;
163         }
164 
165         super.service( req, res );
166     }
167 }