|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.node.ObjectNode; |
4 | 4 | import com.marklogic.client.DatabaseClient; |
5 | | -import com.marklogic.client.FailedRequestException; |
6 | 5 | import com.marklogic.client.eval.ServerEvaluationCall; |
7 | 6 | import com.marklogic.client.ext.file.DocumentFile; |
8 | 7 | import com.marklogic.client.ext.file.DocumentFileProcessor; |
9 | 8 | import com.marklogic.client.ext.helper.LoggingObject; |
10 | 9 | import com.marklogic.client.io.Format; |
11 | 10 | import com.marklogic.client.io.JacksonHandle; |
| 11 | +import com.marklogic.client.io.StringHandle; |
12 | 12 | import org.springframework.util.FileCopyUtils; |
13 | 13 |
|
14 | 14 | import java.io.File; |
@@ -75,48 +75,46 @@ protected void validateTdeTemplate(DocumentFile documentFile) { |
75 | 75 | logger.warn("Could not read TDE template from file, will not validate; cause: " + e.getMessage()); |
76 | 76 | } |
77 | 77 | if (fileContent != null) { |
78 | | - try { |
79 | | - ServerEvaluationCall call = null; |
80 | | - if (Format.XML.equals(documentFile.getFormat())) { |
81 | | - call = buildXqueryCall(documentFile, fileContent); |
82 | | - } else if (Format.JSON.equals(documentFile.getFormat())) { |
83 | | - call = buildJavascriptCall(documentFile, fileContent); |
84 | | - } else { |
85 | | - logger.info("Unrecognized file format, will not try to validate TDE template in file: " + file + "; format: " + documentFile.getFormat()); |
86 | | - } |
| 78 | + ServerEvaluationCall call = null; |
| 79 | + if (Format.XML.equals(documentFile.getFormat())) { |
| 80 | + call = buildXqueryCall(documentFile, fileContent); |
| 81 | + } else if (Format.JSON.equals(documentFile.getFormat())) { |
| 82 | + call = buildJavascriptCall(documentFile, fileContent); |
| 83 | + } else { |
| 84 | + logger.info("Unrecognized file format, will not try to validate TDE template in file: " + file + "; format: " + documentFile.getFormat()); |
| 85 | + } |
87 | 86 |
|
88 | | - if (call != null) { |
89 | | - ObjectNode node = (ObjectNode) call.eval(new JacksonHandle()).get(); |
90 | | - if (node.get("valid").asBoolean()) { |
91 | | - logger.info("TDE template passed validation: " + file); |
92 | | - } else { |
93 | | - throw new RuntimeException(format("TDE template failed validation; file: %s; cause: %s", file, node.get("message").asText())); |
94 | | - } |
| 87 | + if (call != null) { |
| 88 | + ObjectNode node = (ObjectNode) call.eval(new JacksonHandle()).get(); |
| 89 | + if (node.get("valid").asBoolean()) { |
| 90 | + logger.info("TDE template passed validation: " + file); |
| 91 | + } else { |
| 92 | + throw new RuntimeException(format("TDE template failed validation; file: %s; cause: %s", file, node.get("message").asText())); |
95 | 93 | } |
96 | | - } catch (FailedRequestException e) { |
97 | | - logger.warn("Unexpected error when trying to validate TDE template in file: " + file + "; cause: " + e.getMessage()); |
98 | 94 | } |
99 | 95 | } |
100 | 96 | } |
101 | 97 | } |
102 | 98 |
|
103 | 99 | protected ServerEvaluationCall buildJavascriptCall(DocumentFile documentFile, String fileContent) { |
104 | | - StringBuilder script = new StringBuilder("xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');"); |
| 100 | + StringBuilder script = new StringBuilder("var template; xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');"); |
105 | 101 | script.append(format( |
106 | | - "\nreturn tde.validate([xdmp.toJSON(%s)], ['%s'])}, {database: xdmp.database('%s')})", |
107 | | - fileContent, documentFile.getUri(), tdeValidationDatabase |
| 102 | + "\nreturn tde.validate([xdmp.toJSON(template)], ['%s'])}, {database: xdmp.database('%s')})", |
| 103 | + documentFile.getUri(), tdeValidationDatabase |
108 | 104 | )); |
109 | | - return databaseClient.newServerEval().javascript(script.toString()); |
| 105 | + return databaseClient.newServerEval().javascript(script.toString()) |
| 106 | + .addVariable("template", new StringHandle(fileContent).withFormat(Format.JSON)); |
110 | 107 | } |
111 | 108 |
|
112 | 109 | protected ServerEvaluationCall buildXqueryCall(DocumentFile documentFile, String fileContent) { |
113 | 110 | StringBuilder script = new StringBuilder("import module namespace tde = 'http://marklogic.com/xdmp/tde' at '/MarkLogic/tde.xqy'; "); |
114 | | - script.append(format("\nxdmp:invoke-function(function() { \nlet $t := %s ", fileContent)); |
| 111 | + script.append("\ndeclare variable $template external; "); |
| 112 | + script.append("\nxdmp:invoke-function(function() { "); |
115 | 113 | script.append(format( |
116 | | - "\nreturn tde:validate($t, '%s')}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)", |
| 114 | + "\ntde:validate($template, '%s')}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)", |
117 | 115 | documentFile.getUri(), tdeValidationDatabase |
118 | 116 | )); |
119 | | - return databaseClient.newServerEval().xquery(script.toString()); |
| 117 | + return databaseClient.newServerEval().xquery(script.toString()).addVariable("template", new StringHandle(fileContent).withFormat(Format.XML)); |
120 | 118 | } |
121 | 119 |
|
122 | 120 | public DatabaseClient getDatabaseClient() { |
|
0 commit comments