Skip to content

Commit

Permalink
Merge pull request #271 from eclipse-passage/564420-2
Browse files Browse the repository at this point in the history
Bug 564420 conditions | move HC condition miner to new interfaces
  • Loading branch information
eparovyshnaya authored Jun 28, 2020
2 parents ca2af49 + c270ed5 commit 8fa3e5e
Show file tree
Hide file tree
Showing 25 changed files with 775 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
*******************************************************************************/
package org.eclipse.passage.lic.api.conditions;

import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;

/**
* Actions to be performed with {@link LicensingCondition}(s)
*
* @since 0.5.0
* @deprecated use {@link ConditionAction}
*/
@Deprecated
public class ConditionActions {

private ConditionActions() {
Expand All @@ -28,21 +32,27 @@ private ConditionActions() {
* be periodically confirmed with {@link #KEEP} action
*
* @since 0.5.0
* @deprecated use {@link ConditionAction.Aquire}
*/
@Deprecated
public static final String ACQUIRE = "acquire"; //$NON-NLS-1$

/**
* Keep the {@link LicensingCondition} for the usage
*
* @since 0.5.0
* @deprecated use {@link ConditionAction.Keep}
*/
@Deprecated
public static final String KEEP = "keep"; //$NON-NLS-1$

/**
* Release the {@link LicensingCondition} to be available for others
*
* @since 0.5.0
* @deprecated use {@link ConditionAction.Release}
*/
@Deprecated
public static final String RELEASE = "release"; //$NON-NLS-1$

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.conditions;

import java.util.Objects;

public abstract class ConditionAction {

private final String name;

protected ConditionAction(String name) {
Objects.requireNonNull(name, "ConditionAction::name"); //$NON-NLS-1$
this.name = name;
}

@Override
public boolean equals(Object obj) {
if (!ConditionAction.class.isInstance(obj)) {
return false;
}
return name.equals(((ConditionAction) obj).name());
}

@Override
public int hashCode() {
return name.hashCode();
}

public final String name() {
return name;
}

@Override
public String toString() {
return name;
}

public static final class Aquire extends ConditionAction {

public Aquire() {
super("acquire"); //$NON-NLS-1$
}

}

public static final class Keep extends ConditionAction {

public Keep() {
super("keep"); //$NON-NLS-1$
}

}

public static final class Release extends ConditionAction {

public Release() {
super("release"); //$NON-NLS-1$
}

}

public static final class Of extends ConditionAction {

public Of(String name) {
super(name);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.conditions;

import java.util.Objects;

public abstract class UserRole {

private final String role;

protected UserRole(String role) {
Objects.requireNonNull(role, "UserRole::role"); //$NON-NLS-1$
this.role = role;
}

@Override
public boolean equals(Object obj) {
if (!ConditionAction.class.isInstance(obj)) {
return false;
}
return role.equals(((ConditionAction) obj).name());
}

@Override
public int hashCode() {
return role.hashCode();
}

public final String name() {
return role;
}

@Override
public String toString() {
return role;
}

public static final class Admin extends UserRole {

public Admin() {
super("admin"); //$NON-NLS-1$
}

}

public static final class Licensee extends UserRole {

public Licensee() {
super("licensee"); //$NON-NLS-1$
}

}

public static final class Operator extends UserRole {

public Operator() {
super("operator"); //$NON-NLS-1$
}

}

public static final class Of extends UserRole {

public Of(String role) {
super(role);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class HcMessages extends NLS {
public static String HttpClient_final_error_message;
public static String HttpClient_not_ok_response;

public static String RemoteConditionsRequest_failed_to_compose_url;

static {
NLS.initializeMessages(BUNDLE_NAME, HcMessages.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ DecryptedConditions_no_transport_for_content_type=Unknown content type: no regis
DecryptedConditions_reading_error=Error reading remote condition data
HttpClient_final_error_message=Error mining conditions
HttpClient_not_ok_response=Mining connection responded not OK: %d ($s)
RemoteConditionsRequest_failed_to_compose_url=Failed to compose licensing server connection url
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

import java.net.URL;

import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionMiningException;

@SuppressWarnings("restriction")
public interface Request<R> {

URL url();
URL url() throws ConditionMiningException;

Configuration<R> config();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.eclipse.passage.lic.internal.hc.remote.impl;

import java.util.Collection;
import java.util.function.Supplier;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionMiningException;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransportRegistry;
Expand All @@ -24,9 +26,11 @@
public final class RemoteConditions implements MinedConditions {

private final StringServiceId id = new StringServiceId("remote"); //$NON-NLS-1$
private final Supplier<LicensedProduct> product;
private final ConditionTransportRegistry transports;

public RemoteConditions(ConditionTransportRegistry transports) {
public RemoteConditions(Supplier<LicensedProduct> product, ConditionTransportRegistry transports) {
this.product = product;
this.transports = transports;
}

Expand All @@ -39,7 +43,7 @@ public StringServiceId id() {
@Override
public Collection<Condition> all() throws ConditionMiningException {
return new HttpClient().remoteConditions(//
new LicensingServerRequest(), //
new RemoteConditionsRequest(product.get()), //
new DecryptedConditions(transports));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.remote.impl;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;
import org.eclipse.passage.lic.internal.api.conditions.UserRole;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionMiningException;
import org.eclipse.passage.lic.internal.base.NamedData;
import org.eclipse.passage.lic.internal.base.ProductIdentifier;
import org.eclipse.passage.lic.internal.base.ProductVersion;
import org.eclipse.passage.lic.internal.equinox.io.InstallationPath;
import org.eclipse.passage.lic.internal.hc.i18n.HcMessages;
import org.eclipse.passage.lic.internal.hc.remote.Configuration;
import org.eclipse.passage.lic.internal.hc.remote.Request;
import org.eclipse.passage.lic.internal.net.LicensingAction;
import org.eclipse.passage.lic.internal.net.LicensingRole;
import org.eclipse.passage.lic.internal.net.LicensingServerCoordinates;
import org.eclipse.passage.lic.internal.net.LicensingServerCoordinates.HostPort;

/**
* Basing on
*
* @author user
*/
@SuppressWarnings("restriction")
final class RemoteConditionsRequest implements Request<HttpURLConnection> {

private final LicensedProduct product;

public RemoteConditionsRequest(LicensedProduct product) {
this.product = product;
}

@Override
public URL url() throws ConditionMiningException {
try {
HostPort corrdinates = new LicensingServerCoordinates(new InstallationPath()).get();
return new URL("http", //$NON-NLS-1$
corrdinates.host(), //
Integer.parseInt(corrdinates.port()), //
'?' + parameters());
} catch (LicensingException //
| NumberFormatException //
| MalformedURLException //
| UnsupportedEncodingException e) {
throw new ConditionMiningException(HcMessages.RemoteConditionsRequest_failed_to_compose_url, e);
}
}

private String parameters() throws UnsupportedEncodingException {
StringBuilder params = new StringBuilder();
Arrays.stream(//
new NamedData[] { //
new ProductIdentifier(encode(product.identifier())), //
new ProductVersion(encode(product.version())), //
new LicensingAction(new ConditionAction.Aquire()), //
new LicensingRole(new UserRole.Admin()) }) //
.map(NamedData.Writable<String>::new)//
.forEach(writable -> writable.write(params, "=", "&")); //$NON-NLS-1$ //$NON-NLS-2$
return params.toString();

}

private String encode(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, "UTF-8"); //$NON-NLS-1$
}

@Override
public Configuration<HttpURLConnection> config() {
throw new UnsupportedOperationException();
}

}
Loading

0 comments on commit 8fa3e5e

Please sign in to comment.