@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.uri = session.getUpgradeRequest().getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.putAll(session.getUpgradeRequest().getHeaders()); this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol(); List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions(); if (!CollectionUtils.isEmpty(jettyExtensions)) { List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size()); for (ExtensionConfig jettyExtension : jettyExtensions) { extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters())); } this.extensions = Collections.unmodifiableList(extensions); } else { this.extensions = Collections.emptyList(); } if (this.user == null) { this.user = session.getUpgradeRequest().getUserPrincipal(); } }
@OnWebSocketConnect public void onConnect(Session session) throws Exception { this.session = session; socketHealthService.register(this); LOGGER.debug("{} connected", sessionName()); session.getRemote().sendString(consoleLogCharsetJSONMessage); long start = parseStartLine(session.getUpgradeRequest()); LOGGER.debug("{} sending logs for {} starting at line {}.", sessionName(), jobIdentifier, start); try { handler.process(this, jobIdentifier, start); } catch (IOException e) { if ("Connection output is closed".equals(e.getMessage())) { LOGGER.debug("{} client (likely, browser) closed connection prematurely.", sessionName()); close(); // for good measure } else { throw e; } } }
@Before public void setup() { this.session = mock(Session.class); given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class)); given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class)); this.webSocketHandler = mock(WebSocketHandler.class); this.webSocketSession = new JettyWebSocketSession(null, null); this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession); }
@Test @SuppressWarnings("resource") public void getPrincipalNotAvailable() { UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(protocol); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getPrincipalFromNativeSession() { TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(user); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(user, session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
public String getRequestURI() { return session.getUpgradeRequest().getRequestURI().getPath(); } public StringBuffer getRequestURL() {
public StringBuffer getRequestURL() { return new StringBuffer(session.getUpgradeRequest().getRequestURI().toString()); } public DispatcherType getDispatcherType() {
public Object getAttribute(String name) { return ((ServletUpgradeRequest)session.getUpgradeRequest()).getHttpServletRequest().getAttribute(name); } @Override
public String getPathInfo() { return session.getUpgradeRequest().getRequestURI().getPath(); } public String getPathTranslated() {
public String getContextPath() { return ((ServletUpgradeRequest)session.getUpgradeRequest()).getHttpServletRequest().getContextPath(); } public String getLocalAddr() {
public String getPathInfo() { return session.getUpgradeRequest().getRequestURI().getPath(); } public String getPathTranslated() {
public String getPathTranslated() { return session.getUpgradeRequest().getRequestURI().getPath(); } public String getScheme() {
public String getRequestURI() { return session.getUpgradeRequest().getRequestURI().getPath(); } public StringBuffer getRequestURL() {
public String getPathTranslated() { return session.getUpgradeRequest().getRequestURI().getPath(); } public String getScheme() {
public String getContextPath() { return ((ServletUpgradeRequest)session.getUpgradeRequest()).getHttpServletRequest().getContextPath(); } public String getLocalAddr() {
public StringBuffer getRequestURL() { return new StringBuffer(session.getUpgradeRequest().getRequestURI().toString()); } public DispatcherType getDispatcherType() {
public Object getAttribute(String name) { return ((ServletUpgradeRequest)session.getUpgradeRequest()).getHttpServletRequest().getAttribute(name); } @Override
public WebSocketVirtualServletRequest(WebSocketServletHolder websocket, InputStream in, Session session) throws IOException { this.webSocketHolder = websocket; this.in = in; Map<String, List<String>> ugHeaders = session.getUpgradeRequest().getHeaders(); this.requestHeaders = WebSocketUtils.readHeaders(in); for (Map.Entry<String, List<String>> ent : ugHeaders.entrySet()) { if (!requestHeaders.containsKey(ent.getKey())) { requestHeaders.put(ent.getKey(), ent.getValue().get(0)); } } String path = requestHeaders.get(WebSocketUtils.URI_KEY); String origin = websocket.getRequestURI(); if (!path.startsWith(origin)) { LOG.log(Level.WARNING, "invalid path: {0} not within {1}", new Object[]{path, origin}); throw new InvalidPathException(); } this.attributes = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Object v = websocket.getAttribute("org.apache.cxf.transport.endpoint.address"); if (v != null) { attributes.put("org.apache.cxf.transport.endpoint.address", v); } }
@Override public void onWebSocketConnect(final Session sess) { super.onWebSocketConnect(sess); id = WsPool.add(this); run("[WS-OPEN] " + req.getRequestURL(), null, () -> { // add headers (for binding them to the XQuery parameters in the corresponding bind method) final UpgradeRequest ur = sess.getUpgradeRequest(); final BiConsumer<String, String> addHeader = (k, v) -> { if(v != null) headers.put(k, new Atm(v)); }; addHeader.accept("http-version", ur.getHttpVersion()); addHeader.accept("origin", ur.getOrigin()); addHeader.accept("protocol-version", ur.getProtocolVersion()); addHeader.accept("query-string", ur.getQueryString()); addHeader.accept("is-secure", String.valueOf(ur.isSecure())); addHeader.accept("request-uri", ur.getRequestURI().toString()); addHeader.accept("host", ur.getHost()); final TokenList protocols = new TokenList(); for(final String protocol : ur.getSubProtocols()) protocols.add(protocol); headers.put("sub-protocols", StrSeq.get(protocols)); findAndProcess(Annotation._WS_CONNECT, null); }); }