UnsupportedOperationException on Endpoint.publish() with Jetty - java

I am getting the following error:
com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.lang.UnsupportedOperationException: !DelegatingThreadPool
With this code:
ServerStart
Server server = new Server(4433); //Jetty
System.setProperty(
"com.sun.net.httpserver.HttpServerProvider",
"org.eclipse.jetty.http.spi.JettyHttpServerProvider");
JettyHttpServerProvider.setServer(server);
//error occurs here
Endpoint.publish("http://localhost:" + 4433 + "/buecherservice", new BuecherServiceImpl());
//not reached code
server.start();
server.stop();
WebService with Interface:
#WebService
public interface BuecherServiceIntf //Interface
#WebService
public class BuecherServiceImpl implements BuecherServiceIntf
Can someone help?

Change
Server server = new Server(4433);
to the following which is required for http-spi
Server server = new Server(new DelegatingThreadPool());
ServerConnector connector = new ServerConnector(server);
connector.setPort(4433);
server.addConnector(connector);

Related

How to force Spring boot embedded tomcat to use SNI (Server Name Indication)

I need to make sure that my client supports SNI and it works. To do that, I want to start some dummy server (Spring boot application with it's embedded Tomcat 9) that requires hostname from client. The question is how to force Spring boot embedded Tomcat to require SNI from client?
I've read that to do that, we need to provide to server 2 (or more) certificates.
I tried to follow example from this topic. But it does not work for me.
I generated 2 keystores mycompany1.keystore and mycompany2.keystore with 1 certificate in each of them. And 1 truststore mycompany.truststore that stores both these certificates. Certificates have CN my.hostname.com and my.another.hostname.com.
This is the way how I configure embedded tomcat:
#Component
public class MultipleHostsTomcatFactory {
#Value("${abc.com.key-store}")
String abcComKeyStore;
#Value("${xyz.com.key-store}")
String xyzComKeyStore;
#Value("${server.port}")
int serverPort;
#Value("${server.http.port:8080}")
private int httpPort;
#Bean
public ServletWebServerFactory servletContainer() throws Exception {
TomcatServletWebServerFactory factor = new TomcatServletWebServerFactory();
factor.addAdditionalTomcatConnectors(createSSLConnectorForMultipleHosts());
return factor;
}
private Connector createSSLConnectorForMultipleHosts() {
Connector connector = null;
try {
connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("https");
connector.setSecure(true);
connector.setProperty("SSLEnabled", "true");
connector.setProperty("defaultSSLHostConfigName", /*"*.abc.com"*/ "*.hostname.com");
connector.setPort(8444);
// *.abc.com
SSLHostConfig sslHostConfig = new SSLHostConfig();
sslHostConfig.setHostName("*.abc.com");
SSLHostConfigCertificate sslHostConfigCertificate = new SSLHostConfigCertificate(sslHostConfig, SSLHostConfigCertificate.Type.EC);
sslHostConfigCertificate.setCertificateKeystoreFile(abcComKeyStore);
sslHostConfig.addCertificate(sslHostConfigCertificate);
connector.addSslHostConfig(sslHostConfig);
// *.xyz.com
sslHostConfig = new SSLHostConfig();
sslHostConfig.setHostName("*.xyz.com");
sslHostConfigCertificate = new SSLHostConfigCertificate(sslHostConfig, SSLHostConfigCertificate.Type.EC);
sslHostConfigCertificate.setCertificateKeystoreFile(xyzComKeyStore);
sslHostConfig.addCertificate(sslHostConfigCertificate);
connector.addSslHostConfig(sslHostConfig);
} catch (Exception e) {
System.out.println("Catched exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
e.printStackTrace();
}
return connector;
}
}
This code is based on this topic.
But I have an error:
2022-01-13 16:10:21.301 ERROR 22364 --- [ main] org.apache.catalina.util.LifecycleBase : Failed to initialize component [Connector[HTTP/1.1-8444]]
org.apache.catalina.LifecycleException: Protocol handler initialization failed
...
Caused by: java.lang.IllegalArgumentException: Keystore was tampered with, or password was incorrect
...
Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect
...
Caused by: java.security.UnrecoverableKeyException: Password verification failed
Looks like I miss somethimg.
So, how can I force embedded Tomcat to enable SNI?
I solved the problem. Maybe my explanation will help somebody. I just followed the errors that I saw and configured all data that server missed. I don't configure certificate, because I have keystore that stores this certificate. My final server has 2 hosts that support only TLSv1.3 (for my needs):
private Connector createSSLConnectorForMultipleHosts() {
Connector connector = null;
try {
Http2Protocol http2Protocol = new Http2Protocol();
Http11NioProtocol http11Protocol = new Http11NioProtocol();
http2Protocol.setHttp11Protocol(http11Protocol);
connector = new Connector(http11Protocol);
connector.addUpgradeProtocol(http2Protocol);
connector.setScheme("https");
connector.setSecure(true);
connector.setProperty("SSLEnabled", "true");
connector.setProperty("defaultSSLHostConfigName", "my.hostname.com");
connector.setPort(8445);
// first
SSLHostConfig sslHostConfig = new SSLHostConfig();
sslHostConfig.setHostName("my.hostname.com");
sslHostConfig.setCertificateKeystorePassword("mypassword");
sslHostConfig.setCertificateKeystoreFile(firstKeyStore);
sslHostConfig.setProtocols("TLSv1.3");
connector.addSslHostConfig(sslHostConfig);
// second
sslHostConfig = new SSLHostConfig();
sslHostConfig.setHostName("my.another.hostname.com");
sslHostConfig.setCertificateKeystorePassword("mypassword");
sslHostConfig.setCertificateKeystoreFile(secondKeyStore);
sslHostConfig.setProtocols("TLSv1.3");
connector.addSslHostConfig(sslHostConfig);
} catch (Exception e) {
e.printStackTrace();
}
return connector;
}

cannot run telegram bot with proxy

I wrote a robot under the server
And I want to run with a proxy
But I miss the following error
mybot is:
public class StoreBot extends TelegramLongPollingBot {
public StoreBot( DefaultBotOptions botOptions) {
super(botOptions);
}
public int creatorId() {
return 0;
}
public StoreBot() {
}
....
}
mycode for run bot is:
ApiContextInitializer.init();
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi();
// Set up Http proxy
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
botOptions.setProxyHost("2.181.254.198");
botOptions.setProxyPort(8580);
// Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
// Register your newly created AbilityBot
StoreBot bot = new StoreBot(botOptions);
botsApi.registerBot(bot);
when run get this error:
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error removing old webhook
...
Caused by: org.telegram.telegrambots.meta.exceptions.TelegramApiException: Unable to execute deleteWebhook method
...
Caused by: java.net.SocketException: Connection timed out: connect
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(new HttpHost(host, port))
.build();
DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setRequestConfig(requestConfig);
StoreBot bot = new StoreBot(botOptions);

AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started

Im trying to start Appium server programmatically from Java
(OS: Windows7 x64)
using first method from source: http://www.automationtestinghub.com/3-ways-to-start-appium-server-from-java/
The code that I use for starting Appium sever is:
public void startServer() {
//Set Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
//Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");
//added by myself:
builder.usingDriverExecutable(new File("C:/node/node.exe"));
builder.withAppiumJS(new File("C:/Users/[user]/AppData/Roaming/npm/node_modules/appium/lib/appium.js"));
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
}
I'm getting an exception:
Exception in thread "main" io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\node\node.exe Arguments: [C:\Users\Dima\AppData\Roaming\npm\node_modules\appium\lib\appium.js, --port, 4723, --address, 127.0.0.1, --log-level, error, --session-override, --default-capabilities, {\"noReset\": \"false\"}]
Process output: C:\Users[user]\AppData\Roaming\npm\node_modules\appium\lib\appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash'; ^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
I tried every way to start Appium server from the source, but second one causes to the same, but third causes to error
Any ideas? Thanks to all in advance!
Take a look at official documentation:
https://github.com/appium/java-client/blob/master/docs/The-starting-of-an-app-using-Appium-node-server-started-programmatically.md
Make sure your env setup for Appium is correct and that appium service/client versions are compatible.
It did mistake in code. Fixed method is:
public static String runAppiumService(int appiumPort) {
//Build parameters for appium server:
AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder();
appiumServiceBuilder.usingPort(appiumPort)
.withIPAddress(APPIUM_IP)
.withAppiumJS(new File(getAppiumJsPath()))
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File(System.getProperty("user.dir") + "/target/resources/appium_server_logs" + Thread.currentThread().getId()));
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(appiumServiceBuilder);
service.start();
}
public class ServerManager {
AppiumDriverLocalService service;
public static void main(String args[]) {
ServerManager sm = new ServerManager();
sm.startServer();
}
public void startServer() {
final DesiredCapabilities cap = new DesiredCapabilities();
//Set Capabilities
cap.setCapability("noReset", "false");
//Build the Appium service
AppiumServiceBuilder builder = new AppiumServiceBuilder();
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error");
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service = AppiumDriverLocalService.buildService(builder);
service.start();
}
public void stopServer() {
service.stop();
}
}

Mysterious ClasscastException from apache httpclient code

We are using Apache's commons-httpclient-3.1 library in our project. However, due to legacy and other project specific dependencies we are not using any of the implementations of the SecureProtocolSocketFactory interface provided by Apache (like SSLProtocolSocketFactory,StrictSSLProtocolSocketFactory) We have created our own SocketFactory called HttpClient3xSocketFactory which implements SecureProtocolSocketFactory interface.This factory is registered with Apache by calling the Protocol.registerProtocol method of org.apache.commons.httpclient.protocol.Protocol.java. Given below snippet of our code
public class HttpClient3xSocketFactory implements SecureProtocolSocketFactory {
. . .
private static final ProtocolSocketFactory socketFactory = new HttpClient3xSocketFactory();
private static final Protocol httpsProto = new Protocol("https",
socketFactory, 443);
And registered like below
Protocol.registerProtocol("https", httpsProto);
However this generates a mysterious ClasscastException from tunnelCreated() method of Apache's HttpConnection from the last line
if (usingSecureSocket) {
throw new IllegalStateException("Already using a secure socket");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Secure tunnel to " + this.hostName + ":" + this.portNumber);
}
SecureProtocolSocketFactory socketFactory =
(SecureProtocolSocketFactory) protocolInUse.getSocketFactory();
The exception says --
Caused by: java.lang.ClassCastException: com.vmware.vdi.common.saml.HttpClient3xSocketFactory cannot be cast to org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
at org.apache.commons.httpclient.HttpConnection.tunnelCreated(HttpConnection.java:791)
at org.apache.commons.httpclient.HttpMethodDirector.executeConnect(HttpMethodDirector.java:528)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:404)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:178)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:408)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:327)
But com.vmware.vdi.common.saml.HttpClient3xSocketFactory DOES implement org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory interface. Have verified using GetClassLoader method that both are loaded using the same class loader. Also used reflection at run time to check that com.vmware.vdi.common.saml.HttpClient3xSocketFactory actually does implement org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory interface. So I am at a loss to understand as to why the ClassCaseException is generated.

Java: Duplicated port bind by jetty7 under jdk7

I have met an issue that the jetty7 server didn't throw 'Address already in use' exception when the port was occupied by other application. But it had worked well on jdk6.
Environment:
Server: windows 2008 server
Java: 7.0.20.13
Jetty: 7.6.0.RC4
Below is my demo code:
public class Jetty7OnJdk7 {
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector(); //this is the default connect in jetty7
connector.setReuseAddress(false); //prevent the jetty reusing the occupied port
connector.setPort(80);
server.setConnectors(new Connector[]{connector});
server.start();
System.out.println("=========:" + server);
}
}

Categories

Resources