Skip to content

Commit

Permalink
chore: Partial data-extract implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Feb 13, 2024
1 parent da6fc5c commit b867d72
Show file tree
Hide file tree
Showing 20 changed files with 1,555 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,8 @@ private final void writeRecords(IRecordWriter recordWriter, HttpRequest<?> httpR
* @param httpRequest
* @param nextPageRequestProducer
*/
private final void writeRecords(IRecordWriter recordWriter, HttpRequest<?> originalRequest, INextPageRequestProducer nextPageRequestProducer) {
var currentRequest = originalRequest;
while ( currentRequest!=null ) {
HttpResponse<JsonNode> response = currentRequest.asObject(JsonNode.class);
writeRecords(recordWriter, response);
currentRequest = nextPageRequestProducer.getNextPageRequest(originalRequest, response);
}
private final void writeRecords(IRecordWriter recordWriter, HttpRequest<?> initialRequest, INextPageRequestProducer nextPageRequestProducer) {
PagingHelper.processPages(initialRequest, nextPageRequestProducer, r->writeRecords(recordWriter, r));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package com.fortify.cli.common.rest.paging;

import java.util.function.Consumer;

import com.fasterxml.jackson.databind.JsonNode;

import kong.unirest.Header;
Expand Down Expand Up @@ -61,6 +63,23 @@ public static final INextPageRequestProducer asNextPageRequestProducer(UnirestIn
return unirest==null || nextPageUrlProducer==null ? null : new NextPageRequestProducer(unirest, nextPageUrlProducer);
}

public static final void processPages(UnirestInstance unirest, HttpRequest<?> initialRequest, INextPageUrlProducer nextPageUrlProducer, Consumer<HttpResponse<JsonNode>> consumer) {
var nextPageRequestProducer = asNextPageRequestProducer(unirest, nextPageUrlProducer);
if ( nextPageRequestProducer==null ) {
throw new IllegalStateException("Cannot process pages without a valid NextPageRequestProducer");
}
processPages(initialRequest, nextPageRequestProducer, consumer);
}

public static final void processPages(HttpRequest<?> initialRequest, INextPageRequestProducer nextPageRequestProducer, Consumer<HttpResponse<JsonNode>> consumer) {
var currentRequest = initialRequest;
while ( currentRequest!=null ) {
HttpResponse<JsonNode> response = currentRequest.asObject(JsonNode.class);
consumer.accept(response);
currentRequest = nextPageRequestProducer.getNextPageRequest(initialRequest, response);
}
}

@RequiredArgsConstructor
private static final class NextPageRequestProducer implements INextPageRequestProducer {
private final UnirestInstance unirest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@

import java.lang.reflect.Method;

import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import com.fortify.cli.common.spring.expression.wrapper.SimpleExpression;
import com.fortify.cli.common.spring.expression.wrapper.TemplateExpression;

public final class SpelHelper {
private static final SpelExpressionParser parser = new SpelExpressionParser();
private static final TemplateParserContext templateContext = new TemplateParserContext("${","}");

public static final SimpleExpression parseSimpleExpression(String s) {
return new SimpleExpression(parser.parseExpression(s));
}
public static final TemplateExpression parseTemplateExpression(String s) {
return new TemplateExpression(parser.parseExpression(s, templateContext));
}
public static final void registerFunctions(SimpleEvaluationContext context, Class<?> clazz) {
for ( Method m : clazz.getDeclaredMethods() ) {
context.setVariable(m.getName(), m);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* (c) Copyright 2020 Micro Focus or one of its affiliates, a Micro Focus company
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.spring.expression.wrapper;

import org.springframework.expression.Expression;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
* <p>This is a simple wrapper class for a Spring {@link Expression}
* instance. It's main use is in combination with
* {@link SimpleExpressionDeserializer} to allow automatic
* conversion from String values to simple {@link Expression}
* instances in JSON/YAML documents.</p>
*
* <p>The reason for needing this wrapper class is to differentiate
* with templated {@link Expression} instances that are handled
* by {@link TemplateExpressionDeserializer}.</p>
*/
@JsonDeserialize(using = SimpleExpressionDeserializer.class)
public class SimpleExpression extends WrappedExpression {
public SimpleExpression(Expression target) {
super(target);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* (c) Copyright 2020 Micro Focus or one of its affiliates, a Micro Focus company
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.spring.expression.wrapper;

import java.io.IOException;

import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

/**
* This Jackson deserializer allows parsing String values into an
* SpEL Expression object.
*/
@Component
public final class SimpleExpressionDeserializer extends StdDeserializer<SimpleExpression> {
private static final long serialVersionUID = 1L;
private static final SpelExpressionParser parser = new SpelExpressionParser();
public SimpleExpressionDeserializer() { this(null); }
public SimpleExpressionDeserializer(Class<?> vc) { super(vc); }

@Override
public SimpleExpression deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
return node==null || node.isNull() ? null : new SimpleExpression(parser.parseExpression(node.asText()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* (c) Copyright 2020 Micro Focus or one of its affiliates, a Micro Focus company
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.spring.expression.wrapper;

import org.springframework.expression.Expression;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
* <p>This is a simple wrapper class for a Spring {@link Expression}
* instance. It's main use is in combination with
* {@link TemplateExpressionDeserializer} to allow automatic
* conversion from String values to templated {@link Expression}
* instances.</p>
*
* <p>The reason for needing this wrapper class is to differentiate
* with non-templated {@link Expression} instances that are
* handled by {@link SimpleExpressionDeserializer}.</p>
*/
@JsonDeserialize(using = TemplateExpressionDeserializer.class)
public class TemplateExpression extends WrappedExpression {
public TemplateExpression(Expression target) {
super(target);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* (c) Copyright 2020 Micro Focus or one of its affiliates, a Micro Focus company
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.spring.expression.wrapper;

import java.io.IOException;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fortify.cli.common.spring.expression.SpelHelper;

/**
* This {@link PropertyEditor} allows parsing String values into a
* TemplateExpression object.
*/
@Component
public final class TemplateExpressionDeserializer extends StdDeserializer<TemplateExpression> {
private static final long serialVersionUID = 1L;

public TemplateExpressionDeserializer() { this(null); }
public TemplateExpressionDeserializer(Class<?> vc) { super(vc); }

@Override
public TemplateExpression deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
return node==null || node.isNull() ? null : SpelHelper.parseTemplateExpression(node.asText());
}
}
Loading

0 comments on commit b867d72

Please sign in to comment.