Search in sources :

Example 36 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 37 with ApplicationKey

use of com.enonic.xp.app.ApplicationKey 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)

Example 38 with ApplicationKey

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

the class RenderBaseHandlerTest method createDescriptor.

private PageDescriptor createDescriptor() {
    final ApplicationKey applicationKey = ApplicationKey.from("mainapplication");
    final String name = "mypage";
    final DescriptorKey key = DescriptorKey.from(applicationKey, name);
    final String xml = "<?xml version=\"1.0\"?>\n" + "<page>\n" + "  <display-name>Landing page</display-name>\n" + "  <config/>\n" + "</page>";
    final PageDescriptor.Builder builder = PageDescriptor.create();
    parseXml(applicationKey, builder, xml);
    return builder.key(key).displayName("Landing page").build();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) PageDescriptor(com.enonic.xp.page.PageDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 39 with ApplicationKey

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

the class AbstractPortalUrlServiceImplTest method setup.

@BeforeEach
public void setup() {
    final ApplicationKey applicationKey = ApplicationKey.from("myapplication");
    final Application application = Mockito.mock(Application.class);
    when(application.getKey()).thenReturn(applicationKey);
    req = mock(HttpServletRequest.class);
    this.portalRequest = new PortalRequest();
    this.portalRequest.setBranch(Branch.from("draft"));
    this.portalRequest.setApplicationKey(applicationKey);
    this.portalRequest.setBaseUri("/site");
    this.portalRequest.setContentPath(ContentPath.from("context/path"));
    this.portalRequest.setRawRequest(req);
    this.service = new PortalUrlServiceImpl();
    this.service.setMacroService(new MacroServiceImpl());
    this.contentService = Mockito.mock(ContentService.class);
    this.service.setContentService(this.contentService);
    this.styleDescriptorService = Mockito.mock(StyleDescriptorService.class);
    when(this.styleDescriptorService.getByApplications(Mockito.any())).thenReturn(StyleDescriptors.empty());
    this.service.setStyleDescriptorService(this.styleDescriptorService);
    this.applicationService = Mockito.mock(ApplicationService.class);
    when(this.applicationService.getInstalledApplication(applicationKey)).thenReturn(application);
    this.resourceService = Mockito.mock(ResourceService.class);
    this.service.setResourceService(this.resourceService);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ApplicationKey(com.enonic.xp.app.ApplicationKey) MacroServiceImpl(com.enonic.xp.impl.macro.MacroServiceImpl) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) ResourceService(com.enonic.xp.resource.ResourceService) ContentService(com.enonic.xp.content.ContentService) Application(com.enonic.xp.app.Application) PortalRequest(com.enonic.xp.portal.PortalRequest) ApplicationService(com.enonic.xp.app.ApplicationService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 40 with ApplicationKey

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

the class AssetUrlBuilder method buildUrl.

@Override
protected void buildUrl(final StringBuilder url, final Multimap<String, String> params) {
    super.buildUrl(url, params);
    final ApplicationKey applicationKey = new ApplicationResolver().portalRequest(this.portalRequest).application(this.params.getApplication()).resolve();
    final Resource resource = this.resourceService.getResource(ResourceKey.from(applicationKey, "META-INF/MANIFEST.MF"));
    if (!resource.exists()) {
        throw new IllegalArgumentException("Could not find application [" + applicationKey + "]");
    }
    final String fingerprint = RunMode.get() == RunMode.DEV ? String.valueOf(stableTime()) : HexCoder.toHex(resource.getTimestamp());
    appendPart(url, applicationKey + ":" + fingerprint);
    appendPart(url, this.params.getPath());
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) Resource(com.enonic.xp.resource.Resource)

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