/** * Converts a byte array into a MessagePartsDescriptor * @param blockData byte array to convert * @return a MessagePartsDescriptor */ SmartPtrCMessagePartDescriptor CMessagePartDescriptor::fromArray( SmartPtrCDynamicByteArray& buffer) { CAF_CM_STATIC_FUNC("CMessagePartDescriptor", "fromArray"); CAF_CM_VALIDATE_SMARTPTR(buffer); if (buffer->getByteCount() < BLOCK_SIZE) { CAF_CM_EXCEPTION_VA1(E_INVALIDARG, "Input data block is too small - %d", buffer->getByteCount()); } if (CMessagePartsParser::getByte(buffer) != CAF_MSG_VERSION) { CAF_CM_EXCEPTION_VA0(E_INVALIDARG, "Input data block version is incorrect"); } const byte resvd = CMessagePartsParser::getByte(buffer); if (resvd != RESERVED) { CAF_CM_EXCEPTION_VA0(E_INVALIDARG, "Input data block reserved bits are incorrect"); } const uint16 attachmentNumber = CMessagePartsParser::getUint16(buffer); const uint32 partNumber = CMessagePartsParser::getUint32(buffer); const uint32 dataSize = CMessagePartsParser::getUint32(buffer); const uint32 dataOffset = CMessagePartsParser::getUint32(buffer); buffer->verify(); SmartPtrCMessagePartDescriptor messagePartsDescriptor; messagePartsDescriptor.CreateInstance(); messagePartsDescriptor->initialize( attachmentNumber, partNumber, dataSize, dataOffset); return messagePartsDescriptor; }
void CCafMessagePayload::saveToFile( const SmartPtrCDynamicByteArray& payload, const std::string& payloadPath) { CAF_CM_STATIC_FUNC_VALIDATE("CCafMessagePayload", "saveToFile"); CAF_CM_VALIDATE_SMARTPTR(payload); CAF_CM_VALIDATE_STRING(payloadPath); FileSystemUtils::saveByteFile(payloadPath, payload->getPtr(), payload->getByteCount()); }
SmartPtrCDynamicByteArray CCafMessagePayload::createBufferFromStr( const std::string& payloadStr) { CAF_CM_STATIC_FUNC_VALIDATE("CCafMessagePayload", "createBufferFromStr"); CAF_CM_VALIDATE_STRING(payloadStr); SmartPtrCDynamicByteArray rc; rc.CreateInstance(); rc->allocateBytes(static_cast<uint32>(payloadStr.length())); rc->memCpy(payloadStr.c_str(), static_cast<uint32>(payloadStr.length())); return rc; }
std::string CCafMessagePayloadParser::bufferToStr( const SmartPtrCDynamicByteArray& payload) { CAF_CM_STATIC_FUNC_VALIDATE("CCafMessagePayloadParser", "bufferToStr"); CAF_CM_VALIDATE_SMARTPTR(payload); return reinterpret_cast<const char*>(payload->getPtr()); }
std::string CPayloadContentRouterInstance::calcOutputChannel( const SmartPtrCDynamicByteArray& payload) const { CAF_CM_FUNCNAME_VALIDATE("calcOutputChannel"); std::string outputChannel; CAF_CM_ENTER { CAF_CM_PRECOND_ISINITIALIZED(_isInitialized); CAF_CM_VALIDATE_SMARTPTR(payload); const std::string payloadStr = reinterpret_cast<const char*>(payload->getPtr()); CAF_CM_VALIDATE_STRING(payloadStr); for(TConstIterator<Cmapstrstr> valueToChannelIter(_valueToChannelMapping); valueToChannelIter; valueToChannelIter++) { const std::string value = valueToChannelIter->first; SmartPtrCCafRegex regex; regex.CreateInstance(); regex->initialize(value); if (regex->isMatched(payloadStr)) { outputChannel = valueToChannelIter->second; CAF_CM_LOG_DEBUG_VA2("Matched channel - regex: %s, channel: %s", value.c_str(), outputChannel.c_str()); break; } } } CAF_CM_EXIT; return outputChannel; }
void CIntMessage::initializeStr( const std::string& payloadStr, const SmartPtrCHeaders& newHeaders, const SmartPtrCHeaders& origHeaders) { CAF_CM_FUNCNAME_VALIDATE("initializeStr"); CAF_CM_PRECOND_ISNOTINITIALIZED(_isInitialized); std::string payloadStrTmp = payloadStr; if (payloadStr.empty()) { payloadStrTmp = ""; } SmartPtrCDynamicByteArray payload; payload.CreateInstance(); payload->allocateBytes(payloadStrTmp.length()); payload->memCpy(payloadStrTmp.c_str(), payloadStrTmp.length()); initialize(payload, newHeaders, origHeaders); }
SmartPtrCDynamicByteArray CMessagePartDescriptor::toArray( const uint16 attachmentNumber, const uint32 partNumber, const uint32 dataSize, const uint32 dataOffset) { SmartPtrCDynamicByteArray buffer; buffer.CreateInstance(); buffer->allocateBytes(BLOCK_SIZE); CMessagePartsBuilder::put(CAF_MSG_VERSION, buffer); CMessagePartsBuilder::put(RESERVED, buffer); CMessagePartsBuilder::put(attachmentNumber, buffer); CMessagePartsBuilder::put(partNumber, buffer); CMessagePartsBuilder::put(dataSize, buffer); CMessagePartsBuilder::put(dataOffset, buffer); buffer->verify(); return buffer; }
void CMessagePartsBuilder::put( const byte value, SmartPtrCDynamicByteArray& buffer) { CAF_CM_STATIC_FUNC_LOG_VALIDATE("CMessagePartsBuilder", "put(byte)"); CAF_CM_VALIDATE_SMARTPTR(buffer); // const size_t currentPos = buffer->getByteCount() - buffer->getByteCountFromCurrentPos(); buffer->memAppend(&value, sizeof(value)); // CAF_CM_LOG_DEBUG_VA2("buffer - pos: %d, val: %02x", currentPos, value); }
void CMessagePartsBuilder::putBytes( const byte* buf, const uint32 bufLen, SmartPtrCDynamicByteArray& buffer) { CAF_CM_STATIC_FUNC_LOG_VALIDATE("CMessagePartsBuilder", "putBytes"); CAF_CM_VALIDATE_PTR(buf); CAF_CM_VALIDATE_SMARTPTR(buffer); for (uint32 index = 0; index < bufLen; index++) { buffer->memAppend(&buf[index], sizeof(buf[index])); } }
/** * Converts the BLOCK_SIZE data in a ByteBuffer into a MessagePartsDescriptor * <p> * The incoming ByteBuffer position will be modified. * @param buffer ByteBuffer to convert * @return a MessagePartsDescriptor */ SmartPtrCMessagePartDescriptor CMessagePartDescriptor::fromByteBuffer( SmartPtrCDynamicByteArray& buffer) { CAF_CM_STATIC_FUNC("CMessagePartDescriptor", "fromByteBuffer"); CAF_CM_VALIDATE_SMARTPTR(buffer); if (buffer->getByteCountFromCurrentPos() < BLOCK_SIZE) { CAF_CM_EXCEPTION_VA2(E_INVALIDARG, "Input data block is too small - rem: %d, tot: %d", buffer->getByteCountFromCurrentPos(), buffer->getByteCount()); } SmartPtrCDynamicByteArray data; data.CreateInstance(); data->allocateBytes(BLOCK_SIZE); data->memCpy(buffer->getPtrAtCurrentPos(), BLOCK_SIZE); buffer->incrementCurrentPos(BLOCK_SIZE); return fromArray(data); }
SmartPtrIIntMessage COutgoingMessageHandler::rehydrateMultiPartMessage( const SmartPtrCMessageDeliveryRecord& deliveryRecord, const IIntMessage::SmartPtrCHeaders& addlHeaders) { CAF_CM_STATIC_FUNC_LOG("COutgoingMessageHandler", "rehydrateMultiPartMessage"); CAF_CM_VALIDATE_SMARTPTR(deliveryRecord); // addlHeaders are optional uint32 payloadSize = CMessagePartsHeader::BLOCK_SIZE; const std::deque<SmartPtrCMessagePartDescriptorSourceRecord> sourceRecords = deliveryRecord->getMessagePartSources(); for (TConstIterator<std::deque<SmartPtrCMessagePartDescriptorSourceRecord> > sourceRecordIter(sourceRecords); sourceRecordIter; sourceRecordIter++) { const SmartPtrCMessagePartDescriptorSourceRecord sourceRecord = *sourceRecordIter; payloadSize += CMessagePartDescriptor::BLOCK_SIZE + sourceRecord->getDataLength(); } SmartPtrCDynamicByteArray payload; payload.CreateInstance(); payload->allocateBytes(payloadSize); const SmartPtrCDynamicByteArray partsHeader = CMessagePartsHeader::toArray( deliveryRecord->getCorrelationId(), deliveryRecord->getNumberOfParts()); payload->memAppend(partsHeader->getPtr(), partsHeader->getByteCount()); uint32 partNumber = deliveryRecord->getStartingPartNumber(); if (CAF_CM_IS_LOG_DEBUG_ENABLED) { CAF_CM_LOG_DEBUG_VA3("[# sourceRecords=%d][payloadSize=%d][startingPartNumber=%d]", sourceRecords.size(), payloadSize, partNumber); } for (TConstIterator<std::deque<SmartPtrCMessagePartDescriptorSourceRecord> > sourceRecordIter(sourceRecords); sourceRecordIter; sourceRecordIter++) { const SmartPtrCMessagePartDescriptorSourceRecord sourceRecord = *sourceRecordIter; const SmartPtrCDynamicByteArray partDescriptor = CMessagePartDescriptor::toArray( sourceRecord->getAttachmentNumber(), partNumber++, sourceRecord->getDataLength(), sourceRecord->getDataOffset()); payload->memAppend(partDescriptor->getPtr(), partDescriptor->getByteCount()); CAF_CM_LOG_DEBUG_VA3("Reading from file - file: %s, len: %d, offset: %d", sourceRecord->getFilePath().c_str(), sourceRecord->getDataLength(), sourceRecord->getDataOffset()); std::ifstream file(sourceRecord->getFilePath().c_str(), std::ios::binary); try { if (!file.is_open()) { CAF_CM_EXCEPTION_VA1(ERROR_FILE_NOT_FOUND, "Could not open binary file - %s", sourceRecord->getFilePath().c_str()); } file.seekg(sourceRecord->getDataOffset(), std::ios::beg); file.read(reinterpret_cast<char*>(payload->getNonConstPtrAtCurrentPos()), sourceRecord->getDataLength()); payload->verify(); if (! file) { CAF_CM_EXCEPTION_VA3(ERROR_BUFFER_OVERFLOW, "Did not read full contents - file: %s, requested: %d, read: %d", sourceRecord->getFilePath().c_str(), sourceRecord->getDataLength(), file.gcount()); } payload->incrementCurrentPos(sourceRecord->getDataLength()); } CAF_CM_CATCH_ALL; file.close(); CAF_CM_LOG_CRIT_CAFEXCEPTION; CAF_CM_THROWEXCEPTION; } SmartPtrCIntMessage rc; rc.CreateInstance(); rc->initialize(payload, deliveryRecord->getMessageHeaders(), addlHeaders); return rc; }
void CProviderExecutorRequestHandler::processRequest( const SmartPtrCProviderExecutorRequest& request) const { CAF_CM_FUNCNAME_VALIDATE("processRequest"); CAF_CM_VALIDATE_SMARTPTR(request); const std::string outputDir = request->getOutputDirectory(); SmartPtrCLoggingSetter loggingSetter; loggingSetter.CreateInstance(); loggingSetter->initialize(outputDir); SmartPtrIIntMessage message = request->getInternalRequest(); const std::string providerRequestPath = FileSystemUtils::buildPath(outputDir, _sProviderRequestFilename); const std::string stdoutPath = FileSystemUtils::buildPath(outputDir, _sStdoutFilename); const std::string stderrPath = FileSystemUtils::buildPath(outputDir, _sStderrFilename); std::string newProviderRequestPath = FileSystemUtils::normalizePathWithForward( providerRequestPath); // Create temporary request file for use by the provider CCafMessagePayload::saveToFile(message->getPayload(), newProviderRequestPath); Cdeqstr argv; argv.push_back(_providerPath); argv.push_back("-r"); argv.push_back(newProviderRequestPath); CAF_CM_LOG_INFO_VA2("Running command - %s -r %s", _providerPath.c_str(), newProviderRequestPath.c_str()); ProcessUtils::Priority priority = ProcessUtils::NORMAL; std::string appConfigPriority = AppConfigUtils::getOptionalString(_sManagementAgentArea, "provider_process_priority"); if (!appConfigPriority.empty()) { if (CStringUtils::isEqualIgnoreCase("LOW", appConfigPriority)) { priority = ProcessUtils::LOW; } else if (CStringUtils::isEqualIgnoreCase("IDLE", appConfigPriority)) { priority = ProcessUtils::IDLE; } } // Begin impersonation if (!_beginImpersonationTransformer.IsNull()) { message = _beginImpersonationTransformer->transformMessage(message); if (message.IsNull()) { CAF_CM_LOG_WARN_VA0("Begin impersonation transform did not return a message"); } } { CAF_CM_UNLOCK_LOCK; ProcessUtils::runSyncToFiles(argv, stdoutPath, stderrPath, priority); } // End impersonation if (!_endImpersonationTransformer.IsNull()) { message = _endImpersonationTransformer->transformMessage(message); if (message.IsNull()) { CAF_CM_LOG_WARN_VA0("End impersonation transform did not return a message"); } } // Delete temporary request file used by the provider if (FileSystemUtils::doesFileExist(newProviderRequestPath)) { CAF_CM_LOG_INFO_VA1("Removing handler produced request file - %s", newProviderRequestPath.c_str()); FileSystemUtils::removeFile(newProviderRequestPath); } // Delete original request const std::string originalFile = message->findOptionalHeaderAsString(FileHeaders::_sORIGINAL_FILE); if (!originalFile.empty()) { if (FileSystemUtils::doesFileExist(originalFile)) { CAF_CM_LOG_INFO_VA1("Removing original file - %s", originalFile.c_str()); FileSystemUtils::removeFile(originalFile); } } // Package response in envelope and write to global response location const SmartPtrCProviderRequestDoc providerRequest = CCafMessagePayloadParser::getProviderRequest(message->getPayload()); const SmartPtrCResponseDoc response = CResponseFactory::createResponse(providerRequest, outputDir); const std::string relFilename = CStringUtils::createRandomUuid() + "_" + _sResponseFilename; SmartPtrIIntMessage responseMessage = CCafMessageCreator::createPayloadEnvelope( response, relFilename, message->getHeaders()); const std::string directory = AppConfigUtils::getRequiredString("response_dir"); const std::string filePath = FileSystemUtils::buildPath(directory, relFilename); const SmartPtrCDynamicByteArray payload = responseMessage->getPayload(); FileSystemUtils::saveByteFile(filePath, payload->getPtr(), payload->getByteCount(), FileSystemUtils::FILE_MODE_REPLACE, ".writing"); }