io.snice.networking.examples.http.HttpApp Maven / Gradle / Ivy
The newest version!
package io.snice.networking.examples.http;
import io.netty.util.NetUtil;
import io.snice.buffer.Buffers;
import io.snice.codecs.codec.http.HttpHeader;
import io.snice.codecs.codec.http.HttpRequest;
import io.snice.codecs.codec.http.HttpResponse;
import io.snice.networking.common.Transport;
import io.snice.networking.common.event.ConnectionInfoEvent;
import io.snice.networking.common.event.ConnectionStatsEvent;
import io.snice.networking.common.event.SslInfoEvent;
import io.snice.networking.http.HttpApplication;
import io.snice.networking.http.HttpBootstrap;
import io.snice.networking.http.HttpConnection;
import io.snice.networking.http.HttpEnvironment;
import io.snice.networking.http.event.HttpEvent;
import io.snice.networking.http.event.HttpMessageEvent;
import io.snice.preconditions.PreConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class HttpApp extends HttpApplication {
private static final Logger logger = LoggerFactory.getLogger(HttpApp.class);
private final String username;
private final String password;
public HttpApp(final String username, final String password) {
this.username = username;
this.password = password;
}
@Override
public void initialize(final HttpBootstrap bootstrap) {
bootstrap.onConnection(id -> true).accept(b -> {
b.match(HttpEvent::isHttpRequest).map(HttpEvent::toMessageEvent).consume(HttpApp::onHttpRequest);
b.match(HttpEvent::isHttpResponse).map(HttpEvent::toMessageEvent).consume(HttpApp::onHttpResponse);
b.matchEvent(o -> true).consume(HttpApp::onApplicationEvent);
});
}
private static String produceTwiml(final String msg) {
return "" + msg + " ";
}
private static void onApplicationEvent(final HttpConnection connection, final Object event) {
System.err.println("onApplicationEvent: " + event);
if (event instanceof ConnectionInfoEvent cie) {
if (cie.isSslInfoEvent()) {
onSslInfoSuccess(connection, (SslInfoEvent.SslInfoSuccessEvent) cie);
} else if (cie.isConnectionStatsEvent()) {
onConnectionStatsEvent(connection, cie.toConnectionStatsEvent());
}
}
}
private static void onSslInfoSuccess(final HttpConnection connection, final SslInfoEvent.SslInfoSuccessEvent event) {
// Do something fun with it.
}
private static void onConnectionStatsEvent(final HttpConnection connection, final ConnectionStatsEvent event) {
// Do something fun with it.
}
private static void onHttpRequest(final HttpConnection connection, final HttpMessageEvent event) {
final var req = event.getHttpRequest();
connection.onConnectionInfoEvent((c, e) -> {
System.err.println("Connection Handler for incoming connection: " + e);
});
final var resp = HttpResponse.create(200)
.content(Buffers.wrap(produceTwiml("Welcome to this awesome demo")))
.header(HttpHeader.CONTENT_TYPE, "application/xml")
.build();
connection.send(resp);
connection.close();
}
private static void onHttpResponse(final HttpConnection connection, final HttpMessageEvent event) {
final var resp = event.getHttpResponse();
// System.err.println("Received HTTP response outside of a Transaction: " + resp.statusCode() + " " + resp.reasonPhrase());
// resp.headers().forEach(System.err::println);
}
@Override
public void run(final MyHttpConfig configuration, final HttpEnvironment environment) {
new Thread(() -> {
sleep(1000);
sendRequest(environment);
}).start();
}
private void sendRequest(final HttpEnvironment env) {
// final var r = HttpRequest.get("http://127.0.0.1:8000/hello_world.txt")
// final var r = HttpRequest.get("https://api.twilio.com/2010-04-01/Accounts")
// Default behavior is to add this as a form-param body, which is what we want.
final var content = Map.of(
"Url", "http://0fee-135-180-103-205.ngrok.io",
"Timeout", 17,
"To", "+15105179850",
"From", "+14152039455");
final var r = HttpRequest.post("https://api.twilio.com/2010-04-01/Accounts/" + username + "/Calls")
.content(content)
.auth(username, password)
.build();
final var r2 = HttpRequest.get("https://api.twilio.com/2010-04-01/Accounts/" + username + "/Calls")
.auth(username, password)
.header(HttpHeader.CONNECTION, "close")
.build();
// env.connect(Transport.tcp, "127.0.0.1", 9090).thenAccept(c -> {
// sending outside of transaction
// c.send(r);
// });
// api.twilio.com = 3.214.112.227 easier when using wireshark.
env.connect(Transport.tls, "3.214.112.227", 443).thenAccept(c -> {
c.onConnectionInfoEvent((con, e) -> {
System.err.println("Connection Handler for outbound connection: " + e);
});
c.createNewTransaction(r2)
.onResponse((t, resp) -> {
System.err.println("Received a response on the transaction. App data: " + t.applicationData());
System.err.println("Received HTTP response: " + resp.statusCode() + " " + resp.reasonPhrase());
resp.headers().forEach(System.err::println);
System.err.println(resp.content().toString());
// sleep(1000);
// c.send(r2);
})
.withApplicationData("My Data")
.start();
});
}
private static void sleep(final int ms) {
try {
Thread.sleep(ms);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
public static void main(final String... args) throws Exception {
System.out.println("Is IPv4 stack preferred? " + NetUtil.isIpV4StackPreferred());
final var accountSid = System.getenv().get("TWILIO_ACCOUNT_SID");
final var authToken = System.getenv().get("TWILIO_AUTH_TOKEN");
PreConditions.assertNotEmpty(accountSid, "You must specify the Twilio Account Sid by setting the environment variable TWILIO_ACCOUNT_SID");
PreConditions.assertNotEmpty(accountSid, "You must specify the Twilio AuthToken by setting the environment variable TWILIO_AUTH_TOKEN");
final var http = new HttpApp(accountSid, authToken);
http.run("http_app.yml");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy