001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.license.util;
016    
017    import com.liferay.portal.json.JSONObjectImpl;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.license.LicenseInfo;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.security.SecureRandomUtil;
023    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
024    import com.liferay.portal.kernel.util.Constants;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.LicenseUtil;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import java.util.Arrays;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    import java.util.Set;
035    import java.util.UUID;
036    
037    /**
038     * @author Amos Fong
039     */
040    @DoPrivileged
041    public class DefaultLicenseManagerImpl
042            implements com.liferay.portal.kernel.license.util.LicenseManager {
043    
044            @Override
045            public void checkLicense(String productId) {
046            }
047    
048            @Override
049            public List<Map<String, String>> getClusterLicenseProperties(
050                    String clusterNodeId) {
051    
052                    return null;
053            }
054    
055            @Override
056            public String getHostName() {
057                    return PortalUtil.getComputerName();
058            }
059    
060            @Override
061            public Set<String> getIpAddresses() {
062                    return LicenseUtil.getIpAddresses();
063            }
064    
065            @Override
066            public LicenseInfo getLicenseInfo(String productId) {
067                    return null;
068            }
069    
070            @Override
071            public List<Map<String, String>> getLicenseProperties() {
072                    return null;
073            }
074    
075            @Override
076            public Map<String, String> getLicenseProperties(String productId) {
077                    return null;
078            }
079    
080            @Override
081            public int getLicenseState(Map<String, String> licenseProperties) {
082                    String productId = licenseProperties.get("productId");
083    
084                    if (Validator.isNull(productId)) {
085                            return 0;
086                    }
087    
088                    try {
089                            JSONObject jsonObject = new JSONObjectImpl();
090    
091                            byte[] serverIdBytes = LicenseUtil.getServerIdBytes();
092    
093                            jsonObject.put(Constants.CMD, "GET_LICENSE_STATE");
094    
095                            jsonObject.put("hostName", getHostName());
096                            jsonObject.put("ipAddresses", StringUtil.merge(getIpAddresses()));
097                            jsonObject.put("macAddresses", StringUtil.merge(getMacAddresses()));
098                            jsonObject.put("productId", productId);
099    
100                            String productVersion = licenseProperties.get("productVersion");
101    
102                            jsonObject.put("productVersion", productVersion);
103    
104                            UUID uuid = new UUID(
105                                    SecureRandomUtil.nextLong(), SecureRandomUtil.nextLong());
106    
107                            String randomUuid = uuid.toString();
108    
109                            jsonObject.put("randomUuid", randomUuid);
110    
111                            jsonObject.put("serverId", Arrays.toString(serverIdBytes));
112    
113                            String userCount = licenseProperties.get("userCount");
114    
115                            jsonObject.put("userCount", userCount);
116    
117                            jsonObject.put("version", 2);
118    
119                            String response = LicenseUtil.sendRequest(jsonObject.toString());
120    
121                            JSONObject responseJSONObject = new JSONObjectImpl(response);
122    
123                            String errorMessage = responseJSONObject.getString("errorMessage");
124    
125                            if (Validator.isNotNull(errorMessage)) {
126                                    throw new Exception(errorMessage);
127                            }
128    
129                            String responseRandomUuid = responseJSONObject.getString(
130                                    "randomUuid");
131    
132                            if (responseRandomUuid.equals(randomUuid)) {
133                                    int licenseState = responseJSONObject.getInt("licenseState");
134    
135                                    return licenseState;
136                            }
137                    }
138                    catch (Exception e) {
139                            _log.error(e, e);
140                    }
141    
142                    return 0;
143            }
144    
145            @Override
146            public int getLicenseState(String productId) {
147                    Map<String, String> licenseProperties = new HashMap<>();
148    
149                    licenseProperties.put("productId", productId);
150    
151                    return getLicenseState(licenseProperties);
152            }
153    
154            @Override
155            public Set<String> getMacAddresses() {
156                    return LicenseUtil.getMacAddresses();
157            }
158    
159            @Override
160            public void registerLicense(JSONObject jsonObject) throws Exception {
161                    String serverId = jsonObject.getString("serverId");
162    
163                    if (serverId.length() <= 2) {
164                            return;
165                    }
166    
167                    serverId = serverId.substring(1, serverId.length() - 1);
168    
169                    String[] serverIdArray = StringUtil.split(serverId);
170    
171                    byte[] bytes = new byte[serverIdArray.length];
172    
173                    for (int i = 0; i < bytes.length; i++) {
174                            bytes[i] = Byte.valueOf(serverIdArray[i].trim());
175                    }
176    
177                    LicenseUtil.writeServerProperties(bytes);
178            }
179    
180            private static final Log _log = LogFactoryUtil.getLog(
181                    DefaultLicenseManagerImpl.class);
182    
183    }