001/*
002 * (C) Copyright 2016 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 *     akervern, jcarsique
018 */
019
020package org.nuxeo.launcher.connect;
021
022import java.io.IOException;
023import java.util.List;
024import java.util.Optional;
025import java.util.stream.Stream;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.ExceptionUtils;
030import org.nuxeo.connect.NuxeoConnectClient;
031import org.nuxeo.connect.connector.NuxeoClientInstanceType;
032import org.nuxeo.connect.data.ConnectProject;
033import org.nuxeo.connect.identity.LogicalInstanceIdentifier;
034import org.nuxeo.connect.registration.ConnectRegistrationService;
035import org.nuxeo.connect.registration.RegistrationException;
036import org.nuxeo.connect.registration.RegistrationHelper;
037import org.nuxeo.launcher.config.ConfigurationException;
038
039/**
040 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
041 * @since 8.3
042 */
043public class ConnectRegistrationBroker {
044
045    private static final Log log = LogFactory.getLog(ConnectRegistrationBroker.class);
046
047    protected static ConnectRegistrationService registration() {
048        return NuxeoConnectClient.getConnectRegistrationService();
049    }
050
051    public void registerLocal(String strCLID, String description) throws IOException, ConfigurationException {
052        try {
053            registration().localRegisterInstance(strCLID, description);
054        } catch (LogicalInstanceIdentifier.InvalidCLID e) {
055            log.debug(e, e);
056            throw new ConfigurationException("Instance registration failed.", e);
057        }
058    }
059
060    public void registerRemote(String username, char[] password, String projectId, NuxeoClientInstanceType type,
061            String description) throws IOException, ConfigurationException {
062        String strCLID = RegistrationHelper.remoteRegisterInstance(username, new String(password), projectId, type,
063                description);
064        registerLocal(strCLID, description);
065    }
066
067    /**
068     * Renews a registration.
069     *
070     * @since 8.10-HF15
071     */
072    public void remoteRenewRegistration() throws IOException, RegistrationException {
073        try {
074            registration().remoteRenewRegistration();
075        } catch (InterruptedException e) {
076            Thread.currentThread().interrupt();
077            throw new RuntimeException(e);
078        } catch (IOException e) {
079            throw e;
080        } catch (Exception e) {
081            throw new RegistrationException("Instance registration failed. " + e.getMessage());
082        }
083    }
084
085    /**
086     * Find a project by its symbolic name, ignoring case
087     *
088     * @param projectName project symbolic name
089     * @param availableProjects projects in which to to look for {@code project}
090     * @return the project or null if not found
091     */
092    public ConnectProject getProjectByName(final String projectName, List<ConnectProject> availableProjects) {
093        Stream<ConnectProject> projectStream = availableProjects.stream();
094        Optional<ConnectProject> pkg = projectStream.filter(
095                availProject -> projectName.equalsIgnoreCase(availProject.getSymbolicName())).findFirst();
096        if (!pkg.isPresent()) {
097            return null;
098        }
099        return pkg.get();
100    }
101
102    public List<ConnectProject> getAvailableProjects(String username, char[] password) throws ConfigurationException {
103        List<ConnectProject> studioProjects = registration().getAvailableProjectsForRegistration(username,
104                new String(password));
105        if (studioProjects.isEmpty()) {
106            throw new ConfigurationException("Wrong login or password.");
107        }
108        return studioProjects;
109    }
110}