use of com.enonic.xp.service.ServiceDescriptor 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.service.ServiceDescriptor in project xp by enonic.
the class ServiceDescriptorServiceImplTest method testGetByKey.
@Test
public void testGetByKey() throws Exception {
final DescriptorKey key = DescriptorKey.from("myapp1:myservice1");
final ServiceDescriptor descriptor = this.service.getByKey(key);
assertNotNull(descriptor);
assertEquals("[role:system.admin]", descriptor.getAllowedPrincipals().toString());
}
use of com.enonic.xp.service.ServiceDescriptor in project xp by enonic.
the class ServiceDescriptorServiceImplTest method testGetByKey_default.
@Test
public void testGetByKey_default() throws Exception {
final DescriptorKey key = DescriptorKey.from("myapp1:unknown");
final ServiceDescriptor descriptor = this.service.getByKey(key);
assertNotNull(descriptor);
}
use of com.enonic.xp.service.ServiceDescriptor in project xp by enonic.
the class ServiceDescriptorServiceImpl method getByKey.
@Override
public ServiceDescriptor getByKey(final DescriptorKey descriptorKey) {
final ResourceProcessor<DescriptorKey, ServiceDescriptor> processor = newRootProcessor(descriptorKey);
final ServiceDescriptor descriptor = this.resourceService.processResource(processor);
if (descriptor != null) {
return descriptor;
}
return createDefaultDescriptor(descriptorKey);
}
use of com.enonic.xp.service.ServiceDescriptor in project xp by enonic.
the class XmlServiceDescriptorParserTest method assertResult.
private void assertResult() throws Exception {
final ServiceDescriptor result = this.builder.build();
assertEquals("myapplication:myservice", result.getKey().toString());
final PrincipalKeys allowedPrincipals = result.getAllowedPrincipals();
assertNotNull(allowedPrincipals);
assertEquals(1, allowedPrincipals.getSize());
assertTrue(allowedPrincipals.first().equals(PrincipalKey.from("role:system.admin")));
}
Aggregations