/** * @return New instance of {@link XmlProtocolMarshaller}. */ public XmlProtocolMarshaller build() { return new XmlProtocolMarshaller(this); } }
private boolean isBinary(SdkField<?> field, Object val) { return isExplicitPayloadMember(field) && val instanceof SdkBytes; }
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); setContentTypeHeaderIfNeeded("binary/octet-stream"); } else if (isExplicitPayloadMember(field) && val instanceof String) { byte[] content = ((String) val).getBytes(StandardCharsets.UTF_8); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } }
@Override public SdkHttpFullRequest marshall(SdkPojo pojo) { if (rootElement != null) { marshallerContext.xmlGenerator().startElement(rootElement); } doMarshall(pojo); if (rootElement != null) { marshallerContext.xmlGenerator().endElement(); } return finishMarshalling(pojo); }
private SdkHttpFullRequest finishMarshalling(SdkPojo pojo) { // Content may already be set if the payload is binary data. if (hasPayloadMembers(pojo) && request.contentStreamProvider() == null && marshallerContext.xmlGenerator() != null) { String content = marshallerContext.xmlGenerator().stringWriter().getBuffer().toString(); if (!content.isEmpty()) { request.contentStreamProvider(() -> new StringInputStream(content)); request.putHeader("Content-Length", Integer.toString(content.getBytes(StandardCharsets.UTF_8).length)); setContentTypeHeaderIfNeeded("application/xml"); } } return request.build(); }
/** * Creates an instance of {@link XmlProtocolMarshaller} to be used for marshalling the requess. * * @param operationInfo Info required to marshall the request */ public ProtocolMarshaller<SdkHttpFullRequest> createProtocolMarshaller(OperationInfo operationInfo) { return XmlProtocolMarshaller.builder() .endpoint(clientConfiguration.option(SdkClientOption.ENDPOINT)) .xmlGenerator(createGenerator(operationInfo)) .operationInfo(operationInfo) .build(); }
@Override public void marshall(SdkPojo val, XmlMarshallerContext context, String paramName, SdkField<SdkPojo> sdkField, ValueToStringConverter.ValueToString<SdkPojo> converter) { context.protocolMarshaller().doMarshall(val); } };
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); setContentTypeHeaderIfNeeded("binary/octet-stream"); } else if (isExplicitPayloadMember(field) && val instanceof String) { byte[] content = ((String) val).getBytes(StandardCharsets.UTF_8); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } }
@Override public SdkHttpFullRequest marshall(SdkPojo pojo) { if (rootElement != null) { marshallerContext.xmlGenerator().startElement(rootElement); } doMarshall(pojo); if (rootElement != null) { marshallerContext.xmlGenerator().endElement(); } return finishMarshalling(pojo); }
private SdkHttpFullRequest finishMarshalling(SdkPojo pojo) { // Content may already be set if the payload is binary data. if (hasPayloadMembers(pojo) && request.contentStreamProvider() == null && marshallerContext.xmlGenerator() != null) { String content = marshallerContext.xmlGenerator().stringWriter().getBuffer().toString(); if (!content.isEmpty()) { request.contentStreamProvider(() -> new StringInputStream(content)); request.putHeader("Content-Length", Integer.toString(content.getBytes(StandardCharsets.UTF_8).length)); setContentTypeHeaderIfNeeded("application/xml"); } } return request.build(); }
/** * Creates an instance of {@link XmlProtocolMarshaller} to be used for marshalling the requess. * * @param operationInfo Info required to marshall the request */ public ProtocolMarshaller<SdkHttpFullRequest> createProtocolMarshaller(OperationInfo operationInfo) { return XmlProtocolMarshaller.builder() .endpoint(clientConfiguration.option(SdkClientOption.ENDPOINT)) .xmlGenerator(createGenerator(operationInfo)) .operationInfo(operationInfo) .build(); }
@Override public void marshall(SdkPojo val, XmlMarshallerContext context, String paramName, SdkField<SdkPojo> sdkField, ValueToStringConverter.ValueToString<SdkPojo> converter) { context.protocolMarshaller().doMarshall(val); } };
/** * @return New instance of {@link XmlProtocolMarshaller}. */ public XmlProtocolMarshaller build() { return new XmlProtocolMarshaller(this); } }
private boolean isBinary(SdkField<?> field, Object val) { return isExplicitPayloadMember(field) && val instanceof SdkBytes; }