All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dbrain.binder.http.WebSocketServerBuilder Maven / Gradle / Ivy

There is a newer version: 0.13
Show newest version
/*
 * Copyright [2015] [Eric Poitras]
 *
 *     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 org.dbrain.binder.http;

import org.dbrain.binder.directory.ServiceKey;
import org.dbrain.binder.http.conf.WebSocketConfiguredServerConf;
import org.dbrain.binder.http.conf.WebSocketServiceServerConf;

import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.Extension;
import javax.websocket.server.ServerEndpointConfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Builder for WebSocket server endpoint.
 */
public class WebSocketServerBuilder {

    /**
     * Websocket defined using standard javax.websocket.* annotations.
     */
    public static WebSocketServiceServerConf of( Class endpointClass ) {
        return new WebSocketServiceServerConf( ServiceKey.of( endpointClass ) );
    }

    /**
     * Websocket service defined using standard javax.websocket.* annotations.
     */
    public static WebSocketServiceServerConf of( ServiceKey endpointService ) {
        return new WebSocketServiceServerConf( endpointService );
    }

    /**
     * Websocket defined with a server enpoint configuration.
     */
    public static WebSocketConfiguredServerConf of( ServerEndpointConfig endpointConfig ) {
        return new WebSocketConfiguredServerConf( endpointConfig );
    }

    public static WebSocketServerBuilder from( Class endpointClass, String path ) {
        return new WebSocketServerBuilder().endpointClass( endpointClass ).path( path );
    }

    private Class endpointClass;
    private String   path;
    private List                   subprotocols = Collections.emptyList();
    private List                extensions   = Collections.emptyList();
    private List> encoders     = Collections.emptyList();
    private List> decoders     = Collections.emptyList();

    private WebSocketServerBuilder() {
    }

    public WebSocketServerBuilder path( String path ) {
        this.path = path;
        return this;
    }

    public WebSocketServerBuilder endpointClass( Class endpointClass ) {
        this.endpointClass = endpointClass;
        return this;
    }

    public WebSocketServerBuilder subprotocols( String... subprotocols ) {
        this.subprotocols.clear();
        for ( String subprotocol : subprotocols ) {
            this.subprotocols.add( subprotocol );
        }
        return this;
    }

    public WebSocketServerBuilder addExtension( Extension extension ) {
        this.extensions.add( extension );
        return this;
    }

    public WebSocketServerBuilder addEncoder( Class encoder ) {
        this.encoders.add( encoder );
        return this;
    }

    public WebSocketServerBuilder addDecoder( Class decoder ) {
        this.decoders.add( decoder );
        return this;
    }

    public WebSocketConfiguredServerConf build() {

        return of( ServerEndpointConfig.Builder.create( endpointClass, path )
                                               .subprotocols( new ArrayList<>( subprotocols ) )
                                               .extensions( new ArrayList<>( extensions ) )
                                               .encoders( new ArrayList<>( encoders ) )
                                               .decoders( new ArrayList<>( decoders ) )
                                               .build() );

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy