1 package org.esigate.extension;
2
3 import static org.apache.commons.lang3.StringUtils.containsIgnoreCase;
4
5 import java.util.Properties;
6
7 import org.esigate.Driver;
8 import org.esigate.esi.EsiRenderer;
9 import org.esigate.events.Event;
10 import org.esigate.events.EventDefinition;
11 import org.esigate.events.EventManager;
12 import org.esigate.events.IEventListener;
13 import org.esigate.events.impl.RenderEvent;
14 import org.esigate.extension.surrogate.CapabilitiesEvent;
15 import org.esigate.extension.surrogate.Surrogate;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class Esi implements Extension, IEventListener {
32 private static final String[] CAPABILITIES = new String[] {"ESI/1.0", "ESI-Inline/1.0", "X-ESI-Fragment/1.0",
33 "X-ESI-Replace/1.0", "X-ESI-XSLT/1.0", "ESIGATE/4.0"};
34
35 @Override
36 public boolean event(EventDefinition id, Event event) {
37 RenderEvent renderEvent = (RenderEvent) event;
38 boolean doEsi = true;
39
40
41 if (renderEvent.getHttpResponse() != null
42 && renderEvent.getHttpResponse().containsHeader(Surrogate.H_X_ENABLED_CAPABILITIES)) {
43 String enabledCapabilities =
44 renderEvent.getHttpResponse().getFirstHeader(Surrogate.H_X_ENABLED_CAPABILITIES).getValue();
45
46 doEsi = false;
47 for (String capability : CAPABILITIES) {
48 if (containsIgnoreCase(enabledCapabilities, capability)) {
49 doEsi = true;
50 break;
51 }
52 }
53 }
54
55 if (doEsi) {
56 renderEvent.getRenderers().add(new EsiRenderer());
57 }
58
59
60 return true;
61 }
62
63 @Override
64 public void init(Driver driver, Properties properties) {
65 driver.getEventManager().register(EventManager.EVENT_RENDER_PRE, this);
66
67 driver.getEventManager().register(Surrogate.EVENT_SURROGATE_CAPABILITIES, new IEventListener() {
68 @Override
69 public boolean event(EventDefinition id, Event event) {
70 CapabilitiesEvent capEvent = (CapabilitiesEvent) event;
71 for (String capability : CAPABILITIES) {
72 capEvent.getCapabilities().add(capability);
73 }
74 return true;
75 }
76 });
77 }
78
79 }