<% 
' Copyright 2000 Phone.com, Inc.  All rights reserved.
'
'   Subject to the terms and conditions of the SDK License Agreement,
'   Phone.com, Inc. hereby grants you the right to use the UP.SDK
'   software and its related documentation.
'
'   The Phone.com name and logo and the family of terms carrying the "UP."
'   prefix are trademarks of Phone.com, Inc. All other brands and product
'   names may be trademarks of the respective companies with which they
'   are associated.
'
' sendAlert.asp
 '
'   This application returns a WML deck that allows the user
'   to send several different types of demo alerts back to
'   the phone via the UP.Link Messenger service.
'
'   The application sends a "prefetch" notification type to
'   the UP.Link Messenger.  This results in the Messenger
'   requesting the fetch.asp companion script, which loads
'   the alert message and deck to the phone.   The notification
'   can be sent secure or non-secure.  The application must
'   provide an SSL certificate to post a secure notification.
'
'   The example displays a deck containing the value of the last
'   result returned from server.
'
'   The example will not work with a browser connected in HTTP-Direct mode.
'   However, the prefetch URL and its associated argument list can be
'   retrieved directly from the Service Error card; the alert application
'   content of the fetched URL should then trigger a Sim notification. 
'
' Declarations 
' 
Dim upLink, subId, acceptCharset, nl
Dim preFetchUrl, ttl, alertType, title
Dim ntfn, msg, result
Dim deckH
Dim requireSecure, secureRequest, demo, secure
Dim CHTTPStatusNoContent

' Constants
'
'   The Prefetch URL returns a digest containing an alert + WML deck.
'   IMPORTANT:  Set this to the URL for your locally installed copy
'               of the fetch.asp script.  The URL must be accessible
'               via the public Internet.  If you send a secure
'               notification, you must insure that your SSL certificate
'               matches the server domain for the prefetch URL.
'
' preFetchUrl = "http://your.domain.com/.../examples/asp/fetch.asp"
'
'               The Subroutine ConstructPreFetchUrl constructs a
'               preFetchUrl by replacing the name of the sendalert
'               script below to contruct the supposed location of a
'               fetch.asp script. If this URL is not accessible via
'               the public internet to your UPLink, then replace it
'               with a hard-wired preFetchUrl. See further: Sub Main. 

'   The TTL indicates how long the notification will remain pending in
'   the UP.Link Messenger's queue if it cannot be delivered.
'
ttl             = 3600

'   Set to 1 if you require the UP.Link to support secure notifications.
'
requireSecure   = 0

'   Path to the SSL certificate file and its password.
'
certFile        = "mycert.pem"
' Map the path to the location of the certification file 
'
certFilePath    = Server.MapPath(certFile)

password        = "my_password"

alertType       = "D---"
title           = "Call: " & subId

' A newline (linefeed) is appended to each line of WML to
' aid legibility of source dumped to the Sim Info Window.
' 
nl              = Chr(10)

' WML Decks
'

'   A Home deck, deckH
'
deckH =         "<wml>" & nl
deckH = deckH & "  <head>" & nl
deckH = deckH & "    <meta forua=""true"" name=""vnd.up.markable"" "
deckH = deckH &        "content=""no-cache""/>" & nl
deckH = deckH & "  </head>" & nl
deckH = deckH & "  <card id=""homecard"" title=""Send Alert"">" & nl
deckH = deckH & "    <do type=""accept"" label=""Send"">" & nl
deckH = deckH & "      <go href=""#c2""/>" & nl
deckH = deckH & "    </do>" & nl
deckH = deckH & "    <p>" & nl
deckH = deckH & "      Alert demo: " & nl
deckH = deckH & "      <select name=""demo"">" & nl
deckH = deckH & "        <option value=""DISPATCH"">Dispatch</option>" & nl
deckH = deckH & "        <option value=""STOCK"">Stock</option>" & nl
deckH = deckH & "        <option value=""MEETING"">Meeting</option>" & nl
deckH = deckH & "      </select>" & nl
deckH = deckH & "    </p>" & nl
deckH = deckH & "  </card>" & nl
deckH = deckH & "  <card id=""c2"">" & nl
deckH = deckH & "    <onevent type=""onenterbackward"">" & nl
deckH = deckH & "      <prev/>" & nl
deckH = deckH & "    </onevent>" & nl
deckH = deckH & "    <do type=""accept"" label=""Yes"">" & nl
deckH = deckH & "      <go href=""?DEMO=$(demo)&amp;SECURE=yes""/>" & nl
deckH = deckH & "    </do>" & nl
deckH = deckH & "    <do type=""options"" label=""No"">" & nl
deckH = deckH & "      <go href=""?DEMO=$(demo)&amp;SECURE=no""/>" & nl
deckH = deckH & "    </do>" & nl
deckH = deckH & "    <p>" & nl
deckH = deckH & "      Use SSL to secure?" & nl
deckH = deckH & "    </p>" & nl
deckH = deckH & "  </card>" & nl
deckH = deckH & "</wml>" & nl

