Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 568632 Implement feature grant acquire/release #534

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.remote;

import java.util.Collection;

import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.ConditionPack;

public interface Client<C> {
public interface Client<C, T> {

ServiceInvocationResult<Collection<ConditionPack>> remoteConditions(Request<C> request, ResponseHandler miner);
ServiceInvocationResult<T> request(Request<C> request, ResponseHandler<T> handler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.remote;

import java.util.Collection;

import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.conditions.ConditionPack;

public interface ResponseHandler {
public interface ResponseHandler<T> {

Collection<ConditionPack> read(byte[] raw, String contentType) throws LicensingException;
T read(byte[] raw, String contentType) throws LicensingException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.eclipse.passage.lic.internal.hc.i18n.HcMessages;
import org.eclipse.passage.lic.internal.hc.remote.ResponseHandler;

final class DecryptedConditions implements ResponseHandler {
final class DecryptedConditions implements ResponseHandler<Collection<ConditionPack>> {

private final ConditionTransportRegistry transports;
private final FloatingServerConnection coordinates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.ConditionPack;
import org.eclipse.passage.lic.internal.api.diagnostic.Trouble;
import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult;
import org.eclipse.passage.lic.internal.base.diagnostic.BaseDiagnostic;
Expand All @@ -28,14 +26,12 @@
import org.eclipse.passage.lic.internal.hc.remote.Request;
import org.eclipse.passage.lic.internal.hc.remote.ResponseHandler;

public final class HttpClient implements Client<HttpURLConnection> {
public final class HttpClient<T> implements Client<HttpURLConnection, T> {

@Override
public ServiceInvocationResult<Collection<ConditionPack>> remoteConditions(Request<HttpURLConnection> request,
ResponseHandler miner) {
public ServiceInvocationResult<T> request(Request<HttpURLConnection> request, ResponseHandler<T> handler) {
try {
return new BaseServiceInvocationResult<Collection<ConditionPack>>(
netConditions(connection(request), miner));
return new BaseServiceInvocationResult<T>(netResults(connection(request), handler));
} catch (Exception e) {
return new BaseServiceInvocationResult<>(//
new BaseDiagnostic(//
Expand All @@ -50,24 +46,23 @@ private HttpURLConnection connection(Request<HttpURLConnection> request) throws
return request.config().apply((HttpURLConnection) request.url().openConnection());
}

private Collection<ConditionPack> netConditions(HttpURLConnection connection, ResponseHandler miner)
throws Exception {
private T netResults(HttpURLConnection connection, ResponseHandler<T> handler) throws Exception {
// actual connection is happening on the first 'get' (get response code)
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
connection.getInputStream().close(); // close the connection
throw new RuntimeException(String.format(HcMessages.HttpClient_not_ok_response, //
connection.getResponseCode(), //
connection.getResponseMessage()));
}
return read(connection, miner);
return read(connection, handler);
}

private Collection<ConditionPack> read(HttpURLConnection connection, ResponseHandler miner) throws Exception {
private T read(HttpURLConnection connection, ResponseHandler<T> handler) throws Exception {
byte[] content = new byte[connection.getContentLength()];
try (InputStream source = connection.getInputStream()) {
source.read(content); // read all and close the connection briefly
}
return miner.read(content, connection.getHeaderField("Content-Type")); //$NON-NLS-1$
return handler.read(content, connection.getHeaderField("Content-Type")); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ServiceInvocationResult<Collection<ConditionPack>> all(LicensedProduct pr

private ServiceInvocationResult<Collection<ConditionPack>> conditions(LicensedProduct product,
FloatingLicenseAccess access) {
return new HttpClient().remoteConditions(//
return new HttpClient<Collection<ConditionPack>>().request(//
new RemoteConditionsRequest(product, access), //
new DecryptedConditions(transports, access.getServer()));
}
Expand Down