diff --git a/bundles/org.eclipse.passage.lbc.api/src/org/eclipse/passage/lbc/internal/api/BackendLicensingRequest.java b/bundles/org.eclipse.passage.lbc.api/src/org/eclipse/passage/lbc/internal/api/BackendLicensingRequest.java index 81bd8d0b5..2ac9e5731 100644 --- a/bundles/org.eclipse.passage.lbc.api/src/org/eclipse/passage/lbc/internal/api/BackendLicensingRequest.java +++ b/bundles/org.eclipse.passage.lbc.api/src/org/eclipse/passage/lbc/internal/api/BackendLicensingRequest.java @@ -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(); diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BackendAction.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BackendAction.java deleted file mode 100644 index c77e169e9..000000000 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BackendAction.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * 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.lbc.internal.base; - -import java.util.Objects; -import java.util.function.Supplier; - -/** - * @since 1.0 - */ -public abstract class BackendAction implements Supplier { - - private final String name; - - protected BackendAction(String name) { - Objects.requireNonNull(name, "BackendAction::name"); //$NON-NLS-1$ - this.name = name; - } - - @Override - public final String get() { - return name; - } - - @Override - public final int hashCode() { - return name.hashCode(); - } - - @Override - public final boolean equals(Object obj) { - if (!BackendAction.class.isInstance(obj)) { - return false; - } - if (obj == this) { - return true; - } - return name.equals(((BackendAction) obj).get()); - } - - public static final class Acquire extends BackendAction { - - public Acquire() { - super("acquire"); //$NON-NLS-1$ - } - - } - - public static final class Release extends BackendAction { - - public Release() { - super("release"); //$NON-NLS-1$ - } - - } - - public static final class CanTake extends BackendAction { - - public CanTake() { - super("can-take"); //$NON-NLS-1$ - } - - } - - public static final class Of extends BackendAction { - - public Of(String action) { - super(action); - } - - } - -} diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseLicensingRequest.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseLicensingRequest.java index 9dd79102b..4628809a6 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseLicensingRequest.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseLicensingRequest.java @@ -12,34 +12,41 @@ *******************************************************************************/ 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 implements BackendLicensingRequest { + + private final R request; + private final BiFunction parameter; + private final Function body; - private final HttpServletRequest httpRequest; - private final String body; + public BaseLicensingRequest(R request, BiFunction parameter, Function 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 @@ -47,11 +54,4 @@ 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; - } - } diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseRequestDispatcher.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseRequestDispatcher.java index d268d4825..418bfa2f4 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseRequestDispatcher.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/BaseRequestDispatcher.java @@ -29,10 +29,10 @@ public final class BaseRequestDispatcher implements BackendRequestDispatcher { - private final Map chains; + private final Map chains; private final Function action; - public BaseRequestDispatcher(Map chains) { + public BaseRequestDispatcher(Map chains) { this.chains = chains; this.action = r -> r.parameter("action"); //$NON-NLS-1$ } @@ -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$ diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Acquire.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Acquire.java index 1eca9d6fc..3fbf16fbc 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Acquire.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Acquire.java @@ -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 { @@ -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()); } } diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/CanTake.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/CanTake.java index e006942dc..5260a18bf 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/CanTake.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/CanTake.java @@ -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 { @@ -59,8 +60,8 @@ public ServiceInvocationResult apply(ServiceInvocationResult re } @Override - public BackendAction action() { - return new BackendAction.CanTake(); + public LicensingAction action() { + return new LicensingAction(new ConditionAction.Of("can-take")); //$NON-NLS-1$ } } diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Operation.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Operation.java index 0695d9365..bcd77ee34 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Operation.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Operation.java @@ -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 implements Chain { @@ -47,7 +47,7 @@ protected final Optional license(Condition request) { protected abstract ServiceInvocationResult execute(I input); - public abstract BackendAction action(); + public abstract LicensingAction action(); @Override public final String apply(BackendLicensingRequest request) { diff --git a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Release.java b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Release.java index b3bdcdf7e..d41c5df15 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Release.java +++ b/bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/chains/Release.java @@ -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 { @@ -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()); } } diff --git a/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF index b9f6ee81f..9683fded1 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF @@ -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 diff --git a/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyHandler.java b/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyHandler.java index bc07dc428..878ea61dd 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyHandler.java +++ b/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyHandler.java @@ -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; @@ -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; @@ -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 parameter; + private final Function 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 chains() { + private Map 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() { @@ -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(); } } diff --git a/bundles/org.eclipse.passage.lic.hc/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.hc/META-INF/MANIFEST.MF index 0330e15c0..65e52f92c 100644 --- a/bundles/org.eclipse.passage.lic.hc/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.hc/META-INF/MANIFEST.MF @@ -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 diff --git a/bundles/org.eclipse.passage.lic.hc/src/org/eclipse/passage/lic/internal/hc/remote/impl/RequestParameters.java b/bundles/org.eclipse.passage.lic.hc/src/org/eclipse/passage/lic/internal/hc/remote/impl/RequestParameters.java new file mode 100644 index 000000000..37f9deeb5 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.hc/src/org/eclipse/passage/lic/internal/hc/remote/impl/RequestParameters.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * 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.URLEncoder; +import java.util.Arrays; + +import org.eclipse.passage.lic.floating.model.api.FloatingLicenseAccess; +import org.eclipse.passage.lic.internal.api.LicensedProduct; +import org.eclipse.passage.lic.internal.api.conditions.ConditionAction; +import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType; +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.base.conditions.mining.LicensingContentType; +import org.eclipse.passage.lic.internal.net.LicensingAction; + +final class RequestParameters { + + private final LicensedProduct product; + private final FloatingLicenseAccess access; + + RequestParameters(LicensedProduct product, FloatingLicenseAccess access) { + this.product = product; + this.access = access; + } + + String query() throws UnsupportedEncodingException { + StringBuilder params = new StringBuilder(); + Arrays.stream(parameters())// + .map(NamedData.Writable::new)// + .forEach(writable -> writable.write(params, "=", "&")); //$NON-NLS-1$ //$NON-NLS-2$ + return '?' + params.toString(); + + } + + @SuppressWarnings("rawtypes") + private NamedData[] parameters() throws UnsupportedEncodingException { + return new NamedData[] { // + new ProductIdentifier(encode(product.identifier())), // + new ProductVersion(encode(product.version())), // + new LicensingAction(new ConditionAction.Acquire()), // + new LicensingContentType(new ContentType.Xml()), // + new LicenseUser(access.getUser()), // + new ServerAuthenticationType(access.getServer()), // + new ServerAuthenticationExpression(access.getServer()) }; + } + + private String encode(String value) throws UnsupportedEncodingException { + return URLEncoder.encode(value, "UTF-8"); //$NON-NLS-1$ + } + +} diff --git a/tests/org.eclipse.passage.lbc.api.tests/src/org/eclipse/passage/lbc/api/tests/FakeLicensingRequest.java b/tests/org.eclipse.passage.lbc.api.tests/src/org/eclipse/passage/lbc/api/tests/FakeLicensingRequest.java deleted file mode 100644 index b6f684296..000000000 --- a/tests/org.eclipse.passage.lbc.api.tests/src/org/eclipse/passage/lbc/api/tests/FakeLicensingRequest.java +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* - * 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.lbc.api.tests; - -import java.util.Map; - -import org.eclipse.passage.lbc.internal.api.BackendLicensingRequest; -import org.eclipse.passage.lbc.internal.api.Requester; - -public final class FakeLicensingRequest implements BackendLicensingRequest { - - private final Map params; - private final String body; - - public FakeLicensingRequest(Map params) { - this(params, ""); //$NON-NLS-1$ - } - - public FakeLicensingRequest(Map params, String body) { - this.params = params; - this.body = body; - } - - @Override - public String parameter(String key) { - return params.get(key); - } - - @Override - public Requester requester() { - return new Requester() { - - @Override - public String feature() { - return "feature"; //$NON-NLS-1$ - } - - @Override - public String hardware() { - return "hardware"; //$NON-NLS-1$ - } - - @Override - public String process() { - return "process"; //$NON-NLS-1$ - } - - }; - } - - @Override - public String body() { - return body; - } - -} diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/FakeHttpRequest.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/FakeHttpRequest.java deleted file mode 100644 index 1d6566ee1..000000000 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/FakeHttpRequest.java +++ /dev/null @@ -1,397 +0,0 @@ -/******************************************************************************* - * 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.lbc.base.tests; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; -import java.security.Principal; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpUpgradeHandler; -import javax.servlet.http.Part; - -public final class FakeHttpRequest implements HttpServletRequest { - - private final Map parameters; - - public FakeHttpRequest(Map parameters) { - this.parameters = parameters; - } - - @Override - public Object getAttribute(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public Enumeration getAttributeNames() { - throw new UnsupportedOperationException(); - } - - @Override - public String getCharacterEncoding() { - throw new UnsupportedOperationException(); - } - - @Override - public void setCharacterEncoding(String env) throws UnsupportedEncodingException { - throw new UnsupportedOperationException(); - } - - @Override - public int getContentLength() { - throw new UnsupportedOperationException(); - } - - @Override - public long getContentLengthLong() { - throw new UnsupportedOperationException(); - } - - @Override - public String getContentType() { - throw new UnsupportedOperationException(); - } - - @Override - public ServletInputStream getInputStream() throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public String getParameter(String name) { - return parameters.get(name); - } - - @Override - public Enumeration getParameterNames() { - throw new UnsupportedOperationException(); - } - - @Override - public String[] getParameterValues(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public Map getParameterMap() { - throw new UnsupportedOperationException(); - } - - @Override - public String getProtocol() { - throw new UnsupportedOperationException(); - } - - @Override - public String getScheme() { - throw new UnsupportedOperationException(); - } - - @Override - public String getServerName() { - throw new UnsupportedOperationException(); - } - - @Override - public int getServerPort() { - throw new UnsupportedOperationException(); - } - - @Override - public BufferedReader getReader() throws IOException { - return new BufferedReader( - new InputStreamReader(new ByteArrayInputStream("body".getBytes(StandardCharsets.UTF_8)))); //$NON-NLS-1$ - } - - @Override - public String getRemoteAddr() { - throw new UnsupportedOperationException(); - } - - @Override - public String getRemoteHost() { - throw new UnsupportedOperationException(); - } - - @Override - public void setAttribute(String name, Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public void removeAttribute(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public Locale getLocale() { - throw new UnsupportedOperationException(); - } - - @Override - public Enumeration getLocales() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isSecure() { - throw new UnsupportedOperationException(); - } - - @Override - public RequestDispatcher getRequestDispatcher(String path) { - throw new UnsupportedOperationException(); - } - - @Override - public String getRealPath(String path) { - throw new UnsupportedOperationException(); - } - - @Override - public int getRemotePort() { - throw new UnsupportedOperationException(); - } - - @Override - public String getLocalName() { - throw new UnsupportedOperationException(); - } - - @Override - public String getLocalAddr() { - throw new UnsupportedOperationException(); - } - - @Override - public int getLocalPort() { - throw new UnsupportedOperationException(); - } - - @Override - public ServletContext getServletContext() { - throw new UnsupportedOperationException(); - } - - @Override - public AsyncContext startAsync() throws IllegalStateException { - throw new UnsupportedOperationException(); - } - - @Override - public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) - throws IllegalStateException { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isAsyncStarted() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isAsyncSupported() { - throw new UnsupportedOperationException(); - } - - @Override - public AsyncContext getAsyncContext() { - throw new UnsupportedOperationException(); - } - - @Override - public DispatcherType getDispatcherType() { - throw new UnsupportedOperationException(); - } - - @Override - public String getAuthType() { - throw new UnsupportedOperationException(); - } - - @Override - public Cookie[] getCookies() { - throw new UnsupportedOperationException(); - } - - @Override - public long getDateHeader(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public String getHeader(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public Enumeration getHeaders(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public Enumeration getHeaderNames() { - throw new UnsupportedOperationException(); - } - - @Override - public int getIntHeader(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public String getMethod() { - throw new UnsupportedOperationException(); - } - - @Override - public String getPathInfo() { - throw new UnsupportedOperationException(); - } - - @Override - public String getPathTranslated() { - throw new UnsupportedOperationException(); - } - - @Override - public String getContextPath() { - throw new UnsupportedOperationException(); - } - - @Override - public String getQueryString() { - throw new UnsupportedOperationException(); - } - - @Override - public String getRemoteUser() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isUserInRole(String role) { - throw new UnsupportedOperationException(); - } - - @Override - public Principal getUserPrincipal() { - throw new UnsupportedOperationException(); - } - - @Override - public String getRequestedSessionId() { - throw new UnsupportedOperationException(); - } - - @Override - public String getRequestURI() { - throw new UnsupportedOperationException(); - } - - @Override - public StringBuffer getRequestURL() { - throw new UnsupportedOperationException(); - } - - @Override - public String getServletPath() { - throw new UnsupportedOperationException(); - } - - @Override - public HttpSession getSession(boolean create) { - throw new UnsupportedOperationException(); - } - - @Override - public HttpSession getSession() { - throw new UnsupportedOperationException(); - } - - @Override - public String changeSessionId() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isRequestedSessionIdValid() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isRequestedSessionIdFromCookie() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isRequestedSessionIdFromURL() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isRequestedSessionIdFromUrl() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public void login(String username, String password) throws ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public void logout() throws ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public Collection getParts() throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public Part getPart(String name) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - - @Override - public T upgrade(Class handlerClass) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - -} diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/LbcTestsBase.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/LbcTestsBase.java index 2ff84f5c0..53f85774a 100644 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/LbcTestsBase.java +++ b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/LbcTestsBase.java @@ -15,6 +15,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Collections; +import java.util.Map; import org.eclipse.passage.lbc.internal.api.Requester; import org.eclipse.passage.lbc.internal.api.persistence.BoundLicense; @@ -50,6 +51,14 @@ @SuppressWarnings("restriction") public abstract class LbcTestsBase { + protected String requestParameter(Map map, String name) { + return map.get(name); + } + + protected String requestBody(Map map) { + return map.get("body"); //$NON-NLS-1$ + } + protected LicensedProduct product() { return new BaseLicensedProduct(identifierValue(), versionValue()); } @@ -122,12 +131,12 @@ private BaseEvaluationInstructions evaluationInstructions() { return new BaseEvaluationInstructions(new EvaluationType.Hardware(), "expression"); //$NON-NLS-1$ } - private ValidityPeriod validityPeriod() { + protected ValidityPeriod validityPeriod() { return new BaseValidityPeriodClosed(ZonedDateTime.now(), ZonedDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())); // $NON-NLS-1$ } - private VersionMatch versionMatch() { + protected VersionMatch versionMatch() { return new BaseVersionMatch("version", new MatchingRuleDefault()); //$NON-NLS-1$ } diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/ParsedRequestTest.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/ParsedRequestTest.java index bd5d982f0..b1798034c 100644 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/ParsedRequestTest.java +++ b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/ParsedRequestTest.java @@ -19,9 +19,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.eclipse.passage.lbc.api.tests.FakeLicensingRequest; import org.eclipse.passage.lbc.internal.api.BackendLicensingRequest; import org.eclipse.passage.lbc.internal.api.ProductLicensesRequest; +import org.eclipse.passage.lbc.internal.base.BaseLicensingRequest; import org.eclipse.passage.lbc.internal.base.ParsedMiningRequest; import org.junit.Test; @@ -29,7 +29,8 @@ public final class ParsedRequestTest extends LbcTestsBase { @Test public void positive() { - BackendLicensingRequest request = new FakeLicensingRequest(params()); + BackendLicensingRequest request = new BaseLicensingRequest<>(params(), this::requestParameter, + this::requestBody); ProductLicensesRequest miningRequest = Stream.of(request).map(new ParsedMiningRequest()) .collect(Collectors.toList()).get(0); assertEquals(userValue(), miningRequest.requester().hardware()); @@ -38,8 +39,11 @@ public void positive() { } private Map params() { + // FIXME: we should use recent values: + // http://localhost:8090/?licensing.product.identifier=fake-product&licensing.product.version=0.1.27&action=acquire&licensing.content.type=application/xml&user=some_user@some_mail.se&server.auth.type=some_env&server.auth.expression=some_expression Map params = new HashMap<>(); params.put("user", userValue()); //$NON-NLS-1$ + params.put("hardware", userValue()); //$NON-NLS-1$ params.put("licensing.product.identifier", identifierValue()); //$NON-NLS-1$ params.put("licensing.product.version", versionValue()); //$NON-NLS-1$ return params; diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/RequestTest.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/RequestTest.java index fc18b0b4e..6c1fac814 100644 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/RequestTest.java +++ b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/base/tests/RequestTest.java @@ -13,14 +13,10 @@ package org.eclipse.passage.lbc.base.tests; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import java.io.IOException; import java.util.HashMap; import java.util.Map; -import javax.servlet.http.HttpServletRequest; - import org.eclipse.passage.lbc.internal.api.BackendLicensingRequest; import org.eclipse.passage.lbc.internal.base.BaseLicensingRequest; import org.junit.Test; @@ -37,16 +33,11 @@ private Map params() { @Test public void positiveTransition() { - HttpServletRequest httpRequest = new FakeHttpRequest(params()); - BackendLicensingRequest request; - try { - request = new BaseLicensingRequest(httpRequest); - assertEquals("action", request.parameter("action")); //$NON-NLS-1$ //$NON-NLS-2$ - assertEquals("value", request.parameter("key")); //$NON-NLS-1$ //$NON-NLS-2$ - assertEquals(userValue(), request.requester().hardware()); - } catch (IOException e) { - fail(); - } + BackendLicensingRequest request = new BaseLicensingRequest<>(params(), this::requestParameter, + this::requestBody); + assertEquals("action", request.parameter("action")); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("value", request.parameter("key")); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals(userValue(), request.requester().hardware()); } } diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/chains/tests/ChainTestsBase.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/chains/tests/ChainTestsBase.java index 66b3c7a5d..470a89c95 100644 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/chains/tests/ChainTestsBase.java +++ b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/chains/tests/ChainTestsBase.java @@ -12,108 +12,33 @@ *******************************************************************************/ package org.eclipse.passage.lbc.internal.base.chains.tests; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.Collections; +import java.util.HashMap; -import org.eclipse.passage.lbc.api.tests.FakeLicensingRequest; -import org.eclipse.passage.lbc.internal.api.Requester; -import org.eclipse.passage.lbc.internal.api.persistence.BoundLicense; -import org.eclipse.passage.lbc.internal.base.BaseBoundLicense; -import org.eclipse.passage.lbc.internal.base.BaseRequester; -import org.eclipse.passage.lbc.internal.base.ConditionIdentifier; -import org.eclipse.passage.lbc.internal.base.LicenseCapacity; -import org.eclipse.passage.lbc.internal.base.LicenseTaken; +import org.eclipse.passage.lbc.base.tests.LbcTestsBase; +import org.eclipse.passage.lbc.internal.api.BackendLicensingRequest; +import org.eclipse.passage.lbc.internal.base.BaseLicensingRequest; import org.eclipse.passage.lbc.json.JsonSerialization; -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.EvaluationType; -import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriod; -import org.eclipse.passage.lic.internal.api.conditions.VersionMatch; -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.RestrictionLevel; -import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult; -import org.eclipse.passage.lic.internal.base.conditions.BaseCondition; -import org.eclipse.passage.lic.internal.base.conditions.BaseEvaluationInstructions; -import org.eclipse.passage.lic.internal.base.conditions.BaseValidityPeriodClosed; -import org.eclipse.passage.lic.internal.base.conditions.BaseVersionMatch; -import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleDefault; -import org.eclipse.passage.lic.internal.base.conditions.evaluation.BasePermission; -import org.eclipse.passage.lic.internal.base.requirements.BaseFeature; -import org.eclipse.passage.lic.internal.base.requirements.BaseRequirement; -import org.eclipse.passage.lic.internal.base.restrictions.BaseExaminationCertificate; @SuppressWarnings("restriction") -public abstract class ChainTestsBase { +public abstract class ChainTestsBase extends LbcTestsBase { - protected LicensedProduct product() { - return new BaseLicensedProduct(identifierValue(), versionValue()); - } - - protected Requester requester() { - return new BaseRequester("process", "hardware", "feature"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - } - - protected String identifierValue() { - return "identifier"; //$NON-NLS-1$ - } - - protected String featureValue() { - return "feature"; //$NON-NLS-1$ - } - - protected String versionValue() { - return "version"; //$NON-NLS-1$ - } - - protected FakeLicensingRequest certificateRequest() { - return new FakeLicensingRequest(Collections.emptyMap(), new JsonSerialization() + protected BackendLicensingRequest certificateRequest() { + return requestForBody(new JsonSerialization()// .apply(new BaseServiceInvocationResult<>(certificate())).data().get()); } - protected FakeLicensingRequest conditionRequest() { - return new FakeLicensingRequest(Collections.emptyMap(), - new JsonSerialization().apply(new BaseServiceInvocationResult<>(condition())).data().get()); - } - - protected ExaminationCertificate certificate() { - return new BaseExaminationCertificate(Collections.singletonMap(requirement(), permission()), - Collections.emptyList()); - } - - private Permission permission() { - return new BasePermission(product(), condition(), ZonedDateTime.now(), ZonedDateTime.now().plusDays(2)); - } - - private Requirement requirement() { - return new BaseRequirement(new BaseFeature(identifierValue(), versionValue(), featureValue(), "provider"), //$NON-NLS-1$ - new RestrictionLevel.Info(), new Object()); - } - - protected BoundLicense boundLicense(int taken, int capacity) { - return new BaseBoundLicense(new ConditionIdentifier(identifierValue()), new LicenseTaken(key -> taken), - new LicenseCapacity(key -> capacity)); - } - - protected Condition condition() { - return new BaseCondition(identifierValue(), featureValue(), versionMatch(), validityPeriod(), - evaluationInstructions()); - } - - private BaseEvaluationInstructions evaluationInstructions() { - return new BaseEvaluationInstructions(new EvaluationType.Hardware(), "expression"); //$NON-NLS-1$ - } - - private ValidityPeriod validityPeriod() { - return new BaseValidityPeriodClosed(ZonedDateTime.now(), - ZonedDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())); // $NON-NLS-1$ + protected BackendLicensingRequest conditionRequest() { + return requestForBody(new JsonSerialization()// + .apply(new BaseServiceInvocationResult<>(condition())).data().get()); } - private VersionMatch versionMatch() { - return new BaseVersionMatch("version", new MatchingRuleDefault()); //$NON-NLS-1$ + protected BackendLicensingRequest requestForBody(String body) { + HashMap map = new HashMap(); + map.put("body", body); //$NON-NLS-1$ + return new BaseLicensingRequest<>(map, this::requestParameter, this::requestBody); } } diff --git a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/tests/BaseRequestDispatcherTest.java b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/tests/BaseRequestDispatcherTest.java index f2c688ecd..3dd3cb604 100644 --- a/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/tests/BaseRequestDispatcherTest.java +++ b/tests/org.eclipse.passage.lbc.base.tests/src/org/eclipse/passage/lbc/internal/base/tests/BaseRequestDispatcherTest.java @@ -17,18 +17,21 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; +import java.util.Map; -import org.eclipse.passage.lbc.api.tests.FakeLicensingRequest; import org.eclipse.passage.lbc.api.tests.FakeLicensingResponse; +import org.eclipse.passage.lbc.base.tests.LbcTestsBase; +import org.eclipse.passage.lbc.internal.base.BaseLicensingRequest; import org.eclipse.passage.lbc.internal.base.BaseRequestDispatcher; import org.junit.Test; -public class BaseRequestDispatcherTest { +public class BaseRequestDispatcherTest extends LbcTestsBase { @Test public void nullAction() throws IOException { BaseRequestDispatcher dispatcher = new BaseRequestDispatcher(new HashMap<>()); - FakeLicensingRequest request = new FakeLicensingRequest(new HashMap<>()); + BaseLicensingRequest> request = new BaseLicensingRequest<>(new HashMap<>(), + this::requestParameter, this::requestBody); String expected = "{\"error\":\"unsupported action null\"}"; //$NON-NLS-1$ assertEquals(expected, dispatch(dispatcher, request)); } @@ -36,12 +39,14 @@ public void nullAction() throws IOException { @Test public void unknownAction() throws IOException { BaseRequestDispatcher dispatcher = new BaseRequestDispatcher(new HashMap<>()); - FakeLicensingRequest request = new FakeLicensingRequest(Collections.singletonMap("action", "do-it-now")); //$NON-NLS-1$ //$NON-NLS-2$ + BaseLicensingRequest> request = new BaseLicensingRequest<>( + Collections.singletonMap("action", "do-it-now"), this::requestParameter, this::requestBody); //$NON-NLS-1$ //$NON-NLS-2$ String expected = "{\"error\":\"unsupported action do-it-now\"}"; //$NON-NLS-1$ assertEquals(expected, dispatch(dispatcher, request)); } - private String dispatch(BaseRequestDispatcher dispatcher, FakeLicensingRequest request) throws IOException { + private String dispatch(BaseRequestDispatcher dispatcher, BaseLicensingRequest> request) + throws IOException { FakeLicensingResponse result = new FakeLicensingResponse(); dispatcher.dispatch(request, result); return result.toString(); diff --git a/tests/org.eclipse.passage.lic.hc.tests/src/org/eclipse/passage/lic/internal/hc/tests/RemoteConditionsRequestTest.java b/tests/org.eclipse.passage.lic.hc.tests/src/org/eclipse/passage/lic/internal/hc/tests/RemoteConditionsRequestTest.java index 20c841e99..78ad2d570 100644 --- a/tests/org.eclipse.passage.lic.hc.tests/src/org/eclipse/passage/lic/internal/hc/tests/RemoteConditionsRequestTest.java +++ b/tests/org.eclipse.passage.lic.hc.tests/src/org/eclipse/passage/lic/internal/hc/tests/RemoteConditionsRequestTest.java @@ -55,6 +55,7 @@ public final class RemoteConditionsRequestTest { @Test public void urlContainsAllParameters() throws IOException { URL url = url(); + System.out.println(url); assertEquals(host, url.getHost()); assertEquals(port, url.getPort()); assertNotNull(url.getQuery());