/**
   * Select an endpoint that is supported by the stack and has the highest security level.
   *
   * @param endpoints
   * @return encrypted endpoint
   * @throws ServiceResultException error
   */
  public static EndpointDescription select(EndpointDescription[] endpoints)
      throws ServiceResultException {
    // Filter out all but opc.tcp protocol endpoints
    exit:
    {
      EndpointDescription[] tcpEndpoints = EndpointUtil.selectByProtocol(endpoints, "opc.tcp");
      // Filter out all but Signed & Encrypted endpoints
      tcpEndpoints =
          EndpointUtil.selectByMessageSecurityMode(
              tcpEndpoints, MessageSecurityMode.SignAndEncrypt);
      // No suitable endpoint was found
      if (tcpEndpoints.length == 0) break exit;
      // Sort endpoints by security level. The lowest level at the beginning, the highest at the end
      // of the array
      tcpEndpoints = EndpointUtil.sortBySecurityLevel(tcpEndpoints);
      // Choose one endpoint
      return tcpEndpoints[tcpEndpoints.length - 1];
    }

    // Find Https endpoints
    exit:
    {
      EndpointDescription[] httpsEndpoints = EndpointUtil.selectByProtocol(endpoints, "https");
      // No suitable endpoint was found
      if (httpsEndpoints.length == 0) break exit;
      // Choose one endpoint
      return httpsEndpoints[0];
    }

    throw new ServiceResultException("No compatible endpoint was found");
  }
 /**
  * Select the most suitable endpoint.
  *
  * <p>Selection uses the following precedence: 1) Protocol must be opc.tcp (as http is not
  * implemented) 2) Security uses sign & encrypt 3) Select highest security level (determined by
  * the server) 4) Prefer hostname over localhost
  *
  * @param endpoints
  * @return compatible endpoint or null
  */
 public static EndpointDescription selectEndpoint(EndpointDescription[] endpoints) {
   if (endpoints == null) throw new IllegalArgumentException("null arg");
   // Filter out all but opc.tcp protocol endpoints
   endpoints = EndpointUtil.selectByProtocol(endpoints, "opc.tcp");
   // Filter out all but Signed & Encrypted endpoints
   endpoints =
       EndpointUtil.selectByMessageSecurityMode(endpoints, MessageSecurityMode.SignAndEncrypt);
   //
   if (endpoints.length == 0) return null;
   // Sort endpoints by security level. The lowest level at the beginning, the highest at the end
   // of the array
   endpoints = EndpointUtil.sortBySecurityLevel(endpoints);
   EndpointUtil.reverse(endpoints);
   return endpoints[0];
 }