@OnMessage(maxMessageSize = 111222) public String echoText(String msg) { switch (msg) { case "text-max": return String.format("%,d", session.getMaxTextMessageBufferSize()); case "binary-max": return String.format("%,d", session.getMaxBinaryMessageBufferSize()); case "decoders": return join(config.getDecoders(), ", "); case "encoders": return join(config.getEncoders(), ", "); case "subprotocols": if (serverConfig == null) { return "<not a ServerEndpointConfig>"; } else { List<String> protocols = new ArrayList<>(); protocols.addAll(serverConfig.getSubprotocols()); Collections.sort(protocols); return join(protocols, ", "); } case "configurator": if (serverConfig == null) { return "<not a ServerEndpointConfig>"; } else { return serverConfig.getConfigurator().getClass().getName(); } default: // normal echo return msg; } }
@Override public void register(ServerEndpointConfig serverConfig, String contextPath) throws DeploymentException { TyrusEndpointWrapper ew; Class<?> endpointClass = serverConfig.getEndpointClass(); boolean isEndpointClass = false; do { endpointClass = endpointClass.getSuperclass(); if (endpointClass.equals(Endpoint.class)) { isEndpointClass = true; } } while (!endpointClass.equals(Object.class)); if (isEndpointClass) { // we are pretty sure that endpoint class is javax.websocket.Endpoint descendant. //noinspection unchecked ew = new TyrusEndpointWrapper( (Class<? extends Endpoint>) serverConfig.getEndpointClass(), serverConfig, componentProviderService, webSocketContainer, contextPath, serverConfig.getConfigurator()); } else { final ErrorCollector collector = new ErrorCollector(); final AnnotatedEndpoint endpoint = AnnotatedEndpoint.fromClass( serverConfig.getEndpointClass(), componentProviderService, true, collector); final EndpointConfig config = endpoint.getEndpointConfig(); ew = new TyrusEndpointWrapper( endpoint, config, componentProviderService, webSocketContainer, contextPath, config instanceof ServerEndpointConfig ? ((ServerEndpointConfig) config).getConfigurator() : null); if (!collector.isEmpty()) { throw collector.composeComprehensiveException(); } } register(new TyrusEndpoint(ew)); }
/** * Invoked when server side handshake is ready to send response. * * <p>Changes in response parameter will be reflected in data sent back to client. * * @param request original request which caused this handshake. * @param response response to be send. */ public void onHandShakeResponse(UpgradeRequest request, UpgradeResponse response) { final EndpointConfig configuration = this.endpoint.getEndpointConfig(); if (configuration instanceof ServerEndpointConfig) { // http://java.net/jira/browse/TYRUS-62 final ServerEndpointConfig serverEndpointConfig = (ServerEndpointConfig) configuration; serverEndpointConfig .getConfigurator() .modifyHandshake(serverEndpointConfig, createHandshakeRequest(request), response); } }
/** * Published the provided endpoint implementation at the specified path with the specified * configuration. {@link #WsServerContainer(ServletContext)} must be called before calling this * method. * * @param sec The configuration to use when creating endpoint instances * @throws DeploymentException if the endpoint can not be published as requested */ @Override public void addEndpoint(ServerEndpointConfig sec) throws DeploymentException { if (enforceNoAddAfterHandshake && !addAllowed) { throw new DeploymentException(sm.getString("serverContainer.addNotAllowed")); } if (servletContext == null) { throw new DeploymentException(sm.getString("serverContainer.servletContextMissing")); } String path = sec.getPath(); UriTemplate uriTemplate = new UriTemplate(path); if (uriTemplate.hasParameters()) { Integer key = Integer.valueOf(uriTemplate.getSegmentCount()); SortedSet<TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key); if (templateMatches == null) { // Ensure that if concurrent threads execute this block they // both end up using the same TreeSet instance templateMatches = new TreeSet<>(TemplatePathMatchComparator.getInstance()); configTemplateMatchMap.putIfAbsent(key, templateMatches); templateMatches = configTemplateMatchMap.get(key); } if (!templateMatches.add(new TemplatePathMatch(sec, uriTemplate))) { // Duplicate uriTemplate; throw new DeploymentException( sm.getString( "serverContainer.duplicatePaths", path, sec.getEndpointClass(), sec.getEndpointClass())); } } else { // Exact match ServerEndpointConfig old = configExactMatchMap.put(path, sec); if (old != null) { // Duplicate path mappings throw new DeploymentException( sm.getString( "serverContainer.duplicatePaths", path, old.getEndpointClass(), sec.getEndpointClass())); } } endpointsRegistered = true; }
@Override public void modifyHandshake( ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); sec.getUserProperties().put("header", request.getHeaders().toString()); sec.getUserProperties().put(HttpSession.class.getName(), httpSession); if (request.getHeaders().containsKey("origin")) { sec.getUserProperties().put("origin", request.getHeaders().get("origin").get(0)); } if (request.getHeaders().containsKey("user-agent")) { sec.getUserProperties().put("user-agent", request.getHeaders().get("user-agent").get(0)); } }
@Override public Configurator getConfigurator() { if (configurator == null) { configurator = new JavaxWebSocketConfigurator(delegate.getConfigurator()); } return configurator; }
@Override public void modifyHandshake( ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { super.modifyHandshake(sec, request, response); HttpSession httpSession = (HttpSession) request.getHttpSession(); sec.getUserProperties().put("httpSession", httpSession); }
@Override public void modifyHandshake( ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { HttpSession session = (HttpSession) request.getHttpSession(); if (null != session) { config.getUserProperties().put("demo", 1L); } }
/** * Provides the equivalent of {@link #addEndpoint(ServerEndpointConfig)} for publishing plain old * java objects (POJOs) that have been annotated as WebSocket endpoints. * * @param pojo The annotated POJO */ @Override public void addEndpoint(Class<?> pojo) throws DeploymentException { ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class); if (annotation == null) { throw new DeploymentException( sm.getString("serverContainer.missingAnnotation", pojo.getName())); } String path = annotation.value(); // Validate encoders validateEncoders(annotation.encoders()); // Method mapping PojoMethodMapping methodMapping = new PojoMethodMapping(pojo, annotation.decoders(), path); // ServerEndpointConfig ServerEndpointConfig sec; Class<? extends Configurator> configuratorClazz = annotation.configurator(); Configurator configurator = null; if (!configuratorClazz.equals(Configurator.class)) { try { configurator = annotation.configurator().newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new DeploymentException( sm.getString( "serverContainer.configuratorFail", annotation.configurator().getName(), pojo.getClass().getName()), e); } } sec = ServerEndpointConfig.Builder.create(pojo, path) .decoders(Arrays.asList(annotation.decoders())) .encoders(Arrays.asList(annotation.encoders())) .subprotocols(Arrays.asList(annotation.subprotocols())) .configurator(configurator) .build(); sec.getUserProperties().put(PojoEndpointServer.POJO_METHOD_MAPPING_KEY, methodMapping); addEndpoint(sec); }
@OnOpen public void startChatChannel(EndpointConfig config, Session session) { this.endpointConfig = (ServerEndpointConfig) config; ChatServerConfigurator csc = (ChatServerConfigurator) endpointConfig.getConfigurator(); this.transcript = csc.getTranscript(); this.session = session; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException t) { } }
@Override public void modifyHandshake( ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { delegate.modifyHandshake(sec, request, response); // do not store null keys/values because Tomcat 8 uses ConcurrentMap for UserProperties Map<String, Object> userProperties = sec.getUserProperties(); Object httpSession = request.getHttpSession(); LOG.trace("httpSession: {}", httpSession); if (httpSession != null) { userProperties.put("session", httpSession); } Map<String, List<String>> headers = request.getHeaders(); LOG.trace("headers: {}", headers); if (headers != null) { userProperties.put("headers", headers); } Map<String, List<String>> parameterMap = request.getParameterMap(); LOG.trace("parameterMap: {}", parameterMap); if (parameterMap != null) { userProperties.put("parameterMap", parameterMap); } String queryString = request.getQueryString(); LOG.trace("queryString: {}", queryString); if (queryString != null) { userProperties.put("queryString", queryString); } URI requestURI = request.getRequestURI(); LOG.trace("requestURI: {}", requestURI); if (requestURI != null) { userProperties.put("requestURI", requestURI); } Principal userPrincipal = request.getUserPrincipal(); LOG.trace("userPrincipal: {}", userPrincipal); if (userPrincipal != null) { userProperties.put("userPrincipal", userPrincipal); } }
@Override public Class<?> getEndpointClass() { return delegate.getEndpointClass(); }
@Override public void modifyHandshake( ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); config.getUserProperties().put(HttpSession.class.getName(), httpSession); }
@Override public String getPath() { return delegate.getPath(); }
@Override public Map<String, Object> getUserProperties() { return delegate.getUserProperties(); }
@Override public List<Class<? extends Decoder>> getDecoders() { return delegate.getDecoders(); }
@Override public void modifyHandshake( ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { super.modifyHandshake(sec, request, response); sec.getUserProperties().put("request-headers", request.getHeaders()); }
@Override public List<Extension> getExtensions() { return delegate.getExtensions(); }
@Override public List<String> getSubprotocols() { return delegate.getSubprotocols(); }