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