Search in sources :

Example 1 with ResponseProcessorDescriptor

use of com.enonic.xp.site.processor.ResponseProcessorDescriptor in project xp by enonic.

the class PostProcessingRenderer method executeResponseProcessors.

private PortalResponse executeResponseProcessors(final PortalRequest portalRequest, final PortalResponse portalResponse) {
    final ResponseProcessorDescriptors filters = this.processorChainResolver.resolve(portalRequest);
    if (!portalResponse.applyFilters() || filters.isEmpty()) {
        return portalResponse;
    }
    PortalResponse filterResponse = portalResponse;
    for (ResponseProcessorDescriptor filter : filters) {
        final PortalResponse filterPortalResponse = filterResponse;
        final Trace trace = Tracer.newTrace("renderFilter");
        if (trace == null) {
            filterResponse = processorExecutor.execute(filter, portalRequest, filterPortalResponse);
        } else {
            trace.put("app", filter.getApplication().toString());
            trace.put("name", filter.getName());
            trace.put("type", "filter");
            filterResponse = Tracer.trace(trace, () -> processorExecutor.execute(filter, portalRequest, filterPortalResponse));
        }
        if (!filterResponse.applyFilters()) {
            break;
        }
    }
    return filterResponse;
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ResponseProcessorDescriptors(com.enonic.xp.site.processor.ResponseProcessorDescriptors)

Example 2 with ResponseProcessorDescriptor

use of com.enonic.xp.site.processor.ResponseProcessorDescriptor in project xp by enonic.

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorWithByteSourceBody.

@Test
public void testExecuteResponseProcessorWithByteSourceBody() throws Exception {
    final ByteSource data = ByteSource.wrap("DATA".getBytes(StandardCharsets.UTF_8));
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptExports.hasMethod("responseProcessor")).thenReturn(true);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ScriptValue result = Mockito.mock(ScriptValue.class);
    final ScriptValue body = Mockito.mock(ScriptValue.class);
    when(body.getValue()).thenReturn(data.toString());
    when(result.isObject()).thenReturn(true);
    when(result.getMember("body")).thenReturn(body);
    when(scriptExports.executeMethod(anyString(), any(PortalRequestMapper.class), any(PortalResponseMapper.class))).thenReturn(result);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().body(data).build();
    final PortalResponse filteredResponse = filterExecutor.execute(filter, request, response);
    assertNotNull(filteredResponse);
    assertTrue(filteredResponse.getBody() instanceof ByteSource);
    assertArrayEquals(data.read(), ((ByteSource) filteredResponse.getBody()).read());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponse(com.enonic.xp.portal.PortalResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ByteSource(com.google.common.io.ByteSource) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ResponseProcessorDescriptor

use of com.enonic.xp.site.processor.ResponseProcessorDescriptor in project xp by enonic.

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorNotImplementingMethod.

@Test
public void testExecuteResponseProcessorNotImplementingMethod() throws Exception {
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().build();
    try {
        filterExecutor.execute(filter, request, response);
        fail("Expected exception");
    } catch (RenderException e) {
        assertEquals("Missing exported function [responseProcessor] in response filter [/site/processors/filter1.js]", e.getMessage());
    }
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ResponseProcessorDescriptor

use of com.enonic.xp.site.processor.ResponseProcessorDescriptor in project xp by enonic.

the class ProcessorChainResolver method resolve.

public ResponseProcessorDescriptors resolve(PortalRequest request) {
    SiteDescriptor siteDescriptor;
    List<ResponseProcessorDescriptor> filterChain = new ArrayList<>();
    for (SiteConfig siteConfig : request.getSite().getSiteConfigs()) {
        siteDescriptor = siteService.getDescriptor(siteConfig.getApplicationKey());
        if (siteDescriptor != null) {
            for (ResponseProcessorDescriptor filterDescriptor : siteDescriptor.getResponseProcessors()) {
                filterChain.add(this.findIndexToInsert(filterDescriptor, filterChain), filterDescriptor);
            }
        }
    }
    return ResponseProcessorDescriptors.from(filterChain);
}
Also used : SiteDescriptor(com.enonic.xp.site.SiteDescriptor) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ArrayList(java.util.ArrayList) SiteConfig(com.enonic.xp.site.SiteConfig)

Example 5 with ResponseProcessorDescriptor

use of com.enonic.xp.site.processor.ResponseProcessorDescriptor in project xp by enonic.

the class ProcessorChainResolverTest method testFilterChainResolver.

@Test
public void testFilterChainResolver() {
    ApplicationKey ak1 = ApplicationKey.from("ak1");
    SiteConfig sc1 = SiteConfig.create().application(ak1).config(new PropertyTree()).build();
    ApplicationKey ak2 = ApplicationKey.from("ak2");
    SiteConfig sc2 = SiteConfig.create().application(ak2).config(new PropertyTree()).build();
    ApplicationKey ak3 = ApplicationKey.from("ak3");
    SiteConfig sc3 = SiteConfig.create().application(ak3).config(new PropertyTree()).build();
    portalRequest.setSite(Site.create().siteConfigs(SiteConfigs.from(sc1, sc2, sc3)).name("Site").path("/site").parentPath(ContentPath.ROOT).build());
    ResponseProcessorDescriptor fd12 = ResponseProcessorDescriptor.create().application(ak1).name("ak1-2").order(2).build();
    ResponseProcessorDescriptor fd13 = ResponseProcessorDescriptor.create().application(ak1).name("ak1-3").order(3).build();
    ResponseProcessorDescriptor fd15 = ResponseProcessorDescriptor.create().application(ak1).name("ak1-5").order(5).build();
    SiteDescriptor sd1 = SiteDescriptor.create().responseProcessors(ResponseProcessorDescriptors.from(fd13, fd15, fd12)).build();
    Mockito.when(siteService.getDescriptor(Mockito.eq(ak1))).thenReturn(sd1);
    ResponseProcessorDescriptor fd21 = ResponseProcessorDescriptor.create().application(ak2).name("ak2-1").order(1).build();
    ResponseProcessorDescriptor fd22 = ResponseProcessorDescriptor.create().application(ak2).name("ak1-2").order(2).build();
    ResponseProcessorDescriptor fd23 = ResponseProcessorDescriptor.create().application(ak2).name("ak2-3").order(3).build();
    SiteDescriptor sd2 = SiteDescriptor.create().responseProcessors(ResponseProcessorDescriptors.from(fd22, fd23, fd21)).build();
    Mockito.when(siteService.getDescriptor(Mockito.eq(ak2))).thenReturn(sd2);
    ResponseProcessorDescriptor fd32 = ResponseProcessorDescriptor.create().application(ak3).name("ak3-2").order(2).build();
    SiteDescriptor sd3 = SiteDescriptor.create().responseProcessors(ResponseProcessorDescriptors.from(fd32)).build();
    Mockito.when(siteService.getDescriptor(Mockito.eq(ak3))).thenReturn(sd3);
    ResponseProcessorDescriptors filters = resolver.resolve(portalRequest);
    assertEquals(7, filters.getSize());
    ResponseProcessorDescriptor fd = filters.get(0);
    assertEquals(1, fd.getOrder());
    assertEquals(ak2, fd.getApplication());
    fd = filters.get(1);
    assertEquals(2, fd.getOrder());
    assertEquals(ak1, fd.getApplication());
    fd = filters.get(2);
    assertEquals(2, fd.getOrder());
    assertEquals(ak2, fd.getApplication());
    fd = filters.get(3);
    assertEquals(2, fd.getOrder());
    assertEquals(ak3, fd.getApplication());
    fd = filters.get(4);
    assertEquals(3, fd.getOrder());
    assertEquals(ak1, fd.getApplication());
    fd = filters.get(5);
    assertEquals(3, fd.getOrder());
    assertEquals(ak2, fd.getApplication());
    fd = filters.get(6);
    assertEquals(5, fd.getOrder());
    assertEquals(ak1, fd.getApplication());
}
Also used : SiteDescriptor(com.enonic.xp.site.SiteDescriptor) ApplicationKey(com.enonic.xp.app.ApplicationKey) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) SiteConfig(com.enonic.xp.site.SiteConfig) ResponseProcessorDescriptors(com.enonic.xp.site.processor.ResponseProcessorDescriptors) Test(org.junit.jupiter.api.Test)

Aggregations

ResponseProcessorDescriptor (com.enonic.xp.site.processor.ResponseProcessorDescriptor)6 PortalResponse (com.enonic.xp.portal.PortalResponse)4 Test (org.junit.jupiter.api.Test)4 PortalRequest (com.enonic.xp.portal.PortalRequest)3 PortalScriptService (com.enonic.xp.portal.script.PortalScriptService)3 ResourceKey (com.enonic.xp.resource.ResourceKey)3 ScriptExports (com.enonic.xp.script.ScriptExports)3 SiteConfig (com.enonic.xp.site.SiteConfig)2 SiteDescriptor (com.enonic.xp.site.SiteDescriptor)2 ResponseProcessorDescriptors (com.enonic.xp.site.processor.ResponseProcessorDescriptors)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)1 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)1 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)1 ScriptValue (com.enonic.xp.script.ScriptValue)1 Trace (com.enonic.xp.trace.Trace)1 ByteSource (com.google.common.io.ByteSource)1 ArrayList (java.util.ArrayList)1