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 (IOException e) { 076 throw e; 077 } catch (Exception e) { 078 ExceptionUtils.checkInterrupt(e); 079 throw new RegistrationException("Instance registration failed. " + e.getMessage()); 080 } 081 } 082 083 /** 084 * Find a project by its symbolic name, ignoring case 085 * 086 * @param projectName project symbolic name 087 * @param availableProjects projects in which to to look for {@code project} 088 * @return the project or null if not found 089 */ 090 public ConnectProject getProjectByName(final String projectName, List<ConnectProject> availableProjects) { 091 Stream<ConnectProject> projectStream = availableProjects.stream(); 092 Optional<ConnectProject> pkg = projectStream.filter( 093 availProject -> projectName.equalsIgnoreCase(availProject.getSymbolicName())).findFirst(); 094 if (!pkg.isPresent()) { 095 return null; 096 } 097 return pkg.get(); 098 } 099 100 public List<ConnectProject> getAvailableProjects(String username, char[] password) throws ConfigurationException { 101 List<ConnectProject> studioProjects = registration().getAvailableProjectsForRegistration(username, 102 new String(password)); 103 if (studioProjects.isEmpty()) { 104 throw new ConfigurationException("Wrong login or password."); 105 } 106 return studioProjects; 107 } 108}