1 package org.apache.maven.plugins.site;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
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
63
64 public AuthAsyncProxyServlet( File siteTargetPath )
65 {
66 super();
67 this.siteTargetPath = siteTargetPath;
68 }
69
70
71
72
73
74
75 public AuthAsyncProxyServlet( Map<String, String> authentications, File siteTargetPath )
76 {
77 this( siteTargetPath );
78
79 this.authentications = authentications;
80 }
81
82
83
84
85
86
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
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
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
159
160 response.setStatus( HttpServletResponse.SC_OK );
161 return;
162 }
163 }
164
165
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 }