Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit fe32de4

Browse files
authored
Make post processors generic and be able to chain them (#163)
1 parent 266584b commit fe32de4

File tree

11 files changed

+72
-27
lines changed

11 files changed

+72
-27
lines changed

samples/complete/src/main/java/com/github/fonimus/ssh/shell/complete/DemoConfiguration.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
import org.springframework.scheduling.annotation.SchedulingConfigurer;
3030
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
3131
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
32+
import org.springframework.util.CollectionUtils;
3233

3334
import javax.sql.DataSource;
35+
import java.time.LocalDateTime;
36+
import java.time.ZoneId;
37+
import java.time.ZonedDateTime;
3438
import java.util.List;
3539

3640
/**
@@ -47,17 +51,53 @@ public DemoConfiguration(TasksCommand tasksCommand) {
4751
}
4852

4953
@Bean
50-
public PostProcessor<String> quotePostProcessor() {
51-
return new PostProcessor<String>() {
54+
public PostProcessor<String, String> quotePostProcessor() {
55+
return new PostProcessor<String, String>() {
5256

5357
@Override
5458
public String getName() {
5559
return "quote";
5660
}
5761

5862
@Override
59-
public String process(String result, List<String> parameters) {
60-
return "'" + result + "'";
63+
public String process(String input, List<String> parameters) {
64+
return "'" + input + "'";
65+
}
66+
};
67+
}
68+
69+
@Bean
70+
public PostProcessor<String, ZonedDateTime> datePostProcessor() {
71+
return new PostProcessor<String, ZonedDateTime>() {
72+
73+
@Override
74+
public String getName() {
75+
return "date";
76+
}
77+
78+
@Override
79+
public ZonedDateTime process(String input, List<String> parameters) {
80+
return ZonedDateTime.parse(input);
81+
}
82+
};
83+
}
84+
85+
@Bean
86+
public PostProcessor<ZonedDateTime, ZonedDateTime> uctPostProcessor() {
87+
return new PostProcessor<ZonedDateTime, ZonedDateTime>() {
88+
89+
@Override
90+
public String getName() {
91+
return "zoned";
92+
}
93+
94+
@Override
95+
public ZonedDateTime process(ZonedDateTime input, List<String> parameters) {
96+
String zone = "UTC";
97+
if (!CollectionUtils.isEmpty(parameters)) {
98+
zone = parameters.get(0);
99+
}
100+
return input.withZoneSameInstant(ZoneId.of(zone));
61101
}
62102
};
63103
}

starter/src/main/java/com/github/fonimus/ssh/shell/ExtendedShell.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
public class ExtendedShell
4747
extends Shell {
4848

49+
@SuppressWarnings("rawtypes")
4950
private final ResultHandler resultHandler;
5051

5152
private final List<String> postProcessorNames = new ArrayList<>();
@@ -56,7 +57,7 @@ public class ExtendedShell
5657
* @param resultHandler result handler
5758
* @param postProcessors post processors list
5859
*/
59-
public ExtendedShell(ResultHandler resultHandler, List<PostProcessor> postProcessors) {
60+
public ExtendedShell(ResultHandler resultHandler, List<PostProcessor<?,?>> postProcessors) {
6061
super(resultHandler);
6162
this.resultHandler = resultHandler;
6263
if (postProcessors != null) {

starter/src/main/java/com/github/fonimus/ssh/shell/SshShellAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void init() {
138138

139139
@Bean
140140
@Primary
141-
public ExtendedShell sshShell(@Qualifier("main") ResultHandler<Object> resultHandler, List<PostProcessor> postProcessors) {
141+
public ExtendedShell sshShell(@Qualifier("main") ResultHandler<Object> resultHandler, List<PostProcessor<?,?>> postProcessors) {
142142
return new ExtendedShell(new TypePostProcessorResultHandler(resultHandler, postProcessors), postProcessors);
143143
}
144144

starter/src/main/java/com/github/fonimus/ssh/shell/commands/PostProcessorsCommand.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public class PostProcessorsCommand extends AbstractCommand {
4646
public static final String GROUP = "postprocessors";
4747
public static final String COMMAND_POST_PROCESSORS = "postprocessors";
4848

49-
private final List<PostProcessor> postProcessors;
49+
private final List<PostProcessor<?, ?>> postProcessors;
5050

5151
public PostProcessorsCommand(SshShellHelper helper, SshShellProperties properties,
52-
List<PostProcessor> postProcessors) {
52+
List<PostProcessor<?, ?>> postProcessors) {
5353
super(helper, properties, properties.getCommands().getPostprocessors());
5454
this.postProcessors = new ArrayList<>(postProcessors);
5555
this.postProcessors.sort(Comparator.comparing(PostProcessor::getName));
@@ -60,11 +60,14 @@ public PostProcessorsCommand(SshShellHelper helper, SshShellProperties propertie
6060
public CharSequence postprocessors() {
6161
AttributedStringBuilder result = new AttributedStringBuilder();
6262
result.append("Available Post-Processors\n\n", AttributedStyle.BOLD);
63-
for (PostProcessor postProcessor : postProcessors) {
64-
result.append("\t" + postProcessor.getName() + ": ", AttributedStyle.BOLD);
65-
Class<?> cls =
66-
((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
67-
result.append(cls.getName() + "\n", AttributedStyle.DEFAULT);
63+
for (PostProcessor<?, ?> postProcessor : postProcessors) {
64+
result.append("\t" + postProcessor.getName() + ":\n", AttributedStyle.BOLD);
65+
Class<?> input =
66+
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
67+
Class<?> output =
68+
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[1]);
69+
result.append("\t\tinput : " + input.getName() + "\n", AttributedStyle.DEFAULT);
70+
result.append("\t\toutput : " + output.getName() + "\n", AttributedStyle.DEFAULT);
6871
}
6972

7073
return result;

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/PostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
/**
2222
* Post processor interface
2323
*/
24-
public interface PostProcessor<T> {
24+
public interface PostProcessor<I, O> {
2525

2626
String getName();
2727

28-
String process(T result, List<String> parameters) throws PostProcessorException;
28+
O process(I result, List<String> parameters) throws PostProcessorException;
2929
}

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/TypePostProcessorResultHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public class TypePostProcessorResultHandler
3535

3636
public static final ThreadLocal<Throwable> THREAD_CONTEXT = ThreadLocal.withInitial(() -> null);
3737

38-
private ResultHandler<Object> resultHandler;
38+
private final ResultHandler<Object> resultHandler;
3939

40-
private Map<String, PostProcessor> postProcessorMap = new HashMap<>();
40+
private final Map<String, PostProcessor<?,?>> postProcessorMap = new HashMap<>();
4141

42-
public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<PostProcessor> postProcessorList) {
42+
public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<PostProcessor<?,?>> postProcessorList) {
4343
this.resultHandler = resultHandler;
4444
if (postProcessorList != null) {
45-
for (PostProcessor postProcessor : postProcessorList) {
45+
for (PostProcessor<?,?> postProcessor : postProcessorList) {
4646
if (this.postProcessorMap.containsKey(postProcessor.getName())) {
4747
LOGGER.warn("Unable to register post processor for name [{}], it has already been registered",
4848
postProcessor.getName());
@@ -54,6 +54,7 @@ public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<
5454
}
5555
}
5656

57+
@SuppressWarnings({"rawtypes", "unchecked"})
5758
@Override
5859
public void handleResult(Object result) {
5960
if (result == null) {
@@ -73,7 +74,7 @@ public void handleResult(Object result) {
7374
continue;
7475
}
7576
Class<?> cls =
76-
((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
77+
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
7778
if (!cls.isAssignableFrom(obj.getClass())) {
7879
printLogWarn("Post processor [" + name + "] can only apply to class [" + cls.getName() +
7980
"] (current object class is " + obj.getClass().getName() + ")");
@@ -82,7 +83,7 @@ public void handleResult(Object result) {
8283
postProcessorObject.getParameters());
8384
try {
8485
obj = postProcessor.process(obj, postProcessorObject.getParameters());
85-
} catch (PostProcessorException e) {
86+
} catch (Exception e) {
8687
printError(e.getMessage());
8788
return;
8889
}

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/provided/GrepPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
@Slf4j
2828
public class GrepPostProcessor
29-
implements PostProcessor<String> {
29+
implements PostProcessor<String, String> {
3030

3131
@Override
3232
public String getName() {
@@ -45,7 +45,7 @@ public String process(String result, List<String> parameters) {
4545
sb.append(line).append("\n");
4646
}
4747
}
48-
return sb.toString().isEmpty() ? sb.toString() : sb.toString().substring(0, sb.toString().length() - 1);
48+
return sb.toString().isEmpty() ? sb.toString() : sb.substring(0, sb.toString().length() - 1);
4949
}
5050
}
5151

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/provided/HighlightPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
@Slf4j
3030
public class HighlightPostProcessor
31-
implements PostProcessor<String> {
31+
implements PostProcessor<String, String> {
3232

3333
@Override
3434
public String getName() {

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/provided/JsonPointerPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
@Slf4j
3131
public class JsonPointerPostProcessor
32-
implements PostProcessor<String> {
32+
implements PostProcessor<String, String> {
3333

3434
private static final ObjectMapper MAPPER = new ObjectMapper();
3535

starter/src/main/java/com/github/fonimus/ssh/shell/postprocess/provided/PrettyJsonPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
@Slf4j
3131
public class PrettyJsonPostProcessor
32-
implements PostProcessor<Object> {
32+
implements PostProcessor<Object, String> {
3333

3434
private static final ObjectMapper MAPPER = new ObjectMapper();
3535

0 commit comments

Comments
 (0)