Skip to content

Commit

Permalink
[ 1917655 ] prepare for the refactoring by eliminating duplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
knaas committed Mar 18, 2008
1 parent d21604a commit 6f20ff9
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 134 deletions.
Expand Up @@ -36,6 +36,13 @@

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 @@ -82,11 +89,6 @@ public void destroy() {

}

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

public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
Expand Down Expand Up @@ -197,4 +199,37 @@ protected boolean sendError(ServletResponse resp, int statusCode,
String message) throws IOException {
return ServletUtils.sendError(resp, statusCode, message);
}

protected ApplicationContext getContext() {
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 @@ -36,6 +36,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.service.IAcknowledgeService;
Expand All @@ -53,9 +54,14 @@ 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 new InternalOutgoingTransport(resp.getOutputStream());
return createOutgoingTransport(createOutputStream(resp));
}

protected OutputStream createOutputStream(HttpServletResponse resp)
Expand All @@ -80,6 +86,7 @@ protected InputStream createInputStream(HttpServletRequest req)
} else {
reader = req.getReader();
}

String line = null;
do {
line = reader.readLine();
Expand Down Expand Up @@ -401,4 +408,29 @@ protected boolean sendError(HttpServletResponse resp, int statusCode,
String message) throws IOException {
return ServletUtils.sendError(resp, statusCode, message);
}

/**
* Returns the parameter with that name, trimmed to null
*
* @param request
* @param name
* @return
*/
protected String getParameter(HttpServletRequest request, String name) {
return StringUtils.trimToNull(request.getParameter(name));
}

/**
* Returns the parameter with that name, trimmed to null. If the trimmed
* string is null, defaults to the defaultValue.
*
* @param request
* @param name
* @return
*/
protected String getParameter(HttpServletRequest request, String name,
String defaultValue) {
return StringUtils.defaultIfEmpty(StringUtils.trimToNull(request
.getParameter(name)), defaultValue);
}
}
Expand Up @@ -30,10 +30,6 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.springframework.context.ApplicationContext;

/**
* This better be the first filter that executes ! TODO: if this thing fails,
Expand All @@ -51,13 +47,8 @@ public void doFilter(ServletRequest req, ServletResponse resp,
return;
}

ApplicationContext ctx = getContext();
INodeService sc = (INodeService) ctx.getBean(Constants.NODE_SERVICE);

if (!sc.isNodeAuthorized(nodeId, securityToken)) {
IRegistrationService registrationService = (IRegistrationService) ctx
.getBean(Constants.REGISTRATION_SERVICE);
if (registrationService.isAutoRegistration()) {
if (!getNodeService().isNodeAuthorized(nodeId, securityToken)) {
if (getRegistrationService().isAutoRegistration()) {
sendError(resp, WebConstants.REGISTRATION_REQUIRED);
} else {
sendError(resp, HttpServletResponse.SC_FORBIDDEN);
Expand Down
Expand Up @@ -35,69 +35,93 @@
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.iterators.EnumerationIterator;

/**
*
* Configured within symmetric-web.xml
*
* <pre>
* &lt;bean id=&quot;compressionFilter&quot;
* class=&quot;org.jumpmind.symmetric.web.CompressionFilter&quot;&gt;
* &lt;property name=&quot;regexPattern&quot; value=&quot;string&quot; /&gt;
* &lt;property name=&quot;regexPatterns&quot;&gt;
* &lt;list&gt;
* &lt;value value=&quot;string&quot;/&gt;
* &lt;list/&gt;
* &lt;property/&gt;
* &lt;property name=&quot;uriPattern&quot; value=&quot;string&quot; /&gt;
* &lt;property name=&quot;uriPatterns&quot;&gt;
* &lt;list&gt;
* &lt;value value=&quot;string&quot;/&gt;
* &lt;list/&gt;
* &lt;property/&gt;
* &lt;property name=&quot;disabled&quot; value=&quot;boolean&quot; /&gt;
* &lt;property name=&quot;compressType&quot; value=&quot;string&quot; /&gt;
* &lt;/bean&gt;
* </pre>
*/
public class CompressionFilter extends AbstractFilter {

private javawebparts.filter.CompressionFilter delegate;
private String compressType;

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (delegate != null) {
delegate.doFilter(request, response, chain);
}
}

@Override
public void destroy() {
super.destroy();
if (delegate != null) {
delegate.destroy();
}
}

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
delegate = new javawebparts.filter.CompressionFilter();

delegate.init(new CompressionFilterConfig(filterConfig));
}

public void setCompressType(String compressType) {
this.compressType = compressType;
}

private final class CompressionFilterConfig implements FilterConfig {
private final FilterConfig filterConfig;
private final List<String> initParameterNames;

@SuppressWarnings("unchecked")
private CompressionFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
initParameterNames = IteratorUtils.toList(new EnumerationIterator(
filterConfig.getInitParameterNames()));
if (compressType != null) {
initParameterNames.add(compressType);
}
}

