001/*
002 * (C) Copyright 2006-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.platform.ui.web.auth.service;
019
020import java.io.Serializable;
021
022import javax.servlet.http.HttpServletRequest;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
032 * @since 5.7
033 */
034@XObject("loginProvider")
035public class LoginProviderLink implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    protected static final Log log = LogFactory.getLog(LoginProviderLink.class);
040
041    public LoginProviderLink() {
042
043    }
044
045    @Override
046    public boolean equals(Object obj) {
047        if (obj instanceof LoginProviderLink && name != null) {
048            return name.equals(((LoginProviderLink) obj).getName());
049        }
050        return super.equals(obj);
051    }
052
053    @XNode("@name")
054    protected String name;
055
056    @XNode("label")
057    protected String label;
058
059    @XNode("@remove")
060    protected boolean remove = false;
061
062    protected String iconPath;
063
064    protected String link;
065
066    @XNode("@class")
067    protected Class<LoginProviderLinkComputer> urlComputerClass;
068
069    protected LoginProviderLinkComputer urlComputer;
070
071    @XNode("description")
072    protected String description;
073
074    public String getName() {
075        return name;
076    }
077
078    public void setName(String name) {
079        this.name = name;
080    }
081
082    public String getIconPath() {
083        return iconPath;
084    }
085
086    @XNode("iconPath")
087    public void setIconPath(String iconPath) {
088        this.iconPath = Framework.expandVars(iconPath);
089    }
090
091    public String getLink(HttpServletRequest req, String requestedUrl) {
092        if (urlComputerClass != null && urlComputer == null) {
093            try {
094                urlComputer = (LoginProviderLinkComputer) urlComputerClass.newInstance();
095            } catch (ReflectiveOperationException e) {
096                log.error("Unable to instantiate LoginProviderLinkComputer", e);
097            }
098        }
099        if (urlComputer != null) {
100            return urlComputer.computeUrl(req, requestedUrl);
101        } else {
102            return link;
103        }
104    }
105
106    @XNode("link")
107    public void setLink(String link) {
108        this.link = Framework.expandVars(link);
109    }
110
111    public String getDescription() {
112        return description;
113    }
114
115    public void setDescription(String description) {
116        this.description = description;
117    }
118
119    public String getLabel() {
120        if (label == null) {
121            return getName();
122        }
123        return label;
124    }
125
126    public String getLink() {
127        return link;
128    }
129
130    public void merge(LoginProviderLink newLink) {
131        if (newLink.link != null) {
132            link = newLink.link;
133        }
134        if (newLink.description != null) {
135            description = newLink.description;
136        }
137        if (newLink.iconPath != null) {
138            iconPath = newLink.iconPath;
139        }
140    }
141
142    /**
143     * @since 7.10
144     */
145    @Override
146    protected LoginProviderLink clone() {
147        LoginProviderLink clone = new LoginProviderLink();
148        clone.description = description;
149        clone.iconPath = iconPath;
150        clone.label = label;
151        clone.link = link;
152        clone.name = name;
153        clone.remove = remove;
154        clone.urlComputerClass = urlComputerClass;
155        return clone;
156    }
157
158}