Skip to content

Commit 7800e9c

Browse files
Replication works with both HTML/JSON
1 parent 9867e1d commit 7800e9c

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

pkg/repl.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pkg
33
import (
44
"fmt"
55
"io"
6+
"strings"
67

78
log "github.com/sirupsen/logrus"
89
"github.com/spf13/cast"
@@ -64,11 +65,33 @@ func (r Replication) replicate(cmd string, path string) error {
6465
} else if response.IsError() {
6566
return fmt.Errorf("%s > cannot do replication command '%s' for path '%s': %s", r.instance.IDColor(), cmd, path, response.Status())
6667
}
67-
jsonBytes, err := io.ReadAll(response.RawBody())
68+
69+
seBytes, err := io.ReadAll(response.RawBody())
6870
if err != nil {
6971
return fmt.Errorf("%s > cannot read replication command '%s' response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
7072
}
71-
jsonData, err := sling.JsonData(string(jsonBytes))
73+
74+
contentType := response.Header().Get("Content-Type")
75+
responseBody := string(seBytes)
76+
77+
// Older AEM versions use HTML, newer use JSON
78+
if r.isJSONResponse(contentType, responseBody) {
79+
return r.handleJSONResponse(cmd, path, responseBody)
80+
} else {
81+
return r.handleHTMLResponse(cmd, path, responseBody)
82+
}
83+
}
84+
85+
func (r Replication) isJSONResponse(contentType, responseBody string) bool {
86+
if strings.Contains(strings.ToLower(contentType), "application/json") {
87+
return true
88+
}
89+
trimmed := strings.TrimSpace(responseBody)
90+
return strings.HasPrefix(trimmed, "{") && strings.HasSuffix(trimmed, "}")
91+
}
92+
93+
func (r Replication) handleJSONResponse(cmd, path, responseBody string) error {
94+
jsonData, err := sling.JsonData(responseBody)
7295
if err != nil {
7396
return fmt.Errorf("%s > cannot parse replication command '%s' JSON response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
7497
}
@@ -78,6 +101,17 @@ func (r Replication) replicate(cmd string, path string) error {
78101
return nil
79102
}
80103

104+
func (r Replication) handleHTMLResponse(cmd, path, responseBody string) error {
105+
htmlData, err := sling.HtmlData(responseBody)
106+
if err != nil {
107+
return fmt.Errorf("%s > cannot parse replication command '%s' HTML response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
108+
}
109+
if htmlData.IsError() {
110+
return fmt.Errorf("%s > replication command '%s' failed for path '%s': %s", r.instance.IDColor(), cmd, path, htmlData.Message)
111+
}
112+
return nil
113+
}
114+
81115
func (r Replication) ActivateTree(opts replication.ActivateTreeOpts) error {
82116
log.Infof("%s > activating tree at path '%s'", r.instance.IDColor(), opts.StartPath)
83117

0 commit comments

Comments
 (0)