├── .gitignore ├── README.md ├── components ├── pom.xml └── src │ └── main │ └── java │ └── ru │ └── concerteza │ └── springtomcat │ └── components │ ├── access │ ├── AccessConsumer.java │ ├── AccessEvent.java │ └── AccessLogFilter.java │ ├── bruteforce │ ├── BruteforceBlockerAuthenticationManager.java │ └── BruteforceSuspectedException.java │ ├── holder │ ├── SessionHolder.java │ └── SessionHolderFilter.java │ └── registry │ ├── SessionIdRegistryFilter.java │ ├── SessionRegistry.java │ ├── SessionRegistryListener.java │ └── concurrent │ ├── ConcurrentSessionException.java │ ├── ConcurrentSessionStrategy.java │ ├── InvalidateExistedStrategy.java │ ├── OneSessionAllowedStrategy.java │ └── RemoteIpFilter.java ├── etomcat6-test ├── pom.xml └── src │ ├── main │ ├── app-dirs │ │ ├── accessdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── annotationdir │ │ │ └── conf │ │ │ │ └── web.xml │ │ ├── appdir │ │ │ └── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ └── web.xml │ │ ├── bruteforcedir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── holderdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── nosslappdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── registrydir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── webappdir │ │ │ ├── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ └── web.xml │ │ │ └── static │ │ │ │ └── static.html │ │ ├── x509annotationdir │ │ │ └── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ ├── truststore.jks │ │ │ │ └── web.xml │ │ └── x509dir │ │ │ └── conf │ │ │ ├── concerteza.crt │ │ │ ├── etomcat-test-ctx.xml │ │ │ ├── etomcat.p12 │ │ │ ├── etomcat.properties │ │ │ ├── truststore.jks │ │ │ └── web.xml │ └── java │ │ └── ru │ │ └── concerteza │ │ └── springtomcat │ │ └── etomcat6 │ │ ├── components │ │ ├── HolderServlet.java │ │ ├── RegistryServlet.java │ │ ├── TestFilter.java │ │ ├── TestListener.java │ │ ├── TestLogConsumer.java │ │ └── TestServlet.java │ │ └── x509 │ │ ├── AuthUserDetailService.java │ │ ├── SecuredService.java │ │ └── X509Servlet.java │ └── test │ ├── java │ └── ru │ │ └── concerteza │ │ └── springtomcat │ │ └── etomcat6 │ │ ├── AccessLogTest.java │ │ ├── AnnotationConfigTest.java │ │ ├── AnnotationTestSupertype.java │ │ ├── AppTest.java │ │ ├── BruteforceTest.java │ │ ├── HolderTest.java │ │ ├── NoSslTest.java │ │ ├── RegistryTest.java │ │ ├── SslHelper.java │ │ ├── TestSupertype.java │ │ ├── WebAppTest.java │ │ ├── X509AnnotationTest.java │ │ └── X509Test.java │ └── resources │ ├── client-truststore.jks │ ├── etomcat_client.p12 │ ├── global-metod-security-enabler.xml │ └── log4j.properties ├── etomcat6 ├── pom.xml └── src │ └── main │ └── java │ └── ru │ └── concerteza │ └── springtomcat │ └── etomcat6 │ ├── EmbeddedContextConfig.java │ ├── EmbeddedDirContext.java │ ├── EmbeddedLoader.java │ ├── EmbeddedManager.java │ ├── EmbeddedTomcat.java │ ├── EmbeddedWrapper.java │ ├── ExecutorState.java │ ├── config │ ├── ConnectorProperties.java │ ├── ContextProperties.java │ ├── ExecutorProperties.java │ ├── FsProperties.java │ ├── GeneralProperties.java │ ├── HostProperties.java │ ├── NioProperties.java │ ├── SocketProperties.java │ └── SslProperties.java │ ├── context │ ├── EmbeddedAnnotationSpringContext.java │ ├── EmbeddedSpringContext.java │ └── EmbeddedXmlSpringContext.java │ ├── failfast │ ├── FailFastConnector.java │ ├── FailFastProxyOutputStream.java │ ├── FailFastResponse.java │ ├── FailFastResponseWriter.java │ └── SneakThrower.java │ └── valves │ └── PostCharacterEncodingValve.java ├── etomcat8-test ├── pom.xml └── src │ ├── main │ ├── app-dirs │ │ ├── accessdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── annotationdir │ │ │ └── conf │ │ │ │ └── web.xml │ │ ├── appdir │ │ │ └── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ └── web.xml │ │ ├── bruteforcedir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── holderdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── nosslappdir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── registrydir │ │ │ └── conf │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ └── web.xml │ │ ├── webappdir │ │ │ ├── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat-test-ctx.xml │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ └── web.xml │ │ │ └── static │ │ │ │ └── static.html │ │ ├── x509annotationdir │ │ │ └── conf │ │ │ │ ├── concerteza.crt │ │ │ │ ├── etomcat.p12 │ │ │ │ ├── etomcat.properties │ │ │ │ ├── truststore.jks │ │ │ │ └── web.xml │ │ └── x509dir │ │ │ └── conf │ │ │ ├── concerteza.crt │ │ │ ├── etomcat-test-ctx.xml │ │ │ ├── etomcat.p12 │ │ │ ├── etomcat.properties │ │ │ ├── truststore.jks │ │ │ └── web.xml │ └── java │ │ └── ru │ │ └── concerteza │ │ └── springtomcat │ │ └── etomcat8 │ │ ├── components │ │ ├── HolderServlet.java │ │ ├── RegistryServlet.java │ │ ├── TestFilter.java │ │ ├── TestListener.java │ │ ├── TestLogConsumer.java │ │ └── TestServlet.java │ │ └── x509 │ │ ├── AuthUserDetailService.java │ │ ├── SecuredService.java │ │ └── X509Servlet.java │ └── test │ ├── java │ └── ru │ │ └── concerteza │ │ └── springtomcat │ │ └── etomcat8 │ │ ├── AccessLogTest.java │ │ ├── AnnotationConfigTest.java │ │ ├── AnnotationTestSupertype.java │ │ ├── AppTest.java │ │ ├── BruteforceTest.java │ │ ├── HolderTest.java │ │ ├── NoSslTest.java │ │ ├── RegistryTest.java │ │ ├── SslHelper.java │ │ ├── StartStopTest.java │ │ ├── TestSupertype.java │ │ ├── WebAppTest.java │ │ ├── X509AnnotationTest.java │ │ └── X509Test.java │ └── resources │ ├── client-truststore.jks │ ├── etomcat_client.p12 │ ├── global-metod-security-enabler.xml │ └── log4j.properties ├── etomcat8 ├── pom.xml └── src │ └── main │ └── java │ └── ru │ └── concerteza │ └── springtomcat │ └── etomcat8 │ ├── EmbeddedContextConfig.java │ ├── EmbeddedEngine.java │ ├── EmbeddedLoader.java │ ├── EmbeddedManager.java │ ├── EmbeddedResourceRoot.java │ ├── EmbeddedService.java │ ├── EmbeddedTomcat.java │ ├── EmbeddedTomcatStandalone.java │ ├── EmbeddedWrapper.java │ ├── ExecutorState.java │ ├── SameThreadExecutor.java │ ├── config │ ├── ConnectorProperties.java │ ├── ContextProperties.java │ ├── ExecutorProperties.java │ ├── FsProperties.java │ ├── GeneralProperties.java │ ├── HostProperties.java │ ├── NioProperties.java │ ├── SocketProperties.java │ └── SslProperties.java │ ├── context │ ├── EmbeddedAnnotationSpringContext.java │ ├── EmbeddedSpringContext.java │ └── EmbeddedXmlSpringContext.java │ ├── failfast │ ├── FailFastConnector.java │ ├── FailFastProxyOutputStream.java │ ├── FailFastResponse.java │ ├── FailFastResponseWriter.java │ └── SneakThrower.java │ └── valves │ └── PostCharacterEncodingValve.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | *.iml 3 | *.ipr 4 | *.iws 5 | /*/target 6 | /target 7 | /etomcat6-test/src/main/app-dirs/*/work/ 8 | /etomcat8-test/src/main/app-dirs/*/work/ 9 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/access/AccessConsumer.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.access; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | public interface AccessConsumer { 8 | void consume(AccessEvent event); 9 | } 10 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/access/AccessEvent.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.access; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | import org.joda.time.LocalDateTime; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * User: alexey 12 | * Date: 11/4/11 13 | */ 14 | public class AccessEvent implements Serializable{ 15 | private static final long serialVersionUID = -4507075747441342464L; 16 | 17 | private final LocalDateTime start; 18 | private final LocalDateTime end; 19 | private final String remoteIpAddress; 20 | private final String requestMethod; 21 | private final int responseCode; 22 | private final String commonName; 23 | private final String url; 24 | 25 | public AccessEvent(LocalDateTime start, LocalDateTime end, String remoteIpAddress, String requestMethod, int responseCode, String commonName, String url) { 26 | this.start = start; 27 | this.end = end; 28 | this.remoteIpAddress = remoteIpAddress; 29 | this.requestMethod = requestMethod; 30 | this.responseCode = responseCode; 31 | this.commonName = commonName; 32 | this.url = url; 33 | } 34 | 35 | public LocalDateTime getStart() { 36 | return start; 37 | } 38 | 39 | public LocalDateTime getEnd() { 40 | return end; 41 | } 42 | 43 | public String getRemoteIpAddress() { 44 | return remoteIpAddress; 45 | } 46 | 47 | public String getRequestMethod() { 48 | return requestMethod; 49 | } 50 | 51 | public int getResponseCode() { 52 | return responseCode; 53 | } 54 | 55 | public String getCommonName() { 56 | return commonName; 57 | } 58 | 59 | public String getUrl() { 60 | return url; 61 | } 62 | 63 | @Override 64 | public boolean equals(Object obj) { 65 | if (this == obj) return true; 66 | if (obj == null || getClass() != obj.getClass()) return false; 67 | AccessEvent that = (AccessEvent) obj; 68 | return new EqualsBuilder() 69 | .append(this.start, that.start) 70 | .append(this.end, that.end) 71 | .append(this.remoteIpAddress, that.remoteIpAddress) 72 | .append(this.requestMethod, that.requestMethod) 73 | .append(this.responseCode, that.responseCode) 74 | .append(this.commonName, that.commonName) 75 | .append(this.url, that.url) 76 | .isEquals(); 77 | } 78 | 79 | @Override 80 | public int hashCode() { 81 | return new HashCodeBuilder() 82 | .append(start) 83 | .append(end) 84 | .append(remoteIpAddress) 85 | .append(requestMethod) 86 | .append(responseCode) 87 | .append(commonName) 88 | .append(url) 89 | .hashCode(); 90 | } 91 | 92 | @Override 93 | public String toString() { 94 | return new ToStringBuilder(this). 95 | append("start", start). 96 | append("end", end). 97 | append("remoteIpAddress", remoteIpAddress). 98 | append("requestMethod", requestMethod). 99 | append("responseCode", responseCode). 100 | append("commonName", commonName). 101 | append("url", url). 102 | toString(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/access/AccessLogFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.access; 2 | 3 | import org.apache.catalina.connector.ResponseFacade; 4 | import org.apache.commons.lang.UnhandledException; 5 | import org.joda.time.LocalDateTime; 6 | import org.springframework.web.filter.GenericFilterBean; 7 | 8 | import javax.servlet.*; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | import java.lang.reflect.Field; 13 | import java.security.cert.X509Certificate; 14 | import java.util.regex.Matcher; 15 | import java.util.regex.Pattern; 16 | 17 | /** 18 | * User: alexey 19 | * Date: 11/4/11 20 | */ 21 | public class AccessLogFilter extends GenericFilterBean { 22 | private static final Pattern CN_PATTERN = Pattern.compile("CN=(.*?),", Pattern.CASE_INSENSITIVE); 23 | private static final Field RESPONSE_FIELD; 24 | static { 25 | try { 26 | RESPONSE_FIELD = ResponseFacade.class.getDeclaredField("response"); 27 | RESPONSE_FIELD.setAccessible(true); 28 | } catch (NoSuchFieldException e) { 29 | throw new UnhandledException(e); 30 | } 31 | } 32 | 33 | private AccessConsumer consumer; 34 | 35 | // don't want to use listeners here - they will be hard to configure from spring 36 | public void setConsumer(AccessConsumer consumer) { 37 | this.consumer = consumer; 38 | } 39 | 40 | @Override 41 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 42 | HttpServletRequest req = (HttpServletRequest) request; 43 | HttpServletResponse resp = (HttpServletResponse) response; 44 | LocalDateTime start = new LocalDateTime(); 45 | chain.doFilter(request, response); 46 | LocalDateTime end = new LocalDateTime(); 47 | AccessEvent event = createEvent(req, resp, start, end); 48 | consumer.consume(event); 49 | } 50 | 51 | private AccessEvent createEvent(HttpServletRequest request, HttpServletResponse response, LocalDateTime start, LocalDateTime end) { 52 | int status = extractResponseCode(response); 53 | String cn = extractCN(request); 54 | return new AccessEvent(start, end, request.getRemoteAddr(), request.getMethod(), status, cn, request.getRequestURI()); 55 | } 56 | 57 | // in servlet API 2.5 it's better then response wrapping 58 | private int extractResponseCode(HttpServletResponse response) { 59 | try { 60 | ServletResponse temp = response; 61 | while (ServletResponseWrapper.class.isAssignableFrom(temp.getClass())) { 62 | ServletResponseWrapper wrapper = (ServletResponseWrapper) temp; 63 | temp = wrapper.getResponse(); 64 | } 65 | if (!ResponseFacade.class.isAssignableFrom(temp.getClass())) return -1; 66 | org.apache.catalina.connector.Response resp = (org.apache.catalina.connector.Response) RESPONSE_FIELD.get(temp); 67 | return resp.getStatus(); 68 | } catch (IllegalAccessException e) { 69 | throw new UnhandledException(e); 70 | } 71 | } 72 | 73 | private String extractCN(HttpServletRequest req) { 74 | X509Certificate[] certs = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate"); 75 | if (null != certs && certs.length > 0) { 76 | return parseDN(certs[0]); 77 | } 78 | return "NO_CERT_FOUND"; 79 | } 80 | 81 | private String parseDN(X509Certificate cert) { 82 | String dn = cert.getSubjectDN().getName(); 83 | Matcher matcher = CN_PATTERN.matcher(dn); 84 | boolean findCn = matcher.find(); 85 | if (!findCn) throw new IllegalArgumentException(String.format("Cannot find CN part in DN: '%s'", dn)); 86 | return matcher.group(1); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/bruteforce/BruteforceSuspectedException.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.bruteforce; 2 | 3 | 4 | import org.springframework.security.core.AuthenticationException; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 12/16/11 9 | */ 10 | public class BruteforceSuspectedException extends AuthenticationException { 11 | 12 | public BruteforceSuspectedException(String login, int attemptsCount) { 13 | super("Login: '" + login + "', attemptsCount: " + attemptsCount); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/holder/SessionHolder.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.holder; 2 | 3 | import javax.servlet.http.HttpSession; 4 | 5 | /** 6 | * User: alexey 7 | * Date: 11/4/11 8 | */ 9 | public class SessionHolder { 10 | private final ThreadLocal threadLocal = new ThreadLocal(); 11 | 12 | public void set(HttpSession session) { 13 | threadLocal.set(session); 14 | } 15 | 16 | public HttpSession get() { 17 | return threadLocal.get(); 18 | } 19 | 20 | public void remove() { 21 | threadLocal.remove(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/holder/SessionHolderFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.holder; 2 | 3 | import org.springframework.web.filter.GenericFilterBean; 4 | 5 | import javax.servlet.FilterChain; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.ServletRequest; 8 | import javax.servlet.ServletResponse; 9 | import javax.servlet.http.HttpServletRequest; 10 | import java.io.IOException; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 11/4/11 15 | */ 16 | public class SessionHolderFilter extends GenericFilterBean { 17 | private SessionHolder holder; 18 | 19 | @Override 20 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 21 | try { 22 | HttpServletRequest req = (HttpServletRequest) request; 23 | holder.set(req.getSession()); 24 | chain.doFilter(request, response); 25 | } finally { 26 | holder.remove(); 27 | } 28 | } 29 | 30 | public void setHolder(SessionHolder holder) { 31 | this.holder = holder; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/SessionIdRegistryFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry; 2 | 3 | import org.springframework.security.core.Authentication; 4 | import org.springframework.security.core.context.SecurityContextHolder; 5 | import org.springframework.web.filter.GenericFilterBean; 6 | import ru.concerteza.springtomcat.components.registry.concurrent.ConcurrentSessionException; 7 | 8 | import javax.servlet.FilterChain; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.ServletRequest; 11 | import javax.servlet.ServletResponse; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | import java.io.IOException; 15 | 16 | /** 17 | * User: alexey 18 | * Date: 11/4/11 19 | */ 20 | 21 | // depends on Spring Security 22 | // 23 | public class SessionIdRegistryFilter extends GenericFilterBean { 24 | private SessionRegistry registry; 25 | 26 | public void setRegistry(SessionRegistry registry) { 27 | this.registry = registry; 28 | } 29 | 30 | @Override 31 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 32 | HttpServletRequest req = (HttpServletRequest) request; 33 | HttpServletResponse resp = (HttpServletResponse) response; 34 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 35 | if(null == auth) throw new IllegalStateException("Auth info not found on sessionRegistryFilter step, sessionId: " + req.getSession().getId()); 36 | try { 37 | registry.put(auth.getName(), req.getSession()); 38 | chain.doFilter(request, response); 39 | } catch (ConcurrentSessionException e) { 40 | resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 41 | resp.getWriter().write(e.getMessage()); 42 | // tomcat 401 error page here 43 | // resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage()); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/SessionRegistryListener.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry; 2 | 3 | import org.springframework.context.ApplicationListener; 4 | import org.springframework.security.web.session.HttpSessionDestroyedEvent; 5 | 6 | import javax.servlet.http.HttpSessionEvent; 7 | import javax.servlet.http.HttpSessionListener; 8 | 9 | /** 10 | * User: alexey 11 | * Date: 11/4/11 12 | */ 13 | 14 | public class SessionRegistryListener implements ApplicationListener { 15 | private SessionRegistry registry; 16 | 17 | public void setRegistry(SessionRegistry registry) { 18 | this.registry = registry; 19 | } 20 | 21 | @Override 22 | public void onApplicationEvent(HttpSessionDestroyedEvent event) { 23 | registry.remove(event.getSession()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/concurrent/ConcurrentSessionException.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry.concurrent; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 4/29/12 6 | */ 7 | public class ConcurrentSessionException extends RuntimeException { 8 | private static final long serialVersionUID = 3786976580626788476L; 9 | 10 | public ConcurrentSessionException(String message) { 11 | super(message); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/concurrent/ConcurrentSessionStrategy.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry.concurrent; 2 | 3 | import ru.concerteza.springtomcat.components.registry.SessionRegistry; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpSession; 7 | 8 | /** 9 | * User: alexey 10 | * Date: 4/29/12 11 | */ 12 | public interface ConcurrentSessionStrategy { 13 | void onExisted(SessionRegistry.LockBoundedManager manager, String login, HttpSession existed, HttpSession current) throws ConcurrentSessionException; 14 | } 15 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/concurrent/InvalidateExistedStrategy.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry.concurrent; 2 | 3 | import ru.concerteza.springtomcat.components.registry.SessionRegistry; 4 | 5 | import javax.servlet.http.HttpSession; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 4/29/12 10 | */ 11 | public class InvalidateExistedStrategy implements ConcurrentSessionStrategy { 12 | @Override 13 | public void onExisted(SessionRegistry.LockBoundedManager manager, String login, HttpSession existed, HttpSession current) throws ConcurrentSessionException { 14 | manager.remove(existed); 15 | existed.invalidate(); 16 | manager.put(login, current); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/concurrent/OneSessionAllowedStrategy.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry.concurrent; 2 | 3 | import ru.concerteza.springtomcat.components.registry.SessionRegistry; 4 | 5 | import javax.servlet.http.HttpSession; 6 | 7 | import static org.apache.commons.lang.StringUtils.defaultString; 8 | import static ru.concerteza.springtomcat.components.registry.concurrent.RemoteIpFilter.REMOTE_IP_ATTRIBUTE; 9 | 10 | /** 11 | * User: alexey 12 | * Date: 4/29/12 13 | */ 14 | public class OneSessionAllowedStrategy implements ConcurrentSessionStrategy { 15 | @Override 16 | public void onExisted(SessionRegistry.LockBoundedManager manager, String login, HttpSession existed, HttpSession current) throws ConcurrentSessionException { 17 | String existedIp = (String) existed.getAttribute(REMOTE_IP_ATTRIBUTE); 18 | throw new ConcurrentSessionException(defaultString(existedIp, "UNKNOWN")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /components/src/main/java/ru/concerteza/springtomcat/components/registry/concurrent/RemoteIpFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.components.registry.concurrent; 2 | 3 | import org.springframework.web.filter.GenericFilterBean; 4 | 5 | import javax.servlet.FilterChain; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.ServletRequest; 8 | import javax.servlet.ServletResponse; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpSession; 11 | import java.io.IOException; 12 | 13 | /** 14 | * User: alexey 15 | * Date: 4/29/12 16 | */ 17 | public class RemoteIpFilter extends GenericFilterBean { 18 | public static final String REMOTE_IP_ATTRIBUTE = RemoteIpFilter.class.getName() + ".remoteIp"; 19 | private String remoteIpHeader = "X-Forwarded-For"; 20 | 21 | public void setRemoteIpHeader(String remoteIpHeader) { 22 | this.remoteIpHeader = remoteIpHeader; 23 | } 24 | 25 | @Override 26 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 27 | HttpServletRequest req = (HttpServletRequest) request; 28 | HttpSession session = req.getSession(); 29 | String existed = (String) session.getAttribute(REMOTE_IP_ATTRIBUTE); 30 | String current = extractRemoteIp(req); 31 | if(null != existed) { 32 | if(!existed.equals(current)) throw new IllegalStateException( 33 | "Another remote IP found in session, current IP: " + current + ", existed IP: " + existed); 34 | } else { 35 | session.setAttribute(REMOTE_IP_ATTRIBUTE, current); 36 | } 37 | chain.doFilter(request, response); 38 | } 39 | 40 | private String extractRemoteIp(HttpServletRequest req) { 41 | String forwarded = req.getHeader(remoteIpHeader); 42 | return null != forwarded ? forwarded : req.getRemoteAddr(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/accessdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/annotationdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/appdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/appdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/appdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | file:src/main/app-dirs/appdir/conf/etomcat.properties 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/appdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/appdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/appdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/appdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | ru.concerteza.springtomcat.etomcat6.components.TestListener 19 | 20 | 21 | 22 | testFilter 23 | ru.concerteza.springtomcat.etomcat6.components.TestFilter 24 | 25 | 26 | 27 | testFilter 28 | /etomcat_test 29 | 30 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/bruteforcedir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/holderdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/holderdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.HolderServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/nosslappdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/nosslappdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/registrydir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat6.components.RegistryServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/webappdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | file:src/main/app-dirs/webappdir/conf/etomcat.properties 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/webappdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test 5 | etomcat.docBaseDir=static -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | default 9 | org.apache.catalina.servlets.DefaultServlet 10 | 11 | debug 12 | 0 13 | 14 | 15 | listings 16 | false 17 | 18 | 1 19 | 20 | 21 | 22 | default 23 | / 24 | 25 | 26 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/webappdir/static/static.html: -------------------------------------------------------------------------------- 1 | static content -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509annotationdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509annotationdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509annotationdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509annotationdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509annotationdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test 5 | etomcat.ssl.truststoreFile=truststore.jks 6 | etomcat.ssl.truststorePass=amber% 7 | etomcat.ssl.truststoreType=JKS -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509annotationdir/conf/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509annotationdir/conf/truststore.jks -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509annotationdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | x509Servlet 9 | ru.concerteza.springtomcat.etomcat6.x509.X509Servlet 10 | 11 | 12 | 13 | x509Servlet 14 | /etomcat_x509 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509dir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509dir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509dir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509dir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509dir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test 5 | etomcat.ssl.truststoreFile=truststore.jks 6 | etomcat.ssl.truststorePass=amber% 7 | etomcat.ssl.truststoreType=JKS -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509dir/conf/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/main/app-dirs/x509dir/conf/truststore.jks -------------------------------------------------------------------------------- /etomcat6-test/src/main/app-dirs/x509dir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | x509Servlet 9 | ru.concerteza.springtomcat.etomcat6.x509.X509Servlet 10 | 11 | 12 | 13 | x509Servlet 14 | /etomcat_x509 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/HolderServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | import ru.concerteza.springtomcat.components.holder.SessionHolder; 6 | 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | 13 | /** 14 | * User: alexey 15 | * Date: 3/2/12 16 | */ 17 | public class HolderServlet extends HttpServlet { 18 | private static final long serialVersionUID = -4967422466219316383L; 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 23 | SessionHolder holder = context.getBean(SessionHolder.class); 24 | resp.getOutputStream().write(holder.get().getId().getBytes("UTF-8")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/RegistryServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.web.context.WebApplicationContext; 7 | import ru.concerteza.springtomcat.components.holder.SessionHolder; 8 | import ru.concerteza.springtomcat.components.registry.SessionRegistry; 9 | 10 | import javax.servlet.ServletException; 11 | import javax.servlet.http.HttpServlet; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | import java.io.IOException; 15 | 16 | /** 17 | * User: alexey 18 | * Date: 3/2/12 19 | */ 20 | public class RegistryServlet extends HttpServlet { 21 | private static final long serialVersionUID = 4131885764840516610L; 22 | private final Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Override 25 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 26 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 27 | SessionRegistry registry = context.getBean(SessionRegistry.class); 28 | logger.info(registry.dump()); 29 | resp.getOutputStream().write(Boolean.toString(registry.contains("anonymous")).getBytes("UTF-8")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/TestFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import javax.servlet.*; 4 | import java.io.IOException; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 8/29/11 9 | */ 10 | public class TestFilter implements Filter { 11 | private static boolean initialized; 12 | private static boolean filtered; 13 | 14 | @Override 15 | public void init(FilterConfig filterConfig) throws ServletException { 16 | initialized = true; 17 | } 18 | 19 | @Override 20 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 21 | filtered = true; 22 | chain.doFilter(request, response); 23 | } 24 | 25 | @Override 26 | public void destroy() { 27 | // this line is initially left blank 28 | } 29 | 30 | public static boolean isInitialized() { 31 | return initialized; 32 | } 33 | 34 | public static boolean isFiltered() { 35 | return filtered; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/TestListener.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 8/29/11 9 | */ 10 | public class TestListener implements ServletContextListener { 11 | private static boolean initialized = false; 12 | 13 | @Override 14 | public void contextInitialized(ServletContextEvent sce) { 15 | initialized = true; 16 | } 17 | 18 | @Override 19 | public void contextDestroyed(ServletContextEvent sce) { 20 | // this line is initially left blank 21 | } 22 | 23 | public static boolean isInitialized() { 24 | return initialized; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/TestLogConsumer.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import ru.concerteza.springtomcat.components.access.AccessConsumer; 4 | import ru.concerteza.springtomcat.components.access.AccessEvent; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 3/1/12 9 | */ 10 | public class TestLogConsumer implements AccessConsumer { 11 | private static AccessEvent lastEvent; 12 | 13 | @Override 14 | public void consume(AccessEvent event) { 15 | TestLogConsumer.lastEvent = event; 16 | } 17 | 18 | public static AccessEvent getLastEvent() { 19 | return lastEvent; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/components/TestServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.components; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 8/28/11 15 | */ 16 | public class TestServlet extends HttpServlet { 17 | private static final long serialVersionUID = -8124821589871278803L; 18 | public static final String CONTENT = "EmbeddedXmlSpringContext"; 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 23 | resp.getOutputStream().write(context.getClass().getSimpleName().getBytes("UTF-8")); 24 | } 25 | 26 | @Override 27 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 | String param = req.getParameter("foo"); 29 | boolean res = "bar".equals(param); 30 | resp.getOutputStream().write(Boolean.toString(res).getBytes("UTF-8")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/x509/AuthUserDetailService.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.x509; 2 | 3 | import org.springframework.dao.DataAccessException; 4 | import org.springframework.security.core.Authentication; 5 | import org.springframework.security.core.GrantedAuthority; 6 | import org.springframework.security.core.userdetails.*; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 3/4/12 14 | */ 15 | 16 | @Service("authUserDetailsService") 17 | public class AuthUserDetailService implements UserDetailsService { 18 | @Override 19 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { 20 | if("etomcat_client".equals(username)) { 21 | return new User("etomcat_client", "", true, true, true, true, Arrays.asList(new Role("ROLE_ETOMCAT_CLIENT"))); 22 | } 23 | throw new UsernameNotFoundException(username); 24 | } 25 | 26 | private class Role implements GrantedAuthority { 27 | private static final long serialVersionUID = 2191234214967125385L; 28 | private final String authority; 29 | 30 | private Role(String authority) { 31 | this.authority = authority; 32 | } 33 | 34 | @Override 35 | public String getAuthority() { 36 | return authority; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/x509/SecuredService.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.x509; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.stereotype.Service; 5 | 6 | import javax.annotation.security.RolesAllowed; 7 | 8 | /** 9 | * User: alexey 10 | * Date: 3/4/12 11 | */ 12 | 13 | public interface SecuredService { 14 | public static final String GREETING = "Hello ETomcat Client!"; 15 | 16 | String hello(); 17 | 18 | String fail(); 19 | } 20 | 21 | @Service 22 | class SecuredServiceImpl implements SecuredService { 23 | 24 | @RolesAllowed("ROLE_ETOMCAT_CLIENT") 25 | public String hello() { 26 | return GREETING; 27 | } 28 | 29 | @RolesAllowed("ROLE_FAIL") 30 | public String fail() { 31 | return "Access failed"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /etomcat6-test/src/main/java/ru/concerteza/springtomcat/etomcat6/x509/X509Servlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.x509; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 3/4/12 15 | */ 16 | public class X509Servlet extends HttpServlet { 17 | private static final long serialVersionUID = -4988677350741242648L; 18 | 19 | @Override 20 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 22 | SecuredService service = context.getBean(SecuredService.class); 23 | String reply = service.hello(); 24 | resp.getOutputStream().write(reply.getBytes("UTF-8")); 25 | } 26 | 27 | @Override 28 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 29 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 30 | SecuredService service = context.getBean(SecuredService.class); 31 | String reply = service.fail(); 32 | resp.getOutputStream().write(reply.getBytes("UTF-8")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/AccessLogTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import ru.concerteza.springtomcat.components.access.AccessEvent; 10 | import ru.concerteza.springtomcat.etomcat6.components.TestLogConsumer; 11 | import ru.concerteza.springtomcat.etomcat6.components.TestServlet; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 3/1/12 18 | */ 19 | public class AccessLogTest extends TestSupertype { 20 | private final Logger logger = LoggerFactory.getLogger(getClass()); 21 | 22 | @Test 23 | public void testAccessLog() throws Exception { 24 | HttpClient client = new HttpClient(); 25 | // test get 26 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 27 | client.executeMethod(get); 28 | // test data 29 | byte[] responseBody = get.getResponseBody(); 30 | String content = new String(responseBody, "UTF-8"); 31 | assertEquals("Servlet get fail", TestServlet.CONTENT, content); 32 | // test log event 33 | AccessEvent ev = TestLogConsumer.getLastEvent(); 34 | assertEquals("Method log fail", "GET", ev.getRequestMethod()); 35 | assertEquals("Status log fail", 200, ev.getResponseCode()); 36 | assertEquals("URL log fail", "/etomcat_test", ev.getUrl()); 37 | } 38 | 39 | @Override 40 | protected String dirname() { 41 | return "accessdir"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/AnnotationConfigTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import ru.concerteza.springtomcat.etomcat6.context.EmbeddedAnnotationSpringContext; 11 | 12 | import java.io.File; 13 | 14 | /** 15 | * User: alexey 16 | * Date: 4/9/12 17 | */ 18 | public class AnnotationConfigTest extends AnnotationTestSupertype { 19 | 20 | @Test 21 | public void testRunning() throws Exception { 22 | HttpClient client = new HttpClient(); 23 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 24 | client.executeMethod(get); 25 | byte[] responseBody = get.getResponseBody(); 26 | String content = new String(responseBody, "UTF-8"); 27 | Assert.assertEquals("Servlet get fail", EmbeddedAnnotationSpringContext.class.getSimpleName(), content); 28 | } 29 | 30 | @Override 31 | protected String dirname() { 32 | return "annotationdir"; 33 | } 34 | 35 | @Override 36 | protected Class configClass() { 37 | return Config.class; 38 | } 39 | 40 | @Configuration 41 | public static class Config { 42 | @Bean(destroyMethod = "stop") 43 | public EmbeddedTomcat etomcat() { 44 | return new EmbeddedTomcat(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/AnnotationTestSupertype.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.LifecycleException; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.springframework.context.support.AbstractApplicationContext; 7 | import ru.concerteza.springtomcat.etomcat6.context.EmbeddedAnnotationSpringContext; 8 | 9 | import java.io.File; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 4/9/12 14 | */ 15 | public abstract class AnnotationTestSupertype { 16 | private AbstractApplicationContext ctx; 17 | 18 | @Before 19 | public void setUp() throws Exception { 20 | File baseDir = new File("src/main/app-dirs/" + dirname()); 21 | ctx = new EmbeddedAnnotationSpringContext(configClass()); 22 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 23 | } 24 | 25 | @After 26 | public void tearDown() throws LifecycleException { 27 | if (null != ctx) ctx.close(); 28 | } 29 | 30 | protected abstract String dirname(); 31 | 32 | protected abstract Class configClass(); 33 | } 34 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/AppTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.NameValuePair; 6 | import org.apache.commons.httpclient.cookie.CookiePolicy; 7 | import org.apache.commons.httpclient.methods.GetMethod; 8 | import org.apache.commons.httpclient.methods.PostMethod; 9 | import org.junit.*; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | import ru.concerteza.springtomcat.etomcat6.components.TestFilter; 13 | import ru.concerteza.springtomcat.etomcat6.components.TestListener; 14 | import ru.concerteza.springtomcat.etomcat6.components.TestServlet; 15 | 16 | /** 17 | * User: alexey 18 | * Date: 8/28/11 19 | */ 20 | 21 | public class AppTest extends TestSupertype { 22 | private final Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testRunning() throws Exception { 26 | HttpClient client = new HttpClient(); 27 | setupSsl(); 28 | // test get 29 | HttpMethod get = new GetMethod("https://127.0.0.1:8443/etomcat_test"); 30 | get.getParams().setCookiePolicy(CookiePolicy.RFC_2965); 31 | // Thread.sleep(10000000); 32 | client.executeMethod(get); 33 | byte[] responseBody = get.getResponseBody(); 34 | String content = new String(responseBody, "UTF-8"); 35 | Assert.assertEquals("Servlet get fail", TestServlet.CONTENT, content); 36 | Assert.assertTrue("Listener fail", TestListener.isInitialized()); 37 | Assert.assertTrue("Filter init fail", TestFilter.isInitialized()); 38 | Assert.assertTrue("Filter fail", TestFilter.isFiltered()); 39 | // test post 40 | PostMethod post = new PostMethod("https://127.0.0.1:8443/etomcat_test"); 41 | post.getParams().setCookiePolicy(CookiePolicy.RFC_2965); 42 | NameValuePair[] data = { 43 | new NameValuePair("foo", "bar"), 44 | }; 45 | post.setRequestBody(data); 46 | client.executeMethod(post); 47 | byte[] postBody = post.getResponseBody(); 48 | String res = new String(postBody, "UTF-8"); 49 | Assert.assertEquals("Servlet post fail", Boolean.TRUE.toString(), res); 50 | // test 404 51 | GetMethod fail = new GetMethod("https://127.0.0.1:8443/foobar"); 52 | client.executeMethod(fail); 53 | Assert.assertEquals("Wrong result code", 404, fail.getStatusCode()); 54 | } 55 | 56 | @Override 57 | protected String dirname() { 58 | return "appdir"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/BruteforceTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.Credentials; 4 | import org.apache.commons.httpclient.HttpClient; 5 | import org.apache.commons.httpclient.HttpMethod; 6 | import org.apache.commons.httpclient.UsernamePasswordCredentials; 7 | import org.apache.commons.httpclient.auth.AuthScope; 8 | import org.apache.commons.httpclient.methods.GetMethod; 9 | import org.junit.Test; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 3/1/12 18 | */ 19 | public class BruteforceTest extends TestSupertype { 20 | private final Logger logger = LoggerFactory.getLogger(getClass()); 21 | 22 | @Test 23 | public void testBruteforce() throws Exception { 24 | HttpClient client = new HttpClient(); 25 | client.getParams().setAuthenticationPreemptive(true); 26 | Credentials failcreds = new UsernamePasswordCredentials("admin", "fail"); 27 | Credentials wincreds = new UsernamePasswordCredentials("admin", "password"); 28 | // brute 29 | client.getState().setCredentials(AuthScope.ANY, failcreds); 30 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 31 | client.executeMethod(get); 32 | assertEquals("First access fail", 401, get.getStatusCode()); 33 | client.executeMethod(get); 34 | assertEquals("Last access fail", 401, get.getStatusCode()); 35 | // closed 36 | client.executeMethod(get); 37 | assertEquals("Improbable fail", 401, get.getStatusCode()); 38 | // change appearance 39 | client.getState().setCredentials(AuthScope.ANY, wincreds); 40 | client.executeMethod(get); 41 | assertEquals("Bruteforce fail", 401, get.getStatusCode()); 42 | // cooldown 43 | Thread.sleep(1000); 44 | client.executeMethod(get); 45 | assertEquals("Cooldowned access fail", 200, get.getStatusCode()); 46 | } 47 | 48 | @Override 49 | protected String dirname() { 50 | return "bruteforcedir"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/HolderTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | import static org.junit.Assert.assertTrue; 12 | 13 | /** 14 | * User: alexey 15 | * Date: 3/2/12 16 | */ 17 | public class HolderTest extends TestSupertype { 18 | private final Logger logger = LoggerFactory.getLogger(getClass()); 19 | 20 | @Test 21 | public void testAccessLog() throws Exception { 22 | HttpClient client = new HttpClient(); 23 | // test get 24 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 25 | client.executeMethod(get); 26 | // test data 27 | byte[] responseBody = get.getResponseBody(); 28 | String content = new String(responseBody, "UTF-8"); 29 | assertTrue(content.matches("^[A-Z0-9]{32}$")); 30 | } 31 | 32 | @Override 33 | protected String dirname() { 34 | return "holderdir"; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/NoSslTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.NameValuePair; 6 | import org.apache.commons.httpclient.cookie.CookiePolicy; 7 | import org.apache.commons.httpclient.methods.GetMethod; 8 | import org.apache.commons.httpclient.methods.PostMethod; 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | import ru.concerteza.springtomcat.etomcat6.components.TestServlet; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 8/28/11 18 | */ 19 | 20 | public class NoSslTest extends TestSupertype { 21 | private final Logger logger = LoggerFactory.getLogger(getClass()); 22 | 23 | @Test 24 | public void testRunning() throws Exception { 25 | HttpClient client = new HttpClient(); 26 | // test get 27 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 28 | // Thread.sleep(10000000); 29 | client.executeMethod(get); 30 | byte[] responseBody = get.getResponseBody(); 31 | String content = new String(responseBody, "UTF-8"); 32 | Assert.assertEquals("Servlet get fail", TestServlet.CONTENT, content); 33 | } 34 | 35 | @Override 36 | protected String dirname() { 37 | return "nosslappdir"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/RegistryTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.Cookie; 4 | import org.apache.commons.httpclient.HttpClient; 5 | import org.apache.commons.httpclient.HttpMethod; 6 | import org.apache.commons.httpclient.cookie.CookiePolicy; 7 | import org.apache.commons.httpclient.cookie.MalformedCookieException; 8 | import org.apache.commons.httpclient.cookie.RFC2109Spec; 9 | import org.apache.commons.httpclient.methods.GetMethod; 10 | import org.junit.Test; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import javax.servlet.http.HttpServletResponse; 15 | 16 | import java.util.concurrent.atomic.AtomicReference; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.assertTrue; 20 | 21 | /** 22 | * User: alexey 23 | * Date: 3/2/12 24 | */ 25 | public class RegistryTest extends TestSupertype { 26 | private final Logger logger = LoggerFactory.getLogger(getClass()); 27 | private AtomicReference sessionCookie = new AtomicReference(); 28 | 29 | @Test 30 | public void testRegistry() throws Exception { 31 | { 32 | HttpClient client = new HttpClient(); 33 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 34 | // test get 35 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 36 | client.executeMethod(get); 37 | sessionCookie.compareAndSet(null, get.getResponseHeader("Set-Cookie").getValue()); 38 | // test data 39 | byte[] responseBody = get.getResponseBody(); 40 | String content = new String(responseBody, "UTF-8"); 41 | assertEquals("Registry fail", "true", content); 42 | // subsequent requests 43 | HttpMethod get1 = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 44 | String cookie = sessionCookie.get(); 45 | if(null != cookie) get1.setRequestHeader("Cookie", cookie); 46 | client.executeMethod(get1); 47 | sessionCookie.compareAndSet(null, get.getResponseHeader("Set-Cookie").getValue()); 48 | assertEquals(HttpServletResponse.SC_OK, get1.getStatusCode()); 49 | } 50 | { 51 | // cookie test 52 | HttpClient client = new HttpClient(); 53 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 54 | HttpMethod get1 = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 55 | String cookie = sessionCookie.get(); 56 | if(null != cookie) get1.setRequestHeader("Cookie", cookie); 57 | client.executeMethod(get1); 58 | assertEquals(HttpServletResponse.SC_OK, get1.getStatusCode()); 59 | } 60 | { 61 | // concurrent test 62 | HttpClient client = new HttpClient(); 63 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 64 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 65 | get.addRequestHeader("X-Forwarded-For", "192.168.42.42"); 66 | client.executeMethod(get); 67 | assertEquals(HttpServletResponse.SC_UNAUTHORIZED, get.getStatusCode()); 68 | byte[] responseBody = get.getResponseBody(); 69 | String content = new String(responseBody, "UTF-8"); 70 | assertEquals("127.0.0.1", content); 71 | } 72 | } 73 | 74 | @Override 75 | protected String dirname() { 76 | return "registrydir"; 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/SslHelper.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.protocol.Protocol; 4 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 5 | import org.jsslutils.extra.apachehttpclient.SslContextedSecureProtocolSocketFactory; 6 | 7 | import javax.net.ssl.KeyManagerFactory; 8 | import javax.net.ssl.SSLContext; 9 | import javax.net.ssl.TrustManagerFactory; 10 | import java.io.IOException; 11 | import java.security.*; 12 | import java.security.cert.CertificateException; 13 | 14 | /** 15 | * User: alexey 16 | * Date: 4/9/12 17 | */ 18 | public class SslHelper { 19 | public static void setupClientSsl() throws NoSuchProviderException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException { 20 | // truststore 21 | KeyStore trustStore = KeyStore.getInstance("JKS", "SUN"); 22 | trustStore.load(TestSupertype.class.getResourceAsStream("/client-truststore.jks"), "amber%".toCharArray()); 23 | String alg = KeyManagerFactory.getDefaultAlgorithm(); 24 | TrustManagerFactory fac = TrustManagerFactory.getInstance(alg); 25 | fac.init(trustStore); 26 | // keystore 27 | KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE"); 28 | keystore.load(X509Test.class.getResourceAsStream("/etomcat_client.p12"), "etomcat".toCharArray()); 29 | String keyAlg = KeyManagerFactory.getDefaultAlgorithm(); 30 | KeyManagerFactory keyFac = KeyManagerFactory.getInstance(keyAlg); 31 | keyFac.init(keystore, "etomcat".toCharArray()); 32 | // context 33 | SSLContext ctx = SSLContext.getInstance("TLS", "SunJSSE"); 34 | ctx.init(keyFac.getKeyManagers(), fac.getTrustManagers(), new SecureRandom()); 35 | SslContextedSecureProtocolSocketFactory secureProtocolSocketFactory = new SslContextedSecureProtocolSocketFactory(ctx); 36 | Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 8443)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/TestSupertype.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.LifecycleException; 4 | import org.apache.commons.httpclient.protocol.Protocol; 5 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 6 | import org.jsslutils.extra.apachehttpclient.SslContextedSecureProtocolSocketFactory; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.springframework.context.support.AbstractApplicationContext; 10 | import ru.concerteza.springtomcat.etomcat6.context.EmbeddedXmlSpringContext; 11 | 12 | import javax.net.ssl.*; 13 | import java.io.File; 14 | import java.io.IOException; 15 | import java.security.*; 16 | import java.security.cert.CertificateException; 17 | 18 | import static java.io.File.separator; 19 | 20 | /** 21 | * User: alexey 22 | * Date: 8/29/11 23 | */ 24 | public abstract class TestSupertype { 25 | 26 | private AbstractApplicationContext ctx; 27 | 28 | @Before 29 | public void setUp() throws Exception { 30 | File baseDir = new File("src" + separator + "main" + separator + "app-dirs" + separator + dirname()); 31 | File confDir = new File(baseDir, "conf"); 32 | File ctxFile = new File(confDir, "etomcat-test-ctx.xml"); 33 | ctx = new EmbeddedXmlSpringContext("file:" + ctxFile.getPath()); 34 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 35 | } 36 | 37 | @After 38 | public void tearDown() throws LifecycleException { 39 | if(null != ctx) ctx.close(); 40 | } 41 | 42 | protected void setupSsl() throws NoSuchProviderException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException { 43 | KeyStore trustStore = KeyStore.getInstance("JKS", "SUN"); 44 | trustStore.load(TestSupertype.class.getResourceAsStream("/client-truststore.jks"), "amber%".toCharArray()); 45 | String alg = KeyManagerFactory.getDefaultAlgorithm(); 46 | TrustManagerFactory fac = TrustManagerFactory.getInstance(alg); 47 | fac.init(trustStore); 48 | TrustManager[] tms = fac.getTrustManagers(); 49 | SSLContext ctx = SSLContext.getInstance("TLS", "SunJSSE"); 50 | ctx.init(new KeyManager[] {}, tms, new SecureRandom()); 51 | SslContextedSecureProtocolSocketFactory secureProtocolSocketFactory = new SslContextedSecureProtocolSocketFactory(ctx); 52 | Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 8443)); 53 | } 54 | 55 | protected abstract String dirname(); 56 | } 57 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/WebAppTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * User: alexey 11 | * Date: 8/29/11 12 | */ 13 | public class WebAppTest extends TestSupertype { 14 | 15 | @Test 16 | public void testRunning() throws Exception { 17 | HttpClient client = new HttpClient(); 18 | setupSsl(); 19 | HttpMethod method = new GetMethod("https://127.0.0.1:8443/static.html"); 20 | // Thread.sleep(10000000); 21 | client.executeMethod(method); 22 | byte[] responseBody = method.getResponseBody(); 23 | String content = new String(responseBody, "UTF-8"); 24 | Assert.assertEquals("Static fail", "static content", content); 25 | } 26 | 27 | @Override 28 | protected String dirname() { 29 | return "webappdir"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/java/ru/concerteza/springtomcat/etomcat6/X509Test.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.NameValuePair; 6 | import org.apache.commons.httpclient.cookie.CookiePolicy; 7 | import org.apache.commons.httpclient.methods.GetMethod; 8 | import org.apache.commons.httpclient.methods.PostMethod; 9 | import org.apache.commons.httpclient.protocol.Protocol; 10 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 11 | import org.jsslutils.extra.apachehttpclient.SslContextedSecureProtocolSocketFactory; 12 | import org.junit.Assert; 13 | import org.junit.Test; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import ru.concerteza.springtomcat.etomcat6.components.TestFilter; 17 | import ru.concerteza.springtomcat.etomcat6.components.TestListener; 18 | import ru.concerteza.springtomcat.etomcat6.components.TestServlet; 19 | import ru.concerteza.springtomcat.etomcat6.x509.SecuredService; 20 | 21 | import javax.net.ssl.*; 22 | import java.io.IOException; 23 | import java.security.*; 24 | import java.security.cert.CertificateException; 25 | 26 | import static org.junit.Assert.assertEquals; 27 | import static ru.concerteza.springtomcat.etomcat6.SslHelper.setupClientSsl; 28 | 29 | /** 30 | * User: alexey 31 | * Date: 8/28/11 32 | */ 33 | 34 | public class X509Test extends TestSupertype { 35 | private final Logger logger = LoggerFactory.getLogger(getClass()); 36 | 37 | @Test 38 | public void testRunning() throws Exception { 39 | HttpClient client = new HttpClient(); 40 | setupClientSsl(); 41 | // test get 42 | HttpMethod get = new GetMethod("https://127.0.0.1:8443/etomcat_x509"); 43 | client.executeMethod(get); 44 | byte[] responseBody = get.getResponseBody(); 45 | String content = new String(responseBody, "UTF-8"); 46 | assertEquals("Servlet get fail", SecuredService.GREETING, content); 47 | // test assess denied 48 | HttpMethod post = new PostMethod("https://127.0.0.1:8443/etomcat_x509"); 49 | client.executeMethod(post); 50 | assertEquals("Method security fail get fail", 403, post.getStatusCode()); 51 | } 52 | 53 | @Override 54 | protected String dirname() { 55 | return "x509dir"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/resources/client-truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/test/resources/client-truststore.jks -------------------------------------------------------------------------------- /etomcat6-test/src/test/resources/etomcat_client.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat6-test/src/test/resources/etomcat_client.p12 -------------------------------------------------------------------------------- /etomcat6-test/src/test/resources/global-metod-security-enabler.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | -------------------------------------------------------------------------------- /etomcat6-test/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=ALL, console 2 | 3 | log4j.appender.console=org.apache.log4j.ConsoleAppender 4 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 5 | # http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html 6 | log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p %-10.10t %-30.30c] %m%n 7 | log4j.appender.console.encoding=UTF-8 8 | log4j.appender.console.Threshold=ALL 9 | 10 | log4j.logger.ru.concerteza.springtomcat=INFO 11 | log4j.logger.org.springframework=WARN 12 | log4j.logger.org.apache.commons.digester=WARN 13 | log4j.logger.org.apache.tomcat=WARN 14 | log4j.logger.org.apache.catalina=WARN 15 | log4j.logger.org.apache.coyote=WARN 16 | # listing properties 17 | #log4j.logger.org.apache.coyote.http11.Http11NioProtocol=TRACE 18 | # prevent some warning 19 | log4j.logger.org.apache.catalina.startup.DigesterFactory=ERROR 20 | # http client 21 | log4j.logger.httpclient=INFO 22 | log4j.logger.org.apache.commons.httpclient=INFO 23 | 24 | -------------------------------------------------------------------------------- /etomcat6/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | ru.concerteza.spring-embedded-tomcat 6 | spring-embedded-tomcat 7 | 1.5.3-SNAPSHOT 8 | 9 | etomcat6 10 | jar 11 | Apache Tomcat 6 Embedded Into Spring 12 | 13 | Apache Tomcat 6 embedded into Springframework context 14 | https://github.com/alx3apps/spring-embedded-tomcat 15 | 16 | 17 | Apache License 2.0 18 | http://www.apache.org/licenses/LICENSE-2.0 19 | 20 | 21 | 22 | https://github.com/alx3apps/spring-embedded-tomcat 23 | scm:git:https://github.com/alx3apps/spring-embedded-tomcat.git 24 | scm:git:https://github.com/alx3apps/spring-embedded-tomcat.git 25 | HEAD 26 | 27 | 28 | 29 | Alex Kasko 30 | kalexey@concerteza.ru 31 | http://alexkasko.com 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.tomcat 39 | coyote 40 | ${tomcat6.version} 41 | 42 | 43 | org.apache.tomcat 44 | juli 45 | 46 | 47 | 48 | 49 | org.apache.tomcat 50 | catalina 51 | ${tomcat6.version} 52 | 53 | 54 | org.apache.tomcat 55 | juli 56 | 57 | 58 | org.apache.tomcat 59 | annotations-api 60 | 61 | 62 | 63 | 64 | org.apache.tomcat 65 | servlet-api 66 | ${tomcat6.version} 67 | 68 | 69 | org.apache.tomcat.extras 70 | juli 71 | ${tomcat6.version} 72 | 73 | 74 | org.apache.tomcat.extras 75 | juli-adapters 76 | ${tomcat6.version} 77 | 78 | 79 | 80 | org.springframework 81 | spring-context 82 | ${spring.version} 83 | provided 84 | 85 | 86 | org.springframework 87 | spring-web 88 | ${spring.version} 89 | provided 90 | 91 | 92 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/EmbeddedContextConfig.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.Lifecycle; 4 | import org.apache.catalina.LifecycleEvent; 5 | import org.apache.catalina.LifecycleListener; 6 | import org.apache.catalina.core.StandardContext; 7 | import org.apache.catalina.startup.WebRuleSet; 8 | import org.apache.tomcat.util.digester.Digester; 9 | import org.xml.sax.SAXException; 10 | 11 | import java.io.FileInputStream; 12 | import java.io.FileNotFoundException; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | 16 | /** 17 | * User: alexey 18 | * Date: 8/28/11 19 | */ 20 | class EmbeddedContextConfig implements LifecycleListener { 21 | private final String webXmlFile; 22 | 23 | public EmbeddedContextConfig(String webXmlFile) { 24 | this.webXmlFile = webXmlFile; 25 | } 26 | 27 | @Override 28 | @SuppressWarnings("deprecation") 29 | public void lifecycleEvent(LifecycleEvent event) { 30 | if (event.getType().equals(Lifecycle.START_EVENT)) { 31 | StandardContext context = (StandardContext) event.getLifecycle(); 32 | Digester webDigester = org.apache.catalina.startup.DigesterFactory.newDigester(false, false, new WebRuleSet()); 33 | webDigester.push(context); 34 | InputStream is = null; 35 | try { 36 | is = new FileInputStream(webXmlFile); 37 | webDigester.parse(is); 38 | context.setConfigured(true); 39 | } catch (FileNotFoundException e) { 40 | throw new RuntimeException(e); 41 | } catch (SAXException e) { 42 | throw new RuntimeException(e); 43 | } catch (IOException e) { 44 | throw new RuntimeException(e); 45 | } finally { 46 | if (is != null) { 47 | try { 48 | is.close(); 49 | } catch (IOException e) { 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/EmbeddedLoader.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.Container; 4 | import org.apache.catalina.Loader; 5 | import org.apache.catalina.core.StandardContext; 6 | 7 | import java.beans.PropertyChangeListener; 8 | 9 | /** 10 | * User: alexey 11 | * Date: 8/27/11 12 | */ 13 | class EmbeddedLoader implements Loader { 14 | 15 | private static final String[] REPOS = new String[0]; 16 | private StandardContext container; 17 | 18 | @Override 19 | public ClassLoader getClassLoader() { 20 | return getClass().getClassLoader(); 21 | } 22 | 23 | @Override 24 | public Container getContainer() { 25 | return container; 26 | } 27 | 28 | @Override 29 | public void setContainer(Container container) { 30 | this.container = (StandardContext) container; 31 | } 32 | 33 | @Override 34 | public String getInfo() { 35 | return getClass().getName(); 36 | } 37 | 38 | // unimplemented methods futher 39 | 40 | @Override 41 | public void backgroundProcess() { 42 | // this method is initially left blank 43 | } 44 | 45 | @Override 46 | public boolean getDelegate() { 47 | return false; 48 | } 49 | 50 | @Override 51 | public void setDelegate(boolean delegate) { 52 | // this method is initially left blank 53 | } 54 | 55 | @Override 56 | public boolean getReloadable() { 57 | return false; 58 | } 59 | 60 | @Override 61 | public void setReloadable(boolean reloadable) { 62 | // this method is initially left blank 63 | } 64 | 65 | @Override 66 | public void addPropertyChangeListener(PropertyChangeListener listener) { 67 | // this method is initially left blank 68 | } 69 | 70 | @Override 71 | public void addRepository(String repository) { 72 | // this method is initially left blank 73 | } 74 | 75 | @Override 76 | public String[] findRepositories() { 77 | return REPOS; 78 | } 79 | 80 | @Override 81 | public boolean modified() { 82 | return false; 83 | } 84 | 85 | @Override 86 | public void removePropertyChangeListener(PropertyChangeListener listener) { 87 | // this method is initially left blank 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/EmbeddedManager.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.Session; 4 | import org.apache.catalina.session.ManagerBase; 5 | 6 | import java.io.IOException; 7 | import java.util.concurrent.atomic.AtomicInteger; 8 | 9 | /** 10 | * User: alexey 11 | * Date: 8/27/11 12 | */ 13 | class EmbeddedManager extends ManagerBase { 14 | private int maxActiveSessions; 15 | private AtomicInteger rejectedSessions = new AtomicInteger(0); 16 | 17 | public int getMaxActiveSessions() { 18 | return maxActiveSessions; 19 | } 20 | 21 | public void setMaxActiveSessions(int maxActiveSessions) { 22 | this.maxActiveSessions = maxActiveSessions; 23 | } 24 | 25 | @Override 26 | public int getRejectedSessions() { 27 | return rejectedSessions.get(); 28 | } 29 | 30 | @Override 31 | public void setRejectedSessions(int rejectedSessions) { 32 | this.rejectedSessions.set(rejectedSessions); 33 | } 34 | 35 | @Override 36 | public Session createSession(String sessionId) { 37 | if ((maxActiveSessions >= 0) && (sessions.size() >= maxActiveSessions)) { 38 | rejectedSessions.addAndGet(1); 39 | throw new IllegalStateException(sm.getString("standardManager.createSession.ise")); 40 | } 41 | return super.createSession(sessionId); 42 | } 43 | 44 | @Override 45 | public void load() throws ClassNotFoundException, IOException { 46 | // this method is initially left blank 47 | } 48 | 49 | @Override 50 | public void unload() throws IOException { 51 | // this method is initially left blank 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/EmbeddedWrapper.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | import org.apache.catalina.core.StandardWrapper; 4 | 5 | import javax.servlet.ServletException; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 3/8/12 10 | */ 11 | 12 | // fail-fast-on-load servlet wrapper 13 | public class EmbeddedWrapper extends StandardWrapper { 14 | private static final long serialVersionUID = -188901122539015077L; 15 | 16 | @Override 17 | public void load() throws ServletException { 18 | try { 19 | super.load(); 20 | } catch (ServletException e) { 21 | throw new IllegalStateException(e); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/ExecutorState.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6; 2 | 3 | /** 4 | * User: wmel 5 | * Date: 27.06.12 6 | */ 7 | public class ExecutorState { 8 | private final int maxIdleTime; 9 | private final int maxThreads; 10 | private final int minSpareThreads; 11 | 12 | private final int activeCount; 13 | private final long completedCount; 14 | 15 | private final int corePoolSize; 16 | private final int largestPoolSize; 17 | 18 | private final int poolSize; 19 | private final int queueSize; 20 | 21 | public ExecutorState(int maxIdleTime, 22 | int maxThreads, 23 | int minSpareThreads, 24 | int activeCount, 25 | long completedCount, 26 | int corePoolSize, 27 | int largestPoolSize, 28 | int poolSize, 29 | int queueSize) { 30 | this.maxIdleTime = maxIdleTime; 31 | this.maxThreads = maxThreads; 32 | this.minSpareThreads = minSpareThreads; 33 | this.activeCount = activeCount; 34 | this.completedCount = completedCount; 35 | this.corePoolSize = corePoolSize; 36 | this.largestPoolSize = largestPoolSize; 37 | this.poolSize = poolSize; 38 | this.queueSize = queueSize; 39 | } 40 | 41 | public int getMaxIdleTime() { 42 | return maxIdleTime; 43 | } 44 | 45 | public int getMaxThreads() { 46 | return maxThreads; 47 | } 48 | 49 | public int getMinSpareThreads() { 50 | return minSpareThreads; 51 | } 52 | 53 | public int getActiveCount() { 54 | return activeCount; 55 | } 56 | 57 | public long getCompletedCount() { 58 | return completedCount; 59 | } 60 | 61 | public int getCorePoolSize() { 62 | return corePoolSize; 63 | } 64 | 65 | public int getLargestPoolSize() { 66 | return largestPoolSize; 67 | } 68 | 69 | public int getPoolSize() { 70 | return poolSize; 71 | } 72 | 73 | public int getQueueSize() { 74 | return queueSize; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/ContextProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | // http://tomcat.apache.org/tomcat-6.0-doc/config/context.html 9 | public class ContextProperties { 10 | private boolean cookies = true; 11 | private boolean disableURLRewriting = true; 12 | private int cacheMaxSizeKb = 10240; 13 | private int cacheObjectMaxSizeKb = 512; 14 | private int cacheTTLSec = 5; 15 | private boolean cachingAllowed = false; 16 | private int unloadDelayMs = 2000; 17 | private int maxActiveSessions = -1; 18 | private int sessionTimeoutMinutes = 10; 19 | private String postCharacterEncoding = "UTF-8"; 20 | 21 | public boolean isCookies() { 22 | return cookies; 23 | } 24 | 25 | public ContextProperties setCookies(boolean cookies) { 26 | this.cookies = cookies; 27 | return this; 28 | } 29 | 30 | public boolean isDisableURLRewriting() { 31 | return disableURLRewriting; 32 | } 33 | 34 | public ContextProperties setDisableURLRewriting(boolean disableURLRewriting) { 35 | this.disableURLRewriting = disableURLRewriting; 36 | return this; 37 | } 38 | 39 | public int getCacheMaxSizeKb() { 40 | return cacheMaxSizeKb; 41 | } 42 | 43 | public ContextProperties setCacheMaxSizeKb(int cacheMaxSizeKb) { 44 | this.cacheMaxSizeKb = cacheMaxSizeKb; 45 | return this; 46 | } 47 | 48 | public int getCacheObjectMaxSizeKb() { 49 | return cacheObjectMaxSizeKb; 50 | } 51 | 52 | public ContextProperties setCacheObjectMaxSizeKb(int cacheObjectMaxSizeKb) { 53 | this.cacheObjectMaxSizeKb = cacheObjectMaxSizeKb; 54 | return this; 55 | } 56 | 57 | public int getCacheTTLSec() { 58 | return cacheTTLSec; 59 | } 60 | 61 | public ContextProperties setCacheTTLSec(int cacheTTLSec) { 62 | this.cacheTTLSec = cacheTTLSec; 63 | return this; 64 | } 65 | 66 | public boolean isCachingAllowed() { 67 | return cachingAllowed; 68 | } 69 | 70 | public ContextProperties setCachingAllowed(boolean cachingAllowed) { 71 | this.cachingAllowed = cachingAllowed; 72 | return this; 73 | } 74 | 75 | public int getUnloadDelayMs() { 76 | return unloadDelayMs; 77 | } 78 | 79 | public ContextProperties setUnloadDelayMs(int unloadDelayMs) { 80 | this.unloadDelayMs = unloadDelayMs; 81 | return this; 82 | } 83 | 84 | public int getMaxActiveSessions() { 85 | return maxActiveSessions; 86 | } 87 | 88 | public ContextProperties setMaxActiveSessions(int maxActiveSessions) { 89 | this.maxActiveSessions = maxActiveSessions; 90 | return this; 91 | } 92 | 93 | public int getSessionTimeoutMinutes() { 94 | return sessionTimeoutMinutes; 95 | } 96 | 97 | public ContextProperties setSessionTimeoutMinutes(int sessionTimeoutMinutes) { 98 | this.sessionTimeoutMinutes = sessionTimeoutMinutes; 99 | return this; 100 | } 101 | 102 | public String getPostCharacterEncoding() { 103 | return postCharacterEncoding; 104 | } 105 | 106 | public ContextProperties setPostCharacterEncoding(String postCharacterEncoding) { 107 | this.postCharacterEncoding = postCharacterEncoding; 108 | return this; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/ExecutorProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | 5 | /** 6 | * User: alexey 7 | * Date: 11/4/11 8 | */ 9 | 10 | // http://tomcat.apache.org/tomcat-6.0-doc/config/executor.html 11 | public class ExecutorProperties { 12 | private String name = "etomcat-executor"; 13 | private boolean daemon = true; 14 | private String namePrefix = "etomcat-"; 15 | private int maxThreads = 200; 16 | private int minSpareThreads = 2; 17 | private int maxIdleTimeMs = 600000; 18 | private int threadPriority = Thread.NORM_PRIORITY; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public ExecutorProperties setName(String name) { 25 | this.name = name; 26 | return this; 27 | } 28 | 29 | public boolean isDaemon() { 30 | return daemon; 31 | } 32 | 33 | public ExecutorProperties setDaemon(boolean daemon) { 34 | this.daemon = daemon; 35 | return this; 36 | } 37 | 38 | public String getNamePrefix() { 39 | return namePrefix; 40 | } 41 | 42 | public ExecutorProperties setNamePrefix(String namePrefix) { 43 | this.namePrefix = namePrefix; 44 | return this; 45 | } 46 | 47 | public int getMaxThreads() { 48 | return maxThreads; 49 | } 50 | 51 | public ExecutorProperties setMaxThreads(int maxThreads) { 52 | this.maxThreads = maxThreads; 53 | return this; 54 | } 55 | 56 | public int getMinSpareThreads() { 57 | return minSpareThreads; 58 | } 59 | 60 | public ExecutorProperties setMinSpareThreads(int minSpareThreads) { 61 | this.minSpareThreads = minSpareThreads; 62 | return this; 63 | } 64 | 65 | public int getMaxIdleTimeMs() { 66 | return maxIdleTimeMs; 67 | } 68 | 69 | public ExecutorProperties setMaxIdleTimeMs(int maxIdleTimeMs) { 70 | this.maxIdleTimeMs = maxIdleTimeMs; 71 | return this; 72 | } 73 | 74 | public int getThreadPriority() { 75 | return threadPriority; 76 | } 77 | 78 | public ExecutorProperties setThreadPriority(int threadPriority) { 79 | this.threadPriority = threadPriority; 80 | return this; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/FsProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | public class FsProperties { 8 | private String confDir = "conf"; 9 | private String webXmlPath = "web.xml"; 10 | private String workDir = "work"; 11 | 12 | public String getConfDir() { 13 | return confDir; 14 | } 15 | 16 | public FsProperties setConfDir(String confDir) { 17 | this.confDir = confDir; 18 | return this; 19 | } 20 | 21 | public String getWebXmlPath() { 22 | return webXmlPath; 23 | } 24 | 25 | public FsProperties setWebXmlPath(String webXmlPath) { 26 | this.webXmlPath = webXmlPath; 27 | return this; 28 | } 29 | 30 | public String getWorkDir() { 31 | return workDir; 32 | } 33 | 34 | public FsProperties setWorkDir(String workDir) { 35 | this.workDir = workDir; 36 | return this; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/GeneralProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | public class GeneralProperties { 9 | 10 | private int port = 8080; 11 | private String docBaseDir = ""; 12 | private String contextPath = ""; 13 | 14 | public int getPort() { 15 | return port; 16 | } 17 | 18 | public GeneralProperties setPort(int port) { 19 | this.port = port; 20 | return this; 21 | } 22 | 23 | public String getDocBaseDir() { 24 | return docBaseDir; 25 | } 26 | 27 | public GeneralProperties setDocBaseDir(String docBaseDir) { 28 | this.docBaseDir = docBaseDir; 29 | return this; 30 | } 31 | 32 | public boolean isUseFsResources() { 33 | return null != docBaseDir && docBaseDir.length() > 0; 34 | } 35 | 36 | public String getContextPath() { 37 | return contextPath; 38 | } 39 | 40 | public GeneralProperties setContextPath(String contextPath) { 41 | this.contextPath = contextPath; 42 | return this; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/HostProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | // http://tomcat.apache.org/tomcat-6.0-doc/config/host.html 9 | public class HostProperties { 10 | private String name = "localhost"; 11 | private String errorReportValveClass = "org.apache.catalina.valves.ErrorReportValve"; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public HostProperties setName(String name) { 18 | this.name = name; 19 | return this; 20 | } 21 | 22 | public String getErrorReportValveClass() { 23 | return errorReportValveClass; 24 | } 25 | 26 | public HostProperties setErrorReportValveClass(String errorReportValveClass) { 27 | this.errorReportValveClass = errorReportValveClass; 28 | return this; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/config/NioProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | 5 | /** 6 | * User: alexey 7 | * Date: 11/4/11 8 | */ 9 | 10 | // http://tomcat.apache.org/tomcat-6.0-doc/config/http.html 11 | public class NioProperties { 12 | private boolean useSendfile = true; 13 | private int acceptorThreadCount = 2; 14 | private int acceptorThreadPriority = Thread.NORM_PRIORITY; 15 | private int pollerThreadCount = 2; 16 | private int pollerThreadPriority = Thread.NORM_PRIORITY; 17 | private int selectorTimeoutMs = 1000; 18 | private int oomParachute = 1048576; 19 | private int maxSelectors = 200; 20 | private int maxSpareSelectors = -1; 21 | 22 | public boolean isUseSendfile() { 23 | return useSendfile; 24 | } 25 | 26 | public NioProperties setUseSendfile(boolean useSendfile) { 27 | this.useSendfile = useSendfile; 28 | return this; 29 | } 30 | 31 | public int getAcceptorThreadCount() { 32 | return acceptorThreadCount; 33 | } 34 | 35 | public NioProperties setAcceptorThreadCount(int acceptorThreadCount) { 36 | this.acceptorThreadCount = acceptorThreadCount; 37 | return this; 38 | } 39 | 40 | public int getAcceptorThreadPriority() { 41 | return acceptorThreadPriority; 42 | } 43 | 44 | public NioProperties setAcceptorThreadPriority(int acceptorThreadPriority) { 45 | this.acceptorThreadPriority = acceptorThreadPriority; 46 | return this; 47 | } 48 | 49 | public int getPollerThreadCount() { 50 | return pollerThreadCount; 51 | } 52 | 53 | public NioProperties setPollerThreadCount(int pollerThreadCount) { 54 | this.pollerThreadCount = pollerThreadCount; 55 | return this; 56 | } 57 | 58 | public int getPollerThreadPriority() { 59 | return pollerThreadPriority; 60 | } 61 | 62 | public NioProperties setPollerThreadPriority(int pollerThreadPriority) { 63 | this.pollerThreadPriority = pollerThreadPriority; 64 | return this; 65 | } 66 | 67 | public int getSelectorTimeoutMs() { 68 | return selectorTimeoutMs; 69 | } 70 | 71 | public NioProperties setSelectorTimeoutMs(int selectorTimeoutMs) { 72 | this.selectorTimeoutMs = selectorTimeoutMs; 73 | return this; 74 | } 75 | 76 | public int getOomParachute() { 77 | return oomParachute; 78 | } 79 | 80 | public NioProperties setOomParachute(int oomParachute) { 81 | this.oomParachute = oomParachute; 82 | return this; 83 | } 84 | 85 | public int getMaxSelectors() { 86 | return maxSelectors; 87 | } 88 | 89 | public NioProperties setMaxSelectors(int maxSelectors) { 90 | this.maxSelectors = maxSelectors; 91 | return this; 92 | } 93 | 94 | public int getMaxSpareSelectors() { 95 | return maxSpareSelectors; 96 | } 97 | 98 | public NioProperties setMaxSpareSelectors(int maxSpareSelectors) { 99 | this.maxSpareSelectors = maxSpareSelectors; 100 | return this; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/context/EmbeddedAnnotationSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.context; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletContext; 7 | 8 | /** 9 | * User: alexey 10 | * Date: 4/9/12 11 | */ 12 | public class EmbeddedAnnotationSpringContext extends AnnotationConfigApplicationContext implements EmbeddedSpringContext { 13 | private ServletContext servletContext; 14 | 15 | public EmbeddedAnnotationSpringContext() { 16 | } 17 | 18 | public EmbeddedAnnotationSpringContext(Class... annotatedClasses) { 19 | super(annotatedClasses); 20 | } 21 | 22 | public EmbeddedAnnotationSpringContext(String... basePackages) { 23 | super(basePackages); 24 | } 25 | 26 | @Override 27 | public ServletContext getServletContext() { 28 | return servletContext; 29 | } 30 | 31 | @Override 32 | public void bind(ServletContext servletContext) { 33 | servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this); 34 | this.servletContext = servletContext; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/context/EmbeddedSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.context; 2 | 3 | import org.springframework.web.context.WebApplicationContext; 4 | 5 | import javax.servlet.ServletContext; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 4/9/12 10 | */ 11 | public interface EmbeddedSpringContext extends WebApplicationContext { 12 | void bind(ServletContext servletContext); 13 | } 14 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/context/EmbeddedXmlSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.context; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.support.AbstractXmlApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | import org.springframework.context.support.FileSystemXmlApplicationContext; 7 | import org.springframework.web.context.WebApplicationContext; 8 | 9 | import javax.servlet.ServletContext; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 7/24/11 14 | */ 15 | public class EmbeddedXmlSpringContext extends AbstractXmlApplicationContext implements EmbeddedSpringContext { 16 | 17 | private ServletContext servletContext; 18 | 19 | public EmbeddedXmlSpringContext(String configLocation) throws BeansException { 20 | super(null); 21 | setConfigLocations(new String[]{configLocation}); 22 | refresh(); 23 | } 24 | 25 | @Override 26 | public ServletContext getServletContext() { 27 | return servletContext; 28 | } 29 | 30 | @Override 31 | public void bind(ServletContext servletContext) { 32 | servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this); 33 | this.servletContext = servletContext; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/failfast/FailFastConnector.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.failfast; 2 | 3 | import org.apache.catalina.connector.Connector; 4 | import org.apache.catalina.connector.Response; 5 | 6 | /** 7 | * User: alexkasko 8 | * Date: 4/8/14 9 | */ 10 | public class FailFastConnector extends Connector { 11 | public FailFastConnector() throws Exception { 12 | } 13 | 14 | public FailFastConnector(String protocol) throws Exception { 15 | super(protocol); 16 | } 17 | 18 | @Override 19 | public Response createResponse() { 20 | Response response = new FailFastResponse(); 21 | response.setConnector(this); 22 | return (response); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/failfast/FailFastProxyOutputStream.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.failfast; 2 | 3 | import org.apache.catalina.connector.CoyoteOutputStream; 4 | import org.apache.catalina.connector.OutputBuffer; 5 | 6 | /** 7 | * User: alexkasko 8 | * Date: 4/8/14 9 | */ 10 | public class FailFastProxyOutputStream extends CoyoteOutputStream { 11 | protected FailFastProxyOutputStream(OutputBuffer ob) { 12 | super(ob); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/failfast/FailFastResponse.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.failfast; 2 | 3 | import org.apache.catalina.Globals; 4 | import org.apache.catalina.connector.*; 5 | 6 | import java.io.IOException; 7 | import java.io.PrintWriter; 8 | 9 | /** 10 | * User: alexkasko 11 | * Date: 4/8/14 12 | */ 13 | public class FailFastResponse extends Response { 14 | 15 | @Override 16 | public void setConnector(Connector connector) { 17 | this.connector = connector; 18 | if ("AJP/1.3".equals(connector.getProtocol())) { 19 | // default size to size of one ajp-packet 20 | outputBuffer = new OutputBuffer(8184); 21 | } else { 22 | outputBuffer = new OutputBuffer(); 23 | } 24 | outputStream = new FailFastProxyOutputStream(outputBuffer); 25 | writer = new FailFastResponseWriter(outputBuffer); 26 | } 27 | 28 | @Override 29 | public PrintWriter getReporter() throws IOException { 30 | if (outputBuffer.isNew()) { 31 | outputBuffer.checkConverter(); 32 | if (writer == null) { 33 | writer = new CoyoteWriter(outputBuffer); 34 | } 35 | return writer; 36 | } else { 37 | return null; 38 | } 39 | } 40 | 41 | @Override 42 | public PrintWriter getWriter() throws IOException { 43 | 44 | if (usingOutputStream) 45 | throw new IllegalStateException 46 | (sm.getString("coyoteResponse.getWriter.ise")); 47 | 48 | if (Globals.STRICT_SERVLET_COMPLIANCE) { 49 | /* 50 | * If the response's character encoding has not been specified as 51 | * described in getCharacterEncoding (i.e., the method 52 | * just returns the default value ISO-8859-1), 53 | * getWriter updates it to ISO-8859-1 54 | * (with the effect that a subsequent call to getContentType() will 55 | * include a charset=ISO-8859-1 component which will also be 56 | * reflected in the Content-Type response header, thereby satisfying 57 | * the Servlet spec requirement that containers must communicate the 58 | * character encoding used for the servlet response's writer to the 59 | * client). 60 | */ 61 | setCharacterEncoding(getCharacterEncoding()); 62 | } 63 | 64 | usingWriter = true; 65 | outputBuffer.checkConverter(); 66 | if (writer == null) { 67 | writer = new CoyoteWriter(outputBuffer); 68 | } 69 | return writer; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/failfast/FailFastResponseWriter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.failfast; 2 | 3 | import org.apache.catalina.connector.CoyoteWriter; 4 | import org.apache.catalina.connector.OutputBuffer; 5 | 6 | import java.io.IOException; 7 | 8 | import static ru.concerteza.springtomcat.etomcat6.failfast.SneakThrower.sneakyThrow; 9 | 10 | /** 11 | * User: alexkasko 12 | * Date: 4/8/14 13 | */ 14 | public class FailFastResponseWriter extends CoyoteWriter { 15 | public FailFastResponseWriter(OutputBuffer ob) { 16 | super(ob); 17 | } 18 | 19 | public void flush() { 20 | try { 21 | ob.flush(); 22 | } catch (IOException e) { 23 | error = true; 24 | sneakyThrow(e); 25 | } 26 | 27 | } 28 | 29 | public void write(char buf[], int off, int len) { 30 | try { 31 | ob.write(buf, off, len); 32 | } catch (IOException e) { 33 | error = true; 34 | sneakyThrow(e); 35 | } 36 | } 37 | 38 | 39 | public void write(String s, int off, int len) { 40 | try { 41 | ob.write(s, off, len); 42 | } catch (IOException e) { 43 | error = true; 44 | sneakyThrow(e); 45 | } 46 | } 47 | 48 | public boolean checkError() { 49 | try { 50 | flush(); 51 | } catch (Exception e) { 52 | if (e instanceof IOException) { 53 | // NOOP 54 | } else { 55 | sneakyThrow(e); 56 | } 57 | } 58 | return error; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/failfast/SneakThrower.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.failfast; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * User: alexkasko 7 | * Date: 4/8/14 8 | */ 9 | 10 | // http://mail.openjdk.java.net/pipermail/lambda-dev/2010-June/001552.html 11 | class SneakThrower { 12 | 13 | static RuntimeException sneakyThrow(Exception e) { 14 | SneakThrower.sneakyThrow0(e); 15 | return null; 16 | } 17 | 18 | @SuppressWarnings("unchecked") 19 | private static void sneakyThrow0(Throwable t) throws T { 20 | throw (T) t; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /etomcat6/src/main/java/ru/concerteza/springtomcat/etomcat6/valves/PostCharacterEncodingValve.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat6.valves; 2 | 3 | import org.apache.catalina.connector.Request; 4 | import org.apache.catalina.connector.Response; 5 | import org.apache.catalina.valves.ValveBase; 6 | 7 | import javax.servlet.ServletException; 8 | import java.io.IOException; 9 | import java.nio.charset.Charset; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 9/26/12 14 | */ 15 | public class PostCharacterEncodingValve extends ValveBase { 16 | private final Charset postEncoding; 17 | 18 | public PostCharacterEncodingValve(String postEncoding) { 19 | this.postEncoding = Charset.forName(postEncoding); 20 | } 21 | 22 | @Override 23 | public void invoke(Request request, Response response) throws IOException, ServletException { 24 | request.setCharacterEncoding(postEncoding.name()); 25 | getNext().invoke(request, response); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/accessdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/annotationdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | testServlet 8 | ru.concerteza.springtomcat.etomcat8.components.TestServlet 9 | 10 | 11 | 12 | testServlet 13 | /etomcat_test 14 | 15 | 16 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/appdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/appdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/appdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | file:src/main/app-dirs/appdir/conf/etomcat.properties 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/appdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/appdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/appdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/appdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | ru.concerteza.springtomcat.etomcat8.components.TestListener 19 | 20 | 21 | 22 | testFilter 23 | ru.concerteza.springtomcat.etomcat8.components.TestFilter 24 | 25 | 26 | 27 | testFilter 28 | /etomcat_test 29 | 30 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/bruteforcedir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/holderdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.HolderServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/nosslappdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/nosslappdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.TestServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/registrydir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | testServlet 9 | ru.concerteza.springtomcat.etomcat8.components.RegistryServlet 10 | 11 | 12 | 13 | testServlet 14 | /etomcat_test 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/webappdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/conf/etomcat-test-ctx.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | file:src/main/app-dirs/webappdir/conf/etomcat.properties 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/webappdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keyAlias=etomcat_test 5 | etomcat.docBaseDir=static -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | default 9 | org.apache.catalina.servlets.DefaultServlet 10 | 11 | debug 12 | 0 13 | 14 | 15 | listings 16 | false 17 | 18 | 1 19 | 20 | 21 | 22 | default 23 | / 24 | 25 | 26 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/webappdir/static/static.html: -------------------------------------------------------------------------------- 1 | static content -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509annotationdir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509annotationdir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509annotationdir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509annotationdir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509annotationdir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keystoreProvider=SunJSSE 5 | etomcat.ssl.keyAlias=etomcat_test 6 | etomcat.ssl.truststoreFile=truststore.jks 7 | etomcat.ssl.truststorePass=amber% 8 | etomcat.ssl.truststoreType=JKS 9 | etomcat.ssl.truststoreProvider=SUN -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509annotationdir/conf/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509annotationdir/conf/truststore.jks -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509annotationdir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | x509Servlet 9 | ru.concerteza.springtomcat.etomcat8.x509.X509Servlet 10 | 11 | 12 | 13 | x509Servlet 14 | /etomcat_x509 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509dir/conf/concerteza.crt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509dir/conf/concerteza.crt -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509dir/conf/etomcat.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509dir/conf/etomcat.p12 -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509dir/conf/etomcat.properties: -------------------------------------------------------------------------------- 1 | etomcat.port=8443 2 | etomcat.ssl.keystoreFile=etomcat.p12 3 | etomcat.ssl.keystorePass=storepass 4 | etomcat.ssl.keystoreProvider=SunJSSE 5 | etomcat.ssl.keyAlias=etomcat_test 6 | etomcat.ssl.truststoreFile=truststore.jks 7 | etomcat.ssl.truststorePass=amber% 8 | etomcat.ssl.truststoreType=JKS 9 | etomcat.ssl.truststoreProvider=SUN -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509dir/conf/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/main/app-dirs/x509dir/conf/truststore.jks -------------------------------------------------------------------------------- /etomcat8-test/src/main/app-dirs/x509dir/conf/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | x509Servlet 9 | ru.concerteza.springtomcat.etomcat8.x509.X509Servlet 10 | 11 | 12 | 13 | x509Servlet 14 | /etomcat_x509 15 | 16 | 17 | 18 | springSecurityFilterChain 19 | org.springframework.web.filter.DelegatingFilterProxy 20 | 21 | 22 | 23 | springSecurityFilterChain 24 | /* 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.security.web.session.HttpSessionEventPublisher 31 | 32 | 33 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/HolderServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | import ru.concerteza.springtomcat.components.holder.SessionHolder; 6 | 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | 13 | /** 14 | * User: alexey 15 | * Date: 3/2/12 16 | */ 17 | public class HolderServlet extends HttpServlet { 18 | private static final long serialVersionUID = -4967422466219316383L; 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 23 | SessionHolder holder = context.getBean(SessionHolder.class); 24 | resp.getOutputStream().write(holder.get().getId().getBytes("UTF-8")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/RegistryServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.web.context.WebApplicationContext; 7 | import ru.concerteza.springtomcat.components.registry.SessionRegistry; 8 | 9 | import javax.servlet.ServletException; 10 | import javax.servlet.http.HttpServlet; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | import java.io.IOException; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 3/2/12 18 | */ 19 | public class RegistryServlet extends HttpServlet { 20 | private static final long serialVersionUID = 4131885764840516610L; 21 | private final Logger logger = LoggerFactory.getLogger(getClass()); 22 | 23 | @Override 24 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 25 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 26 | SessionRegistry registry = context.getBean(SessionRegistry.class); 27 | logger.info(registry.dump()); 28 | resp.getOutputStream().write(Boolean.toString(registry.contains("anonymous")).getBytes("UTF-8")); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/TestFilter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import javax.servlet.*; 4 | import java.io.IOException; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 8/29/11 9 | */ 10 | public class TestFilter implements Filter { 11 | private static boolean initialized; 12 | private static boolean filtered; 13 | 14 | @Override 15 | public void init(FilterConfig filterConfig) throws ServletException { 16 | initialized = true; 17 | } 18 | 19 | @Override 20 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 21 | filtered = true; 22 | chain.doFilter(request, response); 23 | } 24 | 25 | @Override 26 | public void destroy() { 27 | // this line is initially left blank 28 | } 29 | 30 | public static boolean isInitialized() { 31 | return initialized; 32 | } 33 | 34 | public static boolean isFiltered() { 35 | return filtered; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/TestListener.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 8/29/11 9 | */ 10 | public class TestListener implements ServletContextListener { 11 | private static boolean initialized = false; 12 | 13 | @Override 14 | public void contextInitialized(ServletContextEvent sce) { 15 | initialized = true; 16 | } 17 | 18 | @Override 19 | public void contextDestroyed(ServletContextEvent sce) { 20 | // this line is initially left blank 21 | } 22 | 23 | public static boolean isInitialized() { 24 | return initialized; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/TestLogConsumer.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import ru.concerteza.springtomcat.components.access.AccessConsumer; 4 | import ru.concerteza.springtomcat.components.access.AccessEvent; 5 | 6 | /** 7 | * User: alexey 8 | * Date: 3/1/12 9 | */ 10 | public class TestLogConsumer implements AccessConsumer { 11 | private static AccessEvent lastEvent; 12 | 13 | @Override 14 | public void consume(AccessEvent event) { 15 | TestLogConsumer.lastEvent = event; 16 | } 17 | 18 | public static AccessEvent getLastEvent() { 19 | return lastEvent; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/components/TestServlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.components; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 8/28/11 15 | */ 16 | public class TestServlet extends HttpServlet { 17 | private static final long serialVersionUID = -8124821589871278803L; 18 | public static final String CONTENT = "EmbeddedXmlSpringContext"; 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 23 | resp.getOutputStream().write(context.getClass().getSimpleName().getBytes("UTF-8")); 24 | } 25 | 26 | @Override 27 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 | String param = req.getParameter("foo"); 29 | boolean res = "bar".equals(param); 30 | resp.getOutputStream().write(Boolean.toString(res).getBytes("UTF-8")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/x509/AuthUserDetailService.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.x509; 2 | 3 | import org.springframework.dao.DataAccessException; 4 | import org.springframework.security.core.GrantedAuthority; 5 | import org.springframework.security.core.userdetails.*; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * User: alexey 12 | * Date: 3/4/12 13 | */ 14 | 15 | @Service("authUserDetailsService") 16 | public class AuthUserDetailService implements UserDetailsService { 17 | @Override 18 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { 19 | if("etomcat_client".equals(username)) { 20 | return new User("etomcat_client", "", true, true, true, true, Arrays.asList(new Role("ROLE_ETOMCAT_CLIENT"))); 21 | } 22 | throw new UsernameNotFoundException(username); 23 | } 24 | 25 | private class Role implements GrantedAuthority { 26 | private static final long serialVersionUID = 2191234214967125385L; 27 | private final String authority; 28 | 29 | private Role(String authority) { 30 | this.authority = authority; 31 | } 32 | 33 | @Override 34 | public String getAuthority() { 35 | return authority; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/x509/SecuredService.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.x509; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | import javax.annotation.security.RolesAllowed; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 3/4/12 10 | */ 11 | 12 | public interface SecuredService { 13 | public static final String GREETING = "Hello ETomcat Client!"; 14 | 15 | String hello(); 16 | 17 | String fail(); 18 | } 19 | 20 | @Service 21 | class SecuredServiceImpl implements SecuredService { 22 | 23 | @RolesAllowed("ROLE_ETOMCAT_CLIENT") 24 | public String hello() { 25 | return GREETING; 26 | } 27 | 28 | @RolesAllowed("ROLE_FAIL") 29 | public String fail() { 30 | return "Access failed"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /etomcat8-test/src/main/java/ru/concerteza/springtomcat/etomcat8/x509/X509Servlet.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.x509; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 3/4/12 15 | */ 16 | public class X509Servlet extends HttpServlet { 17 | private static final long serialVersionUID = -4988677350741242648L; 18 | 19 | @Override 20 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 22 | SecuredService service = context.getBean(SecuredService.class); 23 | String reply = service.hello(); 24 | resp.getOutputStream().write(reply.getBytes("UTF-8")); 25 | } 26 | 27 | @Override 28 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 29 | ApplicationContext context = (ApplicationContext) req.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 30 | SecuredService service = context.getBean(SecuredService.class); 31 | String reply = service.fail(); 32 | resp.getOutputStream().write(reply.getBytes("UTF-8")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/AccessLogTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import ru.concerteza.springtomcat.components.access.AccessEvent; 10 | import ru.concerteza.springtomcat.etomcat8.components.TestLogConsumer; 11 | import ru.concerteza.springtomcat.etomcat8.components.TestServlet; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 3/1/12 18 | */ 19 | public class AccessLogTest extends TestSupertype { 20 | private final Logger logger = LoggerFactory.getLogger(getClass()); 21 | 22 | @Test 23 | public void testAccessLog() throws Exception { 24 | HttpClient client = new HttpClient(); 25 | // test get 26 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 27 | client.executeMethod(get); 28 | // test data 29 | byte[] responseBody = get.getResponseBody(); 30 | String content = new String(responseBody, "UTF-8"); 31 | assertEquals("Servlet get fail", TestServlet.CONTENT, content); 32 | // test log event 33 | AccessEvent ev = TestLogConsumer.getLastEvent(); 34 | assertEquals("Method log fail", "GET", ev.getRequestMethod()); 35 | assertEquals("Status log fail", 200, ev.getResponseCode()); 36 | assertEquals("URL log fail", "/etomcat_test", ev.getUrl()); 37 | } 38 | 39 | @Override 40 | protected String dirname() { 41 | return "accessdir"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/AnnotationConfigTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import ru.concerteza.springtomcat.etomcat8.context.EmbeddedAnnotationSpringContext; 11 | 12 | import java.io.File; 13 | 14 | /** 15 | * User: alexey 16 | * Date: 4/9/12 17 | */ 18 | public class AnnotationConfigTest extends AnnotationTestSupertype { 19 | 20 | @Test 21 | public void testRunning() throws Exception { 22 | HttpClient client = new HttpClient(); 23 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 24 | client.executeMethod(get); 25 | byte[] responseBody = get.getResponseBody(); 26 | String content = new String(responseBody, "UTF-8"); 27 | Assert.assertEquals("Servlet get fail", EmbeddedAnnotationSpringContext.class.getSimpleName(), content); 28 | } 29 | 30 | @Override 31 | protected String dirname() { 32 | return "annotationdir"; 33 | } 34 | 35 | @Override 36 | protected Class configClass() { 37 | return Config.class; 38 | } 39 | 40 | @Configuration 41 | public static class Config { 42 | @Bean(destroyMethod = "stop") 43 | public EmbeddedTomcat etomcat() { 44 | return new EmbeddedTomcat(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/AnnotationTestSupertype.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.LifecycleException; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.springframework.context.support.AbstractApplicationContext; 7 | import ru.concerteza.springtomcat.etomcat8.context.EmbeddedAnnotationSpringContext; 8 | 9 | import java.io.File; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 4/9/12 14 | */ 15 | public abstract class AnnotationTestSupertype { 16 | private AbstractApplicationContext ctx; 17 | 18 | @Before 19 | public void setUp() throws Exception { 20 | File baseDir = new File("src/main/app-dirs/" + dirname()); 21 | ctx = new EmbeddedAnnotationSpringContext(configClass()); 22 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 23 | System.out.println(ctx.getBean(EmbeddedTomcat.class).getInnerExecutorState()); 24 | } 25 | 26 | @After 27 | public void tearDown() throws LifecycleException, InterruptedException { 28 | if (null != ctx) ctx.close(); 29 | } 30 | 31 | protected abstract String dirname(); 32 | 33 | protected abstract Class configClass(); 34 | } 35 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/AppTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.NameValuePair; 6 | import org.apache.commons.httpclient.cookie.CookiePolicy; 7 | import org.apache.commons.httpclient.methods.GetMethod; 8 | import org.apache.commons.httpclient.methods.PostMethod; 9 | import org.junit.*; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | import ru.concerteza.springtomcat.etomcat8.components.TestFilter; 13 | import ru.concerteza.springtomcat.etomcat8.components.TestListener; 14 | import ru.concerteza.springtomcat.etomcat8.components.TestServlet; 15 | 16 | /** 17 | * User: alexey 18 | * Date: 8/28/11 19 | */ 20 | 21 | public class AppTest extends TestSupertype { 22 | private final Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testRunning() throws Exception { 26 | HttpClient client = new HttpClient(); 27 | setupSsl(); 28 | // test get 29 | HttpMethod get = new GetMethod("https://127.0.0.1:8443/etomcat_test"); 30 | get.getParams().setCookiePolicy(CookiePolicy.RFC_2965); 31 | // Thread.sleep(10000000); 32 | client.executeMethod(get); 33 | byte[] responseBody = get.getResponseBody(); 34 | String content = new String(responseBody, "UTF-8"); 35 | Assert.assertEquals("Servlet get fail", TestServlet.CONTENT, content); 36 | Assert.assertTrue("Listener fail", TestListener.isInitialized()); 37 | Assert.assertTrue("Filter init fail", TestFilter.isInitialized()); 38 | Assert.assertTrue("Filter fail", TestFilter.isFiltered()); 39 | // test post 40 | PostMethod post = new PostMethod("https://127.0.0.1:8443/etomcat_test"); 41 | post.getParams().setCookiePolicy(CookiePolicy.RFC_2965); 42 | NameValuePair[] data = { 43 | new NameValuePair("foo", "bar"), 44 | }; 45 | post.setRequestBody(data); 46 | client.executeMethod(post); 47 | byte[] postBody = post.getResponseBody(); 48 | String res = new String(postBody, "UTF-8"); 49 | Assert.assertEquals("Servlet post fail", Boolean.TRUE.toString(), res); 50 | // test 404 51 | GetMethod fail = new GetMethod("https://127.0.0.1:8443/foobar"); 52 | client.executeMethod(fail); 53 | Assert.assertEquals("Wrong result code", 404, fail.getStatusCode()); 54 | } 55 | 56 | @Override 57 | protected String dirname() { 58 | return "appdir"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/BruteforceTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.Credentials; 4 | import org.apache.commons.httpclient.HttpClient; 5 | import org.apache.commons.httpclient.HttpMethod; 6 | import org.apache.commons.httpclient.UsernamePasswordCredentials; 7 | import org.apache.commons.httpclient.auth.AuthScope; 8 | import org.apache.commons.httpclient.methods.GetMethod; 9 | import org.junit.Test; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 3/1/12 18 | */ 19 | public class BruteforceTest extends TestSupertype { 20 | private final Logger logger = LoggerFactory.getLogger(getClass()); 21 | 22 | @Test 23 | public void testBruteforce() throws Exception { 24 | HttpClient client = new HttpClient(); 25 | client.getParams().setAuthenticationPreemptive(true); 26 | Credentials failcreds = new UsernamePasswordCredentials("admin", "fail"); 27 | Credentials wincreds = new UsernamePasswordCredentials("admin", "password"); 28 | // brute 29 | client.getState().setCredentials(AuthScope.ANY, failcreds); 30 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 31 | client.executeMethod(get); 32 | assertEquals("First access fail", 401, get.getStatusCode()); 33 | client.executeMethod(get); 34 | assertEquals("Last access fail", 401, get.getStatusCode()); 35 | // closed 36 | client.executeMethod(get); 37 | assertEquals("Improbable fail", 401, get.getStatusCode()); 38 | // change appearance 39 | client.getState().setCredentials(AuthScope.ANY, wincreds); 40 | client.executeMethod(get); 41 | assertEquals("Bruteforce fail", 401, get.getStatusCode()); 42 | // cooldown 43 | Thread.sleep(1000); 44 | client.executeMethod(get); 45 | assertEquals("Cooldowned access fail", 200, get.getStatusCode()); 46 | } 47 | 48 | @Override 49 | protected String dirname() { 50 | return "bruteforcedir"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/HolderTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Test; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import static org.junit.Assert.assertTrue; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 3/2/12 15 | */ 16 | public class HolderTest extends TestSupertype { 17 | private final Logger logger = LoggerFactory.getLogger(getClass()); 18 | 19 | @Test 20 | public void testAccessLog() throws Exception { 21 | HttpClient client = new HttpClient(); 22 | // test get 23 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 24 | client.executeMethod(get); 25 | // test data 26 | byte[] responseBody = get.getResponseBody(); 27 | String content = new String(responseBody, "UTF-8"); 28 | assertTrue(content.matches("^[A-Z0-9]{32}$")); 29 | } 30 | 31 | @Override 32 | protected String dirname() { 33 | return "holderdir"; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/NoSslTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import ru.concerteza.springtomcat.etomcat8.components.TestServlet; 11 | 12 | /** 13 | * User: alexey 14 | * Date: 8/28/11 15 | */ 16 | 17 | public class NoSslTest extends TestSupertype { 18 | private final Logger logger = LoggerFactory.getLogger(getClass()); 19 | 20 | @Test 21 | public void testRunning() throws Exception { 22 | HttpClient client = new HttpClient(); 23 | // test get 24 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 25 | // Thread.sleep(10000000); 26 | client.executeMethod(get); 27 | byte[] responseBody = get.getResponseBody(); 28 | String content = new String(responseBody, "UTF-8"); 29 | Assert.assertEquals("Servlet get fail", TestServlet.CONTENT, content); 30 | } 31 | 32 | @Override 33 | protected String dirname() { 34 | return "nosslappdir"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/RegistryTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.cookie.CookiePolicy; 6 | import org.apache.commons.httpclient.methods.GetMethod; 7 | import org.junit.Test; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | import java.util.concurrent.atomic.AtomicReference; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | /** 18 | * User: alexey 19 | * Date: 3/2/12 20 | */ 21 | public class RegistryTest extends TestSupertype { 22 | private final Logger logger = LoggerFactory.getLogger(getClass()); 23 | private AtomicReference sessionCookie = new AtomicReference(); 24 | 25 | @Test 26 | public void testRegistry() throws Exception { 27 | { 28 | HttpClient client = new HttpClient(); 29 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 30 | // test get 31 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 32 | client.executeMethod(get); 33 | sessionCookie.compareAndSet(null, get.getResponseHeader("Set-Cookie").getValue()); 34 | // test data 35 | byte[] responseBody = get.getResponseBody(); 36 | String content = new String(responseBody, "UTF-8"); 37 | assertEquals("Registry fail", "true", content); 38 | // subsequent requests 39 | HttpMethod get1 = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 40 | String cookie = sessionCookie.get(); 41 | if(null != cookie) get1.setRequestHeader("Cookie", cookie); 42 | client.executeMethod(get1); 43 | sessionCookie.compareAndSet(null, get.getResponseHeader("Set-Cookie").getValue()); 44 | assertEquals(HttpServletResponse.SC_OK, get1.getStatusCode()); 45 | } 46 | { 47 | // cookie test 48 | HttpClient client = new HttpClient(); 49 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 50 | HttpMethod get1 = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 51 | String cookie = sessionCookie.get(); 52 | if(null != cookie) get1.setRequestHeader("Cookie", cookie); 53 | client.executeMethod(get1); 54 | assertEquals(HttpServletResponse.SC_OK, get1.getStatusCode()); 55 | } 56 | { 57 | // concurrent test 58 | HttpClient client = new HttpClient(); 59 | client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 60 | HttpMethod get = new GetMethod("http://127.0.0.1:8080/etomcat_test"); 61 | get.addRequestHeader("X-Forwarded-For", "192.168.42.42"); 62 | client.executeMethod(get); 63 | assertEquals(HttpServletResponse.SC_UNAUTHORIZED, get.getStatusCode()); 64 | byte[] responseBody = get.getResponseBody(); 65 | String content = new String(responseBody, "UTF-8"); 66 | assertEquals("127.0.0.1", content); 67 | } 68 | } 69 | 70 | @Override 71 | protected String dirname() { 72 | return "registrydir"; 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/SslHelper.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.protocol.Protocol; 4 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 5 | import org.jsslutils.extra.apachehttpclient.SslContextedSecureProtocolSocketFactory; 6 | 7 | import javax.net.ssl.KeyManagerFactory; 8 | import javax.net.ssl.SSLContext; 9 | import javax.net.ssl.TrustManagerFactory; 10 | import java.io.IOException; 11 | import java.security.*; 12 | import java.security.cert.CertificateException; 13 | 14 | /** 15 | * User: alexey 16 | * Date: 4/9/12 17 | */ 18 | public class SslHelper { 19 | public static void setupClientSsl() throws NoSuchProviderException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException { 20 | // truststore 21 | KeyStore trustStore = KeyStore.getInstance("JKS", "SUN"); 22 | trustStore.load(TestSupertype.class.getResourceAsStream("/client-truststore.jks"), "amber%".toCharArray()); 23 | String alg = KeyManagerFactory.getDefaultAlgorithm(); 24 | TrustManagerFactory fac = TrustManagerFactory.getInstance(alg); 25 | fac.init(trustStore); 26 | // keystore 27 | KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE"); 28 | keystore.load(X509Test.class.getResourceAsStream("/etomcat_client.p12"), "etomcat".toCharArray()); 29 | String keyAlg = KeyManagerFactory.getDefaultAlgorithm(); 30 | KeyManagerFactory keyFac = KeyManagerFactory.getInstance(keyAlg); 31 | keyFac.init(keystore, "etomcat".toCharArray()); 32 | // context 33 | SSLContext ctx = SSLContext.getInstance("TLS", "SunJSSE"); 34 | ctx.init(keyFac.getKeyManagers(), fac.getTrustManagers(), new SecureRandom()); 35 | SslContextedSecureProtocolSocketFactory secureProtocolSocketFactory = new SslContextedSecureProtocolSocketFactory(ctx); 36 | Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 8443)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/StartStopTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.junit.Test; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.support.AbstractApplicationContext; 7 | import ru.concerteza.springtomcat.etomcat8.context.EmbeddedAnnotationSpringContext; 8 | 9 | import java.io.File; 10 | 11 | /** 12 | * @author alexkasko 13 | * Date: 9/22/14 14 | */ 15 | public class StartStopTest { 16 | 17 | @Test 18 | public void test() throws InterruptedException { 19 | File baseDir = new File("src/main/app-dirs/annotationdir"); 20 | AbstractApplicationContext ctx; 21 | // 1 22 | ctx = new EmbeddedAnnotationSpringContext(Config.class); 23 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 24 | ctx.close(); 25 | // 2 26 | ctx = new EmbeddedAnnotationSpringContext(Config.class); 27 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 28 | ctx.close(); 29 | // 3 30 | ctx = new EmbeddedAnnotationSpringContext(Config.class); 31 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 32 | ctx.close(); 33 | } 34 | 35 | @Configuration 36 | public static class Config { 37 | @Bean(destroyMethod = "stop") 38 | public EmbeddedTomcat etomcat() { 39 | return new EmbeddedTomcat(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/TestSupertype.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.LifecycleException; 4 | import org.apache.commons.httpclient.protocol.Protocol; 5 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 6 | import org.jsslutils.extra.apachehttpclient.SslContextedSecureProtocolSocketFactory; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.springframework.context.support.AbstractApplicationContext; 10 | import ru.concerteza.springtomcat.etomcat8.context.EmbeddedXmlSpringContext; 11 | 12 | import javax.net.ssl.*; 13 | import java.io.File; 14 | import java.io.IOException; 15 | import java.security.*; 16 | import java.security.cert.CertificateException; 17 | 18 | import static java.io.File.separator; 19 | 20 | /** 21 | * User: alexey 22 | * Date: 8/29/11 23 | */ 24 | public abstract class TestSupertype { 25 | 26 | private AbstractApplicationContext ctx; 27 | 28 | @Before 29 | public void setUp() throws Exception { 30 | File baseDir = new File("src" + separator + "main" + separator + "app-dirs" + separator + dirname()); 31 | File confDir = new File(baseDir, "conf"); 32 | File ctxFile = new File(confDir, "etomcat-test-ctx.xml"); 33 | ctx = new EmbeddedXmlSpringContext("file:" + ctxFile.getPath()); 34 | ctx.getBean(EmbeddedTomcat.class).start(baseDir); 35 | } 36 | 37 | @After 38 | public void tearDown() throws LifecycleException, InterruptedException { 39 | if(null != ctx) ctx.close(); 40 | } 41 | 42 | protected void setupSsl() throws NoSuchProviderException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException { 43 | KeyStore trustStore = KeyStore.getInstance("JKS", "SUN"); 44 | trustStore.load(TestSupertype.class.getResourceAsStream("/client-truststore.jks"), "amber%".toCharArray()); 45 | String alg = KeyManagerFactory.getDefaultAlgorithm(); 46 | TrustManagerFactory fac = TrustManagerFactory.getInstance(alg); 47 | fac.init(trustStore); 48 | TrustManager[] tms = fac.getTrustManagers(); 49 | SSLContext ctx = SSLContext.getInstance("TLS", "SunJSSE"); 50 | ctx.init(new KeyManager[] {}, tms, new SecureRandom()); 51 | SslContextedSecureProtocolSocketFactory secureProtocolSocketFactory = new SslContextedSecureProtocolSocketFactory(ctx); 52 | Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 8443)); 53 | } 54 | 55 | protected abstract String dirname(); 56 | } 57 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/WebAppTest.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * User: alexey 11 | * Date: 8/29/11 12 | */ 13 | public class WebAppTest extends TestSupertype { 14 | 15 | @Test 16 | public void testRunning() throws Exception { 17 | HttpClient client = new HttpClient(); 18 | setupSsl(); 19 | HttpMethod method = new GetMethod("https://127.0.0.1:8443/static.html"); 20 | // Thread.sleep(10000000); 21 | client.executeMethod(method); 22 | byte[] responseBody = method.getResponseBody(); 23 | String content = new String(responseBody, "UTF-8"); 24 | Assert.assertEquals("Static fail", "static content", content); 25 | } 26 | 27 | @Override 28 | protected String dirname() { 29 | return "webappdir"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/java/ru/concerteza/springtomcat/etomcat8/X509Test.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.commons.httpclient.HttpClient; 4 | import org.apache.commons.httpclient.HttpMethod; 5 | import org.apache.commons.httpclient.methods.GetMethod; 6 | import org.apache.commons.httpclient.methods.PostMethod; 7 | import org.junit.Test; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import ru.concerteza.springtomcat.etomcat8.x509.SecuredService; 11 | 12 | import static org.junit.Assert.assertEquals; 13 | import static ru.concerteza.springtomcat.etomcat8.SslHelper.setupClientSsl; 14 | 15 | /** 16 | * User: alexey 17 | * Date: 8/28/11 18 | */ 19 | 20 | public class X509Test extends TestSupertype { 21 | private final Logger logger = LoggerFactory.getLogger(getClass()); 22 | 23 | @Test 24 | public void testRunning() throws Exception { 25 | HttpClient client = new HttpClient(); 26 | setupClientSsl(); 27 | // test get 28 | HttpMethod get = new GetMethod("https://127.0.0.1:8443/etomcat_x509"); 29 | client.executeMethod(get); 30 | byte[] responseBody = get.getResponseBody(); 31 | String content = new String(responseBody, "UTF-8"); 32 | assertEquals("Servlet get fail", SecuredService.GREETING, content); 33 | // test assess denied 34 | HttpMethod post = new PostMethod("https://127.0.0.1:8443/etomcat_x509"); 35 | client.executeMethod(post); 36 | assertEquals("Method security fail get fail", 403, post.getStatusCode()); 37 | } 38 | 39 | @Override 40 | protected String dirname() { 41 | return "x509dir"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/resources/client-truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/test/resources/client-truststore.jks -------------------------------------------------------------------------------- /etomcat8-test/src/test/resources/etomcat_client.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alx3apps/spring-embedded-tomcat/e50ed889c0f1aeafb6f5f324de7f927178a13e23/etomcat8-test/src/test/resources/etomcat_client.p12 -------------------------------------------------------------------------------- /etomcat8-test/src/test/resources/global-metod-security-enabler.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | -------------------------------------------------------------------------------- /etomcat8-test/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=ALL, console 2 | 3 | log4j.appender.console=org.apache.log4j.ConsoleAppender 4 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 5 | # http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html 6 | log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p %-10.10t %-30.30c] %m%n 7 | log4j.appender.console.encoding=UTF-8 8 | log4j.appender.console.Threshold=ALL 9 | 10 | log4j.logger.ru.concerteza.springtomcat=INFO 11 | log4j.logger.org.springframework=WARN 12 | log4j.logger.org.apache.commons.digester=WARN 13 | log4j.logger.org.apache.tomcat=WARN 14 | log4j.logger.org.apache.catalina=WARN 15 | log4j.logger.org.apache.coyote=WARN 16 | # listing properties 17 | #log4j.logger.org.apache.coyote.http11.Http11NioProtocol=TRACE 18 | # prevent some warning 19 | log4j.logger.org.apache.catalina.startup.DigesterFactory=ERROR 20 | # http client 21 | log4j.logger.httpclient=INFO 22 | log4j.logger.org.apache.commons.httpclient=INFO 23 | 24 | -------------------------------------------------------------------------------- /etomcat8/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | ru.concerteza.spring-embedded-tomcat 6 | spring-embedded-tomcat 7 | 1.5.3-SNAPSHOT 8 | 9 | etomcat8 10 | jar 11 | Apache Tomcat 8 Embedded Into Spring 12 | 13 | Apache Tomcat 8 embedded into Springframework context 14 | https://github.com/alx3apps/spring-embedded-tomcat 15 | 16 | 17 | Apache License 2.0 18 | http://www.apache.org/licenses/LICENSE-2.0 19 | 20 | 21 | 22 | https://github.com/alx3apps/spring-embedded-tomcat 23 | scm:git:https://github.com/alx3apps/spring-embedded-tomcat.git 24 | scm:git:https://github.com/alx3apps/spring-embedded-tomcat.git 25 | HEAD 26 | 27 | 28 | 29 | Alex Kasko 30 | kalexey@concerteza.ru 31 | http://alexkasko.com 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.tomcat.embed 39 | tomcat-embed-core 40 | ${tomcat8.version} 41 | 42 | 43 | org.apache.tomcat.embed 44 | tomcat-embed-logging-log4j 45 | ${tomcat8.version} 46 | 47 | 48 | 49 | 50 | org.springframework 51 | spring-context 52 | ${spring.version} 53 | provided 54 | 55 | 56 | org.springframework 57 | spring-web 58 | ${spring.version} 59 | provided 60 | 61 | 62 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedEngine.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.LifecycleException; 4 | import org.apache.catalina.core.StandardEngine; 5 | 6 | /** 7 | * @author alexkasko 8 | * Date: 9/22/14 9 | */ 10 | public class EmbeddedEngine extends StandardEngine { 11 | @Override 12 | protected void initInternal() throws LifecycleException { 13 | super.initInternal(); 14 | this.startStopExecutor = new SameThreadExecutor(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedLoader.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.Container; 4 | import org.apache.catalina.Context; 5 | import org.apache.catalina.Loader; 6 | import org.apache.catalina.core.StandardContext; 7 | 8 | import java.beans.PropertyChangeListener; 9 | 10 | /** 11 | * User: alexey 12 | * Date: 8/27/11 13 | */ 14 | class EmbeddedLoader implements Loader { 15 | 16 | private Context context; 17 | 18 | @Override 19 | public ClassLoader getClassLoader() { 20 | return getClass().getClassLoader(); 21 | } 22 | 23 | @Override 24 | public Context getContext() { 25 | return context; 26 | } 27 | 28 | @Override 29 | public void setContext(Context context) { 30 | this.context = context; 31 | } 32 | 33 | // unimplemented methods futher 34 | 35 | @Override 36 | public void backgroundProcess() { 37 | // this method is initially left blank 38 | } 39 | 40 | @Override 41 | public boolean getDelegate() { 42 | return false; 43 | } 44 | 45 | @Override 46 | public void setDelegate(boolean delegate) { 47 | // this method is initially left blank 48 | } 49 | 50 | @Override 51 | public boolean getReloadable() { 52 | return false; 53 | } 54 | 55 | @Override 56 | public void setReloadable(boolean reloadable) { 57 | // this method is initially left blank 58 | } 59 | 60 | @Override 61 | public void addPropertyChangeListener(PropertyChangeListener listener) { 62 | // this method is initially left blank 63 | } 64 | 65 | @Override 66 | public boolean modified() { 67 | return false; 68 | } 69 | 70 | @Override 71 | public void removePropertyChangeListener(PropertyChangeListener listener) { 72 | // this method is initially left blank 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedManager.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.*; 4 | import org.apache.catalina.security.SecurityUtil; 5 | import org.apache.catalina.session.ManagerBase; 6 | import org.apache.catalina.session.StandardSession; 7 | import org.apache.catalina.util.CustomObjectInputStream; 8 | import org.apache.juli.logging.Log; 9 | import org.apache.juli.logging.LogFactory; 10 | import org.apache.tomcat.util.ExceptionUtils; 11 | 12 | import javax.servlet.ServletContext; 13 | import java.io.*; 14 | import java.security.AccessController; 15 | import java.security.PrivilegedActionException; 16 | import java.security.PrivilegedExceptionAction; 17 | import java.util.ArrayList; 18 | import java.util.Iterator; 19 | import java.util.concurrent.atomic.AtomicInteger; 20 | 21 | /** 22 | * User: alexey 23 | * Date: 8/27/11 24 | */ 25 | class EmbeddedManager extends ManagerBase { 26 | 27 | /** 28 | * @inheritDoc 29 | */ 30 | @Override 31 | public String getName() { 32 | return getClass().getName(); 33 | } 34 | 35 | /** 36 | * @inheritDoc 37 | */ 38 | @Override 39 | public void load() throws ClassNotFoundException, IOException { 40 | // noop 41 | } 42 | 43 | /** 44 | * @inheritDoc 45 | */ 46 | @Override 47 | public void unload() throws IOException { 48 | // noop 49 | } 50 | 51 | /** 52 | * @inheritDoc 53 | */ 54 | @Override 55 | protected synchronized void startInternal() throws LifecycleException { 56 | super.startInternal(); 57 | setState(LifecycleState.STARTING); 58 | } 59 | 60 | /** 61 | * @inheritDoc 62 | */ 63 | @Override 64 | protected synchronized void stopInternal() throws LifecycleException { 65 | setState(LifecycleState.STOPPING); 66 | 67 | // Expire all active sessions 68 | Session sessions[] = findSessions(); 69 | for (int i = 0; i < sessions.length; i++) { 70 | Session session = sessions[i]; 71 | try { 72 | if (session.isValid()) { 73 | session.expire(); 74 | } 75 | } catch (Throwable t) { 76 | ExceptionUtils.handleThrowable(t); 77 | } finally { 78 | // Measure against memory leaking if references to the session 79 | // object are kept in a shared field somewhere 80 | session.recycle(); 81 | } 82 | } 83 | 84 | // Require a new random number generator if we are restarted 85 | super.stopInternal(); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedService.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.Executor; 4 | import org.apache.catalina.LifecycleException; 5 | import org.apache.catalina.LifecycleState; 6 | import org.apache.catalina.connector.Connector; 7 | import org.apache.catalina.core.StandardService; 8 | 9 | /** 10 | * @author alexkasko 11 | * Date: 9/22/14 12 | */ 13 | public class EmbeddedService extends StandardService { 14 | @Override 15 | protected void startInternal() throws LifecycleException { 16 | setState(LifecycleState.STARTING); 17 | container.start(); 18 | for (Executor executor : executors) { 19 | executor.start(); 20 | } 21 | mapperListener.start(); 22 | for (Connector connector : connectors) { 23 | connector.start(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedTomcat.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.core.*; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | import ru.concerteza.springtomcat.etomcat8.config.*; 7 | import ru.concerteza.springtomcat.etomcat8.context.EmbeddedSpringContext; 8 | 9 | /** 10 | * User: alexey Date: 6/21/11 11 | */ 12 | 13 | public class EmbeddedTomcat extends EmbeddedTomcatStandalone implements ApplicationContextAware { 14 | 15 | private ApplicationContext springContext; 16 | 17 | @Override 18 | protected void bindContext(StandardContext context) { 19 | // spring binding 20 | EmbeddedSpringContext embeddedSpringContext = (EmbeddedSpringContext) springContext; 21 | embeddedSpringContext.bind(context.getServletContext()); 22 | } 23 | 24 | // setters for spring 25 | @Override 26 | public void setApplicationContext(ApplicationContext springContext) { 27 | this.springContext = springContext; 28 | } 29 | 30 | @Override 31 | public EmbeddedTomcat setGeneralProps(GeneralProperties generalProps) { 32 | super.setGeneralProps(generalProps); 33 | return this; 34 | } 35 | 36 | @Override 37 | public EmbeddedTomcat setFsProps(FsProperties fsProps) { 38 | super.setFsProps(fsProps); 39 | return this; 40 | } 41 | 42 | @Override 43 | public EmbeddedTomcat setHostProps(HostProperties hostProps) { 44 | super.setHostProps(hostProps); 45 | return this; 46 | } 47 | 48 | @Override 49 | public EmbeddedTomcat setContextProps(ContextProperties contextProps) { 50 | super.setContextProps(contextProps); 51 | return this; 52 | } 53 | 54 | @Override 55 | public EmbeddedTomcat setConnectorProps(ConnectorProperties connectorProps) { 56 | super.setConnectorProps(connectorProps); 57 | return this; 58 | } 59 | 60 | @Override 61 | public EmbeddedTomcat setNioProps(NioProperties nioProps) { 62 | super.setNioProps(nioProps); 63 | return this; 64 | } 65 | 66 | @Override 67 | public EmbeddedTomcat setSocketProps(SocketProperties socketProps) { 68 | super.setSocketProps(socketProps); 69 | return this; 70 | } 71 | 72 | @Override 73 | public EmbeddedTomcat setSslProps(SslProperties sslProps) { 74 | super.setSslProps(sslProps); 75 | return this; 76 | } 77 | 78 | @Override 79 | public EmbeddedTomcat setExecutorProps(ExecutorProperties executorProps) { 80 | super.setExecutorProps(executorProps); 81 | return this; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/EmbeddedWrapper.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | import org.apache.catalina.core.StandardWrapper; 4 | 5 | import javax.servlet.ServletException; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 3/8/12 10 | */ 11 | 12 | // fail-fast-on-load servlet wrapper 13 | public class EmbeddedWrapper extends StandardWrapper { 14 | private static final long serialVersionUID = -188901122539015077L; 15 | 16 | @Override 17 | public void load() throws ServletException { 18 | try { 19 | super.load(); 20 | } catch (ServletException e) { 21 | throw new IllegalStateException(e); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/ExecutorState.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8; 2 | 3 | /** 4 | * User: wmel 5 | * Date: 27.06.12 6 | */ 7 | public class ExecutorState { 8 | private final int maxIdleTime; 9 | private final int maxThreads; 10 | private final int minSpareThreads; 11 | 12 | private final int activeCount; 13 | private final long completedCount; 14 | 15 | private final int corePoolSize; 16 | private final int largestPoolSize; 17 | 18 | private final int poolSize; 19 | private final int queueSize; 20 | 21 | public ExecutorState(int maxIdleTime, 22 | int maxThreads, 23 | int minSpareThreads, 24 | int activeCount, 25 | long completedCount, 26 | int corePoolSize, 27 | int largestPoolSize, 28 | int poolSize, 29 | int queueSize) { 30 | this.maxIdleTime = maxIdleTime; 31 | this.maxThreads = maxThreads; 32 | this.minSpareThreads = minSpareThreads; 33 | this.activeCount = activeCount; 34 | this.completedCount = completedCount; 35 | this.corePoolSize = corePoolSize; 36 | this.largestPoolSize = largestPoolSize; 37 | this.poolSize = poolSize; 38 | this.queueSize = queueSize; 39 | } 40 | 41 | public int getMaxIdleTime() { 42 | return maxIdleTime; 43 | } 44 | 45 | public int getMaxThreads() { 46 | return maxThreads; 47 | } 48 | 49 | public int getMinSpareThreads() { 50 | return minSpareThreads; 51 | } 52 | 53 | public int getActiveCount() { 54 | return activeCount; 55 | } 56 | 57 | public long getCompletedCount() { 58 | return completedCount; 59 | } 60 | 61 | public int getCorePoolSize() { 62 | return corePoolSize; 63 | } 64 | 65 | public int getLargestPoolSize() { 66 | return largestPoolSize; 67 | } 68 | 69 | public int getPoolSize() { 70 | return poolSize; 71 | } 72 | 73 | public int getQueueSize() { 74 | return queueSize; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | final StringBuilder sb = new StringBuilder(); 80 | sb.append("ExecutorState"); 81 | sb.append("{maxIdleTime=").append(maxIdleTime); 82 | sb.append(", maxThreads=").append(maxThreads); 83 | sb.append(", minSpareThreads=").append(minSpareThreads); 84 | sb.append(", activeCount=").append(activeCount); 85 | sb.append(", completedCount=").append(completedCount); 86 | sb.append(", corePoolSize=").append(corePoolSize); 87 | sb.append(", largestPoolSize=").append(largestPoolSize); 88 | sb.append(", poolSize=").append(poolSize); 89 | sb.append(", queueSize=").append(queueSize); 90 | sb.append('}'); 91 | return sb.toString(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/ContextProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | // http://tomcat.apache.org/tomcat-6.0-doc/config/context.html 9 | public class ContextProperties { 10 | private boolean cookies = true; 11 | private boolean disableURLRewriting = true; 12 | private int cacheMaxSizeKb = 10240; 13 | private int cacheObjectMaxSizeKb = 512; 14 | private int cacheTTLSec = 5; 15 | private boolean cachingAllowed = false; 16 | private int unloadDelayMs = 2000; 17 | private int maxActiveSessions = -1; 18 | private int sessionTimeoutMinutes = 10; 19 | private String postCharacterEncoding = "UTF-8"; 20 | 21 | public boolean isCookies() { 22 | return cookies; 23 | } 24 | 25 | public ContextProperties setCookies(boolean cookies) { 26 | this.cookies = cookies; 27 | return this; 28 | } 29 | 30 | public boolean isDisableURLRewriting() { 31 | return disableURLRewriting; 32 | } 33 | 34 | public ContextProperties setDisableURLRewriting(boolean disableURLRewriting) { 35 | this.disableURLRewriting = disableURLRewriting; 36 | return this; 37 | } 38 | 39 | public int getCacheMaxSizeKb() { 40 | return cacheMaxSizeKb; 41 | } 42 | 43 | public ContextProperties setCacheMaxSizeKb(int cacheMaxSizeKb) { 44 | this.cacheMaxSizeKb = cacheMaxSizeKb; 45 | return this; 46 | } 47 | 48 | public int getCacheObjectMaxSizeKb() { 49 | return cacheObjectMaxSizeKb; 50 | } 51 | 52 | public ContextProperties setCacheObjectMaxSizeKb(int cacheObjectMaxSizeKb) { 53 | this.cacheObjectMaxSizeKb = cacheObjectMaxSizeKb; 54 | return this; 55 | } 56 | 57 | public int getCacheTTLSec() { 58 | return cacheTTLSec; 59 | } 60 | 61 | public ContextProperties setCacheTTLSec(int cacheTTLSec) { 62 | this.cacheTTLSec = cacheTTLSec; 63 | return this; 64 | } 65 | 66 | public boolean isCachingAllowed() { 67 | return cachingAllowed; 68 | } 69 | 70 | public ContextProperties setCachingAllowed(boolean cachingAllowed) { 71 | this.cachingAllowed = cachingAllowed; 72 | return this; 73 | } 74 | 75 | public int getUnloadDelayMs() { 76 | return unloadDelayMs; 77 | } 78 | 79 | public ContextProperties setUnloadDelayMs(int unloadDelayMs) { 80 | this.unloadDelayMs = unloadDelayMs; 81 | return this; 82 | } 83 | 84 | public int getMaxActiveSessions() { 85 | return maxActiveSessions; 86 | } 87 | 88 | public ContextProperties setMaxActiveSessions(int maxActiveSessions) { 89 | this.maxActiveSessions = maxActiveSessions; 90 | return this; 91 | } 92 | 93 | public int getSessionTimeoutMinutes() { 94 | return sessionTimeoutMinutes; 95 | } 96 | 97 | public ContextProperties setSessionTimeoutMinutes(int sessionTimeoutMinutes) { 98 | this.sessionTimeoutMinutes = sessionTimeoutMinutes; 99 | return this; 100 | } 101 | 102 | public String getPostCharacterEncoding() { 103 | return postCharacterEncoding; 104 | } 105 | 106 | public ContextProperties setPostCharacterEncoding(String postCharacterEncoding) { 107 | this.postCharacterEncoding = postCharacterEncoding; 108 | return this; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/ExecutorProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | 5 | /** 6 | * User: alexey 7 | * Date: 11/4/11 8 | */ 9 | 10 | // http://tomcat.apache.org/tomcat-6.0-doc/config/executor.html 11 | public class ExecutorProperties { 12 | private String name = "etomcat-executor"; 13 | private boolean daemon = true; 14 | private String namePrefix = "etomcat-"; 15 | private int maxThreads = 200; 16 | private int minSpareThreads = 2; 17 | private int maxIdleTimeMs = 600000; 18 | private int threadPriority = Thread.NORM_PRIORITY; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public ExecutorProperties setName(String name) { 25 | this.name = name; 26 | return this; 27 | } 28 | 29 | public boolean isDaemon() { 30 | return daemon; 31 | } 32 | 33 | public ExecutorProperties setDaemon(boolean daemon) { 34 | this.daemon = daemon; 35 | return this; 36 | } 37 | 38 | public String getNamePrefix() { 39 | return namePrefix; 40 | } 41 | 42 | public ExecutorProperties setNamePrefix(String namePrefix) { 43 | this.namePrefix = namePrefix; 44 | return this; 45 | } 46 | 47 | public int getMaxThreads() { 48 | return maxThreads; 49 | } 50 | 51 | public ExecutorProperties setMaxThreads(int maxThreads) { 52 | this.maxThreads = maxThreads; 53 | return this; 54 | } 55 | 56 | public int getMinSpareThreads() { 57 | return minSpareThreads; 58 | } 59 | 60 | public ExecutorProperties setMinSpareThreads(int minSpareThreads) { 61 | this.minSpareThreads = minSpareThreads; 62 | return this; 63 | } 64 | 65 | public int getMaxIdleTimeMs() { 66 | return maxIdleTimeMs; 67 | } 68 | 69 | public ExecutorProperties setMaxIdleTimeMs(int maxIdleTimeMs) { 70 | this.maxIdleTimeMs = maxIdleTimeMs; 71 | return this; 72 | } 73 | 74 | public int getThreadPriority() { 75 | return threadPriority; 76 | } 77 | 78 | public ExecutorProperties setThreadPriority(int threadPriority) { 79 | this.threadPriority = threadPriority; 80 | return this; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/FsProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | public class FsProperties { 8 | private String confDir = "conf"; 9 | private String webXmlPath = "web.xml"; 10 | private String workDir = "work"; 11 | 12 | public String getConfDir() { 13 | return confDir; 14 | } 15 | 16 | public FsProperties setConfDir(String confDir) { 17 | this.confDir = confDir; 18 | return this; 19 | } 20 | 21 | public String getWebXmlPath() { 22 | return webXmlPath; 23 | } 24 | 25 | public FsProperties setWebXmlPath(String webXmlPath) { 26 | this.webXmlPath = webXmlPath; 27 | return this; 28 | } 29 | 30 | public String getWorkDir() { 31 | return workDir; 32 | } 33 | 34 | public FsProperties setWorkDir(String workDir) { 35 | this.workDir = workDir; 36 | return this; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/GeneralProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | public class GeneralProperties { 9 | 10 | private int port = 8080; 11 | private String docBaseDir = ""; 12 | private String contextPath = ""; 13 | 14 | public int getPort() { 15 | return port; 16 | } 17 | 18 | public GeneralProperties setPort(int port) { 19 | this.port = port; 20 | return this; 21 | } 22 | 23 | public String getDocBaseDir() { 24 | return docBaseDir; 25 | } 26 | 27 | public GeneralProperties setDocBaseDir(String docBaseDir) { 28 | this.docBaseDir = docBaseDir; 29 | return this; 30 | } 31 | 32 | public boolean isUseFsResources() { 33 | return null != docBaseDir && docBaseDir.length() > 0; 34 | } 35 | 36 | public String getContextPath() { 37 | return contextPath; 38 | } 39 | 40 | public GeneralProperties setContextPath(String contextPath) { 41 | this.contextPath = contextPath; 42 | return this; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/HostProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | /** 4 | * User: alexey 5 | * Date: 11/4/11 6 | */ 7 | 8 | // http://tomcat.apache.org/tomcat-6.0-doc/config/host.html 9 | public class HostProperties { 10 | private String name = "localhost"; 11 | private String errorReportValveClass = "org.apache.catalina.valves.ErrorReportValve"; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public HostProperties setName(String name) { 18 | this.name = name; 19 | return this; 20 | } 21 | 22 | public String getErrorReportValveClass() { 23 | return errorReportValveClass; 24 | } 25 | 26 | public HostProperties setErrorReportValveClass(String errorReportValveClass) { 27 | this.errorReportValveClass = errorReportValveClass; 28 | return this; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/config/NioProperties.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | 5 | /** 6 | * User: alexey 7 | * Date: 11/4/11 8 | */ 9 | 10 | // http://tomcat.apache.org/tomcat-6.0-doc/config/http.html 11 | public class NioProperties { 12 | private boolean useSendfile = true; 13 | private int acceptorThreadCount = 2; 14 | private int acceptorThreadPriority = Thread.NORM_PRIORITY; 15 | private int pollerThreadCount = 2; 16 | private int pollerThreadPriority = Thread.NORM_PRIORITY; 17 | private int selectorTimeoutMs = 1000; 18 | private int oomParachute = 1048576; 19 | private int maxSelectors = 200; 20 | private int maxSpareSelectors = -1; 21 | 22 | public boolean isUseSendfile() { 23 | return useSendfile; 24 | } 25 | 26 | public NioProperties setUseSendfile(boolean useSendfile) { 27 | this.useSendfile = useSendfile; 28 | return this; 29 | } 30 | 31 | public int getAcceptorThreadCount() { 32 | return acceptorThreadCount; 33 | } 34 | 35 | public NioProperties setAcceptorThreadCount(int acceptorThreadCount) { 36 | this.acceptorThreadCount = acceptorThreadCount; 37 | return this; 38 | } 39 | 40 | public int getAcceptorThreadPriority() { 41 | return acceptorThreadPriority; 42 | } 43 | 44 | public NioProperties setAcceptorThreadPriority(int acceptorThreadPriority) { 45 | this.acceptorThreadPriority = acceptorThreadPriority; 46 | return this; 47 | } 48 | 49 | public int getPollerThreadCount() { 50 | return pollerThreadCount; 51 | } 52 | 53 | public NioProperties setPollerThreadCount(int pollerThreadCount) { 54 | this.pollerThreadCount = pollerThreadCount; 55 | return this; 56 | } 57 | 58 | public int getPollerThreadPriority() { 59 | return pollerThreadPriority; 60 | } 61 | 62 | public NioProperties setPollerThreadPriority(int pollerThreadPriority) { 63 | this.pollerThreadPriority = pollerThreadPriority; 64 | return this; 65 | } 66 | 67 | public int getSelectorTimeoutMs() { 68 | return selectorTimeoutMs; 69 | } 70 | 71 | public NioProperties setSelectorTimeoutMs(int selectorTimeoutMs) { 72 | this.selectorTimeoutMs = selectorTimeoutMs; 73 | return this; 74 | } 75 | 76 | public int getOomParachute() { 77 | return oomParachute; 78 | } 79 | 80 | public NioProperties setOomParachute(int oomParachute) { 81 | this.oomParachute = oomParachute; 82 | return this; 83 | } 84 | 85 | public int getMaxSelectors() { 86 | return maxSelectors; 87 | } 88 | 89 | public NioProperties setMaxSelectors(int maxSelectors) { 90 | this.maxSelectors = maxSelectors; 91 | return this; 92 | } 93 | 94 | public int getMaxSpareSelectors() { 95 | return maxSpareSelectors; 96 | } 97 | 98 | public NioProperties setMaxSpareSelectors(int maxSpareSelectors) { 99 | this.maxSpareSelectors = maxSpareSelectors; 100 | return this; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/context/EmbeddedAnnotationSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.context; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | 6 | import javax.servlet.ServletContext; 7 | 8 | /** 9 | * User: alexey 10 | * Date: 4/9/12 11 | */ 12 | public class EmbeddedAnnotationSpringContext extends AnnotationConfigApplicationContext implements EmbeddedSpringContext { 13 | private ServletContext servletContext; 14 | 15 | public EmbeddedAnnotationSpringContext() { 16 | } 17 | 18 | public EmbeddedAnnotationSpringContext(Class... annotatedClasses) { 19 | super(annotatedClasses); 20 | } 21 | 22 | public EmbeddedAnnotationSpringContext(String... basePackages) { 23 | super(basePackages); 24 | } 25 | 26 | @Override 27 | public ServletContext getServletContext() { 28 | return servletContext; 29 | } 30 | 31 | @Override 32 | public void bind(ServletContext servletContext) { 33 | servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this); 34 | this.servletContext = servletContext; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/context/EmbeddedSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.context; 2 | 3 | import org.springframework.web.context.WebApplicationContext; 4 | 5 | import javax.servlet.ServletContext; 6 | 7 | /** 8 | * User: alexey 9 | * Date: 4/9/12 10 | */ 11 | public interface EmbeddedSpringContext extends WebApplicationContext { 12 | void bind(ServletContext servletContext); 13 | } 14 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/context/EmbeddedXmlSpringContext.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.context; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.support.AbstractXmlApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | import org.springframework.context.support.FileSystemXmlApplicationContext; 7 | import org.springframework.web.context.WebApplicationContext; 8 | 9 | import javax.servlet.ServletContext; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 7/24/11 14 | */ 15 | public class EmbeddedXmlSpringContext extends AbstractXmlApplicationContext implements EmbeddedSpringContext { 16 | 17 | private ServletContext servletContext; 18 | 19 | public EmbeddedXmlSpringContext(String configLocation) throws BeansException { 20 | super(null); 21 | setConfigLocations(new String[]{configLocation}); 22 | refresh(); 23 | } 24 | 25 | @Override 26 | public ServletContext getServletContext() { 27 | return servletContext; 28 | } 29 | 30 | @Override 31 | public void bind(ServletContext servletContext) { 32 | servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this); 33 | this.servletContext = servletContext; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/failfast/FailFastConnector.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.failfast; 2 | 3 | import org.apache.catalina.connector.Connector; 4 | import org.apache.catalina.connector.Response; 5 | 6 | /** 7 | * User: alexkasko 8 | * Date: 4/8/14 9 | */ 10 | public class FailFastConnector extends Connector { 11 | public FailFastConnector() throws Exception { 12 | } 13 | 14 | public FailFastConnector(String protocol) throws Exception { 15 | super(protocol); 16 | } 17 | 18 | @Override 19 | public Response createResponse() { 20 | Response response = new FailFastResponse(); 21 | response.setConnector(this); 22 | return (response); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/failfast/FailFastProxyOutputStream.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.failfast; 2 | 3 | import org.apache.catalina.connector.CoyoteOutputStream; 4 | import org.apache.catalina.connector.OutputBuffer; 5 | 6 | /** 7 | * User: alexkasko 8 | * Date: 4/8/14 9 | */ 10 | public class FailFastProxyOutputStream extends CoyoteOutputStream { 11 | protected FailFastProxyOutputStream(OutputBuffer ob) { 12 | super(ob); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/failfast/FailFastResponse.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.failfast; 2 | 3 | import org.apache.catalina.Globals; 4 | import org.apache.catalina.connector.*; 5 | 6 | import java.io.IOException; 7 | import java.io.PrintWriter; 8 | 9 | /** 10 | * User: alexkasko 11 | * Date: 4/8/14 12 | */ 13 | public class FailFastResponse extends Response { 14 | 15 | @Override 16 | public void setConnector(Connector connector) { 17 | if ("AJP/1.3".equals(connector.getProtocol())) { 18 | // default size to size of one ajp-packet 19 | outputBuffer = new OutputBuffer(8184); 20 | } else { 21 | outputBuffer = new OutputBuffer(); 22 | } 23 | outputStream = new FailFastProxyOutputStream(outputBuffer); 24 | writer = new FailFastResponseWriter(outputBuffer); 25 | } 26 | 27 | @Override 28 | public PrintWriter getReporter() throws IOException { 29 | if (outputBuffer.isNew()) { 30 | outputBuffer.checkConverter(); 31 | if (writer == null) { 32 | writer = new CoyoteWriter(outputBuffer); 33 | } 34 | return writer; 35 | } else { 36 | return null; 37 | } 38 | } 39 | 40 | @Override 41 | public PrintWriter getWriter() throws IOException { 42 | 43 | if (usingOutputStream) 44 | throw new IllegalStateException 45 | (sm.getString("coyoteResponse.getWriter.ise")); 46 | 47 | if (Globals.STRICT_SERVLET_COMPLIANCE) { 48 | /* 49 | * If the response's character encoding has not been specified as 50 | * described in getCharacterEncoding (i.e., the method 51 | * just returns the default value ISO-8859-1), 52 | * getWriter updates it to ISO-8859-1 53 | * (with the effect that a subsequent call to getContentType() will 54 | * include a charset=ISO-8859-1 component which will also be 55 | * reflected in the Content-Type response header, thereby satisfying 56 | * the Servlet spec requirement that containers must communicate the 57 | * character encoding used for the servlet response's writer to the 58 | * client). 59 | */ 60 | setCharacterEncoding(getCharacterEncoding()); 61 | } 62 | 63 | usingWriter = true; 64 | outputBuffer.checkConverter(); 65 | if (writer == null) { 66 | writer = new CoyoteWriter(outputBuffer); 67 | } 68 | return writer; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/failfast/FailFastResponseWriter.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.failfast; 2 | 3 | import org.apache.catalina.connector.CoyoteWriter; 4 | import org.apache.catalina.connector.OutputBuffer; 5 | 6 | import java.io.IOException; 7 | 8 | import static ru.concerteza.springtomcat.etomcat8.failfast.SneakThrower.sneakyThrow; 9 | 10 | /** 11 | * User: alexkasko 12 | * Date: 4/8/14 13 | */ 14 | public class FailFastResponseWriter extends CoyoteWriter { 15 | public FailFastResponseWriter(OutputBuffer ob) { 16 | super(ob); 17 | } 18 | 19 | public void flush() { 20 | try { 21 | ob.flush(); 22 | } catch (IOException e) { 23 | error = true; 24 | sneakyThrow(e); 25 | } 26 | 27 | } 28 | 29 | public void write(char buf[], int off, int len) { 30 | try { 31 | ob.write(buf, off, len); 32 | } catch (IOException e) { 33 | error = true; 34 | sneakyThrow(e); 35 | } 36 | } 37 | 38 | 39 | public void write(String s, int off, int len) { 40 | try { 41 | ob.write(s, off, len); 42 | } catch (IOException e) { 43 | error = true; 44 | sneakyThrow(e); 45 | } 46 | } 47 | 48 | public boolean checkError() { 49 | try { 50 | flush(); 51 | } catch (Exception e) { 52 | if (e instanceof IOException) { 53 | // NOOP 54 | } else { 55 | sneakyThrow(e); 56 | } 57 | } 58 | return error; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/failfast/SneakThrower.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.failfast; 2 | 3 | /** 4 | * User: alexkasko 5 | * Date: 4/8/14 6 | */ 7 | 8 | // http://mail.openjdk.java.net/pipermail/lambda-dev/2010-June/001552.html 9 | class SneakThrower { 10 | 11 | static RuntimeException sneakyThrow(Exception e) { 12 | SneakThrower.sneakyThrow0(e); 13 | return null; 14 | } 15 | 16 | @SuppressWarnings("unchecked") 17 | private static void sneakyThrow0(Throwable t) throws T { 18 | throw (T) t; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /etomcat8/src/main/java/ru/concerteza/springtomcat/etomcat8/valves/PostCharacterEncodingValve.java: -------------------------------------------------------------------------------- 1 | package ru.concerteza.springtomcat.etomcat8.valves; 2 | 3 | import org.apache.catalina.connector.Request; 4 | import org.apache.catalina.connector.Response; 5 | import org.apache.catalina.valves.ValveBase; 6 | 7 | import javax.servlet.ServletException; 8 | import java.io.IOException; 9 | import java.nio.charset.Charset; 10 | 11 | /** 12 | * User: alexey 13 | * Date: 9/26/12 14 | */ 15 | public class PostCharacterEncodingValve extends ValveBase { 16 | private final Charset postEncoding; 17 | 18 | public PostCharacterEncodingValve(String postEncoding) { 19 | this.postEncoding = Charset.forName(postEncoding); 20 | } 21 | 22 | @Override 23 | public void invoke(Request request, Response response) throws IOException, ServletException { 24 | request.setCharacterEncoding(postEncoding.name()); 25 | getNext().invoke(request, response); 26 | } 27 | } 28 | --------------------------------------------------------------------------------