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