View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
5    *               Andrew Wilcox <andrewbwilcox@users.sourceforge.net>,
6    *               Keith Naas <knaas@users.sourceforge.net>
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 3 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, see
20   * <http://www.gnu.org/licenses/>.
21   */
22  
23  package org.jumpmind.symmetric.web;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.jumpmind.symmetric.transport.handler.PushResourceHandler;
36  
37  public class PushServlet extends AbstractTransportResourceServlet<PushResourceHandler> {
38      private static final long serialVersionUID = 1L;
39  
40      private static final Log logger = LogFactory.getLog(PushServlet.class);
41  
42      @Override
43      public boolean isContainerCompatible() {
44          return true;
45      }
46  
47      @Override
48      protected void handleHead(HttpServletRequest req, HttpServletResponse resp) throws Exception {
49          // HTTP OK
50      }
51  
52      @Override
53      protected void handlePut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54  
55          String nodeId = getParameter(req, WebConstants.NODE_ID);
56  
57          if (logger.isDebugEnabled()) {
58              logger.debug(String.format("Push request received from %s", nodeId));
59          }
60  
61          InputStream inputStream = createInputStream(req);
62          OutputStream outputStream = createOutputStream(resp);
63  
64          getTransportResourceHandler().push(inputStream, outputStream);
65  
66          outputStream.flush(); // TODO: why is this necessary?
67  
68          if (logger.isDebugEnabled()) {
69              logger.debug(String.format("Done with Push request from %s", nodeId));
70          }
71      }
72  
73      @Override
74      protected Log getLogger() {
75          return logger;
76      }
77  
78  }