Search in sources :

Example 26 with PortalRequest

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

the class IdProviderControllerServiceImpl method execute.

@Override
public PortalResponse execute(final IdProviderControllerExecutionParams params) throws IOException {
    final IdProviderKey idProviderKey = retrieveIdProviderKey(params);
    final IdProvider idProvider = retrieveIdProvider(idProviderKey);
    final IdProviderDescriptor idProviderDescriptor = retrieveIdProviderDescriptor(idProvider);
    if (idProviderDescriptor != null) {
        final IdProviderControllerScript idProviderControllerScript = idProviderControllerScriptFactory.fromScript(idProviderDescriptor.getResourceKey());
        final String functionName = params.getFunctionName();
        if (idProviderControllerScript.hasMethod(functionName)) {
            PortalRequest portalRequest = params.getPortalRequest();
            if (portalRequest == null) {
                portalRequest = new PortalRequestAdapter().adapt(params.getServletRequest());
            }
            portalRequest.setApplicationKey(idProviderDescriptor.getKey());
            portalRequest.setIdProvider(idProvider);
            final PortalResponse portalResponse = idProviderControllerScript.execute(functionName, portalRequest);
            if (portalResponse != null) {
                final HttpServletResponse response = params.getResponse();
                if (response != null) {
                    responseSerializationService.serialize(portalRequest, portalResponse, response);
                }
            }
            return portalResponse;
        }
    }
    return null;
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) PortalRequestAdapter(com.enonic.xp.portal.impl.PortalRequestAdapter) IdProvider(com.enonic.xp.security.IdProvider) IdProviderDescriptor(com.enonic.xp.idprovider.IdProviderDescriptor) IdProviderKey(com.enonic.xp.security.IdProviderKey) HttpServletResponse(javax.servlet.http.HttpServletResponse) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 27 with PortalRequest

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

the class HtmlLinkProcessor method getImageStyleMap.

private ImmutableMap<String, ImageStyle> getImageStyleMap(final PortalRequest portalRequest) {
    final ImmutableMap.Builder<String, ImageStyle> imageStyleMap = ImmutableMap.builder();
    final StyleDescriptors styleDescriptors = getStyleDescriptors(portalRequest);
    styleDescriptors.stream().flatMap(styleDescriptor -> styleDescriptor.getElements().stream()).filter(elementStyle -> ImageStyle.STYLE_ELEMENT_NAME.equals(elementStyle.getElement())).forEach(elementStyle -> imageStyleMap.put(elementStyle.getName(), (ImageStyle) elementStyle));
    return imageStyleMap.build();
}
Also used : PortalUrlService(com.enonic.xp.portal.url.PortalUrlService) ImmutableSet(com.google.common.collect.ImmutableSet) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) ApplicationKey(com.enonic.xp.app.ApplicationKey) Objects(java.util.Objects) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) List(java.util.List) Site(com.enonic.xp.site.Site) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Splitter(com.google.common.base.Splitter) ApplicationKeys(com.enonic.xp.app.ApplicationKeys) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 28 with PortalRequest

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

the class HtmlLinkProcessor method process.

