Search in sources :

Example 21 with DeploymentException

use of javax.websocket.DeploymentException in project tomcat by apache.

the class WsServerContainer method addEndpoint.

/**
     * 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 cannot 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();
    // Add method mapping to user properties
    PojoMethodMapping methodMapping = new PojoMethodMapping(sec.getEndpointClass(), sec.getDecoders(), path);
    if (methodMapping.getOnClose() != null || methodMapping.getOnOpen() != null || methodMapping.getOnError() != null || methodMapping.hasMessageHandlers()) {
        sec.getUserProperties().put(org.apache.tomcat.websocket.pojo.Constants.POJO_METHOD_MAPPING_KEY, methodMapping);
    }
    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;
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) PojoMethodMapping(org.apache.tomcat.websocket.pojo.PojoMethodMapping)

Example 22 with DeploymentException

use of javax.websocket.DeploymentException in project tomcat by apache.

the class Util method getDecoders.

public static List<DecoderEntry> getDecoders(List<Class<? extends Decoder>> decoderClazzes) throws DeploymentException {
    List<DecoderEntry> result = new ArrayList<>();
    if (decoderClazzes != null) {
        for (Class<? extends Decoder> decoderClazz : decoderClazzes) {
            // Need to instantiate decoder to ensure it is valid and that
            // deployment can be failed if it is not
            @SuppressWarnings("unused") Decoder instance;
            try {
                instance = decoderClazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new DeploymentException(sm.getString("pojoMethodMapping.invalidDecoder", decoderClazz.getName()), e);
            }
            DecoderEntry entry = new DecoderEntry(Util.getDecoderType(decoderClazz), decoderClazz);
            result.add(entry);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) DeploymentException(javax.websocket.DeploymentException) Decoder(javax.websocket.Decoder)

Example 23 with DeploymentException

use of javax.websocket.DeploymentException in project tomcat by apache.

the class WsRemoteEndpointImplBase method setEncoders.

protected void setEncoders(EndpointConfig endpointConfig) throws DeploymentException {
    encoderEntries.clear();
    for (Class<? extends Encoder> encoderClazz : endpointConfig.getEncoders()) {
        Encoder instance;
        try {
            instance = encoderClazz.newInstance();
            instance.init(endpointConfig);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DeploymentException(sm.getString("wsRemoteEndpoint.invalidEncoder", encoderClazz.getName()), e);
        }
        EncoderEntry entry = new EncoderEntry(Util.getEncoderType(encoderClazz), instance);
        encoderEntries.add(entry);
    }
}
Also used : Encoder(javax.websocket.Encoder) Utf8Encoder(org.apache.tomcat.util.buf.Utf8Encoder) CharsetEncoder(java.nio.charset.CharsetEncoder) DeploymentException(javax.websocket.DeploymentException)

Example 24 with DeploymentException

use of javax.websocket.DeploymentException in project tomcat by apache.

the class WsHttpUpgradeHandler method init.

@Override
public void init(WebConnection connection) {
    if (ep == null) {
        throw new IllegalStateException(sm.getString("wsHttpUpgradeHandler.noPreInit"));
    }
    String httpSessionId = null;
    Object session = handshakeRequest.getHttpSession();
    if (session != null) {
        httpSessionId = ((HttpSession) session).getId();
    }
    // Need to call onOpen using the web application's class loader
    // Create the frame using the application's class loader so it can pick
    // up application specific config from the ServerContainerImpl
    Thread t = Thread.currentThread();
    ClassLoader cl = t.getContextClassLoader();
    t.setContextClassLoader(applicationClassLoader);
    try {
        wsRemoteEndpointServer = new WsRemoteEndpointImplServer(socketWrapper, webSocketContainer);
        wsSession = new WsSession(ep, wsRemoteEndpointServer, webSocketContainer, handshakeRequest.getRequestURI(), handshakeRequest.getParameterMap(), handshakeRequest.getQueryString(), handshakeRequest.getUserPrincipal(), httpSessionId, negotiatedExtensions, subProtocol, pathParameters, secure, endpointConfig);
        wsFrame = new WsFrameServer(socketWrapper, wsSession, transformation, applicationClassLoader);
        // WsFrame adds the necessary final transformations. Copy the
        // completed transformation chain to the remote end point.
        wsRemoteEndpointServer.setTransformation(wsFrame.getTransformation());
        ep.onOpen(wsSession, endpointConfig);
        webSocketContainer.registerSession(ep, wsSession);
    } catch (DeploymentException e) {
        throw new IllegalArgumentException(e);
    } finally {
        t.setContextClassLoader(cl);
    }
}
Also used : DeploymentException(javax.websocket.DeploymentException) WsSession(org.apache.tomcat.websocket.WsSession)

Example 25 with DeploymentException

use of javax.websocket.DeploymentException in project tomcat by apache.

the class TesterEndpointConfig method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        ServerEndpointConfig sec = getServerEndpointConfig();
        if (sec == null) {
            sc.addEndpoint(getEndpointClass());
        } else {
            sc.addEndpoint(sec);
        }
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Aggregations

DeploymentException (javax.websocket.DeploymentException)36 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)16 ServerContainer (javax.websocket.server.ServerContainer)11 ServletException (javax.servlet.ServletException)7 WebSocketContainer (javax.websocket.WebSocketContainer)7 IOException (java.io.IOException)6 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 Endpoint (javax.websocket.Endpoint)5 ServerEndpoint (javax.websocket.server.ServerEndpoint)5 ExecutionException (java.util.concurrent.ExecutionException)3 ClientEndpoint (javax.websocket.ClientEndpoint)3 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)3 Extension (javax.websocket.Extension)3 WebSocketExtension (io.undertow.websockets.WebSocketExtension)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 EOFException (java.io.EOFException)2 InetSocketAddress (java.net.InetSocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 HashMap (java.util.HashMap)2