net.aequologica.neo.shakuntala.EventWriterWebsocketImpl Maven / Gradle / Ivy
package net.aequologica.neo.shakuntala;
import static net.aequologica.neo.shakuntala.utils.OpenBrowserInJavaWindowsOrLinux.openBrowserInJavaWindowsOrLinux;
import java.io.IOException;
import java.net.URI;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.client.ClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
@Named(value = "websocket")
public class EventWriterWebsocketImpl extends AbstractEventWriter implements EventWriter {
private static Logger log = LoggerFactory.getLogger(EventWriterWebsocketImpl.class);
final static String WEBSOCKET = "websocket";
final private EventCodec codec;
final private Session session;
final private int bufferSize;
final private URIsAsString uris;
@Inject
@Named("repositoryEventFilter")
private RepositoryEventFilter filter;
@Inject
public EventWriterWebsocketImpl(
@Named("application.conf") final EventSpyConfig config,
@Named("jackson") final EventCodec codec,
@Named("uuid") final UUIDProducer uuid) {
if (!config.isActive("websocket") || config.getURI() == null) {
this.session = null;
this.bufferSize = 0;
this.codec = null;
this.uris = null;
return;
}
UUIDHolder.getInstance().setOnce(uuid.getUUID());
this.codec = codec;
this.bufferSize = config.getBufferSize();
this.uris = new URIsAsString(config, UUIDHolder.getInstance().get().toString());
this.session = createSession(config, this.uris);
}
private static Session createSession(final EventSpyConfig config, final URIsAsString uris) {
Session ret = null;
final ClientManager client = ClientManager.createClient();
if (config.getProxy() != null) {
client.getProperties().put("org.glassfish.tyrus.client.proxy", config.getProxy());
}
client.getProperties().put(ClientProperties.REDIRECT_ENABLED, true);
final Endpoint beanClient = new Endpoint() {
@Override
public void onOpen(Session session, javax.websocket.EndpointConfig endpointConfig) {
session.addMessageHandler(new MessageHandler.Whole() {
@Override
public void onMessage(String message) {
}
});
}
@Override
public void onClose(Session session, CloseReason closeReason) {
}
@Override
public void onError(Session session, Throwable thr) {
}
};
URI thisBuildUri = java.net.URI.create(uris.build);
try {
log.info("[shakuntala] connecting to {}", uris.build);
ret = client.connectToServer(beanClient, ClientEndpointConfig.Builder.create().build(), thisBuildUri );
} catch (Exception ignored ) {
log.debug("[shakuntala] ignored exception [{}]", ignored);
}
if (ret != null && uris.buildBrowser != null) {
if (config.isBrowserOpen()) {
try {
openBrowserInJavaWindowsOrLinux(uris.buildBrowser);
log.info("[shakuntala] build is here: {}", uris.buildBrowser);
} catch (Exception ignored ) {
log.debug("[shakuntala] ignored exception [{}]", ignored);
}
}
}
if (ret != null) {
log.info("[shakuntala] session opened to {}, using proxy {}", thisBuildUri, config.getProxy());
} else {
log.warn("[shakuntala] cannot connect to {}, using proxy {}", thisBuildUri ,config.getProxy());
}
return ret;
}
@Override
public void write(final Event event) throws IOException {
if (session == null || !session.isOpen()) {
return;
}
/*
if (filter.accept(event, EnumSet.of(EventType.ARTIFACT_RESOLVING, EventType.ARTIFACT_RESOLVED))) {
return;
}
*/
if (event.getKey().equals("buildUrl")) {
uris.log();
}
if (codec == null) {
log.trace("[shakuntala] no codec");
return;
}
String str = codec.toString(event);
// TODO instead of throwing exception, maybe chumping, or maybe send message in chunks ?
if (str.length() >= bufferSize) {
throw new IOException("message too large ("+str.length()+"). max size is " + bufferSize);
}
this.session.getBasicRemote().sendText(str);
}
@Override
public void close() throws IOException {
super.close();
if (this.session == null || !this.session.isOpen()) {
return;
}
this.session.close();
}
private static class URIsAsString {
final private String hub;
final private String build;
final private String hubBrowser;
final private String buildBrowser;
private URIsAsString(final EventSpyConfig config, final String uuid) {
hub = addTrailingSlash(config.getURI());
if (hub == null) {
build = null;
} else {
build = hub.concat(uuid);
}
hubBrowser = addTrailingSlash(config.getBrowserURI());
if (hubBrowser == null) {
buildBrowser = null;
} else {
buildBrowser = hubBrowser.concat(uuid);
}
}
static private String addTrailingSlash(Object object ) {
if (object == null) {
return null;
}
String in = object.toString();
if (in.length() == 0) {
return "/";
}
if (!in.endsWith("/")) {
return in + "/";
}
return in;
}
private void log() {
log.debug("[shakuntala] \thub websockets URI : {}\n"
+ "\tbuild websockets URI : {}\n"
+ "\thub browser URI : {}\n"
+ "\tbuild browser URI : {}",
hub,
build,
hubBrowser,
buildBrowser );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy