Repository: incubator-apex-core Updated Branches: refs/heads/devel-3 a3e861ef4 -> d748ed46f
SPOI-2255 #resolve password protect the websocket Project: http://git-wip-us.apache.org/repos/asf/incubator-apex-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-apex-core/commit/55844f86 Tree: http://git-wip-us.apache.org/repos/asf/incubator-apex-core/tree/55844f86 Diff: http://git-wip-us.apache.org/repos/asf/incubator-apex-core/diff/55844f86 Branch: refs/heads/devel-3 Commit: 55844f86af24a46907a8441787bddcf8a437e182 Parents: 9a9f153 Author: David Yan <[email protected]> Authored: Mon Apr 7 14:40:02 2014 -0700 Committer: David Yan <[email protected]> Committed: Fri Aug 28 10:56:56 2015 -0700 ---------------------------------------------------------------------- PubSubWebSocketServlet.java | 50 +++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-apex-core/blob/55844f86/PubSubWebSocketServlet.java ---------------------------------------------------------------------- diff --git a/PubSubWebSocketServlet.java b/PubSubWebSocketServlet.java index 11bf98e..21b48f7 100644 --- a/PubSubWebSocketServlet.java +++ b/PubSubWebSocketServlet.java @@ -4,6 +4,7 @@ */ package com.datatorrent.gateway; +import com.datatorrent.gateway.security.AuthenticationException; import java.io.IOException; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; @@ -23,6 +24,11 @@ import com.datatorrent.lib.util.PubSubMessage.PubSubMessageType; import com.datatorrent.lib.util.PubSubMessageCodec; import com.datatorrent.stram.util.LRUCache; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response.Status; /** * <p>PubSubWebSocketServlet class.</p> @@ -40,9 +46,11 @@ public class PubSubWebSocketServlet extends WebSocketServlet private PubSubMessageCodec<Object> codec = new PubSubMessageCodec<Object>(mapper); private InternalMessageHandler internalMessageHandler = null; private static final int latestTopicCount = 100; + private final DTGateway gateway; private final LRUCache<String, Long> latestTopics = new LRUCache<String, Long>(latestTopicCount, false) { private static final long serialVersionUID = 20140131L; + @Override public Long put(String key, Long value) { @@ -58,26 +66,42 @@ public class PubSubWebSocketServlet extends WebSocketServlet } - /* - private int timeout; - - public void setTimeout(int timeout) { - this.timeout = timeout; - } - */ + public PubSubWebSocketServlet(DTGateway gateway) + { + this.gateway = gateway; + } - /* - private int timeout; - public void setTimeout(int timeout) { - this.timeout = timeout; - } - */ public void setInternalMessageHandler(InternalMessageHandler internalMessageHandler) { this.internalMessageHandler = internalMessageHandler; } @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + if ("simple".equals(gateway.getWebAuthType())) { + Cookie[] cookies = request.getCookies(); + if (cookies != null) { + for (Cookie cookie : cookies) { + if ("session".equals(cookie.getName())) { + try { + gateway.getAuthDatabase().authenticateSession(cookie.getValue()); + } + catch (AuthenticationException ex) { + throw new WebApplicationException(ex, Status.FORBIDDEN); + } + super.service(request, response); + } + } + } + throw new WebApplicationException(Status.FORBIDDEN); + } + else { + super.service(request, response); + } + } + + @Override public WebSocket doWebSocketConnect(HttpServletRequest hsr, String protocol) { return new PubSubWebSocket();
