xdmprotocols/XcapProtocol/XcapOperations/src/XcapRetrieval.cpp
branchRCL_3
changeset 17 2669f8761a99
parent 16 2580314736af
child 18 fbd2e7cec7ef
equal deleted inserted replaced
16:2580314736af 17:2669f8761a99
     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:   CXcapRetrieval
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "XcapCache.h"
       
    23 #include "XcapAppUsage.h"
       
    24 #include "XcapDocument.h"
       
    25 #include "XcapProtocol.h"
       
    26 #include "XcapRetrieval.h"
       
    27 #include "XcapUriParser.h"
       
    28 #include "XdmXmlParser.h"
       
    29 #include "XcapHttpReqGet.h"
       
    30 #include "XcapHttpTransport.h"
       
    31 #include "XcapOperationFactory.h"
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // C++ constructor can NOT contain any code, that
       
    37 // might leave.
       
    38 // ---------------------------------------------------------
       
    39 //
       
    40 CXcapRetrieval::CXcapRetrieval( CXcapDocument& aParentDoc,
       
    41                                 CXcapDocumentNode* aTargetNode,
       
    42                                 CXcapOperationFactory& aOperationFactory ) :
       
    43                                 CXcapHttpOperation( aParentDoc, aTargetNode, aOperationFactory ),
       
    44                                 iCacheOperation( EFalse ),
       
    45                                 iOperationType( aTargetNode == NULL ?
       
    46                                 EXdmDocument : EXdmPartialDocument )                                  
       
    47     {
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------
       
    51 // CXcapRetrieval::NewL
       
    52 //
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 CXcapRetrieval* CXcapRetrieval::NewL( CXcapDocument& aParentDoc,
       
    56                                       CXcapDocumentNode* aTargetNode,
       
    57                                       CXcapOperationFactory& aOperationFactory )
       
    58     {
       
    59     CXcapRetrieval* self = new ( ELeave ) CXcapRetrieval( aParentDoc, aTargetNode, aOperationFactory );
       
    60     CleanupStack::PushL( self );
       
    61     self->BaseConstructL();
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop();
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // CXcapRetrieval::ConstructL
       
    69 //
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 void CXcapRetrieval::ConstructL()
       
    73     {
       
    74 #ifdef _DEBUG
       
    75     iOperationFactory.WriteToLog( _L8( "-> CXcapRetrieval::ConstructL" ) );  
       
    76 #endif
       
    77     CXcapHttpReqGet* request = Transport().GetL( iTargetDoc.Name() );
       
    78     CleanupStack::PushL( request );
       
    79     User::LeaveIfError( iRequestQueue.Append( request ) );
       
    80     request->SetExpiryTimeL( NULL, KDefaultHttpRequestTimeout * 1000000 );
       
    81     CleanupStack::Pop();  //request
       
    82     if( iOperationType != EXdmDocument && iDocumentSubset != NULL )
       
    83         {
       
    84         iUriParser->SetDocumentSubset( iDocumentSubset );
       
    85         //Add namespace mappings
       
    86         User::LeaveIfError( iTargetDoc.ApplicationUsage().Validate(
       
    87                             *iDocumentSubset, iUriParser, ETrue ) );
       
    88         }
       
    89 #ifdef _DEBUG
       
    90     iOperationFactory.WriteToLog( _L8( "<- CXcapRetrieval::ConstructL" ) );  
       
    91 #endif
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------
       
    95 // CXcapRetrieval::ExecuteL
       
    96 //
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CXcapRetrieval::ExecuteL()
       
   100     {
       
   101     #ifdef _DEBUG
       
   102         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::ExecuteL()" ) );  
       
   103     #endif
       
   104     TPtrC8 eTag = iTargetDoc.ETag();
       
   105     if( eTag.Length() > 0 )
       
   106         {
       
   107         #ifdef _DEBUG
       
   108             iOperationFactory.WriteToLog( _L8( " Using ETag \"%S\" - Length: %d" ),
       
   109                                                &eTag, eTag.Length() );  
       
   110         #endif
       
   111         //iActiveRequest->SetHeaderL( KHttpHeaderIfNoneMatch, eTag );
       
   112         }
       
   113     TRAPD( error, iUriParser->ParseL( iActiveRequest->RequestUriL() ) );
       
   114     if( error == KErrNone )
       
   115         {
       
   116         TPtrC8 uri = iUriParser->DesC8();
       
   117         HBufC8* escape = CXcapHttpOperation::EscapeLC( uri );
       
   118         iActiveRequest->UpdateRequestUriL( escape->Des() );
       
   119         CleanupStack::PopAndDestroy();  //escape
       
   120         #ifdef _DEBUG
       
   121             iOperationFactory.WriteToLog( _L8( " New URI: %S" ), &uri );  
       
   122         #endif
       
   123         }
       
   124     }
       
   125         
       
   126 // ---------------------------------------------------------
       
   127 // CXcapRetrieval::OperationCompleteL
       
   128 //
       
   129 /* const TDesC8& aETag, 
       
   130    const TDesC& aDocumentName,
       
   131    const TDesC8& aRootLocation,
       
   132    const TDesC8& aResponseData*/
       
   133 // ---------------------------------------------------------
       
   134 //
       
   135 void CXcapRetrieval::OperationCompleteL()
       
   136     {
       
   137     #ifdef _DEBUG
       
   138         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::OperationCompleteL()" ) );  
       
   139     #endif
       
   140     TInt generalErr = ReinterpretStatus( iRequestData->iHttpStatus );
       
   141     if( generalErr == KErrNone )
       
   142         {
       
   143         #ifdef _DEBUG
       
   144             iOperationFactory.WriteToLog( _L8( " No general errors found" ) );  
       
   145         #endif
       
   146         iCacheOperation ? HandleCacheOperationL() : HandleNetworkOperationL();
       
   147         }
       
   148     else
       
   149         {
       
   150         #ifdef _DEBUG
       
   151             iOperationFactory.WriteToLog( _L8( " General errors found - Status: %d  Error: %d" ),
       
   152                                                    iRequestData->iHttpStatus, generalErr );  
       
   153         #endif
       
   154         iCompleted = ETrue;
       
   155         iResult = generalErr;
       
   156         }
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------
       
   160 // CXcapRetrieval::HandleCacheOperationL
       
   161 //
       
   162 // ---------------------------------------------------------
       
   163 //
       
   164 void CXcapRetrieval::HandleCacheOperationL()
       
   165     {
       
   166     #ifdef _DEBUG
       
   167         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::HandleCacheOperationL() - Operation type: %d  Target node: %x" ),
       
   168                                            iOperationType, iDocumentSubset );  
       
   169     #endif
       
   170     TPtrC8 responseData = Descriptor( iRequestData->iResponseData );
       
   171     iOperationType == EXdmDocument && iDocumentSubset == NULL ? 
       
   172         iXmlParser->ParseDocumentL( &iTargetDoc, responseData ) :
       
   173         iXmlParser->ParseDocumentL( &iTargetDoc, responseData, iDocumentSubset );
       
   174     iCompleted = ETrue;
       
   175     }
       
   176     
       
   177 // ---------------------------------------------------------
       
   178 // CXcapRetrieval::HandleNetworkOperationL
       
   179 //
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 void CXcapRetrieval::HandleNetworkOperationL()
       
   183     {
       
   184     #ifdef _DEBUG
       
   185         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::HandleNetworkOperationL()" ) );  
       
   186     #endif
       
   187     TPtrC name = iTargetDoc.Name();
       
   188     TPtrC8 root = Transport().RootUri();
       
   189     switch( iRequestData->iHttpStatus )
       
   190         {
       
   191         case 200:           //ETag was stale
       
   192             {
       
   193             #ifdef _DEBUG
       
   194                 iOperationFactory.WriteToLog( _L8( " Status 200 - ETag was stale" ) );  
       
   195             #endif
       
   196             TPtrC8 responseData = Descriptor( iRequestData->iResponseData );
       
   197             if( responseData.Length() <= 0 )
       
   198                 break;
       
   199             if( iOperationType == EXdmDocument )
       
   200                 {
       
   201                 TPtrC8 eTag = Descriptor( iRequestData->iETag );
       
   202                 //If the ETag has not changed, server should return
       
   203                 //304 (Not modified), but just in case, let's check the 
       
   204                 //ETag value, anyway.
       
   205                 if( eTag.Length() > 0 && eTag.Compare( iTargetDoc.ETag() ) != 0 )
       
   206                     {
       
   207                     iTargetDoc.SetETag( eTag );
       
   208                     RXcapCache* cache = iTargetDoc.Protocol().Cache();
       
   209                     if( cache && !( iOptionFlags & KNoCache ) )
       
   210                         cache->Store( iTargetDoc.ETag(), name, root, responseData  );
       
   211                     }
       
   212                 else
       
   213                     {
       
   214                     #ifdef _DEBUG
       
   215                         iOperationFactory.WriteToLog( _L8( " ETag values match, do not update cache" ) );  
       
   216                     #endif
       
   217                     }
       
   218                 iXmlParser->ParseDocumentL( &iTargetDoc, responseData );
       
   219                 }
       
   220             else if( iOperationType == EXdmPartialDocument )
       
   221                 {
       
   222                 #ifdef _DEBUG  
       
   223                     iOperationFactory.WriteToLog( _L8( " Parse a partial document" ) );  
       
   224                 #endif
       
   225                 //The target node must be emptied before inserting new content
       
   226                 iDocumentSubset->SetEmptyNode( ETrue );
       
   227                 iDocumentSubset->SetEmptyNode( EFalse );
       
   228                 iXmlParser->ParseDocumentL( responseData, iDocumentSubset );
       
   229                	}
       
   230             iCompleted = ETrue;
       
   231             }
       
   232             break;
       
   233         case 304:
       
   234             {
       
   235             RXcapCache* cache = iTargetDoc.Protocol().Cache();
       
   236             if( cache )
       
   237                 {
       
   238                 #ifdef _DEBUG   //ETag is up to date - cache still valid
       
   239                     iOperationFactory.WriteToLog( _L8( " Status 304 - Cached version is valid" ) );  
       
   240                 #endif
       
   241                 TInt length = iTargetDoc.DataLength();
       
   242                 __ASSERT_DEBUG( length > 0, User::Panic( _L( "CXcapRetrieval" ), 1 ) );
       
   243                 delete iRequestData->iResponseData;
       
   244                 iRequestData->iResponseData = NULL;
       
   245                 iRequestData->iResponseData = HBufC8::NewL( length );
       
   246                 TPtr8 desc( iRequestData->iResponseData->Des() );
       
   247                 cache->FetchDocumentContent( desc, name, root );
       
   248                 if( iOperationType == EXdmDocument && !iTargetDoc.DocumentRoot() )
       
   249                 	{
       
   250                 	#ifdef _DEBUG  
       
   251                     	iOperationFactory.WriteToLog( _L8( " No content => parse cached document" ) );  
       
   252                 	#endif
       
   253 					iXmlParser->ParseDocumentL( &iTargetDoc, desc );
       
   254                 	}
       
   255                	else if( iOperationType == EXdmPartialDocument )
       
   256                		{
       
   257                		#ifdef _DEBUG  
       
   258                     	iOperationFactory.WriteToLog( _L8( " Parse a partial document" ) );  
       
   259                 	#endif
       
   260 					iXmlParser->ParseDocumentL( &iTargetDoc, desc, iDocumentSubset );
       
   261                		}
       
   262                 }
       
   263             iCompleted = ETrue;
       
   264             }
       
   265             break;
       
   266         default:
       
   267             #ifdef _DEBUG
       
   268                 iOperationFactory.WriteToLog( _L8( " Default case - Status: %d" ),
       
   269                                                    iRequestData->iHttpStatus );  
       
   270             #endif
       
   271             iCompleted = ETrue;
       
   272             break;
       
   273         }
       
   274     }
       
   275   
       
   276 // ---------------------------------------------------------
       
   277 // CXcapRetrieval::OperationFailedL
       
   278 //
       
   279 // ---------------------------------------------------------
       
   280 //
       
   281 void CXcapRetrieval::OperationFailedL()
       
   282     {
       
   283     #ifdef _DEBUG
       
   284         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::OperationFailedL() - Error: %d" ),
       
   285                                         iStatus.Int() );  
       
   286     #endif
       
   287     if( iStatus.Int() >= KErrNone )
       
   288         {
       
   289         TInt status = iActiveRequest->ResponseData()->iHttpStatus;
       
   290         TInt completion = iActiveRequest->ResponseData()->iCompletion;
       
   291         iResult = status < KErrNone || completion < KErrNone ? status : ReinterpretStatus( status );
       
   292         }
       
   293     else iResult = iStatus.Int();
       
   294     iCompleted = ETrue;
       
   295     }
       
   296     
       
   297 // ---------------------------------------------------------
       
   298 // CXcapRetrieval::Result
       
   299 //
       
   300 // ---------------------------------------------------------
       
   301 //
       
   302 TInt CXcapRetrieval::Result() const
       
   303     {
       
   304     return iRequestData->iCompletion;
       
   305     }
       
   306 
       
   307 // ---------------------------------------------------------
       
   308 // CXcapRetrieval::~CXcapRetrieval
       
   309 //
       
   310 // ---------------------------------------------------------
       
   311 //
       
   312 CXcapRetrieval::~CXcapRetrieval()
       
   313     {
       
   314     #ifdef _DEBUG
       
   315         iOperationFactory.WriteToLog( _L8( "CXcapRetrieval::~CXcapRetrieval()" ) );  
       
   316     #endif
       
   317     }
       
   318 
       
   319 //  End of File  
       
   320