com.enonic.xp.lib.portal.url.AbstractUrlHandler Maven / Gradle / Ivy
package com.enonic.xp.lib.portal.url;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.enonic.xp.portal.PortalRequest;
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 abstract class AbstractUrlHandler
implements ScriptBean
{
protected PortalRequest request;
protected PortalUrlService urlService;
protected abstract String buildUrl( Multimap map );
public final String createUrl( final ScriptValue params )
{
if ( params == null )
{
return createUrl( new HashMap<>() );
}
return createUrl( params.getMap() );
}
private String createUrl( final Map params )
{
final Multimap map = toMap( params );
return buildUrl( map );
}
private Multimap toMap( final Map params )
{
final Multimap map = HashMultimap.create();
for ( final Map.Entry param : params.entrySet() )
{
final String key = param.getKey();
if ( key.equals( "params" ) )
{
applyParams( map, param.getValue() );
}
else if ( this.isValidParam( key ) )
{
applyParam( map, "_" + key, param.getValue() );
}
}
return map;
}
private void applyParams( final Multimap params, final Object value )
{
if ( value instanceof Map )
{
applyParams( params, (Map) value );
}
}
private void applyParams( final Multimap params, final Map, ?> value )
{
for ( final Map.Entry, ?> entry : value.entrySet() )
{
final String key = entry.getKey().toString();
applyParam( params, key, entry.getValue() );
}
}
private void applyParam( final Multimap params, final String key, final Object value )
{
if ( value instanceof Iterable )
{
applyParam( params, key, (Iterable) value );
}
else
{
params.put( key, value.toString() );
}
}
private void applyParam( final Multimap params, final String key, final Iterable values )
{
for ( final Object value : values )
{
params.put( key, value.toString() );
}
}
protected abstract boolean isValidParam( String param );
@Override
public void initialize( final BeanContext context )
{
this.request = context.getBinding( PortalRequest.class ).get();
this.urlService = context.getService( PortalUrlService.class ).get();
}
}