public String getFilterName() {
return filterConfig.getFilterName();
}

public String getInitParameter(String name) {
if (compressType != null && "compressType".equals(name)) {
return compressType;
}
return filterConfig.getInitParameter(name);
}

public Enumeration<?> getInitParameterNames() {
return Collections.enumeration(initParameterNames);
}

public ServletContext getServletContext() {
return filterConfig.getServletContext();
}
}
private javawebparts.filter.CompressionFilter delegate;
private String compressType;

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (delegate != null) {
delegate.doFilter(request, response, chain);
}
}

@Override
public void destroy() {
super.destroy();
if (delegate != null) {
delegate.destroy();
}
}

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
delegate = new javawebparts.filter.CompressionFilter();

delegate.init(new CompressionFilterConfig(filterConfig));
}

public void setCompressType(String compressType) {
this.compressType = compressType;
}

private final class CompressionFilterConfig implements FilterConfig {
private final FilterConfig filterConfig;
private final List<String> initParameterNames;

@SuppressWarnings("unchecked")
private CompressionFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
initParameterNames = IteratorUtils.toList(new EnumerationIterator(
filterConfig.getInitParameterNames()));
if (compressType != null) {
initParameterNames.add(compressType);
}
}

public String getFilterName() {
return filterConfig.getFilterName();
}

public String getInitParameter(String name) {
if (compressType != null && "compressType".equals(name)) {
return compressType;
}
return filterConfig.getInitParameter(name);
}

public Enumeration<?> getInitParameterNames() {
return Collections.enumeration(initParameterNames);
}

public ServletContext getServletContext() {
return filterConfig.getServletContext();
}
}
}
Expand Up @@ -36,6 +36,30 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
*
* Configured within symmetric-web.xml
*
* <pre>
* &lt;bean id=&quot;nodeConcurrencyFilter&quot;
* class=&quot;org.jumpmind.symmetric.web.NodeConcurrencyFilter&quot;&gt;
* &lt;property name=&quot;regexPattern&quot; value=&quot;string&quot; /&gt;
* &lt;property name=&quot;regexPatterns&quot;&gt;
* &lt;list&gt;
* &lt;value value=&quot;string&quot;/&gt;
* &lt;list/&gt;
* &lt;property/&gt;
* &lt;property name=&quot;uriPattern&quot; value=&quot;string&quot; /&gt;
* &lt;property name=&quot;uriPatterns&quot;&gt;
* &lt;list&gt;
* &lt;value value=&quot;string&quot;/&gt;
* &lt;list/&gt;
* &lt;property/&gt;
* &lt;property name=&quot;disabled&quot; value=&quot;boolean&quot; /&gt;
* &lt;property name=&quot;maxNumberOfConcurrentWorkers&quot; value=&quot;int&quot; /&gt;
* &lt;/bean&gt;
* </pre>
*/
public class NodeConcurrencyFilter extends AbstractFilter {

private static final int TOO_BUSY_LOG_STATEMENTS_PER_MIN = 10;
Expand Down
Expand Up @@ -23,6 +23,8 @@

package org.jumpmind.symmetric.web;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -49,7 +51,7 @@ public void handleGet(HttpServletRequest req, HttpServletResponse resp)
protected void handlePost(HttpServletRequest req, HttpServletResponse resp)
throws Exception {

String nodeId = req.getParameter(WebConstants.NODE_ID);
String nodeId = getParameter(req, WebConstants.NODE_ID);

if (logger.isDebugEnabled()) {
logger
Expand All @@ -62,20 +64,19 @@ protected void handlePost(HttpServletRequest req, HttpServletResponse resp)
"Node must be specified");
return;
}

nodeId = nodeId.trim();
OutputStream outputStream = createOutputStream(resp);
INodeService nodeService = getNodeService();
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeId);
if (nodeSecurity != null) {
if (nodeSecurity.isRegistrationEnabled()) {
getRegistrationService().registerNode(
nodeService.findNode(nodeId), resp.getOutputStream());
nodeService.findNode(nodeId), outputStream);
} else {
if (nodeSecurity.isInitialLoadEnabled()) {
getDataService().insertReloadEvent(
nodeService.findNode(nodeId));
}
IOutgoingTransport out = createOutgoingTransport(resp);
IOutgoingTransport out = createOutgoingTransport(outputStream);
getDataExtractorService().extract(nodeService.findNode(nodeId),
out);
out.close();
Expand Down
Expand Up @@ -42,7 +42,7 @@ public class PushServlet extends AbstractServlet {
protected void handlePut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String nodeId = req.getParameter(WebConstants.NODE_ID);
String nodeId = getParameter(req, WebConstants.NODE_ID);

if (logger.isDebugEnabled()) {
logger
Expand Down

0 comments on commit 6f20ff9

Please sign in to comment.