|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: CXcapAddition |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDES |
|
22 #include "XcapCache.h" |
|
23 #include "XcapAppUsage.h" |
|
24 #include "XcapProtocol.h" |
|
25 #include "XcapDocument.h" |
|
26 #include "XcapAddition.h" |
|
27 #include "XdmXmlParser.h" |
|
28 #include "XcapUriParser.h" |
|
29 #include "CommonDefines.h" |
|
30 #include "XcapHttpReqPut.h" |
|
31 #include "XcapHttpOperation.h" |
|
32 #include "XcapHttpTransport.h" |
|
33 #include "XcapOperationFactory.h" |
|
34 |
|
35 // --------------------------------------------------------- |
|
36 // CXcapAddition::CXcapAddition |
|
37 // |
|
38 // --------------------------------------------------------- |
|
39 // |
|
40 CXcapAddition::CXcapAddition( CXcapDocument& aParentDoc, |
|
41 CXcapDocumentNode* aDocumentSubset, |
|
42 CXcapOperationFactory& aOperationFactory ) : |
|
43 CXcapHttpOperation( aParentDoc, aDocumentSubset, aOperationFactory ), |
|
44 iOperationType( aDocumentSubset == NULL ? |
|
45 EXdmDocument : EXdmPartialDocument ) |
|
46 |
|
47 { |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------- |
|
51 // CXcapAddition::NewL |
|
52 // |
|
53 // --------------------------------------------------------- |
|
54 // |
|
55 CXcapAddition* CXcapAddition::NewL( CXcapDocument& aParentDoc, |
|
56 CXcapDocumentNode* aDocumentSubset, |
|
57 CXcapOperationFactory& aOperationFactory ) |
|
58 { |
|
59 CXcapAddition* self = new ( ELeave ) CXcapAddition( aParentDoc, aDocumentSubset, aOperationFactory ); |
|
60 CleanupStack::PushL( self ); |
|
61 self->BaseConstructL(); |
|
62 self->ConstructL(); |
|
63 CleanupStack::Pop(); |
|
64 return self; |
|
65 } |
|
66 |
|
67 // --------------------------------------------------------- |
|
68 // CXcapAddition::ConstructL |
|
69 // |
|
70 // --------------------------------------------------------- |
|
71 // |
|
72 void CXcapAddition::ConstructL() |
|
73 { |
|
74 CXcapHttpReqPut* request = Transport().PutL( iTargetDoc.Name() ); |
|
75 CleanupStack::PushL( request ); |
|
76 User::LeaveIfError( iRequestQueue.Append( request ) ); |
|
77 request->SetExpiryTimeL( NULL, KDefaultHttpRequestTimeout * 1000000 ); |
|
78 CleanupStack::Pop(); //request |
|
79 //If they point to the same node, we're dealing with a complete document |
|
80 if( iDocumentSubset != NULL && iTargetDoc.DocumentRoot() != iDocumentSubset ) |
|
81 iUriParser->SetDocumentSubset( iDocumentSubset ); |
|
82 } |
|
83 |
|
84 // --------------------------------------------------------- |
|
85 // CXcapAddition::FormatModRequestL |
|
86 // |
|
87 // --------------------------------------------------------- |
|
88 // |
|
89 TInt CXcapAddition::FormatModRequestL( const CXdmDocumentNode* aDocumentNode ) |
|
90 { |
|
91 #ifdef _DEBUG |
|
92 iOperationFactory.WriteToLog( _L8( "CXcapAddition::FormatModRequestL()" ) ); |
|
93 iOperationFactory.WriteToLog( _L8( " Operation type: %d" ), iOperationType ); |
|
94 iOperationFactory.WriteToLog( _L8( " Document subset: %x" ), iDocumentSubset ); |
|
95 #endif |
|
96 TInt validation = CXcapHttpOperation::FormatModRequestL( aDocumentNode ); |
|
97 if( validation == KErrNone ) |
|
98 { |
|
99 //Then set the properties particular to this type of request |
|
100 iOperationType = DetermineOperationType(); |
|
101 if( iOperationType == EXdmDocument ) |
|
102 iActiveRequest->SetHeaderL( KHttpHeaderContentType, iTargetDoc.ApplicationUsage().ContentType() ); |
|
103 } |
|
104 else |
|
105 { |
|
106 #ifdef _DEBUG |
|
107 iOperationFactory.WriteToLog( _L8( " AppUsage reported the following error: %d" ), validation ); |
|
108 #endif |
|
109 } |
|
110 return validation; |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------- |
|
114 // CXcapAddition::DetermineOperationType |
|
115 // |
|
116 // --------------------------------------------------------- |
|
117 // |
|
118 TXdmOperationType CXcapAddition::DetermineOperationType() |
|
119 { |
|
120 #ifdef _DEBUG |
|
121 iOperationFactory.WriteToLog( _L8( "CXcapAddition::DetermineDocType()" ) ); |
|
122 iOperationFactory.WriteToLog( _L8( " Operation type: %d" ), iOperationType ); |
|
123 iOperationFactory.WriteToLog( _L8( " Document subset: %x" ), iDocumentSubset ); |
|
124 iOperationFactory.WriteToLog( _L8( " Document root: %x" ), iTargetDoc.DocumentRoot() ); |
|
125 #endif |
|
126 return iOperationType == EXdmDocument || ( EXdmPartialDocument && |
|
127 iDocumentSubset == iTargetDoc.DocumentRoot() ) ? |
|
128 EXdmDocument : EXdmPartialDocument; |
|
129 } |
|
130 |
|
131 // --------------------------------------------------------- |
|
132 // CXcapAddition::ExecuteL |
|
133 // |
|
134 // --------------------------------------------------------- |
|
135 // |
|
136 void CXcapAddition::ExecuteL() |
|
137 { |
|
138 #ifdef _DEBUG |
|
139 iOperationFactory.WriteToLog( _L8( "CXcapAddition::ExecuteL()" ) ); |
|
140 #endif |
|
141 CXcapHttpOperation::ExecuteL( iActiveRequest, iDocumentSubset ); |
|
142 } |
|
143 |
|
144 // --------------------------------------------------------- |
|
145 // CXcapAddition::OperationCompleteL |
|
146 // |
|
147 // --------------------------------------------------------- |
|
148 // |
|
149 void CXcapAddition::OperationCompleteL() |
|
150 { |
|
151 #ifdef _DEBUG |
|
152 iOperationFactory.WriteToLog( _L8( "CXcapAddition::OperationCompleteL()" ) ); |
|
153 #endif |
|
154 TPtrC8 root = Transport().RootUri(); |
|
155 iRequestData = iActiveRequest->ResponseData(); |
|
156 TPtrC name = iTargetDoc.Name(); |
|
157 switch( iRequestData->iHttpStatus ) |
|
158 { |
|
159 case 200: //Replaced, the document was already there |
|
160 case 201: //"Created" -> Put operation was successful |
|
161 { |
|
162 #ifdef _DEBUG |
|
163 iOperationFactory.WriteToLog( _L8( " Status %d" ), iRequestData->iHttpStatus ); |
|
164 #endif |
|
165 iResult = KErrNone; |
|
166 TPtrC8 eTag = Descriptor( iRequestData->iETag ); |
|
167 iTargetDoc.SetETag( eTag ); |
|
168 RXcapCache* cache = iTargetDoc.Protocol().Cache(); |
|
169 if( cache != NULL ) |
|
170 { |
|
171 CXcapHttpContSupplier* request = ( CXcapHttpContSupplier* )iActiveRequest; |
|
172 TPtrC8 payload = request->RequestBody(); |
|
173 iOperationType == EXdmDocument ? cache->Store( eTag, name, root, payload ) : |
|
174 UpdatePartialToCacheL( cache, payload ); |
|
175 } |
|
176 iCompleted = ETrue; |
|
177 } |
|
178 break; |
|
179 default: |
|
180 #ifdef _DEBUG |
|
181 iOperationFactory.WriteToLog( _L8( " Default case - Status: %d" ), |
|
182 iRequestData->iHttpStatus ); |
|
183 #endif |
|
184 TInt status = iRequestData->iHttpStatus; |
|
185 iResult = ReinterpretStatus( status ); |
|
186 iCompleted = ETrue; |
|
187 break; |
|
188 } |
|
189 } |
|
190 |
|
191 // --------------------------------------------------------- |
|
192 // CXcapAddition::UpdatePartialToCacheL |
|
193 // |
|
194 // --------------------------------------------------------- |
|
195 // |
|
196 void CXcapAddition::UpdatePartialToCacheL( RXcapCache* aCache, const TDesC8& aNewData ) |
|
197 { |
|
198 #ifdef _DEBUG |
|
199 iOperationFactory.WriteToLog( _L8( "CXcapAddition::UpdatePartialToCacheL()" ) ); |
|
200 #endif |
|
201 HBufC8* newData = NULL; |
|
202 TPtrC name = iTargetDoc.Name(); |
|
203 TPtrC8 root = Transport().RootUri(); |
|
204 if( iOptionFlags & KFetchMasterFromCache ) |
|
205 { |
|
206 TInt length = iTargetDoc.DataLength(); |
|
207 //Do nothing, if the document is not in cache |
|
208 if( length > 0 ) |
|
209 { |
|
210 HBufC8* data = HBufC8::NewLC( length ); |
|
211 TPtr8 desc( data->Des() ); |
|
212 aCache->FetchDocumentContent( desc, name, root ); |
|
213 CXcapDocument* copy = iTargetDoc.TempCopyL(); |
|
214 CleanupStack::PushL( copy ); |
|
215 iXmlParser->ParseDocumentL( copy, desc ); |
|
216 RPointerArray<CXdmDocumentNode> array; |
|
217 CleanupClosePushL( array ); |
|
218 copy->Find( *iDocumentSubset, array ); |
|
219 //If the element the subset points to is not present |
|
220 //in the original document, the new data must be |
|
221 //appended to the the subset element parent, instead. |
|
222 CXdmDocumentNode* node = array.Count() > 0 ? |
|
223 iDocumentSubset : iDocumentSubset->Parent(); |
|
224 newData = iXmlParser->FormatToXmlLC( aNewData, copy, node ); |
|
225 CleanupStack::Pop(); //newData |
|
226 CleanupStack::PopAndDestroy( 3 ); //array, tempCopy, data |
|
227 CleanupStack::PushL( newData ); |
|
228 } |
|
229 } |
|
230 else |
|
231 { |
|
232 CXdmDocumentNode* root = iTargetDoc.DocumentRoot(); |
|
233 newData = iXmlParser->FormatToXmlLC( ETrue, &iTargetDoc, root ); |
|
234 } |
|
235 if( newData ) |
|
236 { |
|
237 aCache->Store( iTargetDoc.ETag(), name, root, newData->Des() ); |
|
238 CleanupStack::PopAndDestroy(); //newData |
|
239 } |
|
240 } |
|
241 |
|
242 // --------------------------------------------------------- |
|
243 // CXcapAddition::OperationFailedL |
|
244 // |
|
245 // --------------------------------------------------------- |
|
246 // |
|
247 void CXcapAddition::OperationFailedL() |
|
248 { |
|
249 #ifdef _DEBUG |
|
250 iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::OperationFailedL() - Error: %d" ), |
|
251 iStatus.Int() ); |
|
252 #endif |
|
253 if( iStatus.Int() >= KErrNone ) |
|
254 { |
|
255 TInt status = iActiveRequest->ResponseData()->iHttpStatus; |
|
256 TInt completion = iActiveRequest->ResponseData()->iCompletion; |
|
257 iResult = status < KErrNone || completion < KErrNone ? status : ReinterpretStatus( status ); |
|
258 } |
|
259 else iResult = iStatus.Int(); |
|
260 iCompleted = ETrue; |
|
261 } |
|
262 |
|
263 // --------------------------------------------------------- |
|
264 // CXcapAddition::Result |
|
265 // |
|
266 // --------------------------------------------------------- |
|
267 // |
|
268 TBool CXcapAddition::Result() const |
|
269 { |
|
270 return ETrue; |
|
271 } |
|
272 |
|
273 // --------------------------------------------------------- |
|
274 // CXcapAddition::~CXcapAddition |
|
275 // |
|
276 // --------------------------------------------------------- |
|
277 // |
|
278 CXcapAddition::~CXcapAddition() |
|
279 { |
|
280 #ifdef _DEBUG |
|
281 iOperationFactory.WriteToLog( _L8( "CXcapAddition::~CXcapAddition()" ) ); |
|
282 #endif |
|
283 } |
|
284 |
|
285 // End of File |
|
286 |