PhmsResponseBody.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "PhmsResponseBody.h"
  2. #include "Poco/StreamCopier.h"
  3. #include "Poco/Buffer.h"
  4. #include <fstream>
  5. #include <sstream>
  6. #include "ErrorCode.h"
  7. #include "Poco/Exception.h"
  8. #include "Poco/File.h"
  9. #include "PhmsSession.h"
  10. #include "PhmsLogger.h"
  11. #include "Util.h"
  12. using Poco::StreamCopier;
  13. using Poco::Buffer;
  14. using Poco::Exception;
  15. using Poco::TimeoutException;
  16. using Poco::File;
  17. CPhmsResponseBody::CPhmsResponseBody(void):m_pInputStream(NULL), m_pPhmsSession(NULL)
  18. {
  19. }
  20. CPhmsResponseBody::CPhmsResponseBody(istream* pInputStream, void* pPhmsSession)
  21. :m_pInputStream(pInputStream), m_pPhmsSession(pPhmsSession)
  22. {
  23. }
  24. CPhmsResponseBody::CPhmsResponseBody(CPhmsResponseBody& phmsResponseBody)
  25. {
  26. this->m_pInputStream = phmsResponseBody.GetInputStreamPointer();
  27. this->m_pPhmsSession = phmsResponseBody.GetPhmsSessionPointer();
  28. }
  29. CPhmsResponseBody& CPhmsResponseBody::operator=(CPhmsResponseBody& phmsResponseBody)
  30. {
  31. this->m_pInputStream = phmsResponseBody.GetInputStreamPointer();
  32. this->m_pPhmsSession = phmsResponseBody.GetPhmsSessionPointer();
  33. return *this;
  34. }
  35. CPhmsResponseBody::~CPhmsResponseBody(void)
  36. {
  37. }
  38. //Getter
  39. istream* CPhmsResponseBody::GetInputStreamPointer()
  40. {
  41. return this->m_pInputStream;
  42. }
  43. void* CPhmsResponseBody::GetPhmsSessionPointer()
  44. {
  45. return this->m_pPhmsSession;
  46. }
  47. //Setter
  48. void CPhmsResponseBody::SetInputStreamPointer(istream* pInputStream)
  49. {
  50. this->m_pInputStream = pInputStream;
  51. }
  52. void CPhmsResponseBody::SetPhmsSessionPointer(void* pPhmsSession)
  53. {
  54. this->m_pPhmsSession = pPhmsSession;
  55. }
  56. int CPhmsResponseBody::InputStreamToString(string& stringContent)
  57. {
  58. //流形式实现,此处用string输出流可能有问题,因为用户可能将二进制数据也保存到buffer,用string流可能会出问题。
  59. //应该使用Poco::MemoryIOS
  60. //不过目前系统中暂时没有这么使用的,有这么用的再说
  61. ostringstream oStringStream;
  62. oStringStream.exceptions(ios::failbit|ios::badbit);
  63. m_pInputStream->exceptions(ios::badbit);
  64. try
  65. {
  66. StreamCopier::copyStream(*m_pInputStream, oStringStream);
  67. }
  68. catch(const std::ios::failure& e)
  69. {
  70. CPhmsLogger::GetPhmsLogger()->WriteLog(e.what(), __FUNCTION__, __FILE__, __LINE__);
  71. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_INPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  72. return COMMON_INPUT_STREAM_FAIL;
  73. }
  74. catch(const TimeoutException& e)
  75. {
  76. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  77. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  78. return COMMON_RECV_TIMEOUT;
  79. }
  80. catch(const Exception& e)
  81. {
  82. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  83. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  84. return COMMON_RECV_FAIL;
  85. }
  86. stringContent = oStringStream.str();
  87. return PHMS_SUCCESSFUL_RESULT;
  88. }
  89. int CPhmsResponseBody::InputStreamToFile(string& stringFilePath, bool bFileClearBeforeWrite)
  90. {
  91. try
  92. {
  93. File fileTarget(stringFilePath, "gbk");
  94. fileTarget.createFile();
  95. }
  96. catch(Exception& e)
  97. {
  98. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  99. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_OPEN_FAIL), __FUNCTION__, __FILE__, __LINE__);
  100. return COMMON_FILE_OPEN_FAIL;
  101. }
  102. ofstream ofLocalTargetFile;
  103. ofLocalTargetFile.exceptions(ios::failbit|ios::badbit);
  104. m_pInputStream->exceptions(ios::goodbit);
  105. try
  106. {
  107. if(bFileClearBeforeWrite)
  108. {
  109. ofLocalTargetFile.open(stringFilePath.c_str(), ios_base::trunc|ios_base::binary);
  110. }
  111. else
  112. {
  113. //locale localePre = ofLocalTargetFile.imbue(locale(""));//imbue不起作用
  114. ofLocalTargetFile.open(stringFilePath.c_str(), ios_base::app|ios_base::binary);
  115. //ofLocalTargetFile.imbue(localePre);
  116. }
  117. }
  118. catch(const ios::failure& error)
  119. {
  120. //写日志
  121. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  122. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_OPEN_FAIL), __FUNCTION__, __FILE__, __LINE__);
  123. return COMMON_FILE_OPEN_FAIL;
  124. }
  125. m_pInputStream->exceptions(ios::badbit);
  126. ofLocalTargetFile.exceptions(ios::failbit|ios::badbit);
  127. try
  128. {
  129. StreamCopier::copyStream(*m_pInputStream, ofLocalTargetFile);
  130. }
  131. catch(const std::ios::failure& e)
  132. {
  133. ofLocalTargetFile.close();
  134. CPhmsLogger::GetPhmsLogger()->WriteLog(e.what(), __FUNCTION__, __FILE__, __LINE__);
  135. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_INPUT_STREAM_FAIL), __FUNCTION__, __FILE__, __LINE__);
  136. return COMMON_INPUT_STREAM_FAIL;
  137. }
  138. catch(const TimeoutException& e)
  139. {
  140. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  141. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_TIMEOUT), __FUNCTION__, __FILE__, __LINE__);
  142. return COMMON_RECV_TIMEOUT;
  143. }
  144. catch(const Exception& e)
  145. {
  146. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  147. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_RECV_FAIL), __FUNCTION__, __FILE__, __LINE__);
  148. return COMMON_RECV_FAIL;
  149. }
  150. try
  151. {
  152. ofLocalTargetFile.flush();
  153. }
  154. catch(const ios::failure& error)
  155. {
  156. //写日志
  157. ofLocalTargetFile.close();
  158. CPhmsLogger::GetPhmsLogger()->WriteLog(error.what(), __FUNCTION__, __FILE__, __LINE__);
  159. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_WRITE_FAIL), __FUNCTION__, __FILE__, __LINE__);
  160. return COMMON_FILE_WRITE_FAIL;
  161. }
  162. ofLocalTargetFile.close();
  163. return PHMS_SUCCESSFUL_RESULT;
  164. }