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.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.lang.SystemUtils;
39  import org.mortbay.jetty.Handler;
40  import org.mortbay.jetty.Request;
41  import org.mortbay.jetty.Server;
42  import org.mortbay.jetty.handler.AbstractHandler;
43  import org.mortbay.jetty.servlet.Context;
44  import org.mortbay.jetty.servlet.ServletHolder;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * @author <a href="mailto:olamy@apache.org">olamy</a>
50   * @since 3.0-beta-2
51   * @version $Id: SimpleDavServerHandler.html 816568 2012-05-08 12:09:38Z hboutemy $
52   */
53  public class SimpleDavServerHandler
54  {
55      
56      private Logger log = LoggerFactory.getLogger( getClass() );
57      
58      private Server server;
59      
60      private File siteTargetPath;
61      
62      List<HttpRequest> httpRequests = new ArrayList<HttpRequest>();
63      
64      public SimpleDavServerHandler(final File targetPath )
65          throws Exception
66      {
67          this.siteTargetPath = targetPath;
68          Handler repoHandler = new AbstractHandler()
69          {
70              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
71                  throws IOException, ServletException
72              {
73                  String targetPath = request.getPathInfo();
74                  
75                  HttpRequest rq = new HttpRequest();
76                  rq.method = request.getMethod();
77                  rq.path = targetPath;
78  
79                  @SuppressWarnings( "rawtypes" )
80                  Enumeration headerNames = request.getHeaderNames();
81                  while (headerNames.hasMoreElements())
82                  {
83                      String name = (String) headerNames.nextElement();
84                      rq.headers.put( name, request.getHeader( name ) );
85                  }
86                  
87                  httpRequests.add( rq );
88                  
89                
90                  if ( request.getMethod().equalsIgnoreCase( "PUT" ) )
91                  {
92                      File targetFile = new File( siteTargetPath, targetPath );
93                      log.info( "writing file " + targetFile.getPath() );
94                      FileUtils.writeByteArrayToFile( targetFile, IOUtils.toByteArray( request.getInputStream() ) );
95                  }
96                  
97                  //PrintWriter writer = response.getWriter();
98  
99                  response.setStatus( HttpServletResponse.SC_OK );
100 
101                 ( (Request) request ).setHandled( true );
102             }
103         };
104         server = new Server( 0 );
105         server.setHandler( repoHandler );
106         server.start();
107 
108     }
109 
110     public SimpleDavServerHandler( Servlet servlet )
111         throws Exception
112     {
113         siteTargetPath = null;
114         server = new Server( 0 );
115         Context context = new Context( server, "/", 0 );
116 
117         context.addServlet( new ServletHolder( servlet ), "/" );
118         
119         server.start();
120     }   
121     
122     public int getPort()
123     {
124         return server.getConnectors()[0].getLocalPort();
125     }
126 
127     public void stop()
128         throws Exception
129     {
130         server.stop();
131     }
132     
133     
134     static class HttpRequest
135     {
136         Map<String, String> headers = new HashMap<String,String>();
137         
138         String method;
139         
140         String path;
141         
142         HttpRequest()
143         {
144             // nop
145         }
146 
147         @Override
148         public String toString()
149         {
150             StringBuilder sb = new StringBuilder( method ).append( " path " ).append( path )
151                 .append( SystemUtils.LINE_SEPARATOR );
152             for ( Entry<String, String> entry : headers.entrySet() )
153             {
154                 sb.append( entry.getKey() ).append( " : " ).append( entry.getValue() )
155                     .append( SystemUtils.LINE_SEPARATOR );
156             }
157             return sb.toString();
158         }
159     }
160 }