io.femo.http.middleware.EnvironmentReplacerMiddleware 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.middleware;
import io.femo.http.*;
import io.femo.http.helper.HttpHelper;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by felix on 6/9/16.
*/
public class EnvironmentReplacerMiddleware implements HttpMiddleware {
private Pattern replace;
private Map> replacers;
public EnvironmentReplacerMiddleware () {
replace = Pattern.compile("\\$\\{\\{(.*)\\}\\}");
replacers = new HashMap<>();
replacers.put("req.host", new HttpSupplier() {
@Override
public String get(HttpRequest req, HttpResponse res) {
return req.hasHeader("Host") ? req.header("Host").value() : "";
}
});
replacers.put("req.path", new HttpSupplier() {
@Override
public String get(HttpRequest req, HttpResponse res) {
return req.path();
}
});
}
@Override
public void handle(HttpRequest request, HttpResponse response) throws HttpHandleException {
if(response.hasHeader("Content-Type") && response.header("Content-Type").equals("text/html") ||
response.hasHeader("X-Replace-Env") && response.header("X-Replace-Env").equals("true")) {
String val;
Matcher matcher = replace.matcher(val = response.responseString());
Environment environment = HttpHelper.context().environment();
while (matcher.find()) {
String key = matcher.group(1);
if(replacers.containsKey(key)) {
matcher.reset(val = matcher.replaceFirst(replacers.get(key).get(request, response)));
} else if (environment.has(key)) {
matcher.reset(val = matcher.replaceFirst(environment.get(key).get(request, response)));
}
}
response.entity(val);
}
}
public interface HttpSupplier {
T get(HttpRequest req, HttpResponse res);
}
}