001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.ui.web.auth.service;
021
022import java.io.Serializable;
023
024import javax.servlet.http.HttpServletRequest;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XObject;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
034 * @since 5.7
035 */
036@XObject("loginProvider")
037public class LoginProviderLink implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    protected static final Log log = LogFactory.getLog(LoginProviderLink.class);
042
043    public LoginProviderLink() {
044
045    }
046
047    @Override
048    public boolean equals(Object obj) {
049        if (obj instanceof LoginProviderLink && name != null) {
050            return name.equals(((LoginProviderLink) obj).getName());
051        }
052        return super.equals(obj);
053    }
054
055    @XNode("@name")
056    protected String name;
057
058    @XNode("label")
059    protected String label;
060
061    @XNode("@remove")
062    protected boolean remove = false;
063
064    protected String iconPath;
065
066    protected String link;
067
068    @XNode("@class")
069    protected Class<LoginProviderLinkComputer> urlComputerClass;
070
071    protected LoginProviderLinkComputer urlComputer;
072
073    @XNode("description")
074    protected String description;
075
076    public String getName() {
077        return name;
078    }
079
080    public void setName(String name) {
081        this.name = name;
082    }
083
084    public String getIconPath() {
085        return iconPath;
086    }
087
088    @XNode("iconPath")
089    public void setIconPath(String iconPath) {
090        this.iconPath = Framework.expandVars(iconPath);
091    }
092
093    public String getLink(HttpServletRequest req, String requestedUrl) {
094        if (urlComputerClass != null && urlComputer == null) {
095            try {
096                urlComputer = (LoginProviderLinkComputer) urlComputerClass.newInstance();
097            } catch (ReflectiveOperationException e) {
098                log.error("Unable to instantiate LoginProviderLinkComputer", e);
099            }
100        }
101        if (urlComputer != null) {
102            return urlComputer.computeUrl(req, requestedUrl);
103        } else {
104            return link;
105        }
106    }
107
108    @XNode("link")
109    public void setLink(String link) {
110        this.link = Framework.expandVars(link);
111    }
112
113    public String getDescription() {
114        return description;
115    }
116
117    public void setDescription(String description) {
118        this.description = description;
119    }
120
121    public String getLabel() {
122        if (label == null) {
123            return getName();
124        }
125        return label;
126    }
127
128    public String getLink() {
129        return link;
130    }
131
132    public void merge(LoginProviderLink newLink) {
133        if (newLink.link != null) {
134            link = newLink.link;
135        }
136        if (newLink.description != null) {
137            description = newLink.description;
138        }
139        if (newLink.iconPath != null) {
140            iconPath = newLink.iconPath;
141        }
142    }
143
144    /**
145     * @since 7.10
146     */
147    @Override
148    protected LoginProviderLink clone() {
149        LoginProviderLink clone = new LoginProviderLink();
150        clone.description = description;
151        clone.iconPath = iconPath;
152        clone.label = label;
153        clone.link = link;
154        clone.name = name;
155        clone.remove = remove;
156        clone.urlComputerClass = urlComputerClass;
157        return clone;
158    }
159
160}