Skip to content

Commit

Permalink
Merge pull request #534 from eclipse-passage/568632
Browse files Browse the repository at this point in the history
Bug 568632 Implement feature grant acquire/release
  • Loading branch information
ruspl-afed authored Nov 9, 2020
2 parents 8933b21 + 1529b23 commit 54bf806
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 24 deletions.
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

0 comments on commit 54bf806

Please sign in to comment.