2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FMsg_PushUrlRequestListener.cpp
19 * @brief This is the implementation file for %_PushUrlRequestListener class.
21 * This file contains implementation of _PushUrlRequestListener class.
24 #include <FBaseByteBuffer.h>
25 #include <FBaseString.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringUtil.h>
28 #include <FBaseUtilStringTokenizer.h>
29 #include "FMsg_PushUrlRequestListener.h"
30 #include "FMsg_PushManagerImpl.h"
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Base::Utility;
35 using namespace Tizen::Net::Http;
37 namespace Tizen { namespace Messaging
40 _PushUrlRequestListener::_PushUrlRequestListener(void)
41 : __pPushRequest(null)
45 _PushUrlRequestListener::~_PushUrlRequestListener(void)
47 __pPushRequest = null;
51 _PushUrlRequestListener::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
53 SysLog(NID_MSG, "####### OnTransactionReadyToRead! #######");
57 _PushUrlRequestListener::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
59 SysLog(NID_MSG, "####### OnTransactionAborted! (%s)#######", GetErrorMessage(r));
61 _UserData* pUserData = dynamic_cast<_UserData*> (httpTransaction.GetUserObject());
62 SysTryReturnVoidResult(NID_MSG, pUserData != null, E_SYSTEM, "[E_SYSTEM] Failed to cast user data.");
66 delete &httpTransaction;
70 _PushUrlRequestListener::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
72 SysLog(NID_MSG, "####### OnTransactionReadyToWrite! #######");
76 _PushUrlRequestListener::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool rs)
78 SysLog(NID_MSG, "####### OnTransactionHeaderCompleted! #######");
82 _PushUrlRequestListener::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
84 SysLog(NID_MSG, "####### OnTransactionCompleted! #######");
86 HttpResponse* pResponse = httpTransaction.GetResponse();
87 SysTryReturnVoidResult(NID_MSG, pResponse != null, E_SYSTEM, "[E_SYSTEM] HttpResponse is invalid.");
90 _PushManagerImpl* pManager = null;
92 ByteBuffer tempBuffer;
93 _UserData* pUserData = dynamic_cast<_UserData*> (httpTransaction.GetUserObject());
94 SysTryReturnVoidResult(NID_MSG, pUserData != null, E_SYSTEM, "[E_SYSTEM] Failed to cast user data.");
96 if (pResponse->GetHttpStatusCode() == HTTP_STATUS_OK)
98 ByteBuffer* pBody = pResponse->ReadBodyN();
99 SysTryReturnVoidResult(NID_MSG, pBody != null, E_SYSTEM, "[E_SYSTEM] Failed to read body.");
101 r = tempBuffer.Construct(pBody->GetRemaining() + 1);
102 SysTryReturnVoidResult(NID_MSG, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to construct byte buffer.");
104 r = tempBuffer.CopyFrom(*pBody);
105 SysTryReturnVoidResult(NID_MSG, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to copy from body.");
107 SysLog(NID_MSG, "The read body is %s.", tempBuffer.GetPointer());
109 StringUtil::Utf8ToString((char*) tempBuffer.GetPointer(), response);
111 SysLog(NID_MSG, "The read body response is %ls.", response.GetPointer());
113 StringTokenizer tokens(response, L"&");
115 while (tokens.HasMoreTokens() == true)
124 tokens.GetNextToken(token);
126 r = token.IndexOf(L"=", 0, delimiterIndex);
130 r = token.SubString(0, delimiterIndex, key);
134 r = token.SubString(delimiterIndex + 1, token.GetLength() - delimiterIndex - 1, value);
138 if ((key.IsEmpty() == true) || (value.IsEmpty() == true))
141 if (key.Equals(L"resultcode", false) == true)
143 r = Integer::Parse(value, resultValue);
147 else if (key.Equals(L"url", false) == true)
150 urlValue.Append(L"/spp/pns/api/push");
151 SysLog(NID_MSG, "The read body url is %ls.", urlValue.GetPointer());
153 pManager = __pPushRequest->GetPushManager();
154 pManager->UpdatePushServerUrl(urlValue);
156 r = pManager->SendHttpRequest(urlValue, __pPushRequest, *(pUserData->GetPushMessage()), *(pUserData->GetRegistrationIdList()));
157 SysTryReturnVoidResult(NID_MSG, r == E_SUCCESS, r, "[%s] Failed to submit http request.", GetErrorMessage(r));
161 SysLog(NID_MSG, "Ignore UnKnown option[%ls]", key.GetPointer());
170 delete &httpTransaction;
174 _PushUrlRequestListener::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, String* pCert)
176 SysLog(NID_MSG, "####### OnTransactionCertVerificationRequiredN! #######");
178 result r = httpTransaction.Resume();
179 SysTryReturnVoidResult(NID_MSG, r == E_SUCCESS, r = E_SYSTEM, "[E_SYSTEM] http transaction resume.");
187 _PushUrlRequestListener::SetParams(const _PushRequest* pPushRequestSet)
189 __pPushRequest = pPushRequestSet;
192 _UserData::_UserData(HttpUrlEncodedEntity* pHttpUrlEncodedEntity, PushMessage* pPushMessage, IList* pRegistrationIdList)
193 : __pHttpUrlEncodedEntity(pHttpUrlEncodedEntity),
194 __pPushMessage(pPushMessage),
195 __pRegistrationIdList(pRegistrationIdList)
199 _UserData::~_UserData(void)
201 if (__pHttpUrlEncodedEntity != null)
203 delete __pHttpUrlEncodedEntity;
204 __pHttpUrlEncodedEntity = null;
207 if (__pPushMessage != null)
209 delete __pPushMessage;
210 __pPushMessage = null;
213 if (__pRegistrationIdList != null)
215 delete __pRegistrationIdList;
216 __pRegistrationIdList = null;
221 _UserData::GetPushMessage(void)
223 return __pPushMessage;
227 _UserData::GetRegistrationIdList(void)
229 return __pRegistrationIdList;
232 } } // Tizen::Messaging