-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
28 changed files
with
1,130 additions
and
467 deletions.
There are no files selected for viewing
105 changes: 96 additions & 9 deletions
105
modules/lib/lib-portal/src/main/java/com/enonic/xp/lib/portal/url/PageUrlHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
modules/portal/portal-api/src/main/java/com/enonic/xp/portal/url/PageUrlGeneratorParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
Oops, something went wrong.