use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class PortalUrlServiceImpl_assetUrlTest method createUrlWithLongContentPath.
@Test
public void createUrlWithLongContentPath() {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
final StringBuilder longContentPath = new StringBuilder();
longContentPath.append("/a".repeat(10000));
this.portalRequest.setContentPath(ContentPath.from(longContentPath.toString()));
final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).path("css/my.css");
final String url = this.service.assetUrl(params);
assertEquals("/site/default/draft/_/asset/myapplication:0000000000000001/css/my.css", url);
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class WebAppHandler method getScript.
private ControllerScript getScript(final ApplicationKey applicationKey) {
final ResourceKey script = ResourceKey.from(applicationKey, "/webapp/webapp.js");
final Trace trace = Tracer.current();
if (trace != null) {
trace.put("script", script.getPath());
}
return this.controllerScriptFactory.fromScript(script);
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class AbstractErrorHandlerTest method setup.
@BeforeEach
public void setup() throws Exception {
this.portalRequest = new PortalRequest();
this.portalRequest.setMethod(HttpMethod.GET);
this.portalResponse = PortalResponse.create().build();
final BundleContext bundleContext = Mockito.mock(BundleContext.class);
final Bundle bundle = Mockito.mock(Bundle.class);
Mockito.when(bundle.getBundleContext()).thenReturn(bundleContext);
final Application application = Mockito.mock(Application.class);
Mockito.when(application.getBundle()).thenReturn(bundle);
Mockito.when(application.getClassLoader()).thenReturn(getClass().getClassLoader());
Mockito.when(application.isStarted()).thenReturn(true);
Mockito.when(application.getConfig()).thenReturn(ConfigBuilder.create().build());
final ApplicationService applicationService = Mockito.mock(ApplicationService.class);
Mockito.when(applicationService.getInstalledApplication(ApplicationKey.from("myapplication"))).thenReturn(application);
this.resourceService = Mockito.mock(ResourceService.class);
Mockito.when(resourceService.getResource(Mockito.any())).thenAnswer(invocation -> {
final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
final URL resourceUrl = AbstractErrorHandlerTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
return new UrlResource(resourceKey, resourceUrl);
});
final ScriptAsyncService scriptAsyncService = Mockito.mock(ScriptAsyncService.class);
final ScriptRuntimeFactoryImpl runtimeFactory = new ScriptRuntimeFactoryImpl(applicationService, this.resourceService, scriptAsyncService);
final PortalScriptServiceImpl scriptService = new PortalScriptServiceImpl(runtimeFactory);
scriptService.initialize();
this.factory = new ErrorHandlerScriptFactoryImpl();
this.factory.setScriptService(scriptService);
this.postProcessor = new PostProcessorImpl();
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
ServletRequestHolder.setRequest(req);
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ExceptionRendererImplTest method render_default_error_page_when_error_in_custom_handler.
@Test
void render_default_error_page_when_error_in_custom_handler() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> {
throw new RuntimeException("Something went wrong in the handler script");
};
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
final String body = res.getBody().toString();
assertTrue(body.contains("400 Bad Request"));
assertTrue(body.contains("Custom message"));
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ExceptionRendererImplTest method render_custom_error_for_404_in_site_path.
@Test
void render_custom_error_for_404_in_site_path() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
this.request.setContentPath(ContentPath.from("/mysite/some/long/path"));
final Site site = newSite();
when(contentService.findNearestSiteByPath(eq(ContentPath.from("/mysite/some/long/path")))).thenReturn(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.NOT_FOUND).postProcess(false).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, cause));
assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
assertEquals("Custom message page", res.getBody().toString());
assertFalse(postProcessor.isExecuted());
}
Aggregations