Skip to content

Commit

Permalink
Protocol util - add option to dump the content to a tmp file
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Nioche <[email protected]>
  • Loading branch information
jnioche committed Sep 26, 2023
1 parent bc21ebf commit c630e61
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import crawlercommons.robots.BaseRobotRules;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -31,6 +33,7 @@
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.Config;
import org.apache.storm.utils.Utils;
Expand Down Expand Up @@ -205,6 +208,7 @@ protected static void main(AbstractHttpProtocol protocol, String[] args) throws

Options options = new Options();
options.addOption("f", true, "configuration file");
options.addOption("b", false, "dump binary content to temp file");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
Expand All @@ -214,6 +218,8 @@ protected static void main(AbstractHttpProtocol protocol, String[] args) throws
ConfUtils.loadConf(confFile, conf);
}

boolean binary = cmd.hasOption("b");

protocol.configure(conf);

Set<Runnable> threads = new HashSet<>();
Expand Down Expand Up @@ -258,7 +264,14 @@ public void run() {
.append(response.getContent().length)
.append("\n");
long timeFetching = System.currentTimeMillis() - start;
stringB.append("fetched in : ").append(timeFetching).append(" msec");
stringB.append("fetched in : ").append(timeFetching).append(" msec\n");

if (binary) {
Path p = Files.createTempFile("sc-protocol-", ".dump");
FileUtils.writeByteArrayToFile(p.toFile(), response.getContent());
stringB.append("dumped content to : ").append(p);
}

System.out.println(stringB);
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit c630e61

Please sign in to comment.