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.ArrayList;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  
31  import javax.servlet.Servlet;
32  import javax.servlet.ServletException;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.commons.io.FileUtils;
37  import org.apache.commons.io.IOUtils;
38  import org.apache.commons.lang3.SystemUtils;
39  import org.eclipse.jetty.server.Handler;
40  import org.eclipse.jetty.server.Request;
41  import org.eclipse.jetty.server.Server;
42  import org.eclipse.jetty.server.handler.AbstractHandler;
43  import org.eclipse.jetty.servlet.ServletContextHandler;
44  import org.eclipse.jetty.servlet.ServletContextHandler.Context;
45  import org.eclipse.jetty.servlet.ServletHolder;
46  import org.apache.maven.plugins.site.deploy.HttpRequest;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  /**
51   * @author Olivier Lamy
52   * @since 3.0-beta-2
53   *
54   */
55  public class SimpleDavServerHandler
56  {
57      
58      private Logger log = LoggerFactory.getLogger( getClass() );
59      
60      private Server server;
61      
62      private File siteTargetPath;
63      
64      List<HttpRequest> httpRequests = new ArrayList<HttpRequest>();
65      
66      public SimpleDavServerHandler(final File targetPath )
67          throws Exception
68      {
69          this.siteTargetPath = targetPath;
70          Handler repoHandler = new AbstractHandler()
71          {
72              public void handle( String target, Request r, HttpServletRequest request, HttpServletResponse response )
73                  throws IOException, ServletException
74              {
75                  String targetPath = request.getPathInfo();
76                  
77                  HttpRequest rq = new HttpRequest();
78                  rq.method = request.getMethod();
79                  rq.path = targetPath;
80  
81                  @SuppressWarnings( "rawtypes" )
82                  Enumeration headerNames = request.getHeaderNames();
83                  while ( headerNames.hasMoreElements() )
84                  {
85                      String name = (String) headerNames.nextElement();
86                      rq.headers.put( name, request.getHeader( name ) );
87                  }
88                  
89                  httpRequests.add( rq );
90                  
91                
92                  if ( request.getMethod().equalsIgnoreCase( "PUT" ) )
93                  {
94                      File targetFile = new File( siteTargetPath, targetPath );
95                      log.info( "writing file " + targetFile.getPath() );
96                      FileUtils.writeByteArrayToFile( targetFile, IOUtils.toByteArray( request.getInputStream() ) );
97                  }
98                  
99                  //PrintWriter writer = response.getWriter();
100 
101                 response.setStatus( HttpServletResponse.SC_OK );
102 
103                 ( (Request) request ).setHandled( true );
104             }
105         };
106         server = new Server( 0 );
107         server.setHandler( repoHandler );
108         server.start();
109 
110     }
111 
112     public SimpleDavServerHandler( Servlet servlet )
113         throws Exception
114     {
115         siteTargetPath = null;
116         server = new Server( 0 );
117         ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
118         context.setContextPath("/");
119         server.setHandler(context);
120         context.addServlet( new ServletHolder( servlet ), "/" );
121 
122         server.start();
123     }
124     
125     public int getPort()
126     {
127         return server.getURI().getPort();
128     }
129 
130     public void stop()
131         throws Exception
132     {
133         server.stop();
134     }
135     
136     
137 
138 }