use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class PostProcessEvaluatorTest method testEvaluateInstructionSetContribution.
@Test
public void testEvaluateInstructionSetContribution() throws Exception {
final PostProcessInstruction setCookieInstruction = (portalRequest, instruction) -> {
if (instruction.startsWith("INSTRUCTION")) {
return PortalResponse.create().contribution(HtmlTag.BODY_END, "<script src='my-script.js'/>").build();
}
return null;
};
final PostProcessEvaluator evaluator = new PostProcessEvaluator();
evaluator.input = readResource("postProcessEvalSource6.html");
evaluator.injections = Collections.emptyList();
evaluator.instructions = List.of(setCookieInstruction);
evaluator.portalResponse = PortalResponse.create().build();
final PortalResponse result = evaluator.evaluate();
assertEquals(1, result.getContributions(HtmlTag.BODY_END).size());
assertEquals("<script src='my-script.js'/>", result.getContributions(HtmlTag.BODY_END).get(0));
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class PostProcessEvaluatorTest method testEvaluateInstructions.
@Test
public void testEvaluateInstructions() throws Exception {
final PostProcessInstruction uppercaseInstruction = (portalRequest, instruction) -> {
if (instruction.startsWith("UPPERCASE ")) {
return PortalResponse.create().body(instruction.substring("UPPERCASE ".length()).toUpperCase()).build();
}
return null;
};
final PostProcessInstruction expandInstruction = (portalRequest, instruction) -> {
if (instruction.startsWith("EXPAND ")) {
return PortalResponse.create().body("<!--#UPPERCASE " + instruction.substring("EXPAND ".length()) + "-->").build();
}
return null;
};
final PostProcessEvaluator evaluator = new PostProcessEvaluator();
evaluator.input = readResource("postProcessEvalSource3.html");
evaluator.injections = Collections.emptyList();
evaluator.instructions = List.of(uppercaseInstruction, expandInstruction);
evaluator.portalResponse = PortalResponse.create().build();
final PortalResponse result = evaluator.evaluate();
assertEqualsTrimmed(readResource("postProcessEvalResult3.html"), result.getAsString());
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class PostProcessEvaluatorTest method testEvaluateInstructionsAndContributions.
@Test
public void testEvaluateInstructionsAndContributions() throws Exception {
final PostProcessInjection contributionsInjection = (portalRequest, portalResponse, tag) -> {
switch(tag) {
case HEAD_BEGIN:
return Arrays.asList("<!-- HEAD BEGIN -->");
case HEAD_END:
return Arrays.asList("<!-- HEAD END -->");
case BODY_BEGIN:
return Arrays.asList("<!-- BODY BEGIN -->");
case BODY_END:
return Arrays.asList("<!-- BODY END -->");
default:
return null;
}
};
final PostProcessEvaluator evaluator = new PostProcessEvaluator();
evaluator.input = readResource("postProcessEvalSource2.html");
evaluator.injections = List.of(contributionsInjection);
evaluator.instructions = Collections.emptyList();
evaluator.portalResponse = PortalResponse.create().build();
evaluator.evaluateInstructions();
final PortalResponse result = evaluator.evaluateContributions();
assertEqualsTrimmed(readResource("postProcessEvalResult2.html"), result.getAsString());
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class WebAppHandler method executeController.
private PortalResponse executeController(final PortalRequest req) throws Exception {
final ControllerScript script = getScript(req.getApplicationKey());
final PortalResponse res = script.execute(req);
final WebSocketConfig webSocketConfig = res.getWebSocket();
final WebSocketContext webSocketContext = req.getWebSocketContext();
if ((webSocketContext != null) && (webSocketConfig != null)) {
final WebSocketEndpoint webSocketEndpoint = newWebSocketEndpoint(webSocketConfig, script, req.getApplicationKey());
webSocketContext.apply(webSocketEndpoint);
}
return res;
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class AssetHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
if (request.getMethod() == HttpMethod.OPTIONS) {
// it will be handled by default OPTIONS handler in BaseWebHandler
return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
}
final ResourceKey assetsKey = ResourceKey.assets(applicationKey);
final String assetPath = assetsKey.getPath() + path;
final ResourceKey resourceKey = ResourceKey.from(applicationKey, assetPath);
final Resource resource = resolveResource(resourceKey);
final String type = MediaTypes.instance().fromFile(resource.getKey().getName()).toString();
final PortalResponse.Builder portalResponse = PortalResponse.create().body(resource).contentType(MediaType.parse(type));
if (!nullToEmpty(this.fingerprint).isBlank() && !nullToEmpty(cacheControlHeaderConfig).isBlank() && RunMode.get() != RunMode.DEV && resourceKey.getPath().equals(assetPath) && fingerprintMatches(fingerprint)) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
return portalResponse.build();
}
Aggregations