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

com.github.zhengframework.web.GuiceServerEndpointConfigurator Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.web;

import com.google.inject.Injector;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.websocket.Extension;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

public class GuiceServerEndpointConfigurator extends ServerEndpointConfig.Configurator {

  private final Injector injector;

  @Inject
  public GuiceServerEndpointConfigurator(Injector injector) {
    this.injector = injector;
  }

  @Override
  public String getNegotiatedSubprotocol(final List supported,
      final List requested) {
    for (String proto : requested) {
      if (supported.contains(proto)) {
        return proto;
      }
    }
    return "";
  }

  @Override
  public List getNegotiatedExtensions(final List installed,
      final List requested) {
    final List ret = new ArrayList<>();
    for (Extension req : requested) {
      for (Extension extension : installed) {
        if (extension.getName().equals(req.getName())) {
          ret.add(req);
          break;
        }
      }
    }
    return ret;
  }

  @Override
  public boolean checkOrigin(final String originHeaderValue) {
    //we can't actually do anything here, because have have absolutely no context.
    return true;
  }

  @Override
  public void modifyHandshake(final ServerEndpointConfig sec, final HandshakeRequest request,
      final HandshakeResponse response) {
  }

  @Override
  public  T getEndpointInstance(Class endpointClass) throws InstantiationException {
    return injector.getInstance(endpointClass);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy