1515 */
1616package com .marklogic .hub .deploy .commands ;
1717
18+ import com .fasterxml .jackson .databind .JsonNode ;
19+ import com .fasterxml .jackson .databind .ObjectMapper ;
20+ import com .fasterxml .jackson .databind .node .ObjectNode ;
1821import com .marklogic .appdeployer .AppConfig ;
1922import com .marklogic .appdeployer .command .AbstractCommand ;
2023import com .marklogic .appdeployer .command .CommandContext ;
2831import com .marklogic .client .ext .util .DefaultDocumentPermissionsParser ;
2932import com .marklogic .client .ext .util .DocumentPermissionsParser ;
3033import com .marklogic .client .io .DocumentMetadataHandle ;
34+ import com .marklogic .client .io .JacksonHandle ;
3135import com .marklogic .client .io .StringHandle ;
3236import com .marklogic .hub .HubConfig ;
37+ import com .marklogic .mgmt .util .ObjectMapperFactory ;
3338import org .apache .commons .io .IOUtils ;
3439import org .springframework .beans .factory .annotation .Autowired ;
3540import org .springframework .core .io .Resource ;
4146import java .nio .file .*;
4247import java .nio .file .attribute .BasicFileAttributes ;
4348import java .util .Date ;
49+ import java .util .Iterator ;
4450import java .util .regex .Pattern ;
4551
4652/**
@@ -53,15 +59,18 @@ public class LoadUserArtifactsCommand extends AbstractCommand {
5359 private HubConfig hubConfig ;
5460
5561 private DocumentPermissionsParser documentPermissionsParser = new DefaultDocumentPermissionsParser ();
62+ private ObjectMapper objectMapper ;
5663
5764 public void setForceLoad (boolean forceLoad ) {
5865 this .forceLoad = forceLoad ;
5966 }
6067
6168 private boolean forceLoad = false ;
6269
70+
6371 public LoadUserArtifactsCommand () {
6472 super ();
73+ this .objectMapper = ObjectMapperFactory .getObjectMapper ();
6574 setExecuteSortOrder (SortOrderConstants .DEPLOY_TRIGGERS + 1 );
6675 }
6776
@@ -279,19 +288,60 @@ private void addResourceToWriteSets(
279288 ) throws IOException {
280289 if (forceLoad || propertiesModuleManager .hasFileBeenModifiedSinceLastLoaded (r .getFile ())) {
281290 InputStream inputStream = r .getInputStream ();
282- StringHandle handle = new StringHandle (IOUtils .toString (inputStream ));
283- inputStream .close ();
284- for (DocumentWriteSet writeSet : writeSets ) {
285- writeSet .add (docId , meta , handle );
291+
292+ JsonNode json ;
293+ try {
294+ json = objectMapper .readTree (inputStream );
295+ } finally {
296+ inputStream .close ();
297+ }
298+
299+ if (json instanceof ObjectNode && json .has ("language" )) {
300+ json = replaceLanguageWithLang ((ObjectNode )json );
301+ try {
302+ objectMapper .writeValue (r .getFile (), json );
303+ } catch (Exception ex ) {
304+ logger .warn ("Unable to replace 'language' with 'lang' in artifact file: " + r .getFile ().getAbsolutePath ()
305+ + ". You should replace 'language' with 'lang' yourself in this file. Error cause: " + ex .getMessage (), ex );
306+ }
307+ }
308+
309+ for (DocumentWriteSet writeSet : writeSets ) {
310+ writeSet .add (docId , meta , new JacksonHandle (json ));
286311 }
287312 propertiesModuleManager .saveLastLoadedTimestamp (r .getFile (), new Date ());
288313 }
289314 }
290315
316+ /**
317+ * Per DHFPROD-3193 and an update to MarkLogic 10.0-2, "lang" must now be used instead of "language". To ensure that
318+ * a user artifact is never loaded with "language", this command handles both updating the JSON that will be loaded
319+ * into MarkLogic and updating the artifact file.
320+ *
321+ * @param object
322+ * @return
323+ */
324+ protected ObjectNode replaceLanguageWithLang (ObjectNode object ) {
325+ ObjectNode newObject = objectMapper .createObjectNode ();
326+ newObject .put ("lang" , object .get ("language" ).asText ());
327+ Iterator <String > fieldNames = object .fieldNames ();
328+ while (fieldNames .hasNext ()) {
329+ String fieldName = fieldNames .next ();
330+ if (!"language" .equals (fieldName )) {
331+ newObject .set (fieldName , object .get (fieldName ));
332+ }
333+ }
334+ return newObject ;
335+ }
336+
291337 public void setHubConfig (HubConfig hubConfig ) {
292338 this .hubConfig = hubConfig ;
293339 }
294340
341+ public void setObjectMapper (ObjectMapper objectMapper ) {
342+ this .objectMapper = objectMapper ;
343+ }
344+
295345 abstract class ResourceToURI {
296346 public abstract String toURI (Resource r ) throws IOException ;
297347 }
0 commit comments