@Override
    public void handle(ServiceRequest<GetEndpointsRequest, GetEndpointsResponse> service)
        throws UaException {
      GetEndpointsRequest request = service.getRequest();

      String endpointUrl = request.getEndpointUrl();
      if (endpointUrl == null) endpointUrl = "";

      UaTcpStackServer server = servers.get(endpointUrl);

      EndpointDescription[] endpoints =
          (server != null) ? server.getEndpointDescriptions() : new EndpointDescription[0];

      List<String> profileUris =
          request.getProfileUris() != null
              ? Lists.newArrayList(request.getProfileUris())
              : Lists.newArrayList();

      EndpointDescription[] filtered =
          Arrays.stream(endpoints)
              .filter(ed -> filterProfileUris(ed, profileUris))
              .filter(this::filterEndpointUrls)
              .toArray(EndpointDescription[]::new);

      service.setResponse(new GetEndpointsResponse(service.createResponseHeader(), filtered));
    }
    @Override
    public void handle(ServiceRequest<FindServersRequest, FindServersResponse> service)
        throws UaException {
      FindServersRequest request = service.getRequest();

      List<ApplicationDescription> servers = Lists.newArrayList();
      List<String> serverUris = Lists.newArrayList(request.getServerUris());

      for (UaTcpStackServer server : registered) {
        ApplicationDescription description = server.getApplicationDescription();

        if (serverUris.isEmpty()) {
          servers.add(description);
        } else {
          if (serverUris.contains(description.getApplicationUri())) {
            servers.add(description);
          }
        }
      }

      ResponseHeader header = service.createResponseHeader();
      FindServersResponse response =
          new FindServersResponse(
              header, servers.toArray(new ApplicationDescription[servers.size()]));

      service.setResponse(response);
    }
  public FallbackServer() {
    UaTcpStackServerConfig config =
        UaTcpStackServerConfig.builder()
            .setApplicationName(LocalizedText.english("Stack Discovery Server"))
            .setApplicationUri("urn:digitalpetri:stack:discovery")
            .setProductUri("http://www.digitalpetri.com/opc-ua")
            .build();

    server = new UaTcpStackServer(config);
    server.addRequestHandler(FindServersRequest.class, new FindServersHandler());
    server.addRequestHandler(GetEndpointsRequest.class, new GetEndpointsHandler());
  }
 public void unregisterServer(UaTcpStackServer server) {
   if (registered.remove(server)) {
     server.getDiscoveryUrls().forEach(servers::remove);
   }
 }
 public void registerServer(UaTcpStackServer server) {
   if (registered.add(server)) {
     server.getDiscoveryUrls().forEach(url -> servers.put(url, server));
   }
 }