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