final Message parentThread;
if(parentThreadID != null) {
try {
parentThread = mFreetalk.getMessageManager().get(parentThreadID);
} catch(final NoSuchMessageException e) {
throw new InvalidParameterException("Message specified by ParentThreadID was not found.");
}
} else {
parentThread = null;
}
// evaluate parentMessage
final String parentMsgId = params.get("ParentID"); // may be null
final Message parentMessage;
if (parentMsgId != null) {
try {
parentMessage = mFreetalk.getMessageManager().get(parentMsgId);
} catch(final NoSuchMessageException e) {
throw new InvalidParameterException("Message specified by ParentID was not found");
}
} else {
parentMessage = null;
}
// evaluate targetBoards
final String targetBoardsString = getMandatoryParameter(params, "TargetBoards");
final String[] targetBoardsArray = targetBoardsString.split(",");
if (targetBoardsArray.length == 0) {
throw new InvalidParameterException("Invalid TargetBoards parameter specified");
}
final Set<Board> targetBoards = new HashSet<Board>();
for(String targetBoardName : targetBoardsArray) {
targetBoardName = targetBoardName.trim();
if (targetBoardName.length() == 0) {
throw new InvalidParameterException("Invalid TargetBoards parameter specified");
}
try {
final Board board = mFreetalk.getMessageManager().getBoardByName(targetBoardName);
targetBoards.add(board);
} catch(final NoSuchBoardException e) {
throw new InvalidParameterException("TargetBoard '"+targetBoardName+"' does not exist");
}
}
// evaluate replyToBoard
final String replyToBoardName = params.get("ReplyToBoard"); // may be null
Board replyToBoard = null;
if (replyToBoardName != null ) {
try {
replyToBoard = mFreetalk.getMessageManager().getBoardByName(replyToBoardName);
} catch(final NoSuchBoardException e) {
throw new InvalidParameterException("ReplyToBoard '"+replyToBoardName+"' does not exist");
}
if (!targetBoards.contains(replyToBoard)) {
throw new InvalidParameterException("ReplyToBoard is not contained in TargetBoards");
}
}
// evaluate authorIdentity
final String authorIdentityIDString = getMandatoryParameter(params, "AuthorIdentityID");
final OwnIdentity authorIdentity;
try {
authorIdentity = mFreetalk.getIdentityManager().getOwnIdentity(authorIdentityIDString);
} catch(final NoSuchIdentityException e) {
throw new InvalidParameterException("No own identity found for AuthorIdentityID");
}
// evaluate attachments
int attachmentCount;
try {
attachmentCount = Integer.parseInt(params.get("FileAttachmentCount"));
} catch(final Exception e) {
attachmentCount = 0;
}
final List<Attachment> attachments = new ArrayList<Attachment>(attachmentCount);
for (int x=1; x <= attachmentCount; x++) {
final String uriString = getMandatoryParameter(params, "FileAttachmentURI."+x);
final String mimeTypeString = params.get("FileAttachmentMIMEType."+x);
final String sizeString = params.get("FileAttachmentSize."+x);
long fileSize;
FreenetURI freenetUri;
MimeType mimeType;
try {
freenetUri = new FreenetURI(uriString);
mimeType = mimeTypeString != null ? new MimeType(mimeTypeString) : null;
fileSize = sizeString != null ? Long.parseLong(sizeString) : -1;
} catch(final Exception e) {
throw new InvalidParameterException("Invalid FileAttachment specified ("+x+")");
}
attachments.add(new Attachment(freenetUri, mimeType, fileSize));
}
// evaluate messageTitle
final String messageTitle = getMandatoryParameter(params, "Title");
if (messageTitle.length() > Message.MAX_MESSAGE_TITLE_TEXT_LENGTH) {
throw new InvalidParameterException("Message title is longer than 256 characters");
}
// evaluate messageText
// we expect Data containing the message text
if (data == null) {
throw new InvalidParameterException("No Message text sent");
}
if (data.size() > Message.MAX_MESSAGE_TEXT_BYTE_LENGTH) {
throw new InvalidParameterException("Message text is longer than 64KB");
}
// convert to UTF-8
final byte[] utf8Bytes = new byte[(int)data.size()];
final InputStream is = data.getInputStream();
try {
if (is.read(utf8Bytes) != utf8Bytes.length) {
throw new InvalidParameterException("Internal error reading data from Bucket");
}
} finally {
is.close();
}