Skip to content

Commit

Permalink
#349 add a OkHttpClient instance
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Mar 20, 2018
1 parent 2fe9d6f commit 69ea346
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/extensions-howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ alf.io provides some objects and properties to the script in the script scope:
* **log** Log4j logger
* **extensionLogger** a logger that write in the extension_log table. It implement the [ExtensionLogger](https://github.com/alfio-event/alf.io/blob/master/src/main/java/alfio/extension/ExtensionLogger.java) interface.
* **restTemplate** Spring Framework's [RestTemplate](https://docs.spring.io/spring/docs/4.3.13.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html)
* **httpClient** instance of a [OkHttpClient](http://square.github.io/okhttp/)
* **GSON** Google's [JSON parser/generator](http://static.javadoc.io/com.google.code.gson/gson/2.8.2/com/google/gson/Gson.html)
* **returnClass** `java.lang.Class<?>` the expected result type
* **extensionParameters** a map containing the parameters of an extension
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/alfio/extension/ScriptingExecutionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

Expand All @@ -46,6 +47,9 @@
@Log4j2
public class ScriptingExecutionService {

private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
private static final RestTemplate REST_TEMPLATE = new RestTemplate();

private final static Compilable engine = (Compilable) new ScriptEngineManager().getEngineByName("nashorn");
private final Cache<String, CompiledScript> compiledScriptCache = Caffeine.newBuilder()
.expireAfterAccess(12, TimeUnit.HOURS)
Expand Down Expand Up @@ -101,7 +105,8 @@ private static <T> T executeScript(String name, CompiledScript script, Map<Strin
engineScope.put("log", log);
engineScope.put("extensionLogger", extensionLogger);
engineScope.put("GSON", Json.GSON);
engineScope.put("restTemplate", new RestTemplate());
engineScope.put("restTemplate", REST_TEMPLATE);
engineScope.put("httpClient", HTTP_CLIENT);
engineScope.put("returnClass", clazz);
engineScope.putAll(params);
T res = (T) script.eval(newContext);
Expand Down

0 comments on commit 69ea346

Please sign in to comment.