Search in sources :

Example 16 with ResourceKey

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();
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 17 with ResourceKey

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();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 18 with ResourceKey

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();
}
Also used : ArrayList(java.util.ArrayList) Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 19 with ResourceKey

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));
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) UrlResource(com.enonic.xp.resource.UrlResource) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 20 with ResourceKey

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);
    }
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceNotFoundException(com.enonic.xp.resource.ResourceNotFoundException) ResourceKey(com.enonic.xp.resource.ResourceKey)

Aggregations

ResourceKey (com.enonic.xp.resource.ResourceKey)76 Test (org.junit.jupiter.api.Test)49 Resource (com.enonic.xp.resource.Resource)21 ApplicationKey (com.enonic.xp.app.ApplicationKey)12 ResourceService (com.enonic.xp.resource.ResourceService)12 ScriptExports (com.enonic.xp.script.ScriptExports)11 Application (com.enonic.xp.app.Application)9 UrlResource (com.enonic.xp.resource.UrlResource)9 URL (java.net.URL)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 PortalRequest (com.enonic.xp.portal.PortalRequest)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 ApplicationService (com.enonic.xp.app.ApplicationService)7 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)7 ScriptAsyncService (com.enonic.xp.script.impl.async.ScriptAsyncService)7 ScriptRuntimeFactoryImpl (com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl)7 PortalResponse (com.enonic.xp.portal.PortalResponse)6 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 PropertyTree (com.enonic.xp.data.PropertyTree)5