forked from graniticio/granitic
/
xmlbuilder.go
104 lines (76 loc) · 2.98 KB
/
xmlbuilder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package ws
import (
"errors"
"github.com/graniticio/granitic/config"
"github.com/graniticio/granitic/instance"
"github.com/graniticio/granitic/ioc"
"github.com/graniticio/granitic/logging"
"github.com/graniticio/granitic/ws"
"github.com/graniticio/granitic/ws/xml"
)
const (
xmlResponseWriterName = instance.FrameworkPrefix + "XmlResponseWriter"
xmlUnmarshallerName = instance.FrameworkPrefix + "XmlUnmarshaller"
templateMode = "TEMPLATE"
marshalMode = "MARSHAL"
)
// Creates the components required to support the XmlWs facility and adds them the IoC container.
type XmlWsFacilityBuilder struct {
}
// See FacilityBuilder.BuildAndRegister
func (fb *XmlWsFacilityBuilder) BuildAndRegister(lm *logging.ComponentLoggerManager, ca *config.ConfigAccessor, cc *ioc.ComponentContainer) error {
wc := buildAndRegisterWsCommon(lm, ca, cc)
um := new(xml.StandardXmlUnmarshaller)
cc.WrapAndAddProto(xmlUnmarshallerName, um)
mode, _ := ca.StringVal("XmlWs.ResponseMode")
var rw ws.WsResponseWriter
switch mode {
case templateMode:
rw = fb.createTemplateComponents(ca, cc, wc)
case marshalMode:
rw = fb.createMarshalComponents(ca, cc, wc)
default:
return errors.New("XmlWs.ResponseMode must be set to either TEMPLATE or MARSHAL")
}
buildRegisterWsDecorator(cc, rw, um, wc, lm)
offerAbnormalStatusWriter(rw.(ws.AbnormalStatusWriter), cc, xmlResponseWriterName)
return nil
}
func (fb *XmlWsFacilityBuilder) createTemplateComponents(ca *config.ConfigAccessor, cc *ioc.ComponentContainer, wc *wsCommon) ws.WsResponseWriter {
rw := new(xml.TemplatedXmlResponseWriter)
ca.Populate("XmlWs.ResponseWriter", rw)
cc.WrapAndAddProto(xmlResponseWriterName, rw)
rw.FrameworkErrors = wc.FrameworkErrors
rw.StatusDeterminer = wc.StatusDeterminer
return rw
}
func (fb *XmlWsFacilityBuilder) createMarshalComponents(ca *config.ConfigAccessor, cc *ioc.ComponentContainer, wc *wsCommon) ws.WsResponseWriter {
rw := new(ws.MarshallingResponseWriter)
ca.Populate("XmlWs.ResponseWriter", rw)
cc.WrapAndAddProto(xmlResponseWriterName, rw)
rw.StatusDeterminer = wc.StatusDeterminer
rw.FrameworkErrors = wc.FrameworkErrors
if !cc.ModifierExists(xmlResponseWriterName, "ErrorFormatter") {
rw.ErrorFormatter = new(xml.GraniticXmlErrorFormatter)
}
if !cc.ModifierExists(xmlResponseWriterName, "ResponseWrapper") {
wrap := new(xml.GraniticXmlResponseWrapper)
rw.ResponseWrapper = wrap
}
if !cc.ModifierExists(xmlResponseWriterName, "MarshalingWriter") {
mw := new(xml.XmlMarshalingWriter)
ca.Populate("XmlWs.Marshal", mw)
rw.MarshalingWriter = mw
}
return rw
}
// See FacilityBuilder.FacilityName
func (fb *XmlWsFacilityBuilder) FacilityName() string {
return "XmlWs"
}
// See FacilityBuilder.DependsOnFacilities
func (fb *XmlWsFacilityBuilder) DependsOnFacilities() []string {
return []string{}
}