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