io.femo.http.helper.HttpHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-jdk7 Show documentation
Show all versions of http-jdk7 Show documentation
An easy to use HTTP API, that supports synchronous and asynchronous execution of HTTP request.
On android only asynchronous driver is supported.
This library has been backported to jdk 7 to retain compatibility with android!
The newest version!
package io.femo.http.helper;
import io.femo.http.Driver;
import io.femo.http.HttpContext;
import io.femo.http.HttpRequest;
import io.femo.http.HttpResponse;
import io.femo.http.drivers.server.HttpThread;
import io.femo.support.jdk7.Optional;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.xjs.dynamic.Pluggable;
import org.xjs.dynamic.PluggableAccessor;
import java.net.Socket;
import java.net.SocketAddress;
/**
* Created by Felix Resch on 29-Apr-16.
*/
public class HttpHelper {
private static ThreadLocal context = new ThreadLocal() {
@Contract(" -> !null")
@Override
protected HttpContext initialValue() {
return new DefaultHttpContext();
}
};
@Nullable
public static HttpResponse response() {
if(Thread.currentThread() instanceof Pluggable) {
Optional httpResponse = PluggableAccessor.getFirst(((Pluggable) Thread.currentThread()), HttpResponse.class);
if(httpResponse.isPresent()) {
return httpResponse.get();
}
}
return null;
}
@Nullable
public static HttpRequest request() {
if(Thread.currentThread() instanceof Pluggable) {
Optional httpRequest = PluggableAccessor.getFirst(((Pluggable) Thread.currentThread()), HttpRequest.class);
if(httpRequest.isPresent()) {
return httpRequest.get();
}
}
return null;
}
public static void response(HttpResponse response) {
if(Thread.currentThread() instanceof Pluggable) {
Pluggable pluggable = (Pluggable) Thread.currentThread();
PluggableAccessor.removeAll(pluggable, HttpResponse.class);
PluggableAccessor.add(pluggable, response);
}
}
public static void request(HttpRequest request) {
if(Thread.currentThread() instanceof Pluggable) {
Pluggable pluggable = (Pluggable) Thread.currentThread();
PluggableAccessor.removeAll(pluggable, HttpRequest.class);
PluggableAccessor.add(pluggable, request);
}
}
@Nullable
public static SocketAddress remote() {
if(Thread.currentThread() instanceof Pluggable) {
Optional socketAddress = PluggableAccessor.getFirst(((Pluggable) Thread.currentThread()), SocketAddress.class);
if(socketAddress.isPresent()) {
return socketAddress.get();
}
}
return null;
}
public static void remote(SocketAddress socketAddress) {
if(Thread.currentThread() instanceof Pluggable) {
Pluggable pluggable = (Pluggable) Thread.currentThread();
PluggableAccessor.removeAll(pluggable, SocketAddress.class);
PluggableAccessor.add(pluggable, socketAddress);
}
}
public static HttpContext context() {
return context.get();
}
public static void useDriver(Driver driver) {
context.get().useDriver(driver);
}
@Nullable
public static Pluggable get() {
if(Thread.currentThread() instanceof Pluggable) {
return (Pluggable) Thread.currentThread();
} else {
return null;
}
}
public static void keepOpen() {
if(Thread.currentThread() instanceof Pluggable) {
((HttpSocketOptions)PluggableAccessor.getFirst(((Pluggable) Thread.currentThread()), HttpSocketOptions.class).get()).setClose(false);
}
}
public static void callback(HandledCallback handledCallback) {
if(Thread.currentThread() instanceof Pluggable) {
((HttpSocketOptions)PluggableAccessor.getFirst(((Pluggable) Thread.currentThread()), HttpSocketOptions.class).get()).setHandledCallback(handledCallback);
}
}
}