Skip to content

Commit

Permalink
BaseUrl for page #10900 (#10903)
Browse files Browse the repository at this point in the history
BaseUrl for page #10900
  • Loading branch information
anatol-sialitski authored Feb 18, 2025
1 parent 138d48b commit f8a1540
Show file tree
Hide file tree
Showing 28 changed files with 1,130 additions and 467 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,113 @@
package com.enonic.xp.lib.portal.url;

import java.util.Set;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import com.enonic.xp.portal.url.PageUrlParams;
import com.enonic.xp.portal.url.PortalUrlService;
import com.enonic.xp.script.ScriptValue;
import com.enonic.xp.script.bean.BeanContext;
import com.enonic.xp.script.bean.ScriptBean;

public final class PageUrlHandler
extends AbstractUrlHandler
implements ScriptBean
{
private static final Set<String> VALID_URL_PROPERTY_KEYS = Set.of( "id", "path", "type", "params" );
private Supplier<PortalUrlService> urlServiceSupplier;

@Override
protected String buildUrl( final Multimap<String, String> map )
private String id;

private String path;

private String type;

private String projectName;

private String branch;

private boolean offline;

private Multimap<String, String> queryParams;

public void setId( final String id )
{
this.id = id;
}

public void setPath( final String path )
{
this.path = path;
}

public void setUrlType( final String type )
{
this.type = type;
}

public void setProjectName( final String projectName )
{
final PageUrlParams params = new PageUrlParams().portalRequest( this.request ).setAsMap( map );
return this.urlService.pageUrl( params );
this.projectName = projectName;
}

public void setBranch( final String branch )
{
this.branch = branch;
}

public void setOffline( final Boolean offline )
{
this.offline = Objects.requireNonNullElse( offline, false );
}

public void setQueryParams( final ScriptValue params )
{
if ( params == null )
{
return;
}

this.queryParams = HashMultimap.create();

for ( final Map.Entry<String, Object> param : params.getMap().entrySet() )
{
final Object value = param.getValue();
if ( value instanceof Iterable values )
{
for ( final Object v : values )
{
queryParams.put( param.getKey(), v.toString() );
}
}
else
{
queryParams.put( param.getKey(), value.toString() );
}
}
}

public String createUrl()
{
final PageUrlParams params = new PageUrlParams().id( this.id )
.path( this.path )
.type( this.type )
.projectName( this.projectName )
.branch( this.branch )
.offline( this.offline );

if ( this.queryParams != null )
{
this.queryParams.forEach( params::param );
}

return urlServiceSupplier.get().pageUrl( params );
}

@Override
protected boolean isValidParam( final String param )
public void initialize( final BeanContext context )
{
return VALID_URL_PROPERTY_KEYS.contains( param );
this.urlServiceSupplier = context.getService( PortalUrlService.class );
}
}
33 changes: 31 additions & 2 deletions modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,27 @@ export function attachmentUrl(params: AttachmentUrlParams): string {
export type PageUrlParams = IdXorPath & {
type?: 'server' | 'absolute';
params?: object;
project?: string;
branch?: string;
offline?: boolean;
};

interface PageUrlHandler {
createUrl(value: object): string;
setId(value?: string | null): void;

setPath(value?: string | null): void;

setUrlType(value?: string | null): void;

setQueryParams(value?: ScriptValue | null): void;

setProjectName(value?: string | null): void;

setBranch(value?: string | null): void;

setOffline(value: boolean): void;

createUrl(): string;
}

/**
Expand All @@ -296,13 +313,25 @@ interface PageUrlHandler {
* @param {string} [params.id] Id to the page. If id is set, then path is not used.
* @param {string} [params.path] Path to the page. Relative paths is resolved using the context page.
* @param {string} [params.type=server] URL type. Either `server` (server-relative URL) or `absolute`.
* @param {string} [params.projectName] Project of the context.
* @param {string} [params.branch] Branch of the project for context.
* @param {boolean} [params.offline=false] Set to true if the URL should be generated without context of the current request.
* @param {object} [params.params] Custom parameters to append to the url.
*
* @returns {string} The generated URL.
*/
export function pageUrl(params: PageUrlParams): string {
const bean: PageUrlHandler = __.newBean<PageUrlHandler>('com.enonic.xp.lib.portal.url.PageUrlHandler');
return bean.createUrl(__.toScriptValue(params));

bean.setId(__.nullOrValue(params.id));
bean.setPath(__.nullOrValue(params.path));
bean.setUrlType(params.type || 'server');
bean.setQueryParams(__.toScriptValue(params.params));
bean.setProjectName(__.nullOrValue(params.project));
bean.setBranch(__.nullOrValue(params.branch));
bean.setOffline(params.offline || false);

return bean.createUrl();
}

export interface ServiceUrlParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
Expand All @@ -17,7 +18,7 @@ public final class AttachmentUrlGeneratorParams
{
private final BaseUrlStrategy baseUrlStrategy;

private final Media media;
private final Supplier<Media> mediaSupplier;

private final ProjectName projectName;

Expand All @@ -34,7 +35,7 @@ public final class AttachmentUrlGeneratorParams
private AttachmentUrlGeneratorParams( final Builder builder )
{
this.baseUrlStrategy = Objects.requireNonNull( builder.baseUrlStrategy );
this.media = Objects.requireNonNull( builder.media );
this.mediaSupplier = Objects.requireNonNull( builder.mediaSupplier );
this.projectName = builder.projectName;
this.branch = builder.branch;
this.download = builder.download;
Expand All @@ -48,9 +49,9 @@ public BaseUrlStrategy getBaseUrlStrategy()
return baseUrlStrategy;
}

public Media getMedia()
public Supplier<Media> getMediaSupplier()
{
return media;
return mediaSupplier;
}

public ProjectName getProjectName()
Expand Down Expand Up @@ -78,9 +79,9 @@ public String getLabel()
return label;
}

public Multimap<String, String> getQueryParams()
public Map<String, Collection<String>> getQueryParams()
{
return queryParams;
return queryParams.asMap();
}

public static Builder create()
Expand All @@ -92,7 +93,7 @@ public static class Builder
{
private BaseUrlStrategy baseUrlStrategy;

private Media media;
private Supplier<Media> mediaSupplier;

private ProjectName projectName;

Expand All @@ -112,9 +113,9 @@ public Builder setBaseUrlStrategy( final BaseUrlStrategy baseUrlStrategy )
return this;
}

public Builder setMedia( final Media media )
public Builder setMedia( final Supplier<Media> mediaSupplier )
{
this.media = media;
this.mediaSupplier = mediaSupplier;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
Expand All @@ -17,7 +18,7 @@ public final class ImageUrlGeneratorParams
{
private final BaseUrlStrategy baseUrlStrategy;

private final Media media;
private final Supplier<Media> mediaSupplier;

private final ProjectName projectName;

Expand All @@ -38,7 +39,7 @@ public final class ImageUrlGeneratorParams
private ImageUrlGeneratorParams( final Builder builder )
{
this.baseUrlStrategy = Objects.requireNonNull( builder.baseUrlStrategy );
this.media = Objects.requireNonNull( builder.media );
this.mediaSupplier = Objects.requireNonNull( builder.mediaSupplier );
this.projectName = Objects.requireNonNull( builder.projectName );
this.branch = Objects.requireNonNull( builder.branch );
this.scale = Objects.requireNonNull( builder.scale );
Expand All @@ -54,9 +55,9 @@ public BaseUrlStrategy getBaseUrlStrategy()
return baseUrlStrategy;
}

public Media getMedia()
public Supplier<Media> getMedia()
{
return media;
return mediaSupplier;
}

public ProjectName getProjectName()
Expand Down Expand Up @@ -94,9 +95,9 @@ public String getScale()
return scale;
}

public Multimap<String, String> getQueryParams()
public Map<String, Collection<String>> getQueryParams()
{
return queryParams;
return queryParams.asMap();
}

public static Builder create()
Expand All @@ -108,7 +109,7 @@ public static class Builder
{
private BaseUrlStrategy baseUrlStrategy;

private Media media;
private Supplier<Media> mediaSupplier;

private ProjectName projectName;

Expand All @@ -132,9 +133,9 @@ public Builder setBaseUrlStrategy( final BaseUrlStrategy baseUrlStrategy )
return this;
}

public Builder setMedia( final Media media )
public Builder setMedia( final Supplier<Media> mediaSupplier )
{
this.media = media;
this.mediaSupplier = mediaSupplier;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.enonic.xp.portal.url;

import java.util.Collection;
import java.util.Map;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
public final class PageUrlGeneratorParams
{
private final BaseUrlStrategy baseUrlStrategy;

private final Multimap<String, String> queryParams;

private PageUrlGeneratorParams( final Builder builder )
{
this.baseUrlStrategy = builder.baseUrlStrategy;
this.queryParams = builder.queryParams;
}

public BaseUrlStrategy getBaseUrlStrategy()
{
return baseUrlStrategy;
}

public Map<String, Collection<String>> getQueryParams()
{
return queryParams.asMap();
}

public static Builder create()
{
return new Builder();
}

public static class Builder
{
private BaseUrlStrategy baseUrlStrategy;

private final Multimap<String, String> queryParams = HashMultimap.create();

public Builder setBaseUrlStrategy( final BaseUrlStrategy baseUrlStrategy )
{
this.baseUrlStrategy = baseUrlStrategy;
return this;
}

public Builder addQueryParams( final Map<String, Collection<String>> queryParams )
{
queryParams.forEach( this.queryParams::putAll );
return this;
}

public Builder addQueryParam( final String key, final String value )
{
this.queryParams.put( key, value );
return this;
}

public PageUrlGeneratorParams build()
{
return new PageUrlGeneratorParams( this );
}
}
}
Loading

0 comments on commit f8a1540

Please sign in to comment.