/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jasig.portal.layout.profile;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jasig.portal.security.IPerson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * ChainingProfileMapper which add a prefix on profil Fname if the user is a guest.
 * The prefix added is "guest-[server name]-[sub fname]".
 * If the server name used is not authorized use "guest-[sub fname]" instead.
 * 
 * @author Maxime Bossard - GIP RECIA 2013
 */
public class ServerNameGuestChainingProfileMapper implements IProfileMapper, InitializingBean {

    protected Logger logger = LoggerFactory.getLogger(ServerNameGuestChainingProfileMapper.class);
    
    private static final String SEPARATOR = "-";
    
    private String defaultProfileName = "default";
    
    private Map<String, String> authorizedServerNames;

    private List<IProfileMapper> subMappers = Collections.<IProfileMapper>emptyList();

	@Override
    public String getProfileFname(IPerson person, HttpServletRequest request) {
		final StringBuilder fname = new StringBuilder(64); 
		if (person.isGuest()) {
			final String serverName = authorizedServerNames.get(request.getServerName());
			// If guest user
			fname.append("guest" + ServerNameGuestChainingProfileMapper.SEPARATOR);
			if (StringUtils.hasText(serverName)) {
				// If use an authorized server name we add the guest prefix
				fname.append(serverName);
				fname.append(ServerNameGuestChainingProfileMapper.SEPARATOR);
			}
		}
		
		boolean subFnameFound = false;
        for (IProfileMapper mapper : subMappers) {
            final String subFname = mapper.getProfileFname(person, request);
            if (StringUtils.hasText(subFname)) {
            	fname.append(subFname);
            	subFnameFound = true;
            	break;
            }
        }
        
        if (!subFnameFound) {
        	fname.append(this.defaultProfileName);
        }
        
        logger.debug("Profile fname: [{}].", fname.toString());
        return fname.toString();
    }

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notEmpty(authorizedServerNames, "No authorized server name provided !");
	}
    
    public void setDefaultProfileName(String defaultProfileName) {
        this.defaultProfileName = defaultProfileName;
    }
    
    public void setSubMappers(List<IProfileMapper> subMappers) {
        this.subMappers = subMappers;
    }

    public void setAuthorizedServerNames(Map<String, String> authorizedServerNames) {
		this.authorizedServerNames = authorizedServerNames;
	}

}

@@ -3540,10 +3540,225 @@
 es;%0A%09%7D%0A%0A
+    @Override%0A    public String toString() %7B%0A        return %22ServerNameGuestChainingProfileMapper defaulting to %5B%22 + this.defaultProfileName +%0A                %22%5D with sub-mappers %5B%22 + this.subMappers + %22%5D.%22;%0A    %7D%0A%0A
 %7D%0A

Add toString() to ServerNameGuestChainingProfileMapper .