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.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.Enumeration;
31  import java.util.List;
32  
33  import org.eclipse.jetty.server.Handler;
34  import org.eclipse.jetty.server.Request;
35  import org.eclipse.jetty.server.Server;
36  import org.eclipse.jetty.server.handler.AbstractHandler;
37  import org.eclipse.jetty.servlet.ServletContextHandler;
38  import org.eclipse.jetty.servlet.ServletHolder;
39  
40  /**
41   * @author Olivier Lamy
42   * @since 3.0-beta-2
43   *
44   */
45  public class SimpleDavServerHandler {
46  
47      private Server server;
48  
49      private File siteTargetPath;
50  
51      List<HttpRequest> httpRequests = new ArrayList<>();
52  
53      public SimpleDavServerHandler(final File targetPath) throws Exception {
54          this.siteTargetPath = targetPath;
55          Handler repoHandler = new AbstractHandler() {
56              @Override
57              public void handle(String target, Request r, HttpServletRequest request, HttpServletResponse response)
58                      throws IOException, ServletException {
59                  String targetPath = request.getPathInfo();
60  
61                  HttpRequest rq = new HttpRequest();
62                  rq.method = request.getMethod();
63                  rq.path = targetPath;
64  
65                  @SuppressWarnings("rawtypes")
66                  Enumeration headerNames = request.getHeaderNames();
67                  while (headerNames.hasMoreElements()) {
68                      String name = (String) headerNames.nextElement();
69                      rq.headers.put(name, request.getHeader(name));
70                  }
71  
72                  httpRequests.add(rq);
73  
74                  if (request.getMethod().equalsIgnoreCase("PUT")) {
75                      File targetFile = new File(siteTargetPath, targetPath);
76                      targetFile.getParentFile().mkdirs();
77                      Files.copy(request.getInputStream(), targetFile.toPath());
78                  }
79  
80                  // PrintWriter writer = response.getWriter();
81  
82                  response.setStatus(HttpServletResponse.SC_OK);
83  
84                  ((Request) request).setHandled(true);
85              }
86          };
87          server = new Server(0);
88          server.setHandler(repoHandler);
89          server.start();
90      }
91  
92      public SimpleDavServerHandler(Servlet servlet) throws Exception {
93          siteTargetPath = null;
94          server = new Server(0);
95          ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
96          context.setContextPath("/");
97          server.setHandler(context);
98          context.addServlet(new ServletHolder(servlet), "/");
99  
100         server.start();
101     }
102 
103     public int getPort() {
104         return server.getURI().getPort();
105     }
106 
107     public void stop() throws Exception {
108         server.stop();
109     }
110 }