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 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 {
036
037    protected static final Log log = LogFactory.getLog(LoginProviderLink.class);
038
039    public LoginProviderLink() {
040
041    }
042
043    /**
044     * @since 10.10
045     */
046    public LoginProviderLink(String name, String iconPath, String link, String label, String description,
047            LoginProviderLinkComputer urlComputer) {
048        this.name = name;
049        this.iconPath = iconPath;
050        this.link = link;
051        this.label = label;
052        this.description = description;
053        this.urlComputer = urlComputer;
054    }
055
056    @Override
057    public boolean equals(Object obj) {
058        if (obj instanceof LoginProviderLink && name != null) {
059            return name.equals(((LoginProviderLink) obj).getName());
060        }
061        return super.equals(obj);
062    }
063
064    @XNode("@name")
065    protected String name;
066
067    @XNode("label")
068    protected String label;
069
070    @XNode("@remove")
071    protected boolean remove = false;
072
073    protected String iconPath;
074
075    protected String link;
076
077    @XNode("@class")
078    protected Class<LoginProviderLinkComputer> urlComputerClass;
079
080    protected LoginProviderLinkComputer urlComputer;
081
082    @XNode("description")
083    protected String description;
084
085    public String getName() {
086        return name;
087    }
088
089    public void setName(String name) {
090        this.name = name;
091    }
092
093    public String getIconPath() {
094        return iconPath;
095    }
096
097    @XNode("iconPath")
098    public void setIconPath(String iconPath) {
099        this.iconPath = Framework.expandVars(iconPath);
100    }
101
102    public String getLink(HttpServletRequest req, String requestedUrl) {
103        if (urlComputerClass != null && urlComputer == null) {
104            try {
105                urlComputer = urlComputerClass.getDeclaredConstructor().newInstance();
106            } catch (ReflectiveOperationException e) {
107                log.error("Unable to instantiate LoginProviderLinkComputer", e);
108            }
109        }
110        if (urlComputer != null) {
111            return urlComputer.computeUrl(req, requestedUrl);
112        } else {
113            return link;
114        }
115    }
116
117    @XNode("link")
118    public void setLink(String link) {
119        this.link = Framework.expandVars(link);
120    }
121
122    public String getDescription() {
123        return description;
124    }
125
126    public void setDescription(String description) {
127        this.description = description;
128    }
129
130    public String getLabel() {
131        if (label == null) {
132            return getName();
133        }
134        return label;
135    }
136
137    public String getLink() {
138        return link;
139    }
140
141    public void merge(LoginProviderLink newLink) {
142        if (newLink.link != null) {
143            link = newLink.link;
144        }
145        if (newLink.description != null) {
146            description = newLink.description;
147        }
148        if (newLink.iconPath != null) {
149            iconPath = newLink.iconPath;
150        }
151    }
152
153    /**
154     * @since 7.10
155     */
156    @Override
157    protected LoginProviderLink clone() {
158        LoginProviderLink clone = new LoginProviderLink();
159        clone.description = description;
160        clone.iconPath = iconPath;
161        clone.label = label;
162        clone.link = link;
163        clone.name = name;
164        clone.remove = remove;
165        clone.urlComputerClass = urlComputerClass;
166        return clone;
167    }
168
169}