View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.deploy;
20  
21  import javax.servlet.Servlet;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Enumeration;
30  import java.util.List;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.io.IOUtils;
34  import org.eclipse.jetty.server.Handler;
35  import org.eclipse.jetty.server.Request;
36  import org.eclipse.jetty.server.Server;
37  import org.eclipse.jetty.server.handler.AbstractHandler;
38  import org.eclipse.jetty.servlet.ServletContextHandler;
39  import org.eclipse.jetty.servlet.ServletHolder;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * @author Olivier Lamy
45   * @since 3.0-beta-2
46   *
47   */
48  public class SimpleDavServerHandler {
49  
50      private Logger log = LoggerFactory.getLogger(getClass());
51  
52      private Server server;
53  
54      private File siteTargetPath;
55  
56      List<HttpRequest> httpRequests = new ArrayList<>();
57  
58      public SimpleDavServerHandler(final File targetPath) throws Exception {
59          this.siteTargetPath = targetPath;
60          Handler repoHandler = new AbstractHandler() {
61              public void handle(String target, Request r, HttpServletRequest request, HttpServletResponse response)
62                      throws IOException, ServletException {
63                  String targetPath = request.getPathInfo();
64  
65                  HttpRequest rq = new HttpRequest();
66                  rq.method = request.getMethod();
67                  rq.path = targetPath;
68  
69                  @SuppressWarnings("rawtypes")
70                  Enumeration headerNames = request.getHeaderNames();
71                  while (headerNames.hasMoreElements()) {
72                      String name = (String) headerNames.nextElement();
73                      rq.headers.put(name, request.getHeader(name));
74                  }
75  
76                  httpRequests.add(rq);
77  
78                  if (request.getMethod().equalsIgnoreCase("PUT")) {
79                      File targetFile = new File(siteTargetPath, targetPath);
80                      log.info("writing file " + targetFile.getPath());
81                      FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
82                  }
83  
84                  // PrintWriter writer = response.getWriter();
85  
86                  response.setStatus(HttpServletResponse.SC_OK);
87  
88                  ((Request) request).setHandled(true);
89              }
90          };
91          server = new Server(0);
92          server.setHandler(repoHandler);
93          server.start();
94      }
95  
96      public SimpleDavServerHandler(Servlet servlet) throws Exception {
97          siteTargetPath = null;
98          server = new Server(0);
99          ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
100         context.setContextPath("/");
101         server.setHandler(context);
102         context.addServlet(new ServletHolder(servlet), "/");
103 
104         server.start();
105     }
106 
107     public int getPort() {
108         return server.getURI().getPort();
109     }
110 
111     public void stop() throws Exception {
112         server.stop();
113     }
114 }