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.Map; 025import java.util.Optional; 026import java.util.stream.Stream; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 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 public static final String REGISTRATION_EMAIL = "email"; 046 047 public static final String REGISTRATION_COMPANY = "company"; 048 049 public static final String REGISTRATION_PROJECT = "connectreg:projectName"; 050 051 public static final String REGISTRATION_DESCRIPTION = "description"; 052 053 public static final String REGISTRATION_PASSWORD = "password"; 054 055 public static final String REGISTRATION_PASSWORDND = "password_verif"; 056 057 public static final String REGISTRATION_TERMSNCONDITIONS = "termsAndConditions"; 058 059 public static final String REGISTRATION_COMPANY_REGEX = "^\\w+$"; 060 061 public static final String REGISTRATION_USERNAME_REGEX = "^\\w+$"; 062 063 public static final String REGISTRATION_PROJECT_REGEX = "^(?:[-\\w]+|)$"; 064 065 private static final Log log = LogFactory.getLog(ConnectRegistrationBroker.class); 066 067 protected static ConnectRegistrationService registration() { 068 return NuxeoConnectClient.getConnectRegistrationService(); 069 } 070 071 public void registerTrial(Map<String, String> parameters) throws IOException, RegistrationException, 072 ConfigurationException { 073 try { 074 registration().remoteTrialInstanceRegistration(parameters); 075 } catch (LogicalInstanceIdentifier.InvalidCLID e) { 076 log.debug(e, e); 077 throw new ConfigurationException("Instance registration failed.", e); 078 } 079 } 080 081 public void registerLocal(String strCLID, String description) throws IOException, ConfigurationException { 082 try { 083 registration().localRegisterInstance(strCLID, description); 084 } catch (LogicalInstanceIdentifier.InvalidCLID e) { 085 log.debug(e, e); 086 throw new ConfigurationException("Instance registration failed.", e); 087 } 088 } 089 090 public void registerRemote(String username, char[] password, String projectId, NuxeoClientInstanceType type, 091 String description) throws IOException, ConfigurationException { 092 String strCLID = RegistrationHelper.remoteRegisterInstance(username, new String(password), projectId, type, 093 description); 094 registerLocal(strCLID, description); 095 } 096 097 /** 098 * Find a project by its symbolic name, ignoring case 099 * 100 * @param projectName project symbolic name 101 * @param availableProjects projects in which to to look for {@code project} 102 * @return the project or null if not found 103 */ 104 public ConnectProject getProjectByName(final String projectName, List<ConnectProject> availableProjects) { 105 Stream<ConnectProject> projectStream = availableProjects.stream(); 106 Optional<ConnectProject> pkg = projectStream.filter( 107 availProject -> projectName.equalsIgnoreCase(availProject.getSymbolicName())).findFirst(); 108 if (!pkg.isPresent()) { 109 return null; 110 } 111 return pkg.get(); 112 } 113 114 public List<ConnectProject> getAvailableProjects(String username, char[] password) throws ConfigurationException { 115 List<ConnectProject> studioProjects = registration().getAvailableProjectsForRegistration(username, 116 new String(password)); 117 if (studioProjects.isEmpty()) { 118 throw new ConfigurationException("Wrong login or password."); 119 } 120 return studioProjects; 121 } 122}