use of dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException in project dodo by devhawala.
the class MailingNewImpl method inbasket2_getNextMail.
private static void inbasket2_getNextMail(GetNextMailParams params, GetNextMailResults results) {
// logging callback used before returning
UnaryOperator<String> retlog = m -> {
if (logParamsAndResults) {
if (m == null) {
m = "";
}
StringBuilder sb = new StringBuilder();
results.append(sb, " ", "results");
logf("##\n## procedure MailingNewImpl.inbasket2_getNextMail() %s -- results\n%s\n##\n", m, sb.toString());
}
return null;
};
// log ingoing data
if (logParamsAndResults) {
StringBuilder sb = new StringBuilder();
params.append(sb, " ", "params");
logf("##\n## procedure MailingNewImpl.inbasket2_getNextMail() -- params\n%s\n##\n", sb.toString());
}
// get the session (we do not check the verifier, as we trust our clients...)
dlogf(" ... fetching mail session ...");
MailSession session = mailService.getSession(params.sessionId.get());
if (session == null) {
logf("failed (invalid session id)\n");
retlog.apply("[invalid mail session]");
return;
}
// check for valid index for the mail in the inbox
MailData prevMailData = session.getClientData();
int index = (prevMailData == null) ? 0 : prevMailData.mailboxIndex + 1;
dlogf(" ... mailIndex: %d\n", index);
if (index >= session.getMailCount() || index < 0) {
// not or no longer present => return empty results
retlog.apply("[no more mails available]");
return;
}
// get the mail content
dlogf(" ... getting mailbox entry ... ");
MailboxEntry me = session.getMailEntry(index);
dlogf("%s\n", (me != null) ? "ok" : "failed (null)");
if (me == null) {
// not or no longer present => return empty results
retlog.apply("[mail already deleted]");
return;
}
dlogf(" ... loading mail content file ... ");
ByteArrayOutputStream mailBos = new ByteArrayOutputStream();
try {
me.transferContent(istream -> {
byte[] buffer = new byte[512];
try {
int count = istream.read(buffer);
while (count > 0) {
mailBos.write(buffer, 0, count);
count = istream.read(buffer);
}
} catch (IOException e) {
// ignore that for now
}
});
} catch (IOException e) {
// we must at least be able to read the mail file, so...
retlog.apply("[unable to read mail content]");
return;
}
byte[] mailContentBytes = mailBos.toByteArray();
dlogf("%d bytes\n", mailContentBytes.length);
// extract the relevant fields from the (old format) mail content
dlogf(" ... deserializing mail content ... ");
SerializedFile mailContentFile = SerializedFile.make();
try {
mailContentFile.deserialize(new ByteArrayWireInputStream(mailContentBytes));
} catch (EndOfMessageException e) {
// invalid mail file content??
retlog.apply("[invalid mail file content]");
return;
}
dlogf("ok\n");
dlogf(" ... extracting relevant data\n");
NameList xfrom = null;
NameList xto = null;
NameList xcc = null;
NameList xreplyTo = null;
STRING xsubject = null;
STRING mailText = null;
boolean xisFolderAttachment = false;
SEQUENCE<Attribute> attrs = mailContentFile.file.attributes.value;
for (int i = 0; i < attrs.size(); i++) {
try {
Attribute attr = attrs.get(i);
int attributeType = (int) (attr.type.get() & 0xFFFF_FFFFL);
switch(attributeType) {
case MailingCommon.from:
xfrom = attr.decodeData(NameList::make);
dlogf(" ... found from\n");
break;
case MailingCommon.to:
xto = attr.decodeData(NameList::make);
dlogf(" ... found to\n");
break;
case MailingCommon.cc:
xcc = attr.decodeData(NameList::make);
dlogf(" ... found cc\n");
break;
case MailingCommon.replyTo:
xreplyTo = attr.decodeData(NameList::make);
dlogf(" ... found replyTo\n");
break;
case MailingCommon.subject:
xsubject = attr.decodeData(STRING::make);
dlogf(" ... found subject\n");
break;
case MailingCommon.comments:
mailText = attr.decodeData(STRING::make);
dlogf(" ... found comments\n");
break;
case FilingCommon.atIsDirectory:
xisFolderAttachment = attr.getAsBoolean();
dlogf(" ... found isDirectory\n");
break;
}
} catch (EndOfMessageException e) {
// invalid mail file content??
retlog.apply("[unable to extract attribute mail file content]");
return;
}
}
dlogf(" ... done extracting mail content data\n");
if (xfrom == null || xfrom.size() == 0 || xto == null || xto.size() == 0) {
retlog.apply("[missing one of from, to]");
return;
}
// extract the relevant fields from the (old format) mail envelope
dlogf(" ... loading old-format envelope ... ");
ByteArrayOutputStream envBos = new ByteArrayOutputStream();
try {
me.transferPostboxEnvelope(istream -> {
byte[] buffer = new byte[512];
try {
int count = istream.read(buffer);
while (count > 0) {
envBos.write(buffer, 0, count);
count = istream.read(buffer);
}
} catch (IOException e) {
// ignore that for now
}
});
} catch (IOException e) {
// we must at least be able to read the mail envelope, so...
retlog.apply("[unable to read mail envelope]");
return;
}
byte[] oldEnvelopeBytes = envBos.toByteArray();
dlogf("ok -> %d bytes\n", oldEnvelopeBytes.length);
dlogf(" ... deserializing old-format envelope ... ");
EncodedList oldEnvelope = EncodedList.make();
try {
oldEnvelope.deserialize(new ByteArrayWireInputStream(oldEnvelopeBytes));
} catch (EndOfMessageException e) {
// invalid mail file envelope??
retlog.apply("[invalid mail file envelope]");
return;
}
dlogf("ok\n");
StringBuilder envSb = new StringBuilder();
oldEnvelope.append(envSb, " ", "old-envelope");
dlogf("+++ %s\n", envSb.toString());
dlogf(" ... extracting mail-id ... ");
MessageID xmailId = null;
for (int i = 0; i < oldEnvelope.size(); i++) {
Attribute attr = oldEnvelope.get(i);
if (attr.type.get() == MailingCommon.atMtMessageID) {
try {
xmailId = attr.decodeData(MessageID::make);
} catch (EndOfMessageException e) {
// invalid mail file envelope??
retlog.apply("[cannot decode messageId from mail file envelope]");
return;
}
}
}
if (xmailId == null) {
// invalid mail file envelope??
retlog.apply("[invalid mail file envelope]");
return;
}
dlogf("ok\n");
MessageID mailId = xmailId;
// synthesize a new format envelope from the old format mail data
dlogf(" ... creating new-format envelope\n");
NameList from = xfrom;
NameList to = xto;
NameList cc = xcc;
NameList replyTo = xreplyTo;
STRING subject = (xsubject == null) ? STRING.make() : xsubject;
boolean isFolderAttachment = xisFolderAttachment;
// long mailCreatedOn = me.inboxEntry().getCreatedOn();
SEQUENCE<Attribute> newEnvelope = new SEQUENCE<>(Attribute::make);
newEnvelope.add().set(MailTransport5.atSenderAndDate, ThreePartNameWithTagAndDateString::make, v -> {
v.nameWithTag.name.from(from.get(0));
// check where this thing possibly re-appears in GlobalView and if so implement as non-constant
v.date.set("24-Jan-94 21:40:54");
});
newEnvelope.add().set(MailTransport5.atSenderA, ThreePartNameWithTag::make, v -> {
v.name.from(from.get(0));
});
newEnvelope.add().set(MailTransport5.atSenderB, ListOfThreePartNameWithTag::make, v -> {
copyNames(from, v);
});
newEnvelope.add().set(MailTransport5.atTo, ListOfThreePartNameWithTag::make, v -> {
copyNames(to, v);
});
if (cc != null && cc.size() > 0) {
newEnvelope.add().set(MailTransport5.atCopiesTo, ListOfThreePartNameWithTag::make, v -> {
copyNames(cc, v);
});
}
if (replyTo != null && replyTo.size() > 0) {
newEnvelope.add().set(MailTransport5.atReplyTo, ListOfThreePartNameWithTag::make, v -> {
copyNames(replyTo, v);
});
}
newEnvelope.add().set(MailTransport5.atSubject, STRING::make, v -> v.set(subject));
dlogf(" ... done\n");
// prepare the temp object holding the contents data to be delivered later
// and create the serialized data for later use
MailData mailData = new MailData(((long) session.getSessionId() & 0x0000_0000_FFFF_FFFFL) + index, index);
dlogf(" ... serializing new-format envelope ...");
WireWriter wireWriter = new WireWriter();
try {
newEnvelope.serialize(wireWriter);
} catch (NoMoreWriteSpaceException e) {
// should never happen => ignore
}
mailData.envelopeBytes = wireWriter.getBytes();
dlogf("ok, %d bytes\n", mailData.envelopeBytes.length);
if (mailText != null && mailText.get() != null) {
dlogf(" ... creating mail-text (comments) bytes ... ");
wireWriter = new WireWriter();
try {
mailText.serialize(wireWriter);
} catch (NoMoreWriteSpaceException e) {
// should never happen => ignore
}
// skip the length word
byte[] mailtextBytes = wireWriter.getBytes(2);
if (mailtextBytes.length > 0) {
if (mailtextBytes[mailtextBytes.length - 1] == 0x00) {
mailtextBytes[mailtextBytes.length - 1] = ' ';
}
mailData.mailtextBytes = mailtextBytes;
dlogf("ok, %d bytes\n", mailData.mailtextBytes.length);
} else {
dlogf(" empty mail-text, mail part NOT created\n");
}
}
if (mailContentFile.file.content.data.size() > 0 || xisFolderAttachment) {
mailData.attachmentBytes = mailContentBytes;
dlogf(" ... added mail attachment, %d bytes\n", mailData.attachmentBytes.length);
}
// prepare the mailInfo-Attributes in the return object
dlogf(" ... building mail-infos for return data\n");
dlogf(" ... miMailServer ... ");
results.mailInfos.value.add().set(Inbasket2.miMailServer, MiMailServer::make, v -> {
v.name.from(mailService.getServiceName());
// obscure ...
v.unknown1.set(0xA926);
// ... constants?
v.unknown2.set(0xEB6D);
});
dlogf("ok\n");
dlogf(" ... miMessageId ... ");
results.mailInfos.value.add().set(Inbasket2.miMessageId, MailingCommon.MessageID::make, v -> {
for (int i = 0; i < 5; i++) {
v.get(i).set(mailId.get(i).get());
}
});
dlogf("ok\n");
dlogf(" ... miWhatever ... ");
results.mailInfos.value.add().set(Inbasket2.miWhatever, MiWhatever::make, v -> {
v.value.set(4);
});
dlogf("ok\n");
dlogf(" ... miMailparts ... ");
long[] lengthSum = { 0 };
results.mailInfos.value.add().set(Inbasket2.miMailparts, MiMailParts::make, v -> {
// envelope (always present)
MiMailPart envPart = v.add();
envPart.mailPartType.set(MailTransport5.mptEnvelope);
envPart.mailPartLength.set(mailData.envelopeBytes.length);
lengthSum[0] = mailData.envelopeBytes.length;
mailData.parts.add(mailData.envelopeBytes);
dlogf("envelope ");
// having mail-text?
if (mailData.mailtextBytes != null) {
MiMailPart mailTextPart = v.add();
mailTextPart.mailPartType.set(MailTransport5.mptNoteGV);
mailTextPart.mailPartLength.set(mailData.mailtextBytes.length);
lengthSum[0] += mailData.mailtextBytes.length;
mailData.parts.add(mailData.mailtextBytes);
dlogf("noteGV ");
}
// having mail-attachment?
if (mailData.attachmentBytes != null) {
MiMailPart mailTextPart = v.add();
mailTextPart.mailPartType.set(isFolderAttachment ? MailTransport5.mptAttachmentFolder : 4);
mailTextPart.mailPartLength.set(mailData.attachmentBytes.length);
lengthSum[0] += mailData.attachmentBytes.length;
mailData.parts.add(mailData.attachmentBytes);
dlogf("attachment(%d) ", mailTextPart.mailPartType.get());
}
});
dlogf("... ok\n");
dlogf(" ... miTotalPartsLength ... ");
results.mailInfos.value.add().set(Inbasket2.miTotalPartsLength, MiTotalPartsLength::make, v -> {
v.totalLength.set(lengthSum[0]);
});
dlogf("ok\n");
dlogf(" ... miUser0 ... ");
results.mailInfos.value.add().set(Inbasket2.miSender0, MiSender::make, v -> {
v.senderName.from(from.get(0));
});
dlogf("ok\n");
dlogf(" ... miUser1 ... ");
results.mailInfos.value.add().set(Inbasket2.miSender1, MiSender::make, v -> {
v.senderName.from(from.get(0));
});
dlogf("ok\n");
dlogf(" ... done\n");
// fill the obscure array values in the return data
dlogf(" ... adding obscure array ... ");
if (mailData.mailtextBytes != null && mailData.attachmentBytes != null) {
// all 3 mail parts
int[] vals = { 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0001, 0x0001 };
for (int i = 0; i < vals.length; i++) {
results.unknownSeq.add().set(vals[i]);
}
} else if (mailData.mailtextBytes == null && mailData.attachmentBytes == null) {
// only 1 mail part (envelope)
int[] vals = { 0x0000, 0x0001, 0x0000, 0x0000 };
for (int i = 0; i < vals.length; i++) {
results.unknownSeq.add().set(vals[i]);
}
} else {
// 2 mail parts (envelope and mail-text or attachment)
int[] vals = { 0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
for (int i = 0; i < vals.length; i++) {
results.unknownSeq.add().set(vals[i]);
}
}
dlogf("%d words\n", results.unknownSeq.size());
// done: remember the data container with the mail-parts for later transfer and add the id to the returned data
session.setClientData(mailData);
results.uniqueMailNo.set(mailData.mailTransferId);
dlogf(" ... uniqueMailNo: 0x%08X\n", results.uniqueMailNo.get());
// log outgoing data
retlog.apply(null);
}
use of dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException in project dodo by devhawala.
the class FilingImpl method retrieve.
/*
* Retrieve: PROCEDURE [ file: Handle, content: BulkData.Sink, session: Session ]
* REPORTS [ AccessError, AuthenticationError, ConnectionError,
* HandleError, SessionError, TransferError, UndefinedError ]
* = 13;
*/
private static void retrieve(RetrieveParams params, RECORD results) {
logParams("retrieve", params);
// check session
Session session = resolveSession(params.session);
Volume vol = session.getService().getVolume();
// check specified file handle
Handle fileHandle = Handle.get(params.file);
if (fileHandle == null || fileHandle.isNullHandle()) {
new HandleErrorRecord(HandleProblem.nullDisallowed).raise();
}
FileEntry fe = fileHandle.getFe();
if (fe == null) {
new HandleErrorRecord(HandleProblem.nullDisallowed).raise();
}
// transfer file content
try {
ByteContentSink sink = new ByteContentSink(params.content);
vol.retrieveContent(fe.getFileID(), sink, session.getUsername());
} catch (NoMoreWriteSpaceException | IOException e) {
log("## Filing.Retrieve() => %s : %s\n", e.getClass().getName(), e.getMessage());
new TransferErrorRecord(TransferProblem.aborted).raise();
}
// prolongate the sessions life if this took longer
session.continueUse();
}
use of dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException in project dodo by devhawala.
the class TestCrData method testSequences.
/*
* Test embedded SEQUENCE and StreamOf
*/
@Test
public void testSequences() throws EndOfMessageException, NoMoreWriteSpaceException {
CrSequences rec = new CrSequences();
rec.check1.set(0x1111);
rec.check2.set(0x2222);
rec.check3.set(0x3333);
rec.check4.set(0x4444);
rec.check5.set(0x5555);
for (int i = 0; i < 5; i++) {
INTEGER integer = rec.seqInt.add();
integer.set(5000 + i);
}
for (int i = 0; i < 11; i++) {
Data data = rec.sOfData.add();
data.userdata1.set(7000 + i);
data.userdata2.set(1001000 + i);
}
for (int i = 0; i < 7; i++) {
STRING string = rec.seqString.add();
string.set("value of rec.seqString :: " + i);
}
Content_complex cc = (Content_complex) rec.sOfContent.add().setChoice(ContentType.complex);
cc.flags.get(0).set(true);
cc.flags.get(1).set(false);
cc.flags.get(2).set(true);
cc.flags.get(3).set(false);
cc.string.set("a test string");
cc.longCardinal.set(1234567890L);
rec.sOfContent.add().setChoice(ContentType.empty);
Content_integer ci = (Content_integer) rec.sOfContent.add().setChoice(ContentType.integer);
ci.integer.set(42);
StringBuilder sb = new StringBuilder();
String recStr = rec.append(sb, "", "rec").toString();
// System.out.printf("-------\n%s\n-------\n", recStr);
iWireStream iws = new MockWireStream();
rec.serialize(iws);
CrSequences recReread = new CrSequences();
recReread.deserialize(iws);
sb.setLength(0);
String recRereadStr = recReread.append(sb, "", "rec").toString();
assertEquals("recStr eq recRereadStr", recStr, recRereadStr);
assertTrue(iws.isAtEnd());
}
use of dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException in project dodo by devhawala.
the class TestCrData method testSubstructures3.
@Test
public void testSubstructures3() throws EndOfMessageException, NoMoreWriteSpaceException {
CrSubstructures rec = new CrSubstructures();
rec.check1.set(0x1111);
rec.check2.set(0x2222);
rec.check3.set(0x3333);
rec.data.userdata1.set(11111);
rec.data.userdata2.set(222222222);
rec.content.setChoice(ContentType.string);
Content_string cs = (Content_string) rec.content.getContent();
cs.string.set("the string content in rec.content.string");
StringBuilder sb = new StringBuilder();
String recStr = rec.append(sb, "", "rec").toString();
// System.out.printf("-------\n%s\n-------\n", recStr);
iWireStream iws = new MockWireStream();
rec.serialize(iws);
CrSubstructures recReread = new CrSubstructures();
recReread.deserialize(iws);
sb.setLength(0);
String recRereadStr = recReread.append(sb, "", "rec").toString();
assertEquals("recStr eq recRereadStr", recStr, recRereadStr);
assertTrue(iws.isAtEnd());
}
use of dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException in project dodo by devhawala.
the class TestCrData method testSubstructures4.
@Test
public void testSubstructures4() throws EndOfMessageException, NoMoreWriteSpaceException {
CrSubstructures rec = new CrSubstructures();
rec.check1.set(0x1111);
rec.check2.set(0x2222);
rec.check3.set(0x3333);
rec.data.userdata1.set(11111);
rec.data.userdata2.set(222222222);
Content_complex cc = (Content_complex) rec.content.setChoice(ContentType.complex);
cc.flags.get(0).set(true);
cc.flags.get(1).set(false);
cc.flags.get(2).set(false);
cc.flags.get(3).set(true);
cc.string.set("the string content in rec.content.complex");
cc.longCardinal.set(4000000000L);
StringBuilder sb = new StringBuilder();
String recStr = rec.append(sb, "", "rec").toString();
// System.out.printf("-------\n%s\n-------\n", recStr);
iWireStream iws = new MockWireStream();
rec.serialize(iws);
CrSubstructures recReread = new CrSubstructures();
recReread.deserialize(iws);
sb.setLength(0);
String recRereadStr = recReread.append(sb, "", "rec").toString();
assertEquals("recStr eq recRereadStr", recStr, recRereadStr);
assertTrue(iws.isAtEnd());
}
Aggregations