use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class WebAppHandler method serveAsset.
private WebResponse serveAsset(final ApplicationKey applicationKey, final String path) {
final ResourceKey key = ResourceKey.from(applicationKey, ROOT_ASSET_PREFIX + path);
final Resource resource = this.resourceService.getResource(key);
if (!resource.exists()) {
return null;
}
final String type = MediaTypes.instance().fromFile(key.getName()).toString();
return PortalResponse.create().body(resource).contentType(MediaType.parse(type)).build();
}
use of com.enonic.xp.resource.ResourceKey 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();
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ErrorPageRichBuilder method findAllSourceLines.
private List<String> findAllSourceLines(final ResourceProblemException cause) {
final ResourceKey resourceKey = cause.getResource();
if (resourceKey == null || this.resourceService == null) {
return new ArrayList<>();
}
final Resource resource = resourceService.getResource(resourceKey);
return resource.readLines();
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ResponseSerializerTest method serializeBodyResource.
@Test
public void serializeBodyResource() throws Exception {
final ResourceKey resourceKey = ResourceKey.from("myapp:/site/test/view/body_file.txt");
final URL resourceUrl = ResponseSerializerTest.class.getResource("body_file.txt");
final WebRequest req = new WebRequest();
req.setMethod(HttpMethod.GET);
final WebResponse resp = WebResponse.create().status(HttpStatus.ACCEPTED).contentType(MediaType.PLAIN_TEXT_UTF_8).header("header-test", "header-value").cookie(new Cookie("cookie-name", "cookie-value")).body(new UrlResource(resourceKey, resourceUrl)).build();
final ResponseSerializer serializer = new ResponseSerializer(req, resp);
final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
final ServletOutputStream servletOutputStream = mock(ServletOutputStream.class);
when(httpResponse.getOutputStream()).thenReturn(servletOutputStream);
serializer.serialize(httpResponse);
verify(httpResponse).setHeader("header-test", "header-value");
verify(httpResponse).setStatus(202);
verify(httpResponse).setContentType("text/plain; charset=utf-8");
verify(httpResponse).setContentLength(11);
verify(servletOutputStream).write(any(byte[].class));
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ResponseProcessorExecutor method execute.
public PortalResponse execute(final ResponseProcessorDescriptor filter, final PortalRequest request, final PortalResponse response) {
final String filterName = filter.getName();
final String filterJsPath = "/site/processors/" + filterName + ".js";
final ResourceKey script = ResourceKey.from(filter.getApplication(), filterJsPath);
final ScriptExports filterExports;
try {
filterExports = this.scriptService.execute(script);
} catch (ResourceNotFoundException e) {
LOG.warn("Filter execution failed: {}", e.getMessage());
throw e;
}
final boolean exists = filterExports.hasMethod(RESPONSE_PROCESSOR_METHOD);
if (!exists) {
throw new RenderException("Missing exported function [{0}] in response filter [{1}]", RESPONSE_PROCESSOR_METHOD, filterJsPath);
}
final ApplicationKey previousApp = request.getApplicationKey();
// set application of the filter in the current context PortalRequest
request.setApplicationKey(filter.getApplication());
PortalRequestAccessor.set(request);
try {
return Tracer.trace("controllerScript", () -> executeFilter(filterExports, request, response));
} finally {
PortalRequestAccessor.remove();
request.setApplicationKey(previousApp);
}
}
Aggregations