byte[] bytes = getMessage().getBytes(getCharset());
if (myGzipData) {
try {
bytes = GZipUtils.compress(bytes);
} catch (IOException e) {
throw new EncodeException("Failed to apply GZip coding", e);
}
}
setData(bytes);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream os;
if (myGzipData) {
try {
os = new GZIPOutputStream(bos);
} catch (IOException e) {
throw new EncodeException("Failed to create GZip encoder", e);
}
} else {
os = bos;
}
OutputStreamWriter w = new OutputStreamWriter(os, getCharset());
try {
mySendable.writeMessage(w);
} catch (IOException e) {
throw new EncodeException("Failed to convert message to sendable bytes");
}
setData(bos.toByteArray());
}
setActionLineAppropriately();
setHeaders(new LinkedHashMap<String, String>());
StringBuilder ctBuilder = new StringBuilder();
if (mySendable != null) {
ctBuilder.append(mySendable.getEncodingStyle().getContentType());
} else {
ctBuilder.append(EncodingStyle.detect(getMessage()).getContentType());
}
ctBuilder.append("; charset=");
ctBuilder.append(getCharset().name());
getHeaders().put("Content-Type", ctBuilder.toString());
getHeaders().put("Content-Length", Integer.toString(getData().length));
addSpecificHeaders();
synchronized (ourRfc1123DateFormat) {
getHeaders().put("Date", ourRfc1123DateFormat.format(new Date()));
}
if (getSigner() != null) {
try {
getHeaders().put(HTTP_HEADER_HL7_SIGNATURE, getSigner().sign(getData()));
} catch (SignatureFailureException e) {
throw new EncodeException(e);
}
}
ourLog.trace("Exiting encode()");
}