Skip to content

Commit

Permalink
[ 1917655 ] prepare for the refactoring by moving servlet logic to Re…
Browse files Browse the repository at this point in the history
…sourceHandlers
  • Loading branch information
knaas committed Mar 18, 2008
1 parent ed1fb09 commit 841d051
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 118 deletions.
Expand Up @@ -36,13 +36,6 @@

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.service.IAcknowledgeService;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

Expand Down Expand Up @@ -200,36 +193,9 @@ protected boolean sendError(ServletResponse resp, int statusCode,
return ServletUtils.sendError(resp, statusCode, message);
}

protected ApplicationContext getContext() {
protected ApplicationContext getApplicationContext() {
return WebApplicationContextUtils
.getWebApplicationContext(getServletContext());
}

protected IDataLoaderService getDataLoaderService() {
return (IDataLoaderService) getContext().getBean(
Constants.DATALOADER_SERVICE);
}

protected IDataService getDataService() {
return (IDataService) getContext().getBean(Constants.DATA_SERVICE);
}

protected INodeService getNodeService() {
return (INodeService) getContext().getBean(Constants.NODE_SERVICE);
}

protected IRegistrationService getRegistrationService() {
return (IRegistrationService) getContext().getBean(
Constants.REGISTRATION_SERVICE);
}

protected IDataExtractorService getDataExtractorService() {
return (IDataExtractorService) getContext().getBean(
Constants.DATAEXTRACTOR_SERVICE);
}

protected IAcknowledgeService getAcknowledgeService() {
return (IAcknowledgeService) getContext().getBean(
Constants.ACKNOWLEDGE_SERVICE);
}
}
Expand Up @@ -38,32 +38,13 @@

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.service.IAcknowledgeService;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.transport.IOutgoingTransport;
import org.jumpmind.symmetric.transport.internal.InternalOutgoingTransport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

abstract public class AbstractServlet extends HttpServlet {

protected abstract Log getLogger();

protected IOutgoingTransport createOutgoingTransport(
OutputStream outputStream) throws IOException {
return new InternalOutgoingTransport(outputStream);
}

protected IOutgoingTransport createOutgoingTransport(
HttpServletResponse resp) throws IOException {
return createOutgoingTransport(createOutputStream(resp));
}

protected OutputStream createOutputStream(HttpServletResponse resp)
throws IOException {
return resp.getOutputStream();
Expand Down Expand Up @@ -108,39 +89,11 @@ protected InputStream createInputStream(HttpServletRequest req)
return is;
}

protected ApplicationContext getContext() {
protected ApplicationContext getApplicationContext() {
return WebApplicationContextUtils
.getWebApplicationContext(getServletContext());
}

protected IDataLoaderService getDataLoaderService() {
return (IDataLoaderService) getContext().getBean(
Constants.DATALOADER_SERVICE);
}

protected IDataService getDataService() {
return (IDataService) getContext().getBean(Constants.DATA_SERVICE);
}

protected INodeService getNodeService() {
return (INodeService) getContext().getBean(Constants.NODE_SERVICE);
}

protected IRegistrationService getRegistrationService() {
return (IRegistrationService) getContext().getBean(
Constants.REGISTRATION_SERVICE);
}

protected IDataExtractorService getDataExtractorService() {
return (IDataExtractorService) getContext().getBean(
Constants.DATAEXTRACTOR_SERVICE);
}

protected IAcknowledgeService getAcknowledgeService() {
return (IAcknowledgeService) getContext().getBean(
Constants.ACKNOWLEDGE_SERVICE);
}

@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Expand Down
@@ -0,0 +1,43 @@
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
* Eric Long <erilong@users.sourceforge.net>,
* Keith Naas <knaas@users.sourceforge.net>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/

package org.jumpmind.symmetric.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.jumpmind.symmetric.model.BatchInfo;
import org.springframework.context.ApplicationContext;

public class AckResourceHandler extends ResourceHandler {
public AckResourceHandler(ApplicationContext context,
InputStream inputStream, OutputStream outputStream) {
super(context, inputStream, outputStream);
}

public void ack(List<BatchInfo> batches) throws IOException {
getAcknowledgeService().ack(batches);
}
}
Expand Up @@ -77,7 +77,8 @@ protected void handlePost(HttpServletRequest req, HttpServletResponse resp)
}
}
}
getAcknowledgeService().ack(batches);
new AckResourceHandler(this.getApplicationContext(), req
.getInputStream(), resp.getOutputStream()).ack(batches);
}

