View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
5    *               Eric Long <erilong@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.OutputStream;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.commons.lang.StringUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.jumpmind.symmetric.model.Node;
36  import org.jumpmind.symmetric.transport.handler.RegistrationResourceHandler;
37  
38  public class RegistrationServlet extends AbstractTransportResourceServlet<RegistrationResourceHandler> {
39  
40      private static final long serialVersionUID = 1L;
41  
42      protected static final Log logger = LogFactory.getLog(RegistrationServlet.class);
43  
44      @Override
45      public boolean isContainerCompatible() {
46          return true;
47      }
48  
49      @Override
50      protected void handleGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
51          Node node = transform(req);
52  
53          OutputStream outputStream = createOutputStream(resp);
54          if (!getTransportResourceHandler().registerNode(node, outputStream)) {
55              if (logger.isWarnEnabled()) {
56                  logger.warn(String.format("%s was not allowed to register.", node));
57              }
58              sendError(resp, WebConstants.REGISTRATION_NOT_OPEN, String.format("%s was not allowed to register.", node));
59          }
60      }
61  
62      private Node transform(HttpServletRequest req) {
63          Node node = new Node();
64          node.setNodeGroupId(getParameter(req, WebConstants.NODE_GROUP_ID));
65          node.setSymmetricVersion(getParameter(req, WebConstants.SYMMETRIC_VERSION));
66          node.setExternalId(getParameter(req, WebConstants.EXTERNAL_ID));
67          String syncUrlString = getParameter(req, WebConstants.SYNC_URL);
68          if (StringUtils.isNotBlank(syncUrlString)) {
69              node.setSyncURL(syncUrlString);
70          }
71          node.setSchemaVersion(getParameter(req, WebConstants.SCHEMA_VERSION));
72          node.setDatabaseType(getParameter(req, WebConstants.DATABASE_TYPE));
73          node.setDatabaseVersion(getParameter(req, WebConstants.DATABASE_VERSION));
74          return node;
75      }
76  
77      @Override
78      protected Log getLogger() {
79          return logger;
80      }
81  
82  }