How to use ServerBindException class of org.openqa.selenium.netty.server package

Best Selenium code snippet using org.openqa.selenium.netty.server.ServerBindException

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.netty.server;
19
20import java.io.IOException;
21import java.io.UncheckedIOException;
22
23public class ServerBindException extends UncheckedIOException {
24
25  public ServerBindException(String message, IOException cause) {
26    super(message, cause);
27  }
28
29}
30
Full Screen
copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.environment.webserver;
19
20import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
21import static java.util.Collections.singletonMap;
22import static org.openqa.selenium.json.Json.JSON_UTF_8;
23import static org.openqa.selenium.remote.http.Contents.string;
24
25import com.google.common.collect.ImmutableMap;
26
27import net.jodah.failsafe.Failsafe;
28import net.jodah.failsafe.RetryPolicy;
29
30import org.openqa.selenium.grid.config.CompoundConfig;
31import org.openqa.selenium.grid.config.Config;
32import org.openqa.selenium.grid.config.MapConfig;
33import org.openqa.selenium.grid.config.MemoizedConfig;
34import org.openqa.selenium.grid.server.BaseServerOptions;
35import org.openqa.selenium.grid.server.Server;
36import org.openqa.selenium.internal.Require;
37import org.openqa.selenium.io.TemporaryFilesystem;
38import org.openqa.selenium.net.PortProber;
39import org.openqa.selenium.netty.server.NettyServer;
40import org.openqa.selenium.netty.server.ServerBindException;
41import org.openqa.selenium.remote.http.Contents;
42import org.openqa.selenium.remote.http.HttpClient;
43import org.openqa.selenium.remote.http.HttpHandler;
44import org.openqa.selenium.remote.http.HttpMethod;
45import org.openqa.selenium.remote.http.HttpRequest;
46import org.openqa.selenium.remote.http.HttpResponse;
47
48import java.io.File;
49import java.io.IOException;
50import java.io.UncheckedIOException;
51import java.net.MalformedURLException;
52import java.net.URL;
53import java.time.temporal.ChronoUnit;
54import java.util.logging.Level;
55import java.util.logging.Logger;
56
57public class NettyAppServer implements AppServer {
58
59  private final static Config sslConfig = new MapConfig(
60    singletonMap("server", singletonMap("https-self-signed", true)));
61  private static final Logger LOG = Logger.getLogger(NettyAppServer.class.getName());
62  private Server<?> server;
63  private Server<?> secure;
64  private final RetryPolicy<Object> retryPolicy = new RetryPolicy<>()
65    .withMaxAttempts(5)
66    .withDelay(100, 1000, ChronoUnit.MILLIS)
67    .handle(ServerBindException.class)
68    .onRetry(e -> {
69      LOG.log(Level.WARNING, String.format("NettyAppServer retry #%s. ", e.getAttemptCount()));
70      initValues();
71    })
72    .onRetriesExceeded(e -> LOG.log(Level.WARNING, "NettyAppServer start aborted."));
73
74  public NettyAppServer() {
75    initValues();
76  }
77
78  public NettyAppServer(HttpHandler handler) {
79    this(
80      createDefaultConfig(),
81      Require.nonNull("Handler", handler));
82  }
83
84  private NettyAppServer(Config config, HttpHandler handler) {
85    Require.nonNull("Config", config);
86    Require.nonNull("Handler", handler);
87
88    server = new NettyServer(new BaseServerOptions(new MemoizedConfig(config)), handler);
89    secure = null;
90  }
91
92  private static Config createDefaultConfig() {
93    return new MemoizedConfig(new MapConfig(
94      singletonMap("server", singletonMap("port", PortProber.findFreePort()))));
95  }
96
97  public static void main(String[] args) {
98    MemoizedConfig config = new MemoizedConfig(
99      new MapConfig(singletonMap("server", singletonMap("port", 2310))));
100    BaseServerOptions options = new BaseServerOptions(config);
101
102    HttpHandler handler = new HandlersForTests(
103      options.getHostname().orElse("localhost"),
104      options.getPort(),
105      TemporaryFilesystem.getDefaultTmpFS().createTempDir("netty", "server").toPath());
106
107    NettyAppServer server = new NettyAppServer(
108      config,
109      handler);
110    server.start();
111
112    System.out.printf("Server started. Root URL: %s%n", server.whereIs("/"));
113  }
114
115  private void initValues() {
116    Config config = createDefaultConfig();
117    BaseServerOptions options = new BaseServerOptions(config);
118
119    File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("generated", "pages");
120
121    HttpHandler handler = new HandlersForTests(
122      options.getHostname().orElse("localhost"),
123      options.getPort(),
124      tempDir.toPath());
125
126    server = new NettyServer(options, handler);
127
128    Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig());
129    BaseServerOptions secureOptions = new BaseServerOptions(secureConfig);
130
131    HttpHandler secureHandler = new HandlersForTests(
132      secureOptions.getHostname().orElse("localhost"),
133      secureOptions.getPort(),
134      tempDir.toPath());
135
136    secure = new NettyServer(secureOptions, secureHandler);
137  }
138
139  @Override
140  public void start() {
141    Failsafe.with(retryPolicy).run(
142      () -> {
143        server.start();
144        if (secure != null) {
145          secure.start();
146        }
147      }
148    );
149  }
150
151  @Override
152  public void stop() {
153    server.stop();
154    if (secure != null) {
155      secure.stop();
156    }
157  }
158
159  @Override
160  public String whereIs(String relativeUrl) {
161    return createUrl(server, "http", getHostName(), relativeUrl);
162  }
163
164  @Override
165  public String whereElseIs(String relativeUrl) {
166    return createUrl(server, "http", getAlternateHostName(), relativeUrl);
167  }
168
169  @Override
170  public String whereIsSecure(String relativeUrl) {
171    if (secure == null) {
172      throw new IllegalStateException("Server not configured to return HTTPS url");
173    }
174    return createUrl(secure, "https", getHostName(), relativeUrl);
175  }
176
177  @Override
178  public String whereIsWithCredentials(String relativeUrl, String user, String password) {
179    return String.format(
180      "http://%s:%[email protected]%s:%d/%s",
181      user,
182      password,
183      getHostName(),
184      server.getUrl().getPort(),
185      relativeUrl);
186  }
187
188  private String createUrl(Server<?> server, String protocol, String hostName, String relativeUrl) {
189    if (!relativeUrl.startsWith("/")) {
190      relativeUrl = "/" + relativeUrl;
191    }
192
193    try {
194      return new URL(
195        protocol,
196        hostName,
197        server.getUrl().getPort(),
198        relativeUrl
199      ).toString();
200    } catch (MalformedURLException e) {
201      throw new UncheckedIOException(e);
202    }
203  }
204
205  @Override
206  public String create(Page page) {
207    try (HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(whereIs("/")))) {
208      HttpRequest request = new HttpRequest(HttpMethod.POST, "/common/createPage");
209      request.setHeader(CONTENT_TYPE, JSON_UTF_8);
210      request.setContent(Contents.asJson(ImmutableMap.of("content", page.toString())));
211      HttpResponse response = client.execute(request);
212      return string(response);
213    } catch (IOException ex) {
214      throw new RuntimeException(ex);
215    }
216  }
217
218  @Override
219  public String getHostName() {
220    return AppServer.detectHostname();
221  }
222
223  @Override
224  public String getAlternateHostName() {
225    return AppServer.detectAlternateHostname();
226  }
227}
228
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on ServerBindException

    No relevant questions found for this class 😞

Most used methods in ServerBindException

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)