1 package org.apache.maven.plugins.site.deploy;
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.*;
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
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
59
60 public AuthAsyncProxyServlet( File siteTargetPath )
61 {
62 super();
63 this.siteTargetPath = siteTargetPath;
64 }
65
66
67
68
69
70
71 public AuthAsyncProxyServlet( Map<String, String> authentications, File siteTargetPath )
72 {
73 this( siteTargetPath );
74
75 this.authentications = authentications;
76 }
77
78
79
80
81
82
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
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
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
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 }