Skip to content

Commit

Permalink
Merge pull request #530 from eclipse-passage/565143_to_licensing_action
Browse files Browse the repository at this point in the history
Bug 565143 [Passage] floating licensing
  • Loading branch information
ruspl-afed authored Nov 9, 2020
2 parents f5b5250 + ad09653 commit 8f135c3
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 696 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.api;

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

/**
* @since 1.0
*/
public interface BackendLicensingRequest {

ContentType contentType();

String parameter(String key);

String body();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,46 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.eclipse.passage.lbc.internal.api.BackendLicensingRequest;
import org.eclipse.passage.lbc.internal.api.Requester;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;

/**
* @since 1.0
*/
public final class BaseLicensingRequest implements BackendLicensingRequest {
public final class BaseLicensingRequest<R> implements BackendLicensingRequest {

private final R request;
private final BiFunction<R, String, String> parameter;
private final Function<R, String> body;

private final HttpServletRequest httpRequest;
private final String body;
public BaseLicensingRequest(R request, BiFunction<R, String, String> parameter, Function<R, String> body) {
this.request = request;
this.parameter = parameter;
this.body = body;
}

public BaseLicensingRequest(HttpServletRequest httpRequest) throws IOException {
this.httpRequest = httpRequest;
this.body = body(httpRequest);
@Override
public ContentType contentType() {
return new ContentType.Of(parameter("licensing.content.type")); //$NON-NLS-1$
}

@Override
public String body() {
return body;
return body.apply(request);
}

@Override
public String parameter(String key) {
return httpRequest.getParameter(key);
return parameter.apply(request, key);
}

@Override
public Requester requester() {
return new BaseRequester(parameter("process"), parameter("hardware"), parameter("feature")); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}

private String body(HttpServletRequest httpRequest) throws IOException {
StringBuilder builder = new StringBuilder();
httpRequest.getReader().lines().forEachOrdered(builder::append);
String string = builder.toString();
return string;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

public final class BaseRequestDispatcher implements BackendRequestDispatcher {

private final Map<BackendAction, Chain> chains;
private final Map<String, Chain> chains;
private final Function<BackendLicensingRequest, String> action;

public BaseRequestDispatcher(Map<BackendAction, Chain> chains) {
public BaseRequestDispatcher(Map<String, Chain> chains) {
this.chains = chains;
this.action = r -> r.parameter("action"); //$NON-NLS-1$
}
Expand All @@ -49,8 +49,8 @@ public void dispatch(BackendLicensingRequest request, BackendLicensingResponse r
* @return serialized result to be sent
*/
private String execute(BackendLicensingRequest request) {
// identify the server
return Optional.ofNullable(action.apply(request))//
.map(BackendAction.Of::new)//
.map(chains::get)//
.map(c -> c.apply(request))//
.orElseGet(() -> String.format("{\"error\":\"unsupported action %s\"}", //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
import org.eclipse.passage.lbc.internal.api.persistence.PersistableLicense;
import org.eclipse.passage.lbc.internal.api.persistence.Serialization;
import org.eclipse.passage.lbc.internal.base.AcquiringFailures;
import org.eclipse.passage.lbc.internal.base.BackendAction;
import org.eclipse.passage.lbc.internal.base.SatisfiedRequirements;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.Permission;
import org.eclipse.passage.lic.internal.api.requirements.Requirement;
import org.eclipse.passage.lic.internal.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.internal.api.restrictions.Restriction;
import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult;
import org.eclipse.passage.lic.internal.base.restrictions.BaseExaminationCertificate;
import org.eclipse.passage.lic.internal.net.LicensingAction;

@SuppressWarnings("restriction")
public final class Acquire extends Operation<RequestedCertificate, ExaminationCertificate> {
Expand Down Expand Up @@ -61,8 +62,8 @@ private boolean take(Condition condition) {
}

@Override
public BackendAction action() {
return new BackendAction.Acquire();
public LicensingAction action() {
return new LicensingAction(new ConditionAction.Acquire());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import org.eclipse.passage.lbc.internal.api.persistence.Deserialization;
import org.eclipse.passage.lbc.internal.api.persistence.LoadedLicense;
import org.eclipse.passage.lbc.internal.api.persistence.PersistableLicense;
import org.eclipse.passage.lbc.internal.base.BackendAction;
import org.eclipse.passage.lbc.internal.base.troubles.ConditionEntryNotFound;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;
import org.eclipse.passage.lic.internal.api.diagnostic.Trouble;
import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult;
import org.eclipse.passage.lic.internal.net.LicensingAction;

public final class CanTake extends Operation<RequestedCondition, Boolean> {

Expand Down Expand Up @@ -59,8 +60,8 @@ public ServiceInvocationResult<String> apply(ServiceInvocationResult<Boolean> re
}

@Override
public BackendAction action() {
return new BackendAction.CanTake();
public LicensingAction action() {
return new LicensingAction(new ConditionAction.Of("can-take")); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.eclipse.passage.lbc.internal.api.chains.Chain;
import org.eclipse.passage.lbc.internal.api.persistence.LoadedLicense;
import org.eclipse.passage.lbc.internal.api.persistence.PersistableLicense;
import org.eclipse.passage.lbc.internal.base.BackendAction;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.net.LicensingAction;

public abstract class Operation<I, O> implements Chain {

Expand All @@ -47,7 +47,7 @@ protected final Optional<PersistableLicense> license(Condition request) {

protected abstract ServiceInvocationResult<O> execute(I input);

public abstract BackendAction action();
public abstract LicensingAction action();

@Override
public final String apply(BackendLicensingRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import org.eclipse.passage.lbc.internal.api.persistence.LoadedLicense;
import org.eclipse.passage.lbc.internal.api.persistence.PersistableLicense;
import org.eclipse.passage.lbc.internal.api.persistence.Serialization;
import org.eclipse.passage.lbc.internal.base.BackendAction;
import org.eclipse.passage.lbc.internal.base.ReleaseReport;
import org.eclipse.passage.lbc.internal.base.ReleaseReport.ReleaseResult;
import org.eclipse.passage.lbc.internal.base.SatisfiedRequirements;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.Condition;
import org.eclipse.passage.lic.internal.api.conditions.ConditionAction;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.Permission;
import org.eclipse.passage.lic.internal.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult;
import org.eclipse.passage.lic.internal.net.LicensingAction;

@SuppressWarnings("restriction")
public final class Release extends Operation<RequestedCertificate, ReleaseReport> {
Expand Down Expand Up @@ -63,8 +64,8 @@ private boolean release(Condition condition) {
}

@Override
public BackendAction action() {
return new BackendAction.Release();
public LicensingAction action() {
return new LicensingAction(new ConditionAction.Release());
}

}
3 changes: 2 additions & 1 deletion bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Require-Bundle: javax.servlet;bundle-version="0.0.0",
org.eclipse.passage.lbc.api;bundle-version="0.0.0",
org.eclipse.passage.lbc.base;bundle-version="0.0.0",
org.eclipse.passage.lbc.json;bundle-version="1.0.0",
org.eclipse.passage.lic.base;bundle-version="0.0.0"
org.eclipse.passage.lic.base;bundle-version="0.0.0",
org.eclipse.passage.lic.net;bundle-version="1.0.0"
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.passage.lbc.internal.jetty.JettyActivator
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.jetty;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -24,9 +26,10 @@

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.passage.lbc.internal.api.BackendRequestDispatcher;
import org.eclipse.passage.lbc.internal.api.chains.Chain;
import org.eclipse.passage.lbc.internal.base.BackendAction;
import org.eclipse.passage.lbc.internal.base.BaseLicensingRequest;
import org.eclipse.passage.lbc.internal.base.BaseLicensingResponse;
import org.eclipse.passage.lbc.internal.base.BaseRequestDispatcher;
Expand All @@ -45,19 +48,38 @@
@SuppressWarnings("restriction")
public final class JettyHandler extends AbstractHandler {

private final Logger logger = Log.getLogger(JettyHandler.class);

private final BackendRequestDispatcher dispatcher;
private final LockFolder lock;
private final BiFunction<HttpServletRequest, String, String> parameter;
private final Function<HttpServletRequest, String> body;

public JettyHandler() {
lock = new LockFolder();
dispatcher = new BaseRequestDispatcher(chains());
parameter = this::requestParameter;
body = this::requestBody;
}

private String requestParameter(HttpServletRequest request, String name) {
return request.getParameter(name);
}

private String requestBody(HttpServletRequest request) {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
reader.lines().forEachOrdered(builder::append);
} catch (IOException e) {
logger.warn("Failure during request bidy extraction", e); //$NON-NLS-1$
}
return builder.toString();
}

private Map<BackendAction, Chain> chains() {
private Map<String, Chain> chains() {
return Arrays.asList(acquire(), canTake(), release()) //
.stream() //
.collect(Collectors.toMap(Operation::action, Function.identity()));
.collect(Collectors.toMap(this::key, Function.identity()));
}

private Acquire acquire() {
Expand All @@ -77,7 +99,11 @@ private Release release() {
@Override
public void handle(String target, Request request, HttpServletRequest wrapper, HttpServletResponse response)
throws IOException, ServletException {
dispatcher.dispatch(new BaseLicensingRequest(wrapper), new BaseLicensingResponse(response));
dispatcher.dispatch(new BaseLicensingRequest<>(wrapper, parameter, body), new BaseLicensingResponse(response));
}

private String key(Operation<?, ?> operation) {
return operation.action().get().get().name();
}

}
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lic.hc/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.lic.hc
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.lic.hc
Bundle-Version: 1.0.1.qualifier
Bundle-Version: 1.0.100.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down
Loading

0 comments on commit 8f135c3

Please sign in to comment.