use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class RendererDelegateImplTest method given_Renderable_having_not_matching_Renderer_when_getRenderer_then_Renderer_for_that_type_is_returned.
@Test
public void given_Renderable_having_not_matching_Renderer_when_getRenderer_then_Renderer_for_that_type_is_returned() {
RendererDelegateImpl factory = new RendererDelegateImpl(mock(ContentService.class));
final PortalResponse response = PortalResponse.create().build();
factory.addRenderer(createRenderer(Content.class, response));
// exercise
final PortalResponse renderResponse = factory.render(createContent(), null);
// verify
assertSame(response, renderResponse);
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class WidgetHandlerTest method testOptions.
@Test
public void testOptions() throws Exception {
mockDescriptor(true);
final PortalResponse portalResponse = PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
this.request.setMethod(HttpMethod.OPTIONS);
this.request.setMode(RenderMode.ADMIN);
final WebResponse response = this.handler.handle(this.request, WebResponse.create().build(), null);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatus());
assertEquals("GET,POST,HEAD,OPTIONS,PUT,DELETE,TRACE", response.getHeaders().get("Allow"));
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class WidgetHandlerTest method setup.
@BeforeEach
public final void setup() {
this.request = new PortalRequest();
final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
this.controllerScript = Mockito.mock(ControllerScript.class);
Mockito.when(controllerScriptFactory.fromDir(Mockito.any())).thenReturn(this.controllerScript);
final PortalResponse portalResponse = PortalResponse.create().build();
Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
this.contentService = Mockito.mock(ContentService.class);
this.widgetDescriptorService = Mockito.mock(WidgetDescriptorService.class);
this.handler = new WidgetHandler();
this.handler.setControllerScriptFactory(controllerScriptFactory);
this.handler.setWidgetDescriptorService(this.widgetDescriptorService);
HttpServletRequest rawRequest = Mockito.mock(HttpServletRequest.class);
Mockito.when(rawRequest.isUserInRole(Mockito.anyString())).thenReturn(Boolean.TRUE);
this.request.setRawRequest(rawRequest);
this.request.setMethod(HttpMethod.GET);
this.request.setContentPath(ContentPath.from("/site/somepath/content"));
this.request.setEndpointPath("/_/widgets/demo/mywidget");
this.request.setRawPath("/admin/tool/_/widgets/demo/mywidget");
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class AdminToolHandler method doHandle.
@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
WebHandlerHelper.checkAdminAccess(webRequest);
PortalRequest portalRequest = (PortalRequest) webRequest;
portalRequest.setContextPath(portalRequest.getBaseUri());
final AdminToolHandlerWorker worker = new AdminToolHandlerWorker(portalRequest);
worker.controllerScriptFactory = this.controllerScriptFactory;
worker.adminToolDescriptorService = adminToolDescriptorService;
final DescriptorKey descriptorKey = AdminToolPortalHandler.getDescriptorKey(webRequest);
worker.descriptorKey = descriptorKey == null ? AdminToolPortalHandler.DEFAULT_DESCRIPTOR_KEY : descriptorKey;
final Trace trace = Tracer.newTrace("portalRequest");
if (trace == null) {
return worker.execute();
}
trace.put("path", webRequest.getPath());
trace.put("method", webRequest.getMethod().toString());
trace.put("host", webRequest.getHost());
trace.put("httpRequest", webRequest);
trace.put("httpResponse", webResponse);
trace.put("context", ContextAccessor.current());
return Tracer.traceEx(trace, () -> {
final PortalResponse response = worker.execute();
addTraceInfo(trace, response);
return response;
});
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class DescriptorBasedComponentRenderer method doRender.
private PortalResponse doRender(final R component, final PortalRequest portalRequest) {
final ComponentDescriptor descriptor = resolveDescriptor(component);
if (descriptor == null) {
return renderEmptyComponent(component, portalRequest);
}
// create controller
final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(descriptor.getComponentPath());
// render
final Component previousComponent = portalRequest.getComponent();
final ApplicationKey previousApplication = portalRequest.getApplicationKey();
try {
portalRequest.setComponent(component);
portalRequest.setApplicationKey(descriptor.getKey().getApplicationKey());
final PortalResponse portalResponse = controllerScript.execute(portalRequest);
final RenderMode renderMode = portalRequest.getMode();
final MediaType contentType = portalResponse.getContentType();
if (renderMode == RenderMode.EDIT && contentType != null && contentType.withoutParameters().type().equals("text")) {
final Object bodyObj = portalResponse.getBody();
if (bodyObj == null || (bodyObj instanceof String && nullToEmpty((String) bodyObj).isBlank())) {
if (portalResponse.getStatus().equals(HttpStatus.METHOD_NOT_ALLOWED)) {
final String errorMessage = "No method provided to handle request";
return renderErrorComponentPlaceHolder(component, errorMessage);
}
return renderEmptyComponent(component, portalRequest);
}
}
final PortalResponse injectedResponse = LIVE_EDIT_ATTRIBUTE_INJECTION.injectLiveEditAttribute(portalResponse, component.getType());
return injectedResponse;
} finally {
portalRequest.setComponent(previousComponent);
portalRequest.setApplicationKey(previousApplication);
}
}
Aggregations