View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    * Copyright (C) Keith Naas <knaas@users.sourceforge.net>
6    * Copyright (C) Eric Long <erilong@user.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.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.jumpmind.symmetric.model.BatchInfo;
37  import org.jumpmind.symmetric.transport.AbstractTransportManager;
38  import org.jumpmind.symmetric.transport.handler.AckResourceHandler;
39  
40  public class AckServlet extends AbstractTransportResourceServlet<AckResourceHandler> {
41  
42      private static final BatchIdComparator BATCH_ID_COMPARATOR = new BatchIdComparator();
43  
44      private static final long serialVersionUID = 1L;
45  
46      protected static final Log logger = LogFactory.getLog(AckServlet.class);
47  
48      @Override
49      public boolean isContainerCompatible() {
50          return true;
51      }
52  
53      @SuppressWarnings("unchecked")
54      @Override
55      protected void handlePost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
56  
57          AckResourceHandler ackService = getTransportResourceHandler();
58          if (logger.isDebugEnabled()) {
59              logger.debug("Reading ack: " + req.getParameterMap());
60          }
61          // TODO: fix this; the servlets need to participate in the transport API
62          List<BatchInfo> batches = AbstractTransportManager.readAcknowledgement(req.getParameterMap());
63          Collections.sort(batches, BATCH_ID_COMPARATOR);
64          ackService.ack(batches);
65      }
66  
67      private static class BatchIdComparator implements Comparator<BatchInfo> {
68          public int compare(BatchInfo batchInfo1, BatchInfo batchInfo2) {
69              Long batchId1 = batchInfo1.getBatchId();
70              Long batchId2 = batchInfo1.getBatchId();
71              return batchId1.compareTo(batchId2);
72          }
73      }
74  
75      @Override
76      protected Log getLogger() {
77          return logger;
78      }
79  }