Repository: mthizo247/spring-cloud-netflix-zuul-websocket
Branch: master
Commit: cc3fad4dba3b
Files: 34
Total size: 126.5 KB
Directory structure:
gitextract_fb8rm5ag/
├── .gitignore
├── LICENSE
├── README.md
├── eclipse-code-formatter.xml
├── pom.xml
└── src/
├── main/
│ └── java/
│ └── com/
│ └── github/
│ └── mthizo247/
│ └── cloud/
│ └── netflix/
│ └── zuul/
│ └── web/
│ ├── authentication/
│ │ ├── AbstractHeadersCallback.java
│ │ ├── BasicAuthPrincipalHeadersCallback.java
│ │ ├── CompositeHeadersCallback.java
│ │ ├── LoginCookieHeadersCallback.java
│ │ └── OAuth2BearerPrincipalHeadersCallback.java
│ ├── filter/
│ │ └── ProxyRedirectFilter.java
│ ├── proxytarget/
│ │ ├── AbstractProxyTargetResolver.java
│ │ ├── CompositeProxyTargetResolver.java
│ │ ├── EurekaProxyTargetResolver.java
│ │ ├── LoadBalancedProxyTargetResolver.java
│ │ ├── ProxyTargetResolver.java
│ │ └── UrlProxyTargetResolver.java
│ ├── socket/
│ │ ├── CompositeErrorHandler.java
│ │ ├── EnableZuulWebSocket.java
│ │ ├── ProxySessionException.java
│ │ ├── ProxyWebSocketConnectionManager.java
│ │ ├── ProxyWebSocketErrorHandler.java
│ │ ├── ProxyWebSocketHandler.java
│ │ ├── ReconnectErrorHandler.java
│ │ ├── WebSocketHttpHeadersCallback.java
│ │ ├── WebSocketMessageAccessor.java
│ │ ├── ZuulWebSocketConfiguration.java
│ │ └── ZuulWebSocketProperties.java
│ └── util/
│ ├── DefaultErrorAnalyzer.java
│ ├── ErrorAnalyzer.java
│ └── MapPropertyResolver.java
└── test/
└── java/
└── com/
└── github/
└── mthizo247/
└── cloud/
└── netflix/
└── zuul/
└── web/
└── socket/
├── ProxyWebSocketConnectionManagerTests.java
├── WebSocketMessageAccessorTests.java
└── ZuulWebSocketPropertiesTests.java
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
target/
*.log.gz
*.iml
.idea/
*.log
*.db
/target
target
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================
# spring-cloud-netflix-zuul-websocket
A simple library to enable Zuul reverse proxy web socket support in spring applications.
## USAGE
spring-cloud-netflix-zuul-websocket is available from **Maven Central**
```xml
com.github.mthizo247
spring-cloud-netflix-zuul-websocket
1.0.0-RELEASE
```
### Who is this for?
This is for anyone using **Spring Netflix Zuul** to proxy requests to back-ends which supports web sockets but the proxy layer does not.
**Netflix Zuul** does not natively support web sockets.
### How do I use this?
Enable it like so:
```java
@SpringBootApplication
@EnableZuulWebSocket
@EnableWebSocketMessageBroker
public class ProxyApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyApplication.class, args);
}
}
```
Then in your spring application properties(e.g application.yml)
```
server:
port: 7078
zuul:
routes:
hello:
path: /**
url: http://localhost:7079
customSensitiveHeaders: true
ws:
brokerages:
hello:
end-points: /ws
brokers: /topic
destination-prefixes: /app
```
With this you should have web sockets to your back-end service working correctly.
**Checkout** this [demo](https://github.com/mthizo247/zuul-websocket-support-demo)
================================================
FILE: eclipse-code-formatter.xml
================================================
================================================
FILE: pom.xml
================================================
4.0.0
com.github.mthizo247
spring-cloud-netflix-zuul-websocket
jar
1.0.7.RELEASE
Spring Cloud Netflix Zuul Websocket
Spring Cloud Netflix Zuul Websocket
https://www.discovery.co.za
2017
The Apache Software License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.txt
repo
mthizo247@gmail.com
Ronald Mthombeni
mthizo247@gmail.com
Discovery inc.
http://www.discovery.co.za
salmannoor74415
Salman Noor
salmannoor74415@gmail.com
Discovery inc.
http://www.discovery.co.za
scm:git:git://github.com/mthizo247/spring-cloud-netflix-zuul-websocket
scm:git:git://github.com/mthizo247/spring-cloud-netflix-zuul-websocket
https://github.com/mthizo247/spring-cloud-netflix-zuul-websocket.git
https://github.com/mthizo247/spring-cloud-netflix-zuul-websocket/issues
GitHub Issues
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
1.7
ossrh
https://oss.sonatype.org/service/local/staging/deploy/maven2
ossrh
https://oss.sonatype.org/content/repositories/snapshots
org.springframework.boot
spring-boot-starter-websocket
provided
org.springframework.cloud
spring-cloud-starter-zuul
1.1.5.RELEASE
provided
org.springframework.boot
spring-boot-starter-security
provided
org.springframework.security.oauth
spring-security-oauth2
provided
org.springframework.boot
spring-boot-starter-test
test
org.apache.maven.plugins
maven-release-plugin
2.5.3
false
release
deploy
org.sonatype.plugins
nexus-staging-maven-plugin
1.6.3
true
ossrh
https://oss.sonatype.org/
true
5820c8da7e998d
release
org.apache.maven.plugins
maven-release-plugin
2.5.3
true
org.apache.maven.plugins
maven-source-plugin
3.0.1
attach-sources
jar
org.apache.maven.plugins
maven-javadoc-plugin
2.10.3
attach-javadocs
jar
org.apache.maven.plugins
maven-gpg-plugin
1.5
sign-artifacts
verify
sign
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/authentication/AbstractHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketHttpHeadersCallback;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
/**
* @author Ronald Mthombeni
*/
public abstract class AbstractHeadersCallback implements WebSocketHttpHeadersCallback {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void applyHeaders(
WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
if (shouldApplyHeaders(userAgentSession, headers)) {
applyHeadersInternal(userAgentSession, headers);
}
}
protected abstract void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers);
protected abstract boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers);
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/authentication/BasicAuthPrincipalHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import java.util.Collections;
/**
* @author Ronald Mthombeni
*/
public class BasicAuthPrincipalHeadersCallback extends AbstractHeadersCallback {
@Override
protected void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
Authentication authentication = (Authentication) userAgentSession.getPrincipal();
String usernameColonPwd = authentication.getName() + ":"
+ authentication.getCredentials().toString();
String encodedCredentials = new String(
Base64.encode(usernameColonPwd.getBytes()));
headers.put(HttpHeaders.AUTHORIZATION,
Collections.singletonList("Basic " + encodedCredentials));
if (logger.isDebugEnabled()) {
logger.debug("Added basic authentication header for user " + authentication.getName() + " to web sockets http headers");
}
}
@Override
protected boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
return !headers.containsKey(HttpHeaders.AUTHORIZATION) && userAgentSession.getPrincipal() instanceof Authentication;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/authentication/CompositeHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketHttpHeadersCallback;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import java.util.List;
/**
* Strategy to add different types of headers to web socket connections
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class CompositeHeadersCallback implements WebSocketHttpHeadersCallback {
private List headersCallbacks;
public CompositeHeadersCallback(final List headersCallbacks) {
this.headersCallbacks = headersCallbacks;
}
@Override
public void applyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
for (WebSocketHttpHeadersCallback callback : headersCallbacks) {
callback.applyHeaders(userAgentSession, headers);
}
}
public List getHeadersCallbacks() {
return headersCallbacks;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/authentication/LoginCookieHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import java.util.List;
/**
* @author Ronald Mthombeni
*/
public class LoginCookieHeadersCallback extends AbstractHeadersCallback {
protected final Log logger = LogFactory.getLog(getClass());
@Override
protected void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
List sessionCookies = userAgentSession.getHandshakeHeaders().get(HttpHeaders.COOKIE);
headers.put(HttpHeaders.COOKIE, sessionCookies);
if (logger.isDebugEnabled()) {
logger.debug("Added cookie authentication header to web sockets http headers");
}
}
@Override
protected boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
return !headers.containsKey(HttpHeaders.COOKIE) && userAgentSession.getHandshakeHeaders().containsKey(HttpHeaders.COOKIE);
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/authentication/OAuth2BearerPrincipalHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import java.util.Collections;
/**
* @author Marcin Podlodowski
*/
public class OAuth2BearerPrincipalHeadersCallback extends AbstractHeadersCallback {
@Override
protected void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) userAgentSession.getPrincipal();
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) oAuth2Authentication.getDetails();
String accessToken = details.getTokenValue();
headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + accessToken));
if (logger.isDebugEnabled()) {
logger.debug("Added Oauth2 bearer token authentication header for user " +
oAuth2Authentication.getName() + " to web sockets http headers");
}
}
@Override
protected boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) {
return !headers.containsKey(HttpHeaders.AUTHORIZATION) && userAgentSession.getPrincipal() instanceof OAuth2Authentication;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/filter/ProxyRedirectFilter.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.filter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UrlPathHelper;
import com.netflix.util.Pair;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
/**
* @author Ronald Mthombeni
*/
public class ProxyRedirectFilter extends ZuulFilter {
private static String REDIRECT_TO_URL = "REDIRECT_TO_URL_"
+ ProxyRedirectFilter.class.getName();
RouteLocator routeLocator;
UrlPathHelper urlPathHelper;
public ProxyRedirectFilter(RouteLocator routeLocator) {
this(routeLocator, new UrlPathHelper());
}
public ProxyRedirectFilter(RouteLocator routeLocator, UrlPathHelper urlPathHelper) {
this.routeLocator = routeLocator;
this.urlPathHelper = urlPathHelper;
}
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
boolean isRedirect = ctx.getResponseStatusCode() == 301 || ctx.getResponseStatusCode() == 302;
if (!isRedirect)
return false;
boolean hasCorrectLocation = false;
List> zuulResponseHeaders = ctx.getZuulResponseHeaders();
for (Pair zuulResponseHeader : zuulResponseHeaders) {
if ("Location".equalsIgnoreCase(zuulResponseHeader.first())) {
HttpServletRequest request = ctx.getRequest();
String path = urlPathHelper.getPathWithinApplication(request);
Route route = routeLocator.getMatchingRoute(path);
UriComponents redirectTo = ServletUriComponentsBuilder
.fromHttpUrl(zuulResponseHeader.second()).build();
UriComponents routeLocation = ServletUriComponentsBuilder
.fromHttpUrl(route.getLocation()).build();
if (redirectTo.getHost().equalsIgnoreCase(routeLocation.getHost())
&& redirectTo.getPort() == routeLocation.getPort()) {
String toLocation = ServletUriComponentsBuilder
.fromHttpUrl(zuulResponseHeader.second())
.host(request.getServerName())
.port(request.getServerPort())
.replacePath(
buildRoutePath(route, zuulResponseHeader.second()))
.build().toUriString();
ctx.put(REDIRECT_TO_URL, toLocation);
hasCorrectLocation = true;
break;
}
}
}
return hasCorrectLocation;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
List> zuulResponseHeaders = ctx.getZuulResponseHeaders();
for (Pair zuulResponseHeader : zuulResponseHeaders) {
if ("Location".equalsIgnoreCase(zuulResponseHeader.first())) {
zuulResponseHeader.setSecond(ctx.get(REDIRECT_TO_URL).toString());
break;
}
}
return null;
}
private String buildRoutePath(Route route, String httpUrl) {
String path = ServletUriComponentsBuilder.fromHttpUrl(httpUrl).build().getPath();
return route.getPrefix() == null ? path : route.getPrefix() + path;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/AbstractProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import com.github.mthizo247.cloud.netflix.zuul.web.util.MapPropertyResolver;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
/**
* @author Ronald Mthombeni
* @author Salman Noor
*/
public abstract class AbstractProxyTargetResolver implements ProxyTargetResolver, Ordered {
protected ZuulProperties zuulProperties;
protected int order = 0;
public AbstractProxyTargetResolver(ZuulProperties zuulProperties) {
Assert.notNull(zuulProperties, "zuulProperties must not be null");
this.zuulProperties = zuulProperties;
}
protected ZuulProperties.ZuulRoute resolveRoute(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
ZuulProperties.ZuulRoute zuulRoute = zuulProperties.getRoutes().get(wsBrokerage.getRouteId());
zuulRoute = zuulRoute == null ? zuulProperties.getRoutes().get(wsBrokerage.getId()) : zuulRoute;
return zuulRoute;
}
protected URI resolveUri(ServiceInstance serviceInstance) {
Map metadata = new HashMap<>();
for (Map.Entry entry : serviceInstance.getMetadata().entrySet()) {
metadata.put(entry.getKey(), entry.getValue());
}
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(serviceInstance.getUri());
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(new MapPropertyResolver(metadata));
String configPath = propertyResolver.getProperty("configPath");
if (configPath != null) {
uriBuilder.path(configPath);
}
return uriBuilder.build().toUri();
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/CompositeProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import java.net.URI;
import java.util.List;
/**
* Strategy to resolve zuul proxy target using delegation pattern
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class CompositeProxyTargetResolver implements ProxyTargetResolver {
private List targetResolvers;
public CompositeProxyTargetResolver(final List targetResolvers) {
this.targetResolvers = targetResolvers;
}
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
URI target = null;
for (ProxyTargetResolver resolver : targetResolvers) {
target = resolver.resolveTarget(wsBrokerage);
if (target != null)
break;
}
return target;
}
public List getTargetResolvers() {
return targetResolvers;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/EurekaProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.net.URI;
import java.util.List;
/**
* Strategy to resolve zuul properties from route service is using eaureka service discovery
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class EurekaProxyTargetResolver extends AbstractProxyTargetResolver {
public static final int DEFAULT_ORDER = LoadBalancedProxyTargetResolver.DEFAULT_ORDER + 10;
private DiscoveryClient discoveryClient;
public EurekaProxyTargetResolver(DiscoveryClient discoveryClient, ZuulProperties zuulProperties) {
super(zuulProperties);
Assert.notNull(discoveryClient, "discoveryClient can't be null");
this.discoveryClient = discoveryClient;
this.order = DEFAULT_ORDER;
}
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
ZuulProperties.ZuulRoute zuulRoute = resolveRoute(wsBrokerage);
if (zuulRoute == null || StringUtils.isEmpty(zuulRoute.getServiceId())) return null;
List instances = discoveryClient.getInstances(zuulRoute.getServiceId());
ServiceInstance serviceInstance = CollectionUtils.isEmpty(instances) ? null : instances.get(0);
return serviceInstance != null ? resolveUri(serviceInstance) : null;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/LoadBalancedProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.net.URI;
/**
* Strategy to resolve zuul proxy target from a load balanced client
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class LoadBalancedProxyTargetResolver extends AbstractProxyTargetResolver {
public static final int DEFAULT_ORDER = HIGHEST_PRECEDENCE + 10;
private LoadBalancerClient loadBalancerClient;
public LoadBalancedProxyTargetResolver(LoadBalancerClient loadBalancerClient, ZuulProperties zuulProperties) {
super(zuulProperties);
Assert.notNull(loadBalancerClient, "loadBalancerClient can't be null");
this.loadBalancerClient = loadBalancerClient;
this.order = DEFAULT_ORDER;
}
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
ZuulProperties.ZuulRoute zuulRoute = resolveRoute(wsBrokerage);
if (zuulRoute == null || StringUtils.isEmpty(zuulRoute.getServiceId())) return null;
ServiceInstance serviceInstance = loadBalancerClient.choose(zuulRoute.getServiceId());
return serviceInstance != null ? resolveUri(serviceInstance) : null;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/ProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import java.net.URI;
/**
* Strategy to resolve zuul proxy target
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public interface ProxyTargetResolver {
URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage);
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/proxytarget/UrlProxyTargetResolver.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.proxytarget;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.ZuulWebSocketProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.StringUtils;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Strategy to resolve zuul proxy target from a load balanced client
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class UrlProxyTargetResolver extends AbstractProxyTargetResolver {
public static final int DEFAULT_ORDER = EurekaProxyTargetResolver.DEFAULT_ORDER + 10;
public UrlProxyTargetResolver(ZuulProperties zuulProperties) {
super(zuulProperties);
this.order = DEFAULT_ORDER;
}
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
ZuulProperties.ZuulRoute zuulRoute = resolveRoute(wsBrokerage);
if (zuulRoute == null || StringUtils.isEmpty(zuulRoute.getUrl())) return null;
try {
return new URI(zuulRoute.getUrl());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/CompositeErrorHandler.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
/**
* Created by ronald22 on 06/04/2017.
*/
public class CompositeErrorHandler implements ProxyWebSocketErrorHandler {
protected final Log logger = LogFactory.getLog(getClass());
private final List errorHandlers;
public CompositeErrorHandler(final List handlers) {
this.errorHandlers = handlers;
}
@Override
public void handleError(Throwable t) {
for (ProxyWebSocketErrorHandler handler : errorHandlers) {
callErrorHandler(handler, t);
}
}
private void callErrorHandler(ProxyWebSocketErrorHandler handler, Throwable t) {
try {
handler.handleError(t);
} catch (Throwable e) {
logger.error("Error executing error handler " + handler, e);
}
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/EnableZuulWebSocket.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Import;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Sets up a Zuul web socket configuration so that it can bridge web socket communication
* to backend servers.
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@EnableZuulProxy
@EnableWebSocketMessageBroker
@Import(ZuulWebSocketConfiguration.class)
public @interface EnableZuulWebSocket {
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ProxySessionException.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.messaging.simp.stomp.StompSession;
/**
* Created by ronald22 on 06/04/2017.
*/
public class ProxySessionException extends Exception {
private final ProxyWebSocketConnectionManager connectionManager;
private final StompSession session;
public ProxySessionException(ProxyWebSocketConnectionManager connectionManager,
StompSession session, Throwable cause) {
super(cause);
this.connectionManager = connectionManager;
this.session = session;
}
public ProxyWebSocketConnectionManager getConnectionManager() {
return connectionManager;
}
public StompSession getSession() {
return session;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProxySessionException that = (ProxySessionException) o;
if (connectionManager != null ? !connectionManager.equals(that.connectionManager) : that.connectionManager != null)
return false;
return session != null ? session.equals(that.session) : that.session == null;
}
@Override
public int hashCode() {
int result = connectionManager != null ? connectionManager.hashCode() : 0;
result = 31 * result + (session != null ? session.hashCode() : 0);
return result;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ProxyWebSocketConnectionManager.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandler;
import org.springframework.util.ErrorHandler;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.ConnectionManagerSupport;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import java.lang.reflect.Type;
import java.security.Principal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* A web socket connection manager bridge between client and backend server via zuul
* reverse proxy
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class ProxyWebSocketConnectionManager extends ConnectionManagerSupport
implements StompSessionHandler {
private final WebSocketStompClient stompClient;
private final WebSocketSession userAgentSession;
private final WebSocketHttpHeadersCallback httpHeadersCallback;
private StompSession serverSession;
private Map subscriptions = new ConcurrentHashMap<>();
private ErrorHandler errorHandler;
private SimpMessagingTemplate messagingTemplate;
public ProxyWebSocketConnectionManager(SimpMessagingTemplate messagingTemplate,
WebSocketStompClient stompClient, WebSocketSession userAgentSession,
WebSocketHttpHeadersCallback httpHeadersCallback, String uri) {
super(uri);
this.messagingTemplate = messagingTemplate;
this.stompClient = stompClient;
this.userAgentSession = userAgentSession;
this.httpHeadersCallback = httpHeadersCallback;
}
public void errorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
private WebSocketHttpHeaders buildWebSocketHttpHeaders() {
WebSocketHttpHeaders wsHeaders = new WebSocketHttpHeaders();
if (httpHeadersCallback != null) {
httpHeadersCallback.applyHeaders(userAgentSession, wsHeaders);
}
return wsHeaders;
}
@Override
protected void openConnection() {
connect();
}
public void connect() {
try {
serverSession = stompClient
.connect(getUri().toString(), buildWebSocketHttpHeaders(), this)
.get();
} catch (Exception e) {
logger.error("Error connecting to web socket uri " + getUri(), e);
throw new RuntimeException(e);
}
}
public void reconnect(final long delay) {
if (delay > 0) {
logger.warn("Connection lost or refused, will attempt to reconnect after "
+ delay + " millis");
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
//
}
}
Set destinations = new HashSet<>(subscriptions.keySet());
connect();
for (String destination : destinations) {
try {
subscribe(destination);
} catch (Exception ignored) {
// nothing
}
}
}
@Override
protected void closeConnection() throws Exception {
if (isConnected()) {
this.serverSession.disconnect();
}
}
@Override
protected boolean isConnected() {
return (this.serverSession != null && this.serverSession.isConnected());
}
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
if (logger.isDebugEnabled()) {
logger.debug("Proxied target now connected " + session);
}
}
@Override
public void handleException(StompSession session, StompCommand command,
StompHeaders headers, byte[] payload, Throwable ex) {
if (errorHandler != null) {
errorHandler.handleError(new ProxySessionException(this, session, ex));
}
}
@Override
public void handleTransportError(StompSession session, Throwable ex) {
if (errorHandler != null) {
errorHandler.handleError(new ProxySessionException(this, session, ex));
}
}
@Override
public Type getPayloadType(StompHeaders headers) {
return Object.class;
}
public void sendMessage(final String destination, final Object msg) {
if (msg instanceof String) { // in case of a json string to avoid double
// converstion by the converters
serverSession.send(destination, ((String) msg).getBytes());
return;
}
serverSession.send(destination, msg);
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
if (headers.getDestination() != null) {
String destination = headers.getDestination();
if (logger.isDebugEnabled()) {
logger.debug("Received " + payload + ", To " + headers.getDestination());
}
Principal principal = userAgentSession.getPrincipal();
String userDestinationPrefix = messagingTemplate.getUserDestinationPrefix();
if (principal != null && destination.startsWith(userDestinationPrefix)) {
destination = destination.substring(userDestinationPrefix.length());
destination = destination.startsWith("/") ? destination
: "/" + destination;
messagingTemplate.convertAndSendToUser(principal.getName(), destination,
payload, copyHeaders(headers.toSingleValueMap()));
} else {
messagingTemplate.convertAndSend(destination, payload,
copyHeaders(headers.toSingleValueMap()));
}
}
}
private Map copyHeaders(Map original) {
Map copy = new HashMap<>();
for (String key : original.keySet()) {
copy.put(key, original.get(key));
}
return copy;
}
private void connectIfNecessary() {
if (!isConnected()) {
connect();
}
}
public void subscribe(String destination) throws Exception {
connectIfNecessary();
StompSession.Subscription subscription = serverSession.subscribe(destination,
this);
subscriptions.put(destination, subscription);
}
public void unsubscribe(String destination) {
StompSession.Subscription subscription = subscriptions.remove(destination);
if (subscription != null) {
connectIfNecessary();
subscription.unsubscribe();
}
}
public boolean isConnectedToUserAgent() {
return (userAgentSession != null && userAgentSession.isOpen());
}
public void disconnect() {
try {
closeConnection();
} catch (Exception e) {
// nothing
}
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ProxyWebSocketErrorHandler.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.util.ErrorHandler;
/**
* A simple tagging interface for pluggable web socket connection error handlers
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public interface ProxyWebSocketErrorHandler extends ErrorHandler {
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ProxyWebSocketHandler.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.ProxyTargetResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.util.Assert;
import org.springframework.util.ErrorHandler;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A {@link WebSocketHandlerDecorator} that adds web socket support to zuul reverse proxy.
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class ProxyWebSocketHandler extends WebSocketHandlerDecorator {
private final Logger logger = LoggerFactory.getLogger(ProxyWebSocketHandler.class);
private final WebSocketHttpHeadersCallback headersCallback;
private final SimpMessagingTemplate messagingTemplate;
private final ProxyTargetResolver proxyTargetResolver;
private final ZuulWebSocketProperties zuulWebSocketProperties;
private final WebSocketStompClient stompClient;
private final Map managers = new ConcurrentHashMap<>();
private ErrorHandler errorHandler;
public ProxyWebSocketHandler(WebSocketHandler delegate,
WebSocketStompClient stompClient,
WebSocketHttpHeadersCallback headersCallback,
SimpMessagingTemplate messagingTemplate,
ProxyTargetResolver proxyTargetResolver,
ZuulWebSocketProperties zuulWebSocketProperties) {
super(delegate);
this.stompClient = stompClient;
this.headersCallback = headersCallback;
this.messagingTemplate = messagingTemplate;
this.proxyTargetResolver = proxyTargetResolver;
this.zuulWebSocketProperties = zuulWebSocketProperties;
}
public void errorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
private String getWebSocketServerPath(ZuulWebSocketProperties.WsBrokerage wsBrokerage,
URI uri) {
String path = uri.toString();
if (path.contains(":")) {
path = UriComponentsBuilder.fromUriString(path).build().getPath();
}
for (String endPoint : wsBrokerage.getEndPoints()) {
if (PatternMatchUtils.simpleMatch(toPattern(endPoint), path + "/")) {
return endPoint;
}
}
return null;
}
private ZuulWebSocketProperties.WsBrokerage getWebSocketBrokarage(URI uri) {
String path = uri.toString();
if (path.contains(":")) {
path = UriComponentsBuilder.fromUriString(path).build().getPath();
}
for (Map.Entry entry : zuulWebSocketProperties
.getBrokerages().entrySet()) {
ZuulWebSocketProperties.WsBrokerage wsBrokerage = entry.getValue();
if (wsBrokerage.isEnabled()) {
for (String endPoint : wsBrokerage.getEndPoints()) {
if (PatternMatchUtils.simpleMatch(toPattern(endPoint), path + "/")) {
return wsBrokerage;
}
}
}
}
return null;
}
private String toPattern(String path) {
path = path.startsWith("/") ? "**" + path : "**/" + path;
return path.endsWith("/") ? path + "**" : path + "/**";
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
throws Exception {
disconnectFromProxiedTarget(session);
super.afterConnectionClosed(session, closeStatus);
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage> message)
throws Exception {
super.handleMessage(session, message);
handleMessageFromClient(session, message);
}
private void handleMessageFromClient(WebSocketSession session,
WebSocketMessage> message) throws Exception {
boolean handled = false;
WebSocketMessageAccessor accessor = WebSocketMessageAccessor.create(message);
if (StompCommand.SEND.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
sendMessageToProxiedTarget(session, accessor);
}
if (StompCommand.SUBSCRIBE.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
subscribeToProxiedTarget(session, accessor);
}
if (StompCommand.UNSUBSCRIBE.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
unsubscribeFromProxiedTarget(session, accessor);
}
if (StompCommand.CONNECT.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
connectToProxiedTarget(session);
}
if (!handled) {
if (logger.isDebugEnabled()) {
logger.debug("STOMP COMMAND " + accessor.getCommand()
+ " was not explicitly handled");
}
}
}
private void connectToProxiedTarget(WebSocketSession session) {
URI sessionUri = session.getUri();
ZuulWebSocketProperties.WsBrokerage wsBrokerage = getWebSocketBrokarage(
sessionUri);
Assert.notNull(wsBrokerage, "wsBrokerage must not be null");
String path = getWebSocketServerPath(wsBrokerage, sessionUri);
Assert.notNull(path, "Web socket uri path must be null");
URI routeTarget = proxyTargetResolver.resolveTarget(wsBrokerage);
Assert.notNull(routeTarget, "routeTarget must not be null");
String uri = ServletUriComponentsBuilder
.fromUri(routeTarget)
.path(path)
.replaceQuery(sessionUri.getQuery())
.toUriString();
ProxyWebSocketConnectionManager connectionManager = new ProxyWebSocketConnectionManager(
messagingTemplate, stompClient, session, headersCallback, uri);
connectionManager.errorHandler(this.errorHandler);
managers.put(session, connectionManager);
connectionManager.start();
}
private void disconnectFromProxiedTarget(WebSocketSession session) {
disconnectProxyManager(managers.remove(session));
}
private void disconnectProxyManager(ProxyWebSocketConnectionManager proxyManager) {
if (proxyManager != null) {
try {
proxyManager.disconnect();
} catch (Throwable ignored) {
// nothing
}
}
}
private void unsubscribeFromProxiedTarget(WebSocketSession session,
WebSocketMessageAccessor accessor) {
ProxyWebSocketConnectionManager manager = managers.get(session);
if (manager != null) {
manager.unsubscribe(accessor.getDestination());
}
}
private void sendMessageToProxiedTarget(WebSocketSession session,
WebSocketMessageAccessor accessor) {
ProxyWebSocketConnectionManager manager = managers.get(session);
manager.sendMessage(accessor.getDestination(), accessor.getPayload());
}
private void subscribeToProxiedTarget(WebSocketSession session,
WebSocketMessageAccessor accessor) throws Exception {
ProxyWebSocketConnectionManager manager = managers.get(session);
manager.subscribe(accessor.getDestination());
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ReconnectErrorHandler.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import com.github.mthizo247.cloud.netflix.zuul.web.util.ErrorAnalyzer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.messaging.simp.stomp.ConnectionLostException;
import java.io.IOException;
/**
* Created by ronald22 on 06/04/2017.
*/
public class ReconnectErrorHandler implements ProxyWebSocketErrorHandler {
private static final long CONNECTION_LOST_RECONNECT_INTERVAL = 10000;
private final Log logger = LogFactory
.getLog(ReconnectErrorHandler.class);
private ErrorAnalyzer errorAnalyzer;
public ReconnectErrorHandler(ErrorAnalyzer errorAnalyzer) {
this.errorAnalyzer = errorAnalyzer;
}
@Override
public void handleError(Throwable t) {
if (logger.isErrorEnabled()) {
logger.error("Proxy web socket error occurred.", t);
}
if (!(t instanceof ProxySessionException))
return;
ProxySessionException exception = (ProxySessionException) t;
if (shouldAttemptToReconnect(exception)) {
executeReconnectThread(exception.getConnectionManager());
}
}
private boolean shouldAttemptToReconnect(ProxySessionException proxyException) {
if (!isConnectionLost(proxyException))
return false;
if (!proxyException.getConnectionManager().isConnectedToUserAgent())
return false;
return true;
}
private boolean isConnectionLost(ProxySessionException proxyException) {
Throwable cause = proxyException.getCause();
Throwable[] causeChain = errorAnalyzer.determineCauseChain(cause);
Throwable throwable = errorAnalyzer
.getFirstThrowableOfType(ConnectionLostException.class, causeChain);
if (throwable != null)
return true;
throwable = errorAnalyzer.getFirstThrowableOfType(IOException.class,
causeChain);
return throwable != null
&& throwable.getMessage().toLowerCase().contains(" refused ");
}
private void executeReconnectThread(
final ProxyWebSocketConnectionManager clientConnectionManager) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
clientConnectionManager.reconnect(CONNECTION_LOST_RECONNECT_INTERVAL);
}
});
t.setDaemon(true);
t.start();
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/WebSocketHttpHeadersCallback.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
/**
* Callback strategy to supply web socket headers to handshake requests.
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public interface WebSocketHttpHeadersCallback {
void applyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers);
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/WebSocketMessageAccessor.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.WebSocketMessage;
/**
* An accessor to acess web socket messages before discpatching them to the backend
* server.
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
public final class WebSocketMessageAccessor {
private String[] messageComponents;
private WebSocketMessageAccessor() {
}
public static WebSocketMessageAccessor create(WebSocketMessage> message) {
String msgPayload = message.getPayload().toString();
return create(msgPayload);
}
public static WebSocketMessageAccessor create(String message) {
WebSocketMessageAccessor accessor = new WebSocketMessageAccessor();
accessor.messageComponents = StringUtils.tokenizeToStringArray(message, "\n");
return accessor;
}
public String getCommand() {
if (accessible())
return messageComponents[0];
return null;
}
private boolean accessible() {
return messageComponents != null && messageComponents.length > 0;
}
public String getDestination() {
return getHeader("destination");
}
public String getHeader(String header) {
if (!accessible())
return null;
header = header.endsWith(":") ? header : header + ":";
if (accessible()) {
for (String messageComponent : messageComponents) {
int indx = messageComponent.indexOf(header);
if (indx != -1) {
return messageComponent.substring(indx + header.length());
}
}
}
return null;
}
public String getPayload() {
if (!accessible())
return null;
return messageComponents[messageComponents.length - 1];
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ZuulWebSocketConfiguration.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import com.github.mthizo247.cloud.netflix.zuul.web.authentication.BasicAuthPrincipalHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.authentication.CompositeHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.authentication.LoginCookieHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.authentication.OAuth2BearerPrincipalHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.filter.ProxyRedirectFilter;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.CompositeProxyTargetResolver;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.EurekaProxyTargetResolver;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.LoadBalancedProxyTargetResolver;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.ProxyTargetResolver;
import com.github.mthizo247.cloud.netflix.zuul.web.proxytarget.UrlProxyTargetResolver;
import com.github.mthizo247.cloud.netflix.zuul.web.util.DefaultErrorAnalyzer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;
import org.springframework.web.socket.config.annotation.SockJsServiceRegistration;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Zuul reverse proxy web socket configuration
*
* @author Ronald Mthombeni
* @author Salman Noor
*/
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(WebSocketHandler.class)
@ConditionalOnProperty(prefix = "zuul.ws", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(ZuulWebSocketProperties.class)
@AutoConfigureAfter(DelegatingWebSocketMessageBrokerConfiguration.class)
public class ZuulWebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer
implements ApplicationListener {
@Autowired
ZuulWebSocketProperties zuulWebSocketProperties;
@Autowired
SimpMessagingTemplate messagingTemplate;
@Autowired
ZuulProperties zuulProperties;
@Autowired
@Qualifier("compositeProxyTargetResolver")
ProxyTargetResolver proxyTargetResolver;
@Autowired
ProxyWebSocketErrorHandler proxyWebSocketErrorHandler;
@Autowired
WebSocketStompClient stompClient;
@Autowired
@Qualifier("compositeHeadersCallback")
WebSocketHttpHeadersCallback webSocketHttpHeadersCallback;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
boolean wsEnabled = false;
for (Map.Entry entry : zuulWebSocketProperties
.getBrokerages().entrySet()) {
ZuulWebSocketProperties.WsBrokerage wsBrokerage = entry.getValue();
if (wsBrokerage.isEnabled()) {
this.addStompEndpoint(registry, wsBrokerage.getEndPoints());
wsEnabled = true;
}
}
if (!wsEnabled)
this.addStompEndpoint(registry, UUID.randomUUID().toString());
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
// prefix for subscribe
for (Map.Entry entry : zuulWebSocketProperties
.getBrokerages().entrySet()) {
ZuulWebSocketProperties.WsBrokerage wsBrokerage = entry.getValue();
if (wsBrokerage.isEnabled()) {
config.enableSimpleBroker(
mergeBrokersWithApplicationDestinationPrefixes(wsBrokerage));
// prefix for send
config.setApplicationDestinationPrefixes(
wsBrokerage.getDestinationPrefixes());
}
}
}
private SockJsServiceRegistration addStompEndpoint(StompEndpointRegistry registry, String... endpoint) {
return registry.addEndpoint(endpoint)
// bypasses spring web security
.setAllowedOrigins("*").withSockJS();
}
private String[] mergeBrokersWithApplicationDestinationPrefixes(
ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
List brokers = new ArrayList<>(Arrays.asList(wsBrokerage.getBrokers()));
for (String adp : wsBrokerage.getDestinationPrefixes()) {
if (!brokers.contains(adp)) {
brokers.add(adp);
}
}
return brokers.toArray(new String[brokers.size()]);
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
@Override
public WebSocketHandler decorate(WebSocketHandler handler) {
ProxyWebSocketHandler proxyWebSocketHandler = new ProxyWebSocketHandler(
handler, stompClient, webSocketHttpHeadersCallback,
messagingTemplate,
proxyTargetResolver,
zuulWebSocketProperties);
proxyWebSocketHandler.errorHandler(proxyWebSocketErrorHandler);
return proxyWebSocketHandler;
}
});
}
@Bean
@Primary
public WebSocketHttpHeadersCallback compositeHeadersCallback(final List headersCallbacks) {
return new CompositeHeadersCallback(headersCallbacks);
}
@Bean
@ConditionalOnClass(name = "org.springframework.security.core.Authentication")
public WebSocketHttpHeadersCallback basicAuthPrincipalHeadersCallback() {
return new BasicAuthPrincipalHeadersCallback();
}
@Bean
@ConditionalOnClass(name = "org.springframework.security.core.OAuth2Authentication")
public WebSocketHttpHeadersCallback oauth2BearerPrincipalHeadersCallback() {
return new OAuth2BearerPrincipalHeadersCallback();
}
@Bean
public WebSocketHttpHeadersCallback loginCookieHeadersCallback() {
return new LoginCookieHeadersCallback();
}
@Bean
public ProxyTargetResolver urlProxyTargetResolver(
final ZuulProperties zuulProperties) {
return new UrlProxyTargetResolver(zuulProperties);
}
@Bean
public ProxyTargetResolver discoveryProxyTargetResolver(
final ZuulProperties zuulProperties, final DiscoveryClient discoveryClient) {
return new EurekaProxyTargetResolver(discoveryClient, zuulProperties);
}
@Bean
public ProxyTargetResolver loadBalancedProxyTargetResolver(
final ZuulProperties zuulProperties, final LoadBalancerClient loadBalancerClient) {
return new LoadBalancedProxyTargetResolver(loadBalancerClient, zuulProperties);
}
@Bean
@Primary
public ProxyTargetResolver compositeProxyTargetResolver(final List resolvers) {
return new CompositeProxyTargetResolver(resolvers);
}
@Bean
@ConditionalOnMissingBean(WebSocketStompClient.class)
public WebSocketStompClient stompClient(WebSocketClient webSocketClient, MessageConverter messageConverter,
@Qualifier("proxyStompClientTaskScheduler") TaskScheduler taskScheduler) {
int bufferSizeLimit = 1024 * 1024 * 8;
WebSocketStompClient client = new WebSocketStompClient(webSocketClient);
client.setInboundMessageSizeLimit(bufferSizeLimit);
client.setMessageConverter(messageConverter);
client.setTaskScheduler(taskScheduler);
client.setDefaultHeartbeat(new long[]{0, 0});
return client;
}
@Bean
@ConditionalOnMissingBean(WebSocketClient.class)
public WebSocketClient webSocketClient() {
StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
List transports = new ArrayList<>();
transports.add(new WebSocketTransport(webSocketClient));
return new SockJsClient(transports);
}
@Bean
@Qualifier("proxyStompClientTaskScheduler")
public TaskScheduler stompClientTaskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("ProxyStompClient-");
scheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
return scheduler;
}
@Bean
public ProxyWebSocketErrorHandler reconnectErrorHandler() {
return new ReconnectErrorHandler(new DefaultErrorAnalyzer());
}
@Bean
@Primary
public ProxyWebSocketErrorHandler compositeErrorHandler(final List errorHandlers) {
return new CompositeErrorHandler(errorHandlers);
}
@Bean
public ProxyRedirectFilter proxyRedirectFilter(RouteLocator routeLocator) {
return new ProxyRedirectFilter(routeLocator);
}
@PostConstruct
public void init() {
ignorePattern("**/websocket");
ignorePattern("**/info");
}
private void ignorePattern(String ignoredPattern) {
for (String pattern : zuulProperties.getIgnoredPatterns()) {
if (pattern.toLowerCase().contains(ignoredPattern))
return;
}
zuulProperties.getIgnoredPatterns().add(ignoredPattern);
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
init();
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ZuulWebSocketProperties.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
/**
* @author Ronald Mthombeni
* @author Salman Noor
*/
@ConfigurationProperties("zuul.ws")
public class ZuulWebSocketProperties {
private boolean enabled = true;
private Map brokerages = new HashMap<>();
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Map getBrokerages() {
return brokerages;
}
public void setBrokerages(Map brokerages) {
this.brokerages = brokerages;
}
@PostConstruct
public void init() {
for (Map.Entry entry : this.brokerages.entrySet()) {
WsBrokerage wsBrokerage = entry.getValue();
if (!StringUtils.hasText(wsBrokerage.getId())) {
wsBrokerage.id = entry.getKey();
}
}
}
public static class WsBrokerage {
private boolean enabled = true;
private String id;
private String routeId;
private String[] endPoints;
private String[] brokers;
private String[] destinationPrefixes;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRouteId() {
return routeId;
}
public void setRouteId(String routeId) {
this.routeId = routeId;
}
public String[] getEndPoints() {
return endPoints;
}
public void setEndPoints(String[] endPoints) {
this.endPoints = endPoints;
}
public String[] getBrokers() {
return brokers;
}
public void setBrokers(String[] brokers) {
this.brokers = brokers;
}
public String[] getDestinationPrefixes() {
return destinationPrefixes;
}
public void setDestinationPrefixes(String[] destinationPrefixes) {
this.destinationPrefixes = destinationPrefixes;
}
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/util/DefaultErrorAnalyzer.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.util;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ronald22 on 13/02/2018.
*/
public class DefaultErrorAnalyzer implements ErrorAnalyzer {
@Override
public Throwable[] determineCauseChain(Throwable throwable) {
List chain = new ArrayList<>();
Throwable currentThrowable = throwable;
while (currentThrowable != null) {
chain.add(currentThrowable);
currentThrowable = currentThrowable.getCause();
}
return chain.toArray(new Throwable[chain.size()]);
}
@Override
public Throwable getFirstThrowableOfType(Class extends Throwable> throwableType, Throwable[] chain) {
if (chain != null) {
for (Throwable t : chain) {
if ((t != null) && throwableType.isInstance(t)) {
return t;
}
}
}
return null;
}
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/util/ErrorAnalyzer.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.util;
/**
* Created by ronald22 on 13/02/2018.
*/
public interface ErrorAnalyzer {
Throwable[] determineCauseChain(Throwable throwable);
Throwable getFirstThrowableOfType(Class extends Throwable> throwableType, Throwable[] chain);
}
================================================
FILE: src/main/java/com/github/mthizo247/cloud/netflix/zuul/web/util/MapPropertyResolver.java
================================================
package com.github.mthizo247.cloud.netflix.zuul.web.util;
import org.springframework.core.env.AbstractPropertyResolver;
import java.util.Map;
/**
* @author Ronald Mthombeni
*/
public class MapPropertyResolver extends AbstractPropertyResolver {
private Map map;
public MapPropertyResolver(Map map) {
this.map = map;
}
@Override
protected String getPropertyAsRawString(String key) {
Object v = map.get(key);
return v != null ? v.toString() : null;
}
@Override
public T getProperty(String key, Class targetType) {
Object v = map.get(key);
if (v != null) {
return targetType.cast(v);
}
return null;
}
}
================================================
FILE: src/test/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ProxyWebSocketConnectionManagerTests.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.util.ErrorHandler;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class ProxyWebSocketConnectionManagerTests {
private ProxyWebSocketConnectionManager proxyConnectionManager;
private SimpMessagingTemplate messagingTemplate = mock(SimpMessagingTemplate.class);
private WebSocketStompClient stompClient = mock(WebSocketStompClient.class);
private WebSocketSession wsSession = mock(WebSocketSession.class);
private WebSocketHttpHeadersCallback headersCallback = mock(
WebSocketHttpHeadersCallback.class);
private StompSession serverSession = mock(StompSession.class);
private ListenableFuture listenableFuture = (ListenableFuture) mock(
ListenableFuture.class);
private ErrorHandler errHandler = mock(ErrorHandler.class);
@Before
public void init() throws Exception {
String uri = "http://example.com";
proxyConnectionManager = new ProxyWebSocketConnectionManager(messagingTemplate,
stompClient, wsSession, headersCallback, uri);
proxyConnectionManager.errorHandler(errHandler);
when(listenableFuture.get()).thenReturn(serverSession);
when(stompClient.connect(uri, new WebSocketHttpHeaders(),
proxyConnectionManager)).thenReturn(listenableFuture);
}
@Test
public void sendStringMessageAsBytes() throws Exception {
String destination = "/app/messages";
String message = "hello";
proxyConnectionManager.start();
proxyConnectionManager.sendMessage(destination, message);
verify(serverSession).send(destination, message.getBytes());
}
@Test
public void handlesExcpetionUsingErrorHandler() throws Exception {
StompHeaders headers = new StompHeaders();
byte[] payload = new byte[0];
Throwable exception = new Exception("E");
proxyConnectionManager.handleException(serverSession, StompCommand.MESSAGE,
headers, payload, exception);
verify(errHandler).handleError(new ProxySessionException(proxyConnectionManager, serverSession, exception));
}
@Test
public void handlesTransportErrorUsingErrorHandler() throws Exception {
Throwable exception = new Exception("E");
proxyConnectionManager.handleTransportError(serverSession, exception);
verify(errHandler).handleError(new ProxySessionException(proxyConnectionManager, serverSession, exception));
}
}
================================================
FILE: src/test/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/WebSocketMessageAccessorTests.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
/**
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class WebSocketMessageAccessorTests {
private WebSocketMessageAccessor accessor;
@Test
public void connect() throws Exception {
accessor = WebSocketMessageAccessor.create("CONNECT\n"
+ "accept-version:1.1,1.0\n" + "heart-beat:10000,10000\n" + "\n");
assertThat(accessor.getCommand(), is("CONNECT"));
}
@Test
public void subscribe() throws Exception {
accessor = WebSocketMessageAccessor.create(
"SUBSCRIBE\n" + "id:sub-0\n" + "destination:/topic/greetings\n" + "\n");
assertThat(accessor.getCommand(), is("SUBSCRIBE"));
assertThat(accessor.getDestination(), is("/topic/greetings"));
}
@Test
public void send() throws Exception {
accessor = WebSocketMessageAccessor.create("SEND\n" + "destination:/app/hello\n"
+ "content-length:16\n" + "\n" + "{\"name\":\"hell9\"}");
assertThat(accessor.getCommand(), is("SEND"));
assertThat(accessor.getDestination(), is("/app/hello"));
assertThat(accessor.getPayload(), is("{\"name\":\"hell9\"}"));
}
}
================================================
FILE: src/test/java/com/github/mthizo247/cloud/netflix/zuul/web/socket/ZuulWebSocketPropertiesTests.java
================================================
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mthizo247.cloud.netflix.zuul.web.socket;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
/**
* @author Ronald Mthombeni
* @author Salman Noor
*/
public class ZuulWebSocketPropertiesTests {
private ZuulWebSocketProperties properties;
@Before
public void setUp() throws Exception {
properties = new ZuulWebSocketProperties();
}
@Test
public void brokerageIdSet() throws Exception {
ZuulWebSocketProperties.WsBrokerage brokerage = new ZuulWebSocketProperties.WsBrokerage();
brokerage.setId("bar");
Map brokerages = new HashMap<>();
brokerages.put("foo", brokerage);
properties.setBrokerages(brokerages);
properties.init();
assertThat(brokerage.getId(), is("bar"));
}
@Test
public void brokerageIdNotSet() throws Exception {
ZuulWebSocketProperties.WsBrokerage brokerage = new ZuulWebSocketProperties.WsBrokerage();
Map brokerages = new HashMap<>();
brokerages.put("foo", brokerage);
properties.setBrokerages(brokerages);
properties.init();
assertThat(brokerage.getId(), is("foo"));
}
}