Search in sources :

Example 21 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class LocalizeFunctionTest method not_in_request_context.

@Test
public void not_in_request_context() {
    final PortalRequest savedPortalRequest = PortalRequestAccessor.get();
    PortalRequestAccessor.set(null);
    try {
        Mockito.when(localeService.getBundle(Mockito.eq(ApplicationKey.from("com.enonic.myapp")), Mockito.eq(new Locale("en", "US")))).thenReturn(messageBundle);
        Mockito.when(messageBundle.localize(Mockito.eq("myPhrase"), ArgumentMatchers.<String>any())).thenReturn("localizedString");
        final Object result = execute("i18n.localize", "_key=myPhrase", "_locale=en-US  ", "_application=com.enonic.myapp", "a=5", "b=2");
        assertEquals("localizedString", result);
    } finally {
        PortalRequestAccessor.set(savedPortalRequest);
    }
}
Also used : Locale(java.util.Locale) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 22 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class LocalizeParamsTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    request = new PortalRequest();
    request.setSite(Site.create().name(ContentName.from("test")).parentPath(ContentPath.ROOT).language(DEFAULT_LOCALE).build());
    PortalRequestAccessor.set(request);
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MultipartHandler method initialize.

@Override
public void initialize(final BeanContext context) {
    final PortalRequest request = context.getBinding(PortalRequest.class).get();
    final MultipartService service = context.getService(MultipartService.class).get();
    this.form = service.parse(request.getRawRequest());
}
Also used : MultipartService(com.enonic.xp.web.multipart.MultipartService) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 24 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class FilterNextFunctionWrapper method apply.

@Override
public Object apply(final Object scriptRequestObject) {
    if (functionWasCalled) {
        throw scriptError("Filter 'next' function was called multiple times", null);
    }
    functionWasCalled = true;
    ScriptValue scriptRequestParam = scriptService.toScriptValue(this.script, scriptRequestObject);
    try {
        final PortalRequest portalRequest = new PortalRequestSerializer(request, scriptRequestParam).serialize();
        final WebResponse newResponse = webHandlerChain.handle(portalRequest, response);
        final PortalResponseMapper response = new PortalResponseMapper((PortalResponse) newResponse);
        return scriptService.toNativeObject(this.script, response);
    } catch (ResourceProblemException | WebException e) {
        throw e;
    } catch (Exception e) {
        throw scriptError("Error executing filter script: " + script, e);
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) WebResponse(com.enonic.xp.web.WebResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) WebException(com.enonic.xp.web.WebException) WebException(com.enonic.xp.web.WebException) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 25 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class ServiceHandler method doHandle.

@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    final String restPath = findRestPath(webRequest);
    final Matcher matcher = PATTERN.matcher(restPath);
    if (!matcher.find()) {
        throw WebException.notFound("Not a valid service url pattern");
    }
    final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
    portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(0));
    final ServiceHandlerWorker worker = new ServiceHandlerWorker(portalRequest);
    worker.applicationKey = ApplicationKey.from(matcher.group(1));
    worker.name = matcher.group(2);
    worker.contentResolver = new ContentResolver(this.contentService);
    worker.resourceService = this.resourceService;
    worker.serviceDescriptorService = this.serviceDescriptorService;
    worker.controllerScriptFactory = this.controllerScriptFactory;
    return worker.execute();
}
Also used : Matcher(java.util.regex.Matcher) PortalRequest(com.enonic.xp.portal.PortalRequest) ContentResolver(com.enonic.xp.portal.impl.ContentResolver)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7