Skip to content

Commit

Permalink
chore: Refactor IUnirestInstanceSupplier/IProductHelper/AbstractRestC…
Browse files Browse the repository at this point in the history
…allCommand implementations

refactor: Separate IProductHelper/IUnirestInstanceSupplier implementations. Previously, all
  IProductHelper mixin implementations also implemented IUnirestInstanceSupplier, this is now
  changed to have dedicated mixins implementing IUnirestInstanceSupplier, and non-mixin classes
  implementing IProductHelper. This is cleaner, and allows for using multiple product helpers
  within a single command implementation (as required for upcoming data extraction commands).

refactor: Improve AbstractRestCallCommand, simplify AbstractRestCallCommand implementations by
  re-using updated IProductHelper implementations.
  • Loading branch information
rsenden committed Feb 9, 2024
1 parent 13ea96d commit da6fc5c
Show file tree
Hide file tree
Showing 97 changed files with 242 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ protected final void addInputTransformersForCommand(StandardOutputConfig standar
for ( var mixin : commandHelper.getCommandSpec().mixins().values() ) {
addInputTransformersFromObject(standardOutputConfig, mixin.userObject());
}
addInputTransformersFromObject(standardOutputConfig, getProductHelper());
addInputTransformersFromObject(standardOutputConfig, cmd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.json.JsonHelper;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.cli.cmd.IBaseRequestSupplier;
import com.fortify.cli.common.output.product.IProductHelperSupplier;
import com.fortify.cli.common.output.product.IProductHelper;
import com.fortify.cli.common.output.transform.IInputTransformer;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.common.rest.paging.INextPageUrlProducer;
import com.fortify.cli.common.rest.paging.INextPageUrlProducerSupplier;
import com.fortify.cli.common.rest.unirest.IUnirestInstanceSupplier;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.JavaHelper;
import com.fortify.cli.common.util.DisableTest.TestType;
import com.fortify.cli.common.util.StringUtils;

Expand All @@ -41,17 +43,11 @@

/**
* Abstract base class for 'fcli <product> rest call' commands. Concrete implementations must
* implement the various abstract methods. As dictated by the {@link IProductHelperSupplier},
* implementations must implement the {@link IProductHelperSupplier#getProductHelper()} method,
* but please note that the provided product helper may not implement any of the
* {@link IInputTransformer}, {@link IRecordTransformer}, {@link INextPageUrlProducerSupplier}
* or {@link INextPageUrlProducer} as this would break the --no-transform and --no-paging options.
* Instead, subclasses should implement the corresponding _* methods defined in this class,
* to enable/disable paging and transformations on demand.
* implement the various abstract methods.
*
* @author Ruud Senden
*/
public abstract class AbstractRestCallCommand extends AbstractOutputCommand implements IBaseRequestSupplier, IProductHelperSupplier, IInputTransformer, IRecordTransformer, INextPageUrlProducerSupplier {
public abstract class AbstractRestCallCommand extends AbstractOutputCommand implements IBaseRequestSupplier, IUnirestInstanceSupplier, IInputTransformer, IRecordTransformer, INextPageUrlProducerSupplier {
@EnvSuffix("URI") @Parameters(index = "0", arity = "1..1", descriptionKey = "api.uri") String uri;

@Option(names = {"--request", "-X"}, required = false, defaultValue = "GET")
Expand All @@ -77,13 +73,17 @@ private static class TransformArgGroup {

@Override
public HttpRequest<?> getBaseRequest() {
if ( getProductHelper() instanceof IUnirestInstanceSupplier ) {
UnirestInstance unirest = ((IUnirestInstanceSupplier)getProductHelper()).getUnirestInstance();
return prepareRequest(unirest);
}
throw new RuntimeException("Class doesn't implement IUnirestInstanceSupplier: "+getProductHelper().getClass().getName());
return prepareRequest(getUnirestInstance());
}

@Override
public final UnirestInstance getUnirestInstance() {
return getUnirestInstanceSupplier().getUnirestInstance();
}

protected abstract IUnirestInstanceSupplier getUnirestInstanceSupplier();
protected abstract IProductHelper getProductHelper();

@Override
public boolean isSingular() {
return false;
Expand Down Expand Up @@ -116,9 +116,15 @@ public final INextPageUrlProducer getNextPageUrlProducer() {
return result;
}

protected abstract JsonNode _transformRecord(JsonNode input);
protected abstract JsonNode _transformInput(JsonNode input);
protected abstract INextPageUrlProducer _getNextPageUrlProducer();
private final JsonNode _transformRecord(JsonNode input) {
return applyOnProductHelper(IRecordTransformer.class, t->t.transformRecord(input), input);
}
private final JsonNode _transformInput(JsonNode input) {
return applyOnProductHelper(IInputTransformer.class, t->t.transformInput(input), input);
}
private final INextPageUrlProducer _getNextPageUrlProducer() {
return applyOnProductHelper(INextPageUrlProducerSupplier.class, s->s.getNextPageUrlProducer(), null);
}

@SneakyThrows
protected final HttpRequest<?> prepareRequest(UnirestInstance unirest) {
Expand All @@ -139,4 +145,8 @@ protected final HttpRequest<?> prepareRequest(UnirestInstance unirest) {
return request;
}

private final <T,R> R applyOnProductHelper(Class<T> type, Function<T,R> f, R defaultValue) {
return JavaHelper.as(getProductHelper(), type).map(f).orElse(defaultValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.output.product.IProductHelperSupplier;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.output.writer.ISingularSupplier;
import com.fortify.cli.common.rest.cli.mixin.StandardWaitHelperProgressMonitorMixin;
Expand All @@ -28,7 +27,7 @@
import lombok.Getter;
import picocli.CommandLine.Mixin;

public abstract class AbstractWaitForCommand extends AbstractRunnableCommand implements IActionCommandResultSupplier, IProductHelperSupplier, ISingularSupplier, Runnable {
public abstract class AbstractWaitForCommand extends AbstractRunnableCommand implements IActionCommandResultSupplier, IUnirestInstanceSupplier, ISingularSupplier, Runnable {
@Getter @Mixin private OutputHelperMixins.WaitFor outputHelper;
@Mixin private WaitHelperControlPropertiesMixin controlProperties;
@Mixin private WaitHelperWaitTypeMixin waitTypeSupplier;
Expand All @@ -37,15 +36,16 @@ public abstract class AbstractWaitForCommand extends AbstractRunnableCommand imp
@Override
public void run() {
initMixins();
var productHelper = getProductHelper();
if ( productHelper instanceof IUnirestInstanceSupplier ) {
UnirestInstance unirest = ((IUnirestInstanceSupplier)productHelper).getUnirestInstance();
wait(unirest);
} else {
throw new RuntimeException("Class doesn't implement IUnirestInstanceSupplier: "+productHelper.getClass().getName());
}
wait(getUnirestInstance());
}

@Override
public final UnirestInstance getUnirestInstance() {
return getUnirestInstanceSupplier().getUnirestInstance();
}

protected abstract IUnirestInstanceSupplier getUnirestInstanceSupplier();

private void wait(UnirestInstance unirest) {
configure(unirest,
WaitHelper.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.cli;
package com.fortify.cli.fod._common.output.cli.cmd;

import com.fortify.cli.common.output.cli.cmd.IBaseRequestSupplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.cli;
package com.fortify.cli.fod._common.output.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.cli;
package com.fortify.cli.fod._common.output.cli.cmd;

import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.product.IProductHelperSupplier;
import com.fortify.cli.common.rest.unirest.IUnirestInstanceSupplier;
import com.fortify.cli.fod._common.output.mixin.FoDProductHelperStandardMixin;
import com.fortify.cli.fod._common.rest.helper.FoDProductHelper;
import com.fortify.cli.fod._common.session.cli.mixin.FoDUnirestInstanceSupplierMixin;

import kong.unirest.UnirestInstance;
import lombok.Getter;
Expand All @@ -24,9 +25,10 @@
public abstract class AbstractFoDOutputCommand extends AbstractOutputCommand
implements IProductHelperSupplier, IUnirestInstanceSupplier
{
@Getter @Mixin FoDProductHelperStandardMixin productHelper;
@Getter @Mixin private FoDUnirestInstanceSupplierMixin unirestInstanceSupplier;
@Getter private final FoDProductHelper productHelper = FoDProductHelper.INSTANCE;

public final UnirestInstance getUnirestInstance() {
return productHelper.getUnirestInstance();
return unirestInstanceSupplier.getUnirestInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.mixin;
package com.fortify.cli.fod._common.output.cli.mixin;

import com.fortify.cli.common.output.cli.mixin.IOutputHelper;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.mixin;
package com.fortify.cli.fod._common.rest.helper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.product.IProductHelper;
import com.fortify.cli.common.output.transform.IInputTransformer;
import com.fortify.cli.common.rest.paging.INextPageUrlProducer;
import com.fortify.cli.common.rest.paging.INextPageUrlProducerSupplier;
import com.fortify.cli.fod._common.rest.helper.FoDInputTransformer;
import com.fortify.cli.fod._common.rest.helper.FoDPagingHelper;

// IMPORTANT: When updating/adding any methods in this class, FoDRestCallCommand
// also likely needs to be updated
public class FoDProductHelperStandardMixin extends FoDProductHelperBasicMixin
implements IInputTransformer, INextPageUrlProducerSupplier
public class FoDProductHelper implements IProductHelper, IInputTransformer, INextPageUrlProducerSupplier
{
public static final FoDProductHelper INSTANCE = new FoDProductHelper();
private FoDProductHelper() {}
@Override
public INextPageUrlProducer getNextPageUrlProducer() {
return FoDPagingHelper.nextPageUrlProducer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod.release.cli.mixin.FoDReleaseByQualifiedNameOrIdResolverMixin;

import kong.unirest.UnirestInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.helper.FoDFileTransferHelper;
import com.fortify.cli.fod.release.cli.mixin.FoDReleaseByQualifiedNameOrIdResolverMixin;
import kong.unirest.HttpRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.rest.helper.FoDFileTransferHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDBaseRequestOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDBaseRequestOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.rest.helper.FoDFileTransferHelper;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDEntitlementFrequencyTypeMixins;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
import com.fortify.cli.fod.release.cli.mixin.FoDReleaseByQualifiedNameOrIdResolverMixin;
import com.fortify.cli.fod.release.helper.FoDReleaseDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.rest.cli.cmd.AbstractWaitForCommand;
import com.fortify.cli.common.rest.wait.WaitHelper.WaitHelperBuilder;
import com.fortify.cli.fod._common.output.mixin.FoDProductHelperStandardMixin;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanStatus;
import com.fortify.cli.fod._common.scan.helper.FoDScanStatus.FoDScanStatusIterable;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
import com.fortify.cli.fod._common.session.cli.mixin.FoDUnirestInstanceSupplierMixin;

import kong.unirest.UnirestInstance;
import lombok.Getter;
Expand All @@ -31,7 +31,7 @@

@CommandGroup("*-scan")
public abstract class AbstractFoDScanWaitForCommand extends AbstractWaitForCommand {
@Getter @Mixin FoDProductHelperStandardMixin productHelper;
@Getter @Mixin private FoDUnirestInstanceSupplierMixin unirestInstanceSupplier;
@Mixin private FoDScanResolverMixin.PositionalParameterMulti scansResolver;
@Option(names={"-s", "--any-state"}, required=true, split=",", defaultValue="Completed", completionCandidates = FoDScanStatusIterable.class)
private Set<String> states;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.fod._common.output.mixin;
package com.fortify.cli.fod._common.session.cli.mixin;

import org.apache.http.impl.client.HttpClientBuilder;

import com.fortify.cli.common.http.proxy.helper.ProxyHelper;
import com.fortify.cli.common.output.product.IProductHelper;
import com.fortify.cli.common.rest.unirest.config.UnirestJsonHeaderConfigurer;
import com.fortify.cli.common.rest.unirest.config.UnirestUnexpectedHttpResponseConfigurer;
import com.fortify.cli.common.rest.unirest.config.UnirestUrlConfigConfigurer;
Expand All @@ -28,8 +27,7 @@
import kong.unirest.UnirestInstance;
import kong.unirest.apache.ApacheClient;

public class FoDProductHelperBasicMixin extends AbstractSessionUnirestInstanceSupplierMixin<FoDSessionDescriptor>
implements IProductHelper
public final class FoDUnirestInstanceSupplierMixin extends AbstractSessionUnirestInstanceSupplierMixin<FoDSessionDescriptor>
{
@Override
protected final FoDSessionDescriptor getSessionDescriptor(String sessionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.common.variable.DefaultVariablePropertyName;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod.access_control.helper.FoDUserGroupCreateRequest;
import com.fortify.cli.fod.access_control.helper.FoDUserGroupHelper;
import com.fortify.cli.fod.access_control.helper.FoDUserHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.common.variable.DefaultVariablePropertyName;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.output.cli.cmd.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod.access_control.cli.mixin.FoDUserGroupResolverMixin;
import com.fortify.cli.fod.access_control.helper.FoDUserGroupDescriptor;
Expand Down
Loading

0 comments on commit da6fc5c

Please sign in to comment.