public String process(final String text, final String urlType, final PortalRequest portalRequest, final List<Integer> imageWidths, final String imageSizes) {
    String processedHtml = text;
    final ImmutableMap<String, ImageStyle> imageStyleMap = getImageStyleMap(portalRequest);
    final Matcher contentMatcher = CONTENT_PATTERN.matcher(text);
    while (contentMatcher.find()) {
        if (contentMatcher.groupCount() >= NB_GROUPS) {
            final String tagName = contentMatcher.group(TAG_NAME_INDEX);
            final String attr = contentMatcher.group(ATTR_INDEX);
            final String attrValue = contentMatcher.group(ATTR_VALUE_INDEX);
            final String link = contentMatcher.group(LINK_INDEX);
            final String type = contentMatcher.group(TYPE_INDEX);
            final String mode = contentMatcher.group(MODE_INDEX);
            final String id = contentMatcher.group(ID_INDEX);
            final String urlParamsString = contentMatcher.groupCount() == PARAMS_INDEX ? contentMatcher.group(PARAMS_INDEX) : null;
            switch(type) {
                case CONTENT_TYPE:
                    PageUrlParams pageUrlParams = new PageUrlParams().type(urlType).id(id).portalRequest(portalRequest);
                    final String pageUrl = portalUrlService.pageUrl(pageUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + pageUrl + "\"");
                    break;
                case IMAGE_TYPE:
                    final Map<String, String> urlParams = extractUrlParams(urlParamsString);
                    ImageStyle imageStyle = getImageStyle(imageStyleMap, urlParams);
                    ImageUrlParams imageUrlParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, null)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                    final String imageUrl = portalUrlService.imageUrl(imageUrlParams);
                    final StringBuilder replacement = new StringBuilder("\"" + imageUrl + "\"");
                    if ("img".equals(tagName) && "src".equals(attr)) {
                        final String srcsetValues = Objects.requireNonNullElse(imageWidths, List.<Integer>of()).stream().map(imageWidth -> {
                            final ImageUrlParams imageParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, imageWidth)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                            return portalUrlService.imageUrl(imageParams) + " " + imageWidth + "w";
                        }).collect(Collectors.joining(","));
                        if (!srcsetValues.isEmpty()) {
                            replacement.append(" srcset=\"").append(srcsetValues).append("\"");
                        }
                        if (imageSizes != null && !imageSizes.isBlank()) {
                            replacement.append(" sizes=\"").append(imageSizes).append("\"");
                        }
                    }
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), replacement.toString());
                    break;
                default:
                    AttachmentUrlParams attachmentUrlParams = new AttachmentUrlParams().type(urlType).id(id).download(DOWNLOAD_MODE.equals(mode)).portalRequest(portalRequest);
                    final String attachmentUrl = portalUrlService.attachmentUrl(attachmentUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + attachmentUrl + "\"");
                    break;
            }
        }
    }
    return processedHtml;
}
Also used : PortalUrlService(com.enonic.xp.portal.url.PortalUrlService) ImmutableSet(com.google.common.collect.ImmutableSet) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) ApplicationKey(com.enonic.xp.app.ApplicationKey) Objects(java.util.Objects) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) List(java.util.List) Site(com.enonic.xp.site.Site) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Splitter(com.google.common.base.Splitter) ApplicationKeys(com.enonic.xp.app.ApplicationKeys) Matcher(java.util.regex.Matcher) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) ImageStyle(com.enonic.xp.style.ImageStyle) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams)

Example 29 with PortalRequest

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

the class ContentResolverTest method resolve_no_site_in_live_mode.

@Test
void resolve_no_site_in_live_mode() {
    final Content content = newContent();
    final PortalRequest request = new PortalRequest();
    request.setContentPath(ContentPath.from("/mysite/landing-page"));
    when(this.contentService.getByPath(ContentPath.from("/mysite/landing-page"))).thenReturn(content);
    when(this.contentService.findNearestSiteByPath(ContentPath.from("/mysite/landing-page"))).thenReturn(null);
    final ContentResolverResult result = new ContentResolver(contentService).resolve(request);
    assertSame(content, result.getContent());
    assertNull(result.getNearestSite());
    assertNull(result.getSiteRelativePath());
}
Also used : Content(com.enonic.xp.content.Content) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 30 with PortalRequest

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

the class ContentResolverTest method resolve_no_site_edit_mode.

@Test
void resolve_no_site_edit_mode() {
    final Content content = newContent();
    final PortalRequest request = new PortalRequest();
    request.setMode(RenderMode.EDIT);
    request.setContentPath(ContentPath.from("/c8da0c10-0002-4b68-b407-87412f3e45c8"));
    when(this.contentService.getById(ContentId.from("c8da0c10-0002-4b68-b407-87412f3e45c8"))).thenReturn(content);
    when(this.contentService.getNearestSite(ContentId.from("c8da0c10-0002-4b68-b407-87412f3e45c8"))).thenReturn(null);
    final ContentResolverResult result = new ContentResolver(contentService).resolve(request);
    assertSame(content, result.getContent());
    assertNull(result.getNearestSite());
    assertNull(result.getSiteRelativePath());
}
Also used : Content(com.enonic.xp.content.Content) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

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