View Javadoc

1   package org.apache.maven.plugins.site;
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.ArrayList;
25  import java.util.Enumeration;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.ServletRequest;
31  import javax.servlet.ServletResponse;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.commons.io.FileUtils;
36  import org.apache.commons.io.IOUtils;
37  import org.apache.maven.plugins.site.SimpleDavServerHandler.HttpRequest;
38  import org.mortbay.jetty.security.B64Code;
39  import org.mortbay.proxy.AsyncProxyServlet;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * @author <a href="mailto:olamy@apache.org">olamy</a>
45   * @since 
46   * @version $Id: AuthAsyncProxyServlet.html 816568 2012-05-08 12:09:38Z hboutemy $
47   */
48  public class AuthAsyncProxyServlet
49      extends AsyncProxyServlet
50  {
51      private Map<String, String> authentications;
52  
53      private long sleepTime = 0;
54  
55      private Logger log = LoggerFactory.getLogger( getClass() );
56  
57      List<HttpRequest> httpRequests = new ArrayList<HttpRequest>();
58  
59      private File siteTargetPath;
60  
61      /**
62       * Constructor for non authentication servlet.
63       */
64      public AuthAsyncProxyServlet( File siteTargetPath )
65      {
66          super();
67          this.siteTargetPath = siteTargetPath;
68      }
69  
70      /**
71       * Constructor for authentication servlet.
72       * 
73       * @param authentications a map of user/password
74       */
75      public AuthAsyncProxyServlet( Map<String, String> authentications, File siteTargetPath )
76      {
77          this( siteTargetPath );
78  
79          this.authentications = authentications;
80      }
81  
82      /**
83       * Constructor for authentication servlet.
84       * 
85       * @param authentications a map of user/password
86       * @param sleepTime a positive time to sleep the service thread (for timeout)
87       */
88      public AuthAsyncProxyServlet( Map<String, String> authentications, long sleepTime, File siteTargetPath )
89      {
90          this( siteTargetPath );
91  
92          this.authentications = authentications;
93          this.sleepTime = sleepTime;
94      }
95  
96      /** {@inheritDoc} */
97      public void service( ServletRequest req, ServletResponse res )
98          throws ServletException, IOException
99      {
100         final HttpServletRequest request = (HttpServletRequest) req;
101         final HttpServletResponse response = (HttpServletResponse) res;
102 
103         log.info( "handle " );
104 
105         if ( this.authentications != null && !this.authentications.isEmpty() )
106         {
107             String proxyAuthorization = request.getHeader( "Proxy-Authorization" );
108             if ( proxyAuthorization != null && proxyAuthorization.startsWith( "Basic " ) )
109             {
110                 String proxyAuth = proxyAuthorization.substring( 6 );
111                 String authorization = B64Code.decode( proxyAuth );
112                 String[] authTokens = authorization.split( ":" );
113                 String user = authTokens[0];
114                 String password = authTokens[1];
115 
116                 if ( this.authentications.get( user ) == null )
117                 {
118                     throw new IllegalArgumentException( user + " not found in the map!" );
119                 }
120 
121                 if ( sleepTime > 0 )
122                 {
123                     try
124                     {
125                         Thread.sleep( sleepTime );
126                     }
127                     catch ( InterruptedException e )
128                     {
129                         // nop
130                     }
131                 }
132                 String authPass = this.authentications.get( user ).toString();
133                 if ( password.equals( authPass ) )
134                 {
135                     String targetPath = request.getServletPath();
136 
137                     HttpRequest rq = new HttpRequest();
138                     rq.method = request.getMethod();
139                     rq.path = targetPath;
140 
141                     @SuppressWarnings( "rawtypes" )
142                     Enumeration headerNames = request.getHeaderNames();
143                     while ( headerNames.hasMoreElements() )
144                     {
145                         String name = (String) headerNames.nextElement();
146                         rq.headers.put( name, request.getHeader( name ) );
147                     }
148 
149                     httpRequests.add( rq );
150 
151                     if ( request.getMethod().equalsIgnoreCase( "PUT" ) && targetPath != null )
152                     {
153                         File targetFile = new File( siteTargetPath, targetPath );
154                         log.info( "writing file " + targetFile.getPath() );
155                         FileUtils.writeByteArrayToFile( targetFile, IOUtils.toByteArray( request.getInputStream() ) );
156                     }
157 
158                     //PrintWriter writer = response.getWriter();
159 
160                     response.setStatus( HttpServletResponse.SC_OK );
161                     return;
162                 }
163             }
164 
165             // Proxy-Authenticate Basic realm="CCProxy Authorization"
166             response.addHeader( "Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"" );
167             response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
168             return;
169         }
170 
171         super.service( req, res );
172     }
173 }