use of com.helger.commons.codec.IByteArrayCodec in project as2-lib by phax.
the class HTTPHelper method readAndDecodeHttpRequest.
@Nonnull
public static DataSource readAndDecodeHttpRequest(@Nonnull final IAS2HttpRequestDataProvider aRDP, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws IOException, MessagingException {
// Main read
DataSource aPayload = HTTPHelper.readHttpRequest(aRDP, aResponseHandler, aMsg, aIncomingDumper);
// Check the transfer encoding of the request. If none is provided, check
// the partnership for a default one. If none is in the partnership used the
// default one
final String sCTE = aMsg.partnership().getContentTransferEncodingReceive(EContentTransferEncoding.AS2_DEFAULT.getID());
final String sContentTransferEncoding = aMsg.getHeaderOrDefault(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
if (StringHelper.hasText(sContentTransferEncoding)) {
final IContentTransferEncoding aCTE = EContentTransferEncoding.getFromIDCaseInsensitiveOrNull(sContentTransferEncoding);
if (aCTE == null) {
if (LOGGER.isWarnEnabled())
LOGGER.warn("Unsupported Content-Transfer-Encoding '" + sContentTransferEncoding + "' is used - ignoring!");
} else {
// Decode data if necessary
final IByteArrayCodec aCodec = aCTE.createCodec();
// TODO: Handle decoding when large file support is on
if (!(aCodec instanceof IdentityCodec<?>) && aPayload instanceof ByteArrayDataSource) {
byte[] aActualBytes = ((ByteArrayDataSource) aPayload).directGetBytes();
// Remember original length before continuing
final int nOriginalContentLength = aActualBytes.length;
if (LOGGER.isInfoEnabled())
LOGGER.info("Incoming message uses Content-Transfer-Encoding '" + sContentTransferEncoding + "' - decoding");
aActualBytes = aCodec.getDecoded(aActualBytes);
aPayload = new ByteArrayDataSource(aActualBytes, aPayload.getContentType(), aPayload.getName());
// Remember that we potentially did something
aMsg.attrs().putIn(MA_HTTP_ORIGINAL_CONTENT_TRANSFER_ENCODING, sContentTransferEncoding);
aMsg.attrs().putIn(MA_HTTP_ORIGINAL_CONTENT_LENGTH, nOriginalContentLength);
}
}
}
return aPayload;
}
use of com.helger.commons.codec.IByteArrayCodec in project ph-web by phax.
the class EContentTransferEncodingTest method testEncodeDecode.
@Test
public void testEncodeDecode() {
final byte[] aSrc = "Hello wörld".getBytes(StandardCharsets.UTF_16);
for (final EContentTransferEncoding e : EContentTransferEncoding.values()) {
final IByteArrayCodec aCodec = e.createCodec();
// Encode
final byte[] aEncoded = aCodec.getEncoded(aSrc);
assertNotNull(aEncoded);
// Decode
final byte[] aDecoded = aCodec.getDecoded(aEncoded);
assertNotNull(aDecoded);
// Consistency check
assertArrayEquals(aSrc, aDecoded);
}
}
Aggregations