use of org.openrdf.query.parser.sparql.ast.TokenMgrError in project QueryAnalysis by Wikidata.
the class OutputHandlerAnonymizer method writeLine.
@Override
public void writeLine(String queryToAnalyze, Validity validityStatus, String userAgent, String timeStamp, long currentLine, int currentDay, String currentFile) throws IOException {
List<Object> line = new ArrayList<>();
QueryHandler queryHandler = queryHandlerFactory.getQueryHandler(validityStatus, currentLine, currentDay, queryToAnalyze, userAgent, currentFile, threadNumber);
if (queryHandler.getValidityStatus().equals(QueryHandler.Validity.VALID)) {
ASTQueryContainer qc;
try {
qc = SyntaxTreeBuilder.parseQuery(queryToAnalyze);
} catch (TokenMgrError | ParseException e) {
logger.error("Failed to parse the query although it was found valid - this is a serious bug.", e);
return;
}
try {
StandardizingSPARQLParser.debug(qc);
StringEscapesProcessor.process(qc);
BaseDeclProcessor.process(qc, OpenRDFQueryHandler.BASE_URI);
StandardizingPrefixDeclProcessor.process(qc);
StandardizingSPARQLParser.anonymize(qc);
} catch (MalformedQueryException e) {
logger.error("Failed to debug or anonymize query. " + queryToAnalyze);
}
String renderedQueryString;
try {
renderedQueryString = qc.jjtAccept(new RenderVisitor(), "").toString();
} catch (VisitorException e) {
logger.error("Failed to render the query.", e);
return;
}
try {
new StandardizingSPARQLParser().parseQuery(renderedQueryString, OpenRDFQueryHandler.BASE_URI);
} catch (MalformedQueryException e) {
String queryName = this.threadNumber + "_" + this.failedQueriesNumber + ".query";
logger.error("Anonymized query was not valid anymore. " + queryName, e);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.outputFile.substring(0, this.outputFile.lastIndexOf("/")) + "failedQueriesFolder/" + queryName))) {
bw.write(queryToAnalyze);
this.failedQueriesNumber++;
} catch (IOException i) {
logger.error("Could not write the failed query to failed queries folder.", i);
}
return;
} catch (ClassCastException e) {
logger.error("Unexpected class cast exception after anonymization.", e);
}
String encodedRenderedQueryString;
try {
encodedRenderedQueryString = URLEncoder.encode(renderedQueryString, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("Apparently this system does not support UTF-8. Please fix this before running the program again.");
return;
}
line.add(encodedRenderedQueryString);
line.add(timeStamp);
if (queryHandler.getSourceCategory().equals(QueryHandler.SourceCategory.USER)) {
line.add("organic");
} else {
line.add("robotic");
}
if (QueryHandler.isOrganicUserAgent(queryHandler.getUserAgent())) {
line.add("browser");
} else if (Anonymizer.allowedToolNames.contains(queryHandler.getToolName())) {
line.add(queryHandler.getToolName());
} else if (Anonymizer.allowedUserAgents.containsKey(queryHandler.getUserAgent())) {
line.add(Anonymizer.allowedUserAgents.get(queryHandler.getUserAgent()));
} else {
line.add("other");
}
csvPrinter.printRecord(line);
}
}
Aggregations