' A subroutine to construct a preFetchUrl using current the URL placement
'
Sub ConstructPreFetchUrl
Dim anURL

  anURL = Request.ServerVariables("SERVER_NAME")
  anURL = anURL & Request.ServerVariables("URL")
  anURL = Replace(anURL, "sendAlert", "fetch")
  preFetchUrl   = "http://" & anURL

End Sub

' A subroutine to report a success deck, deckS
'
Sub SuccessDeck()
    Dim deckS
    deckS =         "<wml>" & nl
    deckS = deckS & "  <head>" & nl
    deckS = deckS & "    <meta forua=""true"" "
    deckS = deckS & "      http-equiv=""cache-control"" "
    deckS = deckS & "      content=""true""/>" & nl
    deckS = deckS & "  </head>" & nl
    deckS = deckS & "  <card>" & nl
    deckS = deckS & "    <p>" & nl
    deckS = deckS & "      Alert sent with URL: " & preFetchUrl & nl
    deckS = deckS & "      Secure=" & secure & " " & nl
    deckS = deckS & "    </p>" & nl
    deckS = deckS & "  </card>" & nl
    deckS = deckS & "</wml>" & nl
    call OutputDeck(deckS)
End Sub

' A subroutine to report an error deck, deckE
'
Sub ErrorDeck()
    deckE =         "<wml>" & nl
    deckE = deckE & "  <head>" & nl
    deckE = deckE & "    <meta forua=""true"" " 
    deckE = deckE & "      http-equiv=""cache-control"" " 
    deckE = deckE & "      content=""max-age=0""/>" & nl
    deckE = deckE & "  </head>" & nl
    deckE = deckE & "  <card>" & nl
    deckE = deckE & "    <do type=""accept"" label=""Fetch?"">" & nl
    deckE = deckE & "      <go href=""" & preFetchUrl & """/>" & nl
    deckE = deckE & "    </do>" & nl
    deckE = deckE & "    <do type=""options"" label=""Home"">" & nl
    deckE = deckE & "      <go href=""device:home""/>" & nl
    deckE = deckE & "    </do>" & nl
    deckE = deckE & "    <p>" & nl
    deckE = deckE & "      Service Error" & nl
    deckE = deckE & "    </p>" & nl
    deckE = deckE & "    <p mode=""nowrap"">" & nl
    deckE = deckE & "      " & msg & nl
    deckE = deckE & "    </p>" & nl
    deckE = deckE & "    <p mode=""nowrap"">" & nl
    deckE = deckE & "      " & result & nl
    deckE = deckE & "    </p>" & nl
    deckE = deckE & "    <p mode=""nowrap"">" & nl
    deckE = deckE & "      " & preFetchUrl & nl
    deckE = deckE & "    </p>" & nl
    deckE = deckE & "  </card>" & nl
    deckE = deckE & "</wml>" & nl
    call WriteDeck(deckE)
End Sub

' Main routine
'
Sub Main()
    ' Get the subscriber ID and UPLink name and accept charset
    ' from the Request object.
    upLink          = Request.ServerVariables("HTTP_X_UP_UPLINK")
    subId           = Request.ServerVariables("HTTP_X_UP_SUBNO")
    acceptCharset   = Request.ServerVariables("HTTP_ACCEPT_CHARSET")
    
    ' Check whether a query string was appended to the Request
    '
    qCount  = Request.QueryString.Count
    If qCount       = 0 Then
        ' No, so just write out the Home deck.
        '
        call WriteDeck(deckH)
    Else
        ' Check for and extract query strings
        '
        If Request.QueryString("DEMO") <> "" Then
            demo    = Request.QueryString("DEMO")(1)
        End If

        If Request.QueryString("SECURE") <> "" Then
            secure  = Request.QueryString("SECURE") (1)
        End If
    
        Call ConstructPreFetchUrl
        ' Or declare a constant URL instead.

        Call PostPrefetch
    End If 
End Sub

' Subroutine to post a prefetch notification 
'
Sub PostPrefetch()
    Dim protocolType, retVal, clientNtfnCharset
    
    ' The CHTTPStatusNoContent constant is used because the 
    ' UP.Link Server's response does not contain any content.
    ' See further: 
    '
    '   Tools and API Reference, 
    '   COM Notification Library Reference, 
    '   Non-secure Notification Class, NtfnPostPrefetch
    '
    ' Declare a string constant to match this return value
    '
    Const CHTTPStatusNoContent = "204" 

    ' Append the user's demo selection string to the requested URL
    '
    preFetchUrl     = preFetchUrl & "?NEXT=" & demo

    ' Create an instance of a notification COM object, ntfn, 
    ' with a security qualification matching the user's selection.
    '
    If secure = "yes" Then
        Set ntfn    = Server.CreateObject("Ntfn3SClient.Ntfn3SClient.1")
    Else
        Set ntfn    = Server.CreateObject("Ntfn3Client.Ntfn3Client.1")
    End If

    ' A way to confirm creation of an object instance will be to check
    ' for a value of its connection type protocol 
    protocolType    = ntfn.NtfnGetProtocolType

    If protocolType = "" Then
        msg         = "Notification class error,"
        result      = "No alert sent."
        call ErrorDeck()
    Else
        ' Set the UP.Link server name
        '
        ntfn.NtfnSetHost upLink

        If secure   = "yes" Then
            ' Set the Certificate and Password
            '
            ntfn.NtfnLoadCertAndKey certFilePath, password

            ' Set the fail-over state to a constant 
            ' 
            ntfn.NtfnRequireSecureConnection requireSecure
        End If 

        clientNtfnCharset = ntfn.NtfnGetCharset 

        ' Set the alert title charset back to its current value.
        ntfn.NtfnSetCharset clientNtfnCharset

        ' Post the prefetch notification
        '
        ntfn.NtfnPostPrefetch subId, preFetchUrl, ttl

        ' Get the notification return status (long integer),
        ' convert it to type string, and assign it to result value.
        ' 
        result      = Cstr(ntfn.NtfnGetLastResult)
        
        If result   = CHTTPStatusNoContent Then
            call SuccessDeck()
        Else
            ' Get the notification error detail
            '
            msg     = ntfn.NtfnGetErrorDetail

            If msg  <> NULL Then
                call ErrorDeck()
            Else
                msg = "No error detail, error number: " 
                call ErrorDeck()
            End If
        End If
    End If
End Sub

' Subroutine to dispatch a deck without using a digest
'
Sub OutputDeck(aDeck)
    Response.ContentType = "text/vnd.wap.wml"
    Response.write("<?xml version=""1.0""?>")
    Response.write("<!DOCTYPE wml PUBLIC ""-//PHONE.COM//DTD WML 1.1//EN"" ""http://www.phone.com/dtd/wml11.dtd"">")
    Response.write(aDeck)
End Sub


' Subroutine to dispatch a deck in a digest to the subscriber
'
Sub WriteDeck(aDeck)

    ' Create a new COM Digest object.
    '
    set myDigest    = Server.CreateObject("PDCDigestUtils.CPDCDigest")
    
    ' Add the deck to the digest object.
    '
    myDigest.DigestAddDeck "?", aDeck
    
    ' Suppress content type output within the digest to prevent a 
    ' server from encapsulating WML content within a second boundary.
    '
    myDigest.DigestSuppressContentType 1
    
    ' Serialize the digest into a single output buffer, digestString.
    '
    digestString    = myDigest.DigestSerialize(0)
    
    ' Specify a deck of type WML to the digest object.
    '
    myDigest.DeckType         = 1
    
    ' Specify content type through the server response, 
    ' rather than within the digest.
    '
    Response.ContentType    = myDigest.DigestGetContentType
    
    ' Write the response buffer to the client.
    '
    Response.BinaryWrite(digestString)

End Sub

' Main routine starts here
'
Call Main
%>
