15
15
16
16
package software .amazon .awssdk .awscore .protocol .json ;
17
17
18
+ import static software .amazon .awssdk .core .SdkSystemSetting .BINARY_ION_ENABLED ;
19
+ import static software .amazon .awssdk .core .SdkSystemSetting .CBOR_ENABLED ;
20
+
18
21
import java .util .ArrayList ;
19
22
import java .util .List ;
20
23
import java .util .function .Function ;
21
24
import java .util .function .Supplier ;
22
25
import software .amazon .awssdk .annotations .SdkProtectedApi ;
23
26
import software .amazon .awssdk .annotations .SdkTestInternalApi ;
24
27
import software .amazon .awssdk .annotations .ThreadSafe ;
25
- import software .amazon .awssdk .awscore .AwsRequest ;
26
28
import software .amazon .awssdk .awscore .exception .AwsServiceException ;
27
29
import software .amazon .awssdk .awscore .http .response .AwsJsonResponseHandler ;
28
30
import software .amazon .awssdk .awscore .internal .protocol .json .AwsJsonErrorUnmarshaller ;
36
38
import software .amazon .awssdk .core .http .NewJsonResponseHandler ;
37
39
import software .amazon .awssdk .core .internal .protocol .json .unmarshall .JsonProtocolUnmarshaller ;
38
40
import software .amazon .awssdk .core .protocol .OperationInfo ;
41
+ import software .amazon .awssdk .core .protocol .ProtocolRequestMarshaller ;
39
42
import software .amazon .awssdk .core .protocol .SdkPojo ;
40
- import software .amazon .awssdk .core .protocol .json .BaseJsonProtocolFactory ;
41
43
import software .amazon .awssdk .core .protocol .json .JsonClientMetadata ;
42
44
import software .amazon .awssdk .core .protocol .json .JsonErrorResponseMetadata ;
43
45
import software .amazon .awssdk .core .protocol .json .JsonErrorShapeMetadata ;
44
46
import software .amazon .awssdk .core .protocol .json .JsonOperationMetadata ;
47
+ import software .amazon .awssdk .core .protocol .json .JsonProtocolMarshallerBuilder ;
45
48
import software .amazon .awssdk .core .protocol .json .StructuredJsonGenerator ;
46
- import software .amazon .awssdk .core .runtime .transform .JsonUnmarshallerContext ;
47
- import software .amazon .awssdk .core .runtime .transform .Unmarshaller ;
48
49
import software .amazon .awssdk .http .SdkHttpFullResponse ;
49
50
50
51
/**
53
54
*/
54
55
@ ThreadSafe
55
56
@ SdkProtectedApi
56
- public final class AwsJsonProtocolFactory extends BaseJsonProtocolFactory < AwsRequest , AwsServiceException > {
57
+ public final class AwsJsonProtocolFactory {
57
58
59
+ private final JsonClientMetadata jsonClientMetadata ;
58
60
private final AwsJsonProtocolMetadata protocolMetadata ;
59
61
private final List <AwsJsonErrorUnmarshaller > errorUnmarshallers = new ArrayList <>();
60
62
61
- public AwsJsonProtocolFactory (JsonClientMetadata metadata , AwsJsonProtocolMetadata protocolMetadata ) {
62
- super ( metadata ) ;
63
- this .protocolMetadata = protocolMetadata ;
63
+ private AwsJsonProtocolFactory (Builder builder ) {
64
+ this . jsonClientMetadata = builder . metadata ;
65
+ this .protocolMetadata = builder . protocolMetadata . build () ;
64
66
createErrorUnmarshallers ();
65
67
}
66
68
67
69
/**
68
- * Returns the response handler to be used for handling a successful response.
70
+ * Creates a new response handler with the given {@link JsonOperationMetadata} and a supplier of the POJO response
71
+ * type.
69
72
*
70
- * @param operationMetadata Additional context information about an operation to create the appropriate response handler.
73
+ * @param operationMetadata Metadata about operation being unmarshalled.
74
+ * @param pojoSupplier {@link Supplier} of the POJO response type.
75
+ * @param <T> Type being unmarshalled.
76
+ * @return HttpResponseHandler that will handle the HTTP response and unmarshall into a POJO.
71
77
*/
72
- @ Override
73
- public <T > HttpResponseHandler <T > createResponseHandler (
74
- JsonOperationMetadata operationMetadata ,
75
- Unmarshaller <T , JsonUnmarshallerContext > responseUnmarshaller ) {
76
- return getSdkFactory ().createResponseHandler (operationMetadata , responseUnmarshaller );
78
+ public <T extends SdkPojo > HttpResponseHandler <T > createResponseHandler (JsonOperationMetadata operationMetadata ,
79
+ Supplier <SdkPojo > pojoSupplier ) {
80
+ return createResponseHandler (operationMetadata , r -> pojoSupplier .get ());
77
81
}
78
82
79
- public <T extends SdkPojo > HttpResponseHandler <T > createResponseHandler (
80
- JsonOperationMetadata operationMetadata , Supplier <SdkPojo > pojoSupplier ) {
83
+ /**
84
+ * Creates a new response handler with the given {@link JsonOperationMetadata} and a supplier of the POJO response
85
+ * type.
86
+ *
87
+ * @param operationMetadata Metadata about operation being unmarshalled.
88
+ * @param pojoSupplier {@link Supplier} of the POJO response type. Has access to the HTTP response, primarily for polymorphic
89
+ * deserialization as seen in event stream (i.e. unmarshalled event depends on ':event-type' header).
90
+ * @param <T> Type being unmarshalled.
91
+ * @return HttpResponseHandler that will handle the HTTP response and unmarshall into a POJO.
92
+ */
93
+ public <T extends SdkPojo > HttpResponseHandler <T > createResponseHandler (JsonOperationMetadata operationMetadata ,
94
+ Function <SdkHttpFullResponse , SdkPojo > pojoSupplier ) {
81
95
JsonProtocolUnmarshaller <T > unmarshaller = new JsonProtocolUnmarshaller <>(getSdkFactory ().createObjectMapper ());
82
96
return new AwsJsonResponseHandler <>(
83
97
new NewJsonResponseHandler <>(unmarshaller ,
84
- r -> pojoSupplier . get () ,
98
+ pojoSupplier ,
85
99
operationMetadata .isHasStreamingSuccessResponse (),
86
100
operationMetadata .isPayloadJson ()));
87
101
}
88
102
89
- public <T extends SdkPojo > HttpResponseHandler <T > createResponseHandler (
90
- JsonOperationMetadata operationMetadata , Function <SdkHttpFullResponse , SdkPojo > pojoSupplier ) {
91
- JsonProtocolUnmarshaller <T > unmarshaller = new JsonProtocolUnmarshaller <>(getSdkFactory ().createObjectMapper ());
92
- return new NewJsonResponseHandler <>(unmarshaller ,
93
- pojoSupplier ,
94
- operationMetadata .isHasStreamingSuccessResponse (),
95
- operationMetadata .isPayloadJson ());
96
- }
97
-
98
103
/**
99
104
* Creates a response handler for handling a error response (non 2xx response).
100
105
*/
101
- @ Override
102
106
public HttpResponseHandler <AwsServiceException > createErrorResponseHandler (
103
107
JsonErrorResponseMetadata errorResponseMetadata ) {
104
108
return getSdkFactory ().createErrorResponseHandler (errorUnmarshallers , errorResponseMetadata
105
109
.getCustomErrorCodeFieldName ());
106
110
}
107
111
108
- protected StructuredJsonGenerator createGenerator (OperationInfo operationInfo ) {
112
+ private StructuredJsonGenerator createGenerator (OperationInfo operationInfo ) {
109
113
if (operationInfo .hasPayloadMembers () || protocolMetadata .protocol () == AwsJsonProtocol .AWS_JSON ) {
110
114
return createGenerator ();
111
115
} else {
@@ -165,4 +169,83 @@ private AwsStructuredJsonFactory getSdkFactory() {
165
169
return AwsStructuredPlainJsonFactory .SDK_JSON_FACTORY ;
166
170
}
167
171
}
172
+
173
+ public <T extends software .amazon .awssdk .awscore .AwsRequest > ProtocolRequestMarshaller <T > createProtocolMarshaller (
174
+ OperationInfo operationInfo , T origRequest ) {
175
+ return JsonProtocolMarshallerBuilder .<T >standard ()
176
+ .jsonGenerator (createGenerator (operationInfo ))
177
+ .contentType (getContentType ())
178
+ .operationInfo (operationInfo )
179
+ .originalRequest (origRequest )
180
+ .sendExplicitNullForPayload (false )
181
+ .build ();
182
+ }
183
+
184
+ private boolean isCborEnabled () {
185
+ return jsonClientMetadata .isSupportsCbor () && CBOR_ENABLED .getBooleanValueOrThrow ();
186
+ }
187
+
188
+ private boolean isIonEnabled () {
189
+ return jsonClientMetadata .isSupportsIon ();
190
+ }
191
+
192
+ private boolean isIonBinaryEnabled () {
193
+ return BINARY_ION_ENABLED .getBooleanValueOrThrow ();
194
+ }
195
+
196
+ public static Builder builder () {
197
+ return new Builder ();
198
+ }
199
+
200
+ /**
201
+ * Builder for {@link AwsJsonProtocolFactory}.
202
+ */
203
+ public static final class Builder {
204
+
205
+ private final JsonClientMetadata metadata = new JsonClientMetadata ();
206
+ private final AwsJsonProtocolMetadata .Builder protocolMetadata = AwsJsonProtocolMetadata .builder ();
207
+
208
+ private Builder () {
209
+ }
210
+
211
+ public Builder addErrorMetadata (JsonErrorShapeMetadata errorShapeMetadata ) {
212
+ metadata .addErrorMetadata (errorShapeMetadata );
213
+ return this ;
214
+ }
215
+
216
+ public Builder contentTypeOverride (String contentType ) {
217
+ metadata .withContentTypeOverride (contentType );
218
+ return this ;
219
+ }
220
+
221
+ public Builder supportsCbor (boolean supportsCbor ) {
222
+ metadata .withSupportsCbor (supportsCbor );
223
+ return this ;
224
+ }
225
+
226
+ public Builder supportsIon (boolean supportsIon ) {
227
+ metadata .withSupportsIon (supportsIon );
228
+ return this ;
229
+ }
230
+
231
+ public Builder baseServiceExceptionClass (Class <? extends RuntimeException > baseServiceExceptionClass ) {
232
+ metadata .withBaseServiceExceptionClass (baseServiceExceptionClass );
233
+ return this ;
234
+ }
235
+
236
+ public Builder protocol (AwsJsonProtocol protocol ) {
237
+ protocolMetadata .protocol (protocol );
238
+ return this ;
239
+ }
240
+
241
+ public Builder protocolVersion (String protocolVersion ) {
242
+ protocolMetadata .protocolVersion (protocolVersion );
243
+ return this ;
244
+ }
245
+
246
+ public AwsJsonProtocolFactory build () {
247
+ return new AwsJsonProtocolFactory (this );
248
+ }
249
+
250
+ }
168
251
}
0 commit comments