11package com .marklogic .appdeployer .scaffold ;
22
3- import java .io .File ;
4- import java .io .IOException ;
5-
6- import org .springframework .util .FileCopyUtils ;
7-
83import com .fasterxml .jackson .core .JsonProcessingException ;
94import com .fasterxml .jackson .core .PrettyPrinter ;
105import com .fasterxml .jackson .core .util .DefaultPrettyPrinter ;
116import com .fasterxml .jackson .databind .ObjectMapper ;
127import com .fasterxml .jackson .databind .node .ArrayNode ;
138import com .fasterxml .jackson .databind .node .ObjectNode ;
14- import com .marklogic .client .helper .LoggingObject ;
159import com .marklogic .appdeployer .AppConfig ;
1610import com .marklogic .appdeployer .util .RestApiUtil ;
11+ import com .marklogic .client .helper .LoggingObject ;
12+ import org .springframework .util .FileCopyUtils ;
13+
14+ import java .io .File ;
15+ import java .io .IOException ;
1716
1817/**
1918 * Lots of protected methods in here to encourage subclassing and overriding behavior.
2019 */
2120public class ScaffoldGenerator extends LoggingObject {
2221
23- protected ObjectMapper objectMapper = new ObjectMapper ();
24- private PrettyPrinter prettyPrinter = new DefaultPrettyPrinter ();
22+ protected ObjectMapper objectMapper = new ObjectMapper ();
23+ private PrettyPrinter prettyPrinter = new DefaultPrettyPrinter ();
2524
26- public void generateScaffold (String path , AppConfig config ) {
27- File rootDir = new File (path );
25+ public void generateScaffold (String path , AppConfig config ) {
26+ File rootDir = new File (path );
2827
29- File configDir = getConfigDir (rootDir );
30- configDir .mkdirs ();
28+ File configDir = getConfigDir (rootDir );
29+ configDir .mkdirs ();
3130
32- File modulesDir = getModulesDir (rootDir );
33- modulesDir .mkdirs ();
31+ File modulesDir = getModulesDir (rootDir );
32+ modulesDir .mkdirs ();
3433
35- // Config
36- generateRestApiFile (configDir , config );
37- generateContentDatabaseFile (configDir , config );
38- generateSecurityFiles (configDir , config );
34+ generateContentDatabaseFile (configDir , config );
35+ generateSecurityFiles (configDir , config );
3936
40- // Modules
41- generateRestPropertiesFile (modulesDir , config );
42- generateSearchOptions (modulesDir , config );
37+ if (!config .isNoRestServer ()) {
38+ generateRestApiFile (configDir , config );
39+ generateRestPropertiesFile (modulesDir , config );
40+ generateSearchOptions (modulesDir , config );
41+ }
4342
44- }
43+ }
4544
46- private void generateSearchOptions (File modulesDir , AppConfig config ) {
47- File optionsDir = new File (modulesDir , "options" );
48- optionsDir .mkdirs ();
49- String xml = "<options xmlns='http://marklogic.com/appservices/search'>\n <search-option>unfiltered</search-option>\n <quality-weight>0</quality-weight>\n </options>" ;
50- writeFile (xml .getBytes (), new File (optionsDir , config .getName () + "-options.xml" ));
51- }
45+ private void generateSearchOptions (File modulesDir , AppConfig config ) {
46+ File optionsDir = new File (modulesDir , "options" );
47+ optionsDir .mkdirs ();
48+ String xml = "<options xmlns='http://marklogic.com/appservices/search'>\n <search-option>unfiltered</search-option>\n <quality-weight>0</quality-weight>\n </options>" ;
49+ writeFile (xml .getBytes (), new File (optionsDir , config .getName () + "-options.xml" ));
50+ }
5251
53- protected void generateRestPropertiesFile (File modulesDir , AppConfig config ) {
54- writeFile (buildRestPropertiesJson (config ), new File (modulesDir , "rest-properties.json" ));
55- }
52+ protected void generateRestPropertiesFile (File modulesDir , AppConfig config ) {
53+ writeFile (buildRestPropertiesJson (config ), new File (modulesDir , "rest-properties.json" ));
54+ }
5655
57- protected ObjectNode buildRestPropertiesJson (AppConfig config ) {
58- ObjectNode node = objectMapper .createObjectNode ();
59- node .put ("debug" , false );
60- node .put ("validate-queries" , true );
61- node .put ("document-transform-all" , false );
62- node .put ("validate-options" , true );
63- return node ;
64- }
56+ protected ObjectNode buildRestPropertiesJson (AppConfig config ) {
57+ ObjectNode node = objectMapper .createObjectNode ();
58+ node .put ("debug" , false );
59+ node .put ("validate-queries" , true );
60+ node .put ("document-transform-all" , false );
61+ node .put ("validate-options" , true );
62+ return node ;
63+ }
6564
66- protected void generateSecurityFiles (File configDir , AppConfig config ) {
67- File rolesDir = new File (configDir , "security/roles" );
68- rolesDir .mkdirs ();
65+ protected void generateSecurityFiles (File configDir , AppConfig config ) {
66+ File rolesDir = new File (configDir , "security/roles" );
67+ rolesDir .mkdirs ();
6968 writeFile (buildNobodyRole (config ), new File (rolesDir , "1-" + config .getName () + "-nobody-role.json" ));
7069 writeFile (buildReaderRole (config ), new File (rolesDir , "2-" + config .getName () + "-reader-role.json" ));
7170 writeFile (buildWriterRole (config ), new File (rolesDir , "3-" + config .getName () + "-writer-role.json" ));
7271 writeFile (buildInternalRole (config ), new File (rolesDir , "4-" + config .getName () + "-internal-role.json" ));
7372 writeFile (buildAdminRole (config ), new File (rolesDir , "5-" + config .getName () + "-admin-role.json" ));
7473
75- File usersDir = new File (configDir , "security/users" );
76- usersDir .mkdirs ();
77- writeFile (buildReaderUser (config ), new File (usersDir , config .getName () + "-reader-user.json" ));
74+ File usersDir = new File (configDir , "security/users" );
75+ usersDir .mkdirs ();
76+ writeFile (buildReaderUser (config ), new File (usersDir , config .getName () + "-reader-user.json" ));
7877 writeFile (buildWriterUser (config ), new File (usersDir , config .getName () + "-writer-user.json" ));
7978 writeFile (buildAdminUser (config ), new File (usersDir , config .getName () + "-admin-user.json" ));
80- }
79+ }
8180
8281 protected ObjectNode buildNobodyRole (AppConfig config ) {
8382 ObjectNode node = objectMapper .createObjectNode ();
@@ -143,14 +142,14 @@ protected ObjectNode buildPrivilege(String name, String action, String kind) {
143142 }
144143
145144 protected ObjectNode buildReaderUser (AppConfig config ) {
146- ObjectNode node = objectMapper .createObjectNode ();
147- String name = config .getName () + "-reader" ;
148- node .put ("user-name" , name );
149- node .put ("password" , name );
150- ArrayNode roles = node .putArray ("role" );
151- roles .add (config .getName () + "-reader" );
152- return node ;
153- }
145+ ObjectNode node = objectMapper .createObjectNode ();
146+ String name = config .getName () + "-reader" ;
147+ node .put ("user-name" , name );
148+ node .put ("password" , name );
149+ ArrayNode roles = node .putArray ("role" );
150+ roles .add (config .getName () + "-reader" );
151+ return node ;
152+ }
154153
155154 protected ObjectNode buildWriterUser (AppConfig config ) {
156155 ObjectNode node = objectMapper .createObjectNode ();
@@ -173,64 +172,64 @@ protected ObjectNode buildAdminUser(AppConfig config) {
173172 }
174173
175174 protected void generateRestApiFile (File configDir , AppConfig config ) {
176- writeFile (buildRestApiJson (config ).getBytes (), new File (configDir , "rest-api.json" ));
177- }
178-
179- protected String buildRestApiJson (AppConfig config ) {
180- return RestApiUtil .buildDefaultRestApiJson ();
181- }
182-
183- protected void generateContentDatabaseFile (File configDir , AppConfig config ) {
184- File databasesDir = new File (configDir , "databases" );
185- databasesDir .mkdirs ();
186-
187- writeFile (buildContentDatabaseJson (config ), new File (databasesDir , "content-database.json" ));
188- }
189-
190- protected ObjectNode buildContentDatabaseJson (AppConfig config ) {
191- ObjectNode node = objectMapper .createObjectNode ();
192- node .put ("database-name" , "%%DATABASE%%" );
193- ArrayNode array = node .putArray ("range-element-index" );
194- ObjectNode index = array .addObject ();
195- index .put ("scalar-type" , "string" );
196- index .put ("namespace-uri" , "CHANGEME" );
197- index .put ("localname" , "CHANGEME" );
198- index .put ("collation" , "http://marklogic.com/collation/" );
199- index .put ("range-value-positions" , false );
200- index .put ("invalid-values" , "reject" );
201- return node ;
202- }
203-
204- protected void writeFile (ObjectNode node , File f ) {
205- try {
206- byte [] bytes = objectMapper .writer (prettyPrinter ).writeValueAsBytes (node );
207- writeFile (bytes , f );
208- } catch (JsonProcessingException je ) {
209- throw new RuntimeException ("Unable to process JSON for file: " + f .getAbsolutePath () + "; cause: "
210- + je .getMessage (), je );
211- }
212- }
213-
214- protected void writeFile (byte [] bytes , File f ) {
215- if (f .exists ()) {
216- logger .info ("Not writing file, as it already exists: " + f .getAbsolutePath ());
217- } else {
218- try {
219- logger .info ("Writing: " + f .getAbsolutePath ());
220- FileCopyUtils .copy (bytes , f );
221- } catch (IOException ie ) {
222- throw new RuntimeException ("Unable to write file at: " + f .getAbsolutePath () + "; cause: "
223- + ie .getMessage (), ie );
224- }
225- }
226- }
227-
228- protected File getConfigDir (File rootDir ) {
229- return new File (rootDir , "src/main/ml-config" );
230- }
231-
232- protected File getModulesDir (File rootDir ) {
233- return new File (rootDir , "src/main/ml-modules" );
234- }
175+ writeFile (buildRestApiJson (config ).getBytes (), new File (configDir , "rest-api.json" ));
176+ }
177+
178+ protected String buildRestApiJson (AppConfig config ) {
179+ return RestApiUtil .buildDefaultRestApiJson ();
180+ }
181+
182+ protected void generateContentDatabaseFile (File configDir , AppConfig config ) {
183+ File databasesDir = new File (configDir , "databases" );
184+ databasesDir .mkdirs ();
185+
186+ writeFile (buildContentDatabaseJson (config ), new File (databasesDir , "content-database.json" ));
187+ }
188+
189+ protected ObjectNode buildContentDatabaseJson (AppConfig config ) {
190+ ObjectNode node = objectMapper .createObjectNode ();
191+ node .put ("database-name" , "%%DATABASE%%" );
192+ ArrayNode array = node .putArray ("range-element-index" );
193+ ObjectNode index = array .addObject ();
194+ index .put ("scalar-type" , "string" );
195+ index .put ("namespace-uri" , "CHANGEME" );
196+ index .put ("localname" , "CHANGEME" );
197+ index .put ("collation" , "http://marklogic.com/collation/" );
198+ index .put ("range-value-positions" , false );
199+ index .put ("invalid-values" , "reject" );
200+ return node ;
201+ }
202+
203+ protected void writeFile (ObjectNode node , File f ) {
204+ try {
205+ byte [] bytes = objectMapper .writer (prettyPrinter ).writeValueAsBytes (node );
206+ writeFile (bytes , f );
207+ } catch (JsonProcessingException je ) {
208+ throw new RuntimeException ("Unable to process JSON for file: " + f .getAbsolutePath () + "; cause: "
209+ + je .getMessage (), je );
210+ }
211+ }
212+
213+ protected void writeFile (byte [] bytes , File f ) {
214+ if (f .exists ()) {
215+ logger .info ("Not writing file, as it already exists: " + f .getAbsolutePath ());
216+ } else {
217+ try {
218+ logger .info ("Writing: " + f .getAbsolutePath ());
219+ FileCopyUtils .copy (bytes , f );
220+ } catch (IOException ie ) {
221+ throw new RuntimeException ("Unable to write file at: " + f .getAbsolutePath () + "; cause: "
222+ + ie .getMessage (), ie );
223+ }
224+ }
225+ }
226+
227+ protected File getConfigDir (File rootDir ) {
228+ return new File (rootDir , "src/main/ml-config" );
229+ }
230+
231+ protected File getModulesDir (File rootDir ) {
232+ return new File (rootDir , "src/main/ml-modules" );
233+ }
235234
236235}
0 commit comments