Search in sources :

Example 26 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey in project xp by enonic.

the class ServiceHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    // Retrieves the ServiceDescriptor
    final DescriptorKey descriptorKey = DescriptorKey.from(applicationKey, name);
    final ServiceDescriptor serviceDescriptor = serviceDescriptorService.getByKey(descriptorKey);
    if (serviceDescriptor == null) {
        throw WebException.notFound(String.format("Service [%s] not found", descriptorKey));
    }
    // Checks if the access to ServiceDescriptor is allowed
    final PrincipalKeys principals = ContextAccessor.current().getAuthInfo().getPrincipals();
    if (!serviceDescriptor.isAccessAllowed(principals)) {
        throw WebException.forbidden(String.format("You don't have permission to access [%s]", descriptorKey));
    }
    final ContentResolverResult resolvedContent = contentResolver.resolve(request);
    final Site site = resolvedContent.getNearestSite();
    // Checks if the application is set on the current site
    if (site != null) {
        final PropertyTree siteConfig = site.getSiteConfig(applicationKey);
        if (siteConfig == null) {
            throw WebException.forbidden(String.format("Service [%s] forbidden for this site", descriptorKey));
        }
    }
    // Checks if the application is set on the current application
    final ApplicationKey baseApplicationKey = getBaseApplicationKey();
    if (baseApplicationKey != null && !baseApplicationKey.equals(applicationKey)) {
        throw WebException.forbidden(String.format("Service [%s] forbidden for this application", descriptorKey));
    }
    // Prepares the request
    this.request.setApplicationKey(applicationKey);
    this.request.setContent(resolvedContent.getContent());
    this.request.setSite(site);
    // Executes the service
    final ControllerScript controllerScript = getScript();
    final PortalResponse portalResponse = controllerScript.execute(this.request);
    final WebSocketConfig webSocketConfig = portalResponse.getWebSocket();
    final WebSocketContext webSocketContext = this.request.getWebSocketContext();
    if ((webSocketContext != null) && (webSocketConfig != null)) {
        final WebSocketEndpoint webSocketEndpoint = newWebSocketEndpoint(webSocketConfig);
        webSocketContext.apply(webSocketEndpoint);
    }
    return portalResponse;
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) PortalResponse(com.enonic.xp.portal.PortalResponse) WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) WebSocketEndpoint(com.enonic.xp.web.websocket.WebSocketEndpoint) DescriptorKey(com.enonic.xp.page.DescriptorKey) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Example 27 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey in project xp by enonic.

the class MacroInstruction method evaluate.

@Override
public PortalResponse evaluate(final PortalRequest portalRequest, final String instruction) {
    if (!Instruction.isInstruction(instruction, "MACRO")) {
        return null;
    }
    // parse instruction
    final Instruction macroInstruction;
    try {
        macroInstruction = new InstructionParser().parse(instruction);
    } catch (RenderException e) {
        return null;
    }
    final String macroName = macroInstruction.attribute(MACRO_NAME);
    if (macroName == null) {
        return null;
    }
    // resolve macro processor
    final Site site = portalRequest.getSite();
    if (site == null) {
        throw new RenderException("Macro controller script could not be resolved, context site could not be found.");
    }
    final MacroDescriptor macroDescriptor = resolveMacroDescriptor(site.getSiteConfigs(), macroName);
    if (macroDescriptor == null) {
        final String editModeMacro = toMacroInstruction(macroInstruction);
        return PortalResponse.create().body(editModeMacro).build();
    }
    final MacroProcessor macroProcessor = resolveMacroProcessor(macroDescriptor);
    if (macroProcessor == null) {
        throw new RenderException("Macro controller not found: " + macroName);
    }
    // execute macro
    final MacroContext context = createContext(macroInstruction, macroDescriptor, portalRequest);
    final ApplicationKey previousAppKey = portalRequest.getApplicationKey();
    try {
        portalRequest.setApplicationKey(macroDescriptor.getKey().getApplicationKey());
        return macroProcessor.process(context);
    } finally {
        portalRequest.setApplicationKey(previousAppKey);
    }
}
Also used : Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) MacroContext(com.enonic.xp.portal.macro.MacroContext) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction)

Example 28 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey in project xp by enonic.

the class ApplicationServiceImpl method doInstallLocalApplication.

private Application doInstallLocalApplication(final ByteSource byteSource) {
    final ApplicationKey applicationKey = getApplicationKey(byteSource);
    final Application application = installOrUpdateApplication(byteSource, applicationKey, true, false);
    LOG.info("Local application [{}] installed successfully", applicationKey);
    doStartApplication(applicationKey, false, false);
    return application;
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) Application(com.enonic.xp.app.Application)

Example 29 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey in project xp by enonic.

the class ApplicationServiceImplTest method configuration_comes_last.

@Test
public void configuration_comes_last() throws Exception {
    final ApplicationKey key = ApplicationKey.from("myapp");
    final Bundle bundle = deployAppBundle("myapp");
    applicationRegistry.installApplication(bundle);
    final Application app = service.getInstalledApplication(key);
    applicationRegistry.configureApplication(bundle, ConfigBuilder.create().add("a", "b").build());
    assertEquals(ConfigBuilder.create().add("a", "b").build(), app.getConfig());
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) Bundle(org.osgi.framework.Bundle) Application(com.enonic.xp.app.Application) Test(org.junit.jupiter.api.Test)

Example 30 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey in project xp by enonic.

the class ApplicationServiceImplTest method stop_application.

@Test
public void stop_application() throws Exception {
    final Bundle bundle = deployAppBundle("app1");
    applicationRegistry.installApplication(bundle);
    bundle.start();
    assertEquals(Bundle.ACTIVE, bundle.getState());
    final ApplicationKey applicationKey = ApplicationKey.from("app1");
    this.service.stopApplication(applicationKey, true);
    assertEquals(Bundle.RESOLVED, bundle.getState());
    verify(this.eventPublisher, times(1)).publish(Mockito.argThat(new ApplicationEventMatcher(ApplicationClusterEvents.stop(applicationKey))));
    verify(this.eventPublisher, times(1)).publish(Mockito.argThat(new ApplicationEventMatcher(ApplicationClusterEvents.stopped(applicationKey))));
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) Bundle(org.osgi.framework.Bundle) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationKey (com.enonic.xp.app.ApplicationKey)78 Test (org.junit.jupiter.api.Test)40 Application (com.enonic.xp.app.Application)16 SiteDescriptor (com.enonic.xp.site.SiteDescriptor)12 Bundle (org.osgi.framework.Bundle)10 ResourceKey (com.enonic.xp.resource.ResourceKey)6 PropertyTree (com.enonic.xp.data.PropertyTree)5 ExtraData (com.enonic.xp.content.ExtraData)4 Site (com.enonic.xp.site.Site)4 Node (com.enonic.xp.node.Node)3 DescriptorKey (com.enonic.xp.page.DescriptorKey)3 PortalRequest (com.enonic.xp.portal.PortalRequest)3 PortalResponse (com.enonic.xp.portal.PortalResponse)3 XDataName (com.enonic.xp.schema.xdata.XDataName)3 ByteSource (com.google.common.io.ByteSource)3 ApplicationInvalidator (com.enonic.xp.app.ApplicationInvalidator)2 ExtraDatas (com.enonic.xp.content.ExtraDatas)2 PropertySet (com.enonic.xp.data.PropertySet)2 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)2 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)2