messagingfw/wappushfw/examples/PushAppHandlerEx/test/wappushmsggen.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "wappushmsggen.h"
       
    18 
       
    19 _LIT8(KXmlVersion, "<?xml version=\"1.0\"?>\r\n");
       
    20 _LIT8(KBadXmlVersion, "<?xml version=\"2.0\"?>\r\n");  // Unsupported XML version
       
    21 _LIT8(KSiStart, "<!DOCTYPE si PUBLIC \"-//WAPFORUM//DTD SI 1.0//EN\"\r\n\"http://www.wapforum.org/DTD/si.dtd\">\r\n<si>\r\n<indication ");
       
    22 _LIT8(KSiEnd, "</indication>\r\n</si>\r\n");
       
    23 _LIT8(KSlStart, "<!DOCTYPE sl PUBLIC \"-//WAPFORUM//DTD SL 1.0//EN\"\r\n\"http://www.wapforum.org/DTD/sl.dtd\">\r\n<sl ");
       
    24 _LIT8(KSlEnd, ">\r\n</sl>\r\n");
       
    25 
       
    26 // Valid XML tags and data
       
    27 _LIT8(KHRefField, "href=\"");
       
    28 _LIT8(KSiIdField, "si-id=\"");
       
    29 _LIT8(KSiCreatedField, "created=\"");
       
    30 _LIT8(KSiExpiresField, "si-expires=\"");
       
    31 _LIT8(KActionField, "action=\"");
       
    32 
       
    33 
       
    34 //Invalid XML tags & data 
       
    35 _LIT8(KBadHRefField, "hreg=\"");
       
    36 _LIT8(KBadSiIdField, "si-eyed=\"");
       
    37 _LIT8(KBadSiCreatedField, "crated=\"");
       
    38 _LIT8(KBadSiExpiresField, "si-xpires=\"");
       
    39 _LIT8(KBadActionField, "adtion=\"");
       
    40 
       
    41 
       
    42 _LIT8(KCrLf, "\r\n");
       
    43 _LIT8(KDoubleQuote,"\"");
       
    44 
       
    45 /** amount used to lengthen the buffer when appending data to it that would otherwise overflow it
       
    46 */
       
    47 const TInt KMsgBufferSegmentLength=256;
       
    48 
       
    49 /** public constructor */
       
    50 CWapPushMsgGen* CWapPushMsgGen::NewL()
       
    51 	{
       
    52 	CWapPushMsgGen* thisptr = new(ELeave) CWapPushMsgGen();
       
    53 	return thisptr;
       
    54 	}
       
    55 
       
    56 
       
    57 /** destructor */
       
    58 CWapPushMsgGen::~CWapPushMsgGen()
       
    59 	{
       
    60 	delete iMsgBuffer;
       
    61 	delete iMsgFieldBuffer;
       
    62 	}
       
    63 	
       
    64 
       
    65 /** appends a field to the end of the current message being generated
       
    66  @param aField	The type of field to be set
       
    67  @param aFieldValue The value of the field to be set. This should not 
       
    68  include CR/LF at the end for fields included in " "
       
    69 */
       
    70 void CWapPushMsgGen::AppendFieldL(TMsgField aFieldType, const TDesC8& aFieldValue)
       
    71 	{
       
    72 	if (!iMsgFieldBuffer)
       
    73 		iMsgFieldBuffer=HBufC8::NewL(KMsgBufferSegmentLength);
       
    74 
       
    75 	switch (aFieldType)
       
    76 		{
       
    77 	case EHRef:
       
    78 		AppendMsgFieldDesL(KHRefField);
       
    79 		break;
       
    80 	case EServiceIndicationID:
       
    81 		AppendMsgFieldDesL(KSiIdField);
       
    82 		break;
       
    83 	case EServiceIndicationCreated:
       
    84 		AppendMsgFieldDesL(KSiCreatedField);
       
    85 		break;
       
    86 	case EServiceIndicationExpires:
       
    87 		AppendMsgFieldDesL(KSiExpiresField);
       
    88 		break;
       
    89 	case EServiceIndicationText:
       
    90 		{
       
    91 		_LIT8(KEndIndication,">\r\n");
       
    92 		AppendMsgFieldDesL(KEndIndication);
       
    93 		}
       
    94 		break;
       
    95 	case EAction:
       
    96 		AppendMsgFieldDesL(KActionField);
       
    97 		break;
       
    98 
       
    99 	case EBadHRef:
       
   100 		AppendMsgFieldDesL(KBadHRefField);
       
   101 		break;
       
   102 	case EBadServiceIndicationID:
       
   103 		AppendMsgFieldDesL(KBadSiIdField);
       
   104 		break;
       
   105 	case EBadServiceIndicationCreated:
       
   106 		AppendMsgFieldDesL(KBadSiCreatedField);
       
   107 		break;
       
   108 	case EBadServiceIndicationExpires:
       
   109 		AppendMsgFieldDesL(KBadSiExpiresField);
       
   110 		break;
       
   111 	case EBadAction:
       
   112 		AppendMsgFieldDesL(KBadActionField);
       
   113 		break;
       
   114 	case EXMLNoFieldStr:
       
   115 	case EWBXMLNoFieldStr:
       
   116 		//Present so that can create an empty body using the msg Gen
       
   117 		break;
       
   118 	default:
       
   119 		Panic(EInvalidPushMessageField);
       
   120 		}
       
   121 
       
   122 	AppendMsgFieldDesL(aFieldValue);
       
   123 	//if (iMsgType != EServiceIndicationText)
       
   124 	if (aFieldType != EServiceIndicationText  && aFieldType != EXMLNoFieldStr  
       
   125 						&& aFieldType != EWBXMLNoFieldStr)
       
   126 		AppendMsgFieldDesL(KDoubleQuote);
       
   127 	if (aFieldType!= EWBXMLNoFieldStr)
       
   128 		AppendMsgFieldDesL(KCrLf);
       
   129 	}
       
   130 
       
   131 
       
   132 /** closes the current message and returns the whole generated message
       
   133  @return returns a dynamic descriptor that is on the cleanup stack
       
   134 */	
       
   135 HBufC8* CWapPushMsgGen::CreateMsgLC()
       
   136 	{
       
   137 	TInt size = (*iMsgBuffer).Length() + (*iMsgFieldBuffer).Length();
       
   138 	switch(iMsgType)
       
   139 		{
       
   140 	case EServiceIndication:
       
   141 		size+= KSiEnd().Length();
       
   142 		break;
       
   143 	case EServiceLoad:
       
   144 		size+=KSlEnd().Length();
       
   145 		break;
       
   146 	case ECorruptMsgTest:
       
   147 	case ECorruptXmlVersionTest:
       
   148 	case EEmptyMsgBody:
       
   149 		// No action - don't add anything extra to the body
       
   150 		break;
       
   151 	default:
       
   152 		Panic(EInvalidPushMessageType);
       
   153 		};
       
   154 
       
   155 	HBufC8* finalMsg=HBufC8::NewLC(size);
       
   156 	TPtr8 finalMsgPtr = finalMsg->Des();
       
   157 	finalMsgPtr.Append(*iMsgBuffer);
       
   158 	finalMsgPtr.Append(*iMsgFieldBuffer);
       
   159 
       
   160 	switch(iMsgType)
       
   161 		{
       
   162 	case EServiceIndication:
       
   163 		finalMsgPtr.Append(KSiEnd);
       
   164 		break;
       
   165 	case EServiceLoad:
       
   166 		finalMsgPtr.Append(KSlEnd);
       
   167 		break;
       
   168 	case ECorruptMsgTest:
       
   169 	case ECorruptXmlVersionTest:
       
   170 	case EEmptyMsgBody:
       
   171 		// Do nothing  - leave body as is
       
   172 		break;
       
   173 	default:
       
   174 		Panic(EInvalidPushMessageType);
       
   175 		}
       
   176 	return finalMsg;
       
   177 }
       
   178 
       
   179 
       
   180 /** starts a new message 
       
   181  This starts a new message by clearing the current one. It then appends the
       
   182  header for the type of message being generated
       
   183 
       
   184   @param aMsgType an enum of type TMsgType specifing the type of message to be generated
       
   185 */	
       
   186 void CWapPushMsgGen::StartNewMsgL(TMsgType aMsgType)
       
   187 	{
       
   188 	delete iMsgBuffer;
       
   189 	iMsgBuffer = NULL;
       
   190 	delete iMsgFieldBuffer;
       
   191 	iMsgFieldBuffer = NULL;
       
   192 	iMsgType = aMsgType;
       
   193 	iMsgBuffer = HBufC8::NewL(KMsgBufferSegmentLength);
       
   194 
       
   195 	
       
   196 	switch (iMsgType)
       
   197 		{
       
   198 	case EServiceIndication:
       
   199 		AppendMsgDesL(KXmlVersion);
       
   200 		AppendMsgDesL(KSiStart);
       
   201 		break;
       
   202 	case EServiceLoad:
       
   203 		AppendMsgDesL(KXmlVersion);
       
   204 		AppendMsgDesL(KSlStart);
       
   205 		break;
       
   206 	case ECorruptMsgTest:
       
   207 		AppendMsgDesL(KXmlVersion);
       
   208 		break;
       
   209 	case ECorruptXmlVersionTest:
       
   210 		AppendMsgDesL(KBadXmlVersion);
       
   211 		break;
       
   212 	case EEmptyMsgBody:
       
   213 		// do nothing - all we want is a Null Descriptor
       
   214 		break;
       
   215 	default:
       
   216 		Panic(EInvalidPushMessageType);
       
   217 		}
       
   218 	}
       
   219 
       
   220 
       
   221 /** used to append a descriptor to the current message buffer
       
   222   @param aDes, the descriptor to be added to the message buffer
       
   223 */
       
   224 void CWapPushMsgGen::AppendMsgDesL(const TDesC8& aDes)
       
   225 	{
       
   226 	TInt maxLength = iMsgBuffer->Des().MaxLength();
       
   227 	if (maxLength < aDes.Length() + (*iMsgBuffer).Length())
       
   228 		{
       
   229 		TInt newMaxLength = maxLength;
       
   230 		newMaxLength += aDes.Length() > KMsgBufferSegmentLength? aDes.Length() : KMsgBufferSegmentLength;
       
   231 		iMsgBuffer = iMsgBuffer->ReAllocL(newMaxLength);
       
   232 		}
       
   233 	iMsgBuffer->Des().Append(aDes);
       
   234 	}
       
   235 
       
   236 
       
   237 /**  used to append a descriptor to the current message field buffer 
       
   238   @param aDes, the descriptor to be added to the message buffer
       
   239 */
       
   240 void CWapPushMsgGen::AppendMsgFieldDesL(const TDesC8& aDes)
       
   241 	{
       
   242 	TInt maxLength = iMsgFieldBuffer->Des().MaxLength();
       
   243 	if (maxLength < aDes.Length() + (*iMsgFieldBuffer).Length())
       
   244 		{
       
   245 		TInt newMaxLength = maxLength;
       
   246 		newMaxLength += aDes.Length() > KMsgBufferSegmentLength? aDes.Length() : KMsgBufferSegmentLength;
       
   247 		iMsgFieldBuffer = iMsgFieldBuffer->ReAllocL(newMaxLength);
       
   248 		}
       
   249 	iMsgFieldBuffer->Des().Append(aDes);
       
   250 	}
       
   251 
       
   252 /** Panic function
       
   253 	
       
   254 */
       
   255 void CWapPushMsgGen::Panic(TWapPushMsgGenPanic aPanicCode)
       
   256 	{
       
   257 	_LIT(KWapPushMsgGenName,"PushMsgGen");
       
   258 	User::Panic(KWapPushMsgGenName, aPanicCode);
       
   259 	}