private String getBatchIdFrom(String webParameter) {
Expand Down
Expand Up @@ -113,13 +113,13 @@ private String formatDate(Date date) {
}

private List<IncomingBatch> findIncomingBatchErrors() {
IIncomingBatchService incomingBatchService = (IIncomingBatchService) getContext()
IIncomingBatchService incomingBatchService = (IIncomingBatchService) getApplicationContext()
.getBean(Constants.INCOMING_BATCH_SERVICE);
return incomingBatchService.findIncomingBatchErrors(MAX_ERRORS);
}

private List<OutgoingBatch> findOutgoingBatchErrors() {
IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getContext()
IOutgoingBatchService outgoingBatchService = (IOutgoingBatchService) getApplicationContext()
.getBean(Constants.OUTGOING_BATCH_SERVICE);
return outgoingBatchService.getOutgoingBatcheErrors(MAX_ERRORS);
}
Expand Down
Expand Up @@ -30,13 +30,18 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IRegistrationService;

/**
* This better be the first filter that executes ! TODO: if this thing fails,
* should it prevent further processing of the request?
*
*/
public class AuthenticationFilter extends AbstractFilter {
private INodeService nodeService;
private IRegistrationService registrationService;

public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
String securityToken = req.getParameter(WebConstants.SECURITY_TOKEN);
Expand All @@ -47,8 +52,8 @@ public void doFilter(ServletRequest req, ServletResponse resp,
return;
}

if (!getNodeService().isNodeAuthorized(nodeId, securityToken)) {
if (getRegistrationService().isAutoRegistration()) {
if (!nodeService.isNodeAuthorized(nodeId, securityToken)) {
if (registrationService.isAutoRegistration()) {
sendError(resp, WebConstants.REGISTRATION_REQUIRED);
} else {
sendError(resp, HttpServletResponse.SC_FORBIDDEN);
Expand All @@ -58,4 +63,12 @@ public void doFilter(ServletRequest req, ServletResponse resp,

chain.doFilter(req, resp);
}

public void setNodeService(INodeService nodeService) {
this.nodeService = nodeService;
}

public void setRegistrationService(IRegistrationService registrationService) {
this.registrationService = registrationService;
}
}
@@ -0,0 +1,68 @@
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
* Eric Long <erilong@users.sourceforge.net>,
* Keith Naas <knaas@users.sourceforge.net>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/

package org.jumpmind.symmetric.web;

import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.model.NodeSecurity;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.transport.IOutgoingTransport;
import org.springframework.context.ApplicationContext;

public class PullResourceHandler extends ResourceHandler {
private static final Log logger = LogFactory
.getLog(PullResourceHandler.class);

public PullResourceHandler(ApplicationContext context,
InputStream inputStream, OutputStream outputStream) {
super(context, inputStream, outputStream);
}

public void pull(String nodeId) throws Exception {
INodeService nodeService = getNodeService();
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeId);
if (nodeSecurity != null) {
if (nodeSecurity.isRegistrationEnabled()) {
getRegistrationService().registerNode(
nodeService.findNode(nodeId), outputStream);
} else {
if (nodeSecurity.isInitialLoadEnabled()) {
getDataService().insertReloadEvent(
nodeService.findNode(nodeId));
}
IOutgoingTransport outgoingTransport = createOutgoingTransport(outputStream);
getDataExtractorService().extract(nodeService.findNode(nodeId),
outgoingTransport);
outgoingTransport.close();
}
} else {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Node %s does not exist.", nodeId));
}
}
}
}
Expand Up @@ -23,6 +23,7 @@

package org.jumpmind.symmetric.web;

import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -31,9 +32,6 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.model.NodeSecurity;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.transport.IOutgoingTransport;

public class PullServlet extends AbstractServlet {

Expand Down Expand Up @@ -64,28 +62,11 @@ protected void handlePost(HttpServletRequest req, HttpServletResponse resp)
"Node must be specified");
return;
}
InputStream inputStream = createInputStream(req);
OutputStream outputStream = createOutputStream(resp);
INodeService nodeService = getNodeService();
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeId);
if (nodeSecurity != null) {
if (nodeSecurity.isRegistrationEnabled()) {
getRegistrationService().registerNode(
nodeService.findNode(nodeId), outputStream);
} else {
if (nodeSecurity.isInitialLoadEnabled()) {
getDataService().insertReloadEvent(
nodeService.findNode(nodeId));
}
IOutgoingTransport out = createOutgoingTransport(outputStream);
getDataExtractorService().extract(nodeService.findNode(nodeId),
out);
out.close();
}
} else {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Node %s does not exist.", nodeId));
}
}
new PullResourceHandler(getApplicationContext(), inputStream,
outputStream).pull(nodeId);

if (logger.isDebugEnabled()) {
logger.debug(String
.format("Done with Pull request from %s", nodeId));
Expand Down

0 comments on commit 841d051

Please sign in to comment.