ncdengine/provider/server/src/ncdsendhttprequestoperationimpl.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdsendhttprequestoperationimpl.h"
       
    20 
       
    21 #include <s32mem.h>
       
    22 #include <apmstd.h>
       
    23 
       
    24 #include "catalogsbasemessage.h"
       
    25 #include "catalogshttpincludes.h"
       
    26 #include "catalogsutils.h"
       
    27 #include "catalogscontext.h"
       
    28 #include "ncdproviderdefines.h"
       
    29 #include "catalogshttprequestadapter.h"
       
    30 #include "catalogshttpresponsecomposer.h"
       
    31 #include "ncdproviderutils.h"
       
    32 #include "ncdhttputils.h"
       
    33 #include "ncdgeneralmanager.h"
       
    34 
       
    35 #include "catalogsdebug.h"
       
    36 
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // NewL
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CNcdSendHttpRequestOperation* CNcdSendHttpRequestOperation::NewL( 
       
    45     HBufC8* aUri,
       
    46     HBufC8* aRequest,        
       
    47     const TNcdConnectionMethod& aMethod,
       
    48     CNcdGeneralManager& aGeneralManager,
       
    49     MNcdOperationRemoveHandler& aRemoveHandler, 
       
    50     MCatalogsHttpSession& aHttpSession,
       
    51     MCatalogsSession& aSession )
       
    52     {
       
    53     CNcdSendHttpRequestOperation* self = new( ELeave ) CNcdSendHttpRequestOperation(      
       
    54         aGeneralManager,
       
    55         aRemoveHandler, 
       
    56         aHttpSession,
       
    57         aSession );
       
    58     CleanupClosePushL( *self );
       
    59     self->ConstructL( aMethod, aUri, aRequest );
       
    60     CleanupStack::Pop();
       
    61     return self;
       
    62     }
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // Destructor
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CNcdSendHttpRequestOperation::~CNcdSendHttpRequestOperation()
       
    70     {
       
    71     DLTRACEIN( ( "" ) );    
       
    72     if ( iTransaction ) 
       
    73         {
       
    74         iTransaction->Release();
       
    75         iTransaction = NULL;
       
    76         }
       
    77     delete iUri;
       
    78     delete iRequest;
       
    79     delete iResponse;
       
    80     delete iAdapter;
       
    81     delete iBody;
       
    82     }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // HTTP event handler
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CNcdSendHttpRequestOperation::HandleHttpEventL( 
       
    90     MCatalogsHttpOperation& aOperation, 
       
    91     TCatalogsHttpEvent aEvent )
       
    92     {
       
    93     DLTRACEIN((""));
       
    94     
       
    95     if ( aEvent.iOperationState == ECatalogsHttpOpInProgress &&
       
    96          aEvent.iProgressState == ECatalogsHttpResponseBodyReceived ) 
       
    97         {
       
    98         // Append the body
       
    99         iBody->InsertL( iBody->Size(), aOperation.Body() );
       
   100         }
       
   101     else if ( aEvent.iOperationState == ECatalogsHttpOpCompleted ) 
       
   102         {
       
   103         DLTRACE(("Operation complete"));
       
   104         // Compose the response here
       
   105         TCatalogsHttpResponseComposer composer;
       
   106         iResponse = composer.ComposeResponseL( 
       
   107             aOperation, 
       
   108             iBody->Ptr( 0 ) );
       
   109         
       
   110         delete iBody;
       
   111         iBody = NULL;
       
   112         aOperation.Release();
       
   113         iTransaction = NULL;
       
   114         iOperationState = EStateComplete;
       
   115         RunOperation();
       
   116         }
       
   117     }
       
   118     
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // HTTP error handler
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 TBool CNcdSendHttpRequestOperation::HandleHttpError(
       
   125     MCatalogsHttpOperation& aOperation,
       
   126     TCatalogsHttpError aError )
       
   127     {
       
   128     DLTRACEIN((""));
       
   129     aOperation.Cancel();
       
   130     iTransaction = NULL;
       
   131     iError = aError.iError;
       
   132     iOperationState = EStateComplete;
       
   133     
       
   134     RunOperation();
       
   135         
       
   136     return ETrue;
       
   137     }
       
   138     
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Cancel
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CNcdSendHttpRequestOperation::Cancel() 
       
   145     {    
       
   146     DLTRACEIN(( "" ));
       
   147     if ( iTransaction ) 
       
   148         {
       
   149         iTransaction->Cancel();
       
   150         iTransaction = NULL;
       
   151         }
       
   152     }
       
   153 
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // ReceiveMessage
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void CNcdSendHttpRequestOperation::ReceiveMessage( 
       
   160     MCatalogsBaseMessage* aMessage,
       
   161     TInt aFunctionNumber )
       
   162     {
       
   163     DLTRACEIN(( "Function: %d", aFunctionNumber ));
       
   164     
       
   165     TInt err = KErrNone;
       
   166         
       
   167     // Response to pause and resume messages, other messages are handled
       
   168     // by the base class
       
   169     switch ( aFunctionNumber )
       
   170         {
       
   171         case ENCDOperationFunctionGetData: 
       
   172             {
       
   173             DLTRACE(( "ENCDOperationFunctionGetData "));
       
   174             // Only error will come from CompleteMessageL
       
   175             TRAP( err, GetResponseL( *aMessage ) );
       
   176             break;
       
   177             }
       
   178                         
       
   179         default:
       
   180             {
       
   181             DLTRACE(("Calling baseclass"));
       
   182             // Call implementation in the base class
       
   183             CNcdBaseOperation::ReceiveMessage( aMessage, aFunctionNumber );
       
   184             DLTRACEOUT(( "Called baseclass" ));
       
   185             return;
       
   186             }
       
   187         }                    
       
   188     
       
   189     HandleError( err );
       
   190     DLTRACEOUT(( "" ));
       
   191     }
       
   192 
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // RunOperation
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 TInt CNcdSendHttpRequestOperation::RunOperation()
       
   199     {
       
   200     DLTRACEIN(( "Pending message: %X", iPendingMessage ));
       
   201     
       
   202     
       
   203     if ( !iPendingMessage ) 
       
   204         {      
       
   205         DLTRACE(("No pending message"));  
       
   206         return KErrNotReady;
       
   207         }
       
   208     
       
   209     TRAPD( err, HandleStateL() );
       
   210     HandleError( err );
       
   211     
       
   212     DLTRACEOUT(("err: %d", err));
       
   213     return err;
       
   214     }
       
   215 
       
   216 
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // 
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 void CNcdSendHttpRequestOperation::HandleStateL()
       
   223     {
       
   224     switch ( iOperationState ) 
       
   225         {
       
   226         case EStateRunning: 
       
   227             {
       
   228             DLTRACE(("Creating request"));
       
   229             DASSERT( iRequest );            
       
   230             iTransaction = iAdapter->CreateTransactionL( 
       
   231                 *iUri,
       
   232                 *iRequest,
       
   233                 *this );
       
   234             
       
   235             iTransaction->Config().SetConnectionMethod( 
       
   236                 iConnectionMethod );
       
   237                 
       
   238             TInt err = iTransaction->Start();
       
   239             if ( err != KErrNone ) 
       
   240                 {
       
   241                 DLERROR(("Start failed with %d", err));
       
   242                 iTransaction->Cancel();
       
   243                 iTransaction = NULL;
       
   244                 User::Leave( err );
       
   245                 }
       
   246                         
       
   247             break;
       
   248             }
       
   249             
       
   250         case EStateComplete:
       
   251             {
       
   252             // Returns false if there was no error
       
   253             if ( !HandleError( iError ) ) 
       
   254                 {
       
   255                 DLTRACE(("Completing the operation"));                
       
   256                 CNcdBaseOperation::CompleteMessage( 
       
   257                     iPendingMessage,
       
   258                     ENCDOperationMessageCompletionComplete, 
       
   259                     iProgress,
       
   260                     KErrNone );
       
   261                 
       
   262                 }
       
   263             break;
       
   264             }
       
   265         
       
   266         default: 
       
   267             {
       
   268             DASSERT( 0 );
       
   269             }
       
   270         }
       
   271     
       
   272     }
       
   273     
       
   274     
       
   275 // ---------------------------------------------------------------------------
       
   276 // 
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 TBool CNcdSendHttpRequestOperation::HandleError( TInt aError ) 
       
   280     {
       
   281     DLTRACEIN(("aError: %d", aError ));
       
   282     if ( aError != KErrNone ) 
       
   283         {
       
   284         DLERROR(("Error %d occurred", aError ));
       
   285         iError = aError;
       
   286         iOperationState = EStateCancelled;
       
   287         Cancel();
       
   288         if ( iPendingMessage )
       
   289             {
       
   290             // ignoring error because operation already failed
       
   291             CNcdBaseOperation::CompleteMessage( iPendingMessage,
       
   292                 ENCDOperationMessageCompletionError, iError );
       
   293             }
       
   294         return ETrue;
       
   295         }    
       
   296     return EFalse;
       
   297     }
       
   298     
       
   299     
       
   300 // ---------------------------------------------------------------------------
       
   301 // 
       
   302 // ---------------------------------------------------------------------------
       
   303 //    
       
   304 void CNcdSendHttpRequestOperation::GetResponseL( 
       
   305     MCatalogsBaseMessage& aMessage )
       
   306     {
       
   307     DLTRACEIN((""));
       
   308     
       
   309     if ( !iResponse ) 
       
   310         {
       
   311         User::Leave( KErrCorrupt );
       
   312         }
       
   313                 
       
   314     aMessage.CompleteAndReleaseL(
       
   315         *iResponse, 
       
   316         KErrNone );
       
   317     
       
   318     }
       
   319     
       
   320     
       
   321 // ---------------------------------------------------------------------------
       
   322 // Constructor
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 CNcdSendHttpRequestOperation::CNcdSendHttpRequestOperation(
       
   326     CNcdGeneralManager& aGeneralManager,
       
   327     MNcdOperationRemoveHandler& aRemoveHandler,
       
   328     MCatalogsHttpSession& aHttpSession,
       
   329     MCatalogsSession& aSession )
       
   330     :
       
   331     CNcdBaseOperation( aGeneralManager, &aRemoveHandler, ESendHttpRequestOperation,
       
   332         aSession ), 
       
   333     iHttpSession( aHttpSession )
       
   334     {
       
   335     }
       
   336 
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // ConstructL
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 void CNcdSendHttpRequestOperation::ConstructL(
       
   343     const TNcdConnectionMethod& aMethod,
       
   344     HBufC8* aUri,
       
   345     HBufC8* aRequest )
       
   346     {
       
   347     DLTRACEIN( ( "" ) );
       
   348 
       
   349     // Call ConstructL for the base class
       
   350     CNcdBaseOperation::ConstructL();
       
   351 
       
   352     iAdapter = CCatalogsHttpRequestAdapter::NewL( iHttpSession );
       
   353     iBody = CBufFlat::NewL( NcdProviderDefines::KNcdBufferExpandSize );
       
   354     iGeneralManager.HttpUtils().ConvertConnectionMethod( 
       
   355         aMethod,
       
   356         iConnectionMethod );
       
   357     iUri = aUri;
       
   358     iRequest = aRequest;
       
   359     }
       
   360