/** * 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]; }
/** * Figure out some random hostname for this computer * * @return * @throws SocketException */ public static String getHostname() throws SocketException { try { String hostname = java.net.InetAddress.getLocalHost().getHostName(); if (hostname != null) return hostname; } catch (UnknownHostException e) { } Set<InetAddress> addrs = EndpointUtil.getInetAddresses(); for (InetAddress addr : addrs) { String hostaddr = addr.getHostAddress(); String hostname = EndpointUtil.inetAddressToName(addr); if (!hostaddr.equals(hostname)) return hostname; } return "localhost"; }
public static EndpointDescription select(EndpointDescription[] endpoints, String url) throws ServiceResultException { // Use best endpoint of the same uri EndpointDescription[] endpointsOfTheSameUri = EndpointUtil.select(endpoints, url, null, null, null, null); if (endpointsOfTheSameUri.length > 0) { return select(endpointsOfTheSameUri); } // Use best endpoint of the same scheme String scheme = UriUtil.getTransportProtocol(url); EndpointDescription[] endpointsOfTheSameScheme = EndpointUtil.select(endpoints, null, scheme, null, null, null); if (endpointsOfTheSameScheme.length > 0) { return select(endpointsOfTheSameScheme); } // Just choose one endpoint return select(endpoints); }