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;
}
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);
}
}
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();
}
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);
}
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());
}
Aggregations