PDA

View Full Version : [PATCH]: Respond with the correct HTTP Status code in agent $Ext.nd.NotesDXLExporter



Herm
31 Jul 2007, 12:03 PM
When the agent $Ext.nd.NotesDXLExporter handles an error, the response it sends back has an HTTP Status code of "200 OK". Because it is an error, it should send back a 4xx or 5xx status code as per RFC 2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).

I was using Firebug to debug a problem in my app and didn't pick up the problem because the agent came back with a 200 when in fact it had an exporter error. Firebug shows errors (4xx/5xx) in red and other status codes (200, 3xx) in black so it's helpful and a time saver to send back the correct status codes.

By my reading of the spec, I think it should send a "500 Internal Server Error" rather than a 4xx because it can also respond with an explanation of the error (as indeed it does).

Suggested changes to alpha 2's $Ext.nd.NotesDXLExporter Initialize in this colour:


Sub Initialize
On Error Goto ErrHandler

Dim session As New NotesSession
Dim db As NotesDatabase
Dim docCtxt As NotesDocument

Dim nc As NotesNoteCollection
Dim noteDocument As NotesDocument
Dim noteForm As NotesForm
Dim noteView As NotesView
Dim noteOutline As NotesOutline
Dim noteID As String

Dim sType As String ' document, form, view or outline
Dim sName As String
Dim sDesignUnid As String
Dim sDocUnid As String
Dim useDisk As String

Set db = session.CurrentDatabase
Set nc = db.CreateNoteCollection(False)
Call nc.BuildCollection

Set docCtxt = session.DocumentContext

' parse query string for params
Dim objQS As New QSParser
sType = objQS.GetQSVal("type")
sName = objQS.GetQSVal("name")
sDesignUnid = objQS.GetQSVal("designunid")
sDocUnid = objQS.GetQSVal("docunid")
useDisk = objQS.GetQSVal("useDisk")

Dim stream As NotesStream
Set stream = session.CreateStream

If useDisk = "true" Then
Dim TempDir As String
Dim FileName As String
TempDir$ = Environ("Temp")
FileName$ = TempDir$ + "\" + sType + sName + sDesignUnid + sDocUnid + ".xml"
stream.Open(FileName$)
End If


Dim exporter As NotesDXLExporter
Set exporter = session.CreateDXLExporter
exporter.OutputDOCTYPE = False

Select Case sType

Case "document" :
Set noteDocument = db.GetDocumentByUNID(sDocUnid)
Call exporter.SetInput(noteDocument)

Case "form" :
' if we don't have a form name we might have the design unid of the form
If (sName = "") Then
If (sDesignUnid <> "") Then
Set noteDocument = db.GetDocumentByUNID(sDesignUnid)
sName = noteDocument.GetItemValue("form")
Set noteForm = db.GetForm(sName)
Else
Set noteDocument = db.GetDocumentByUNID(sDocUnid)
sName = noteDocument.GetItemValue("form")
Set noteForm = db.GetForm(sName)
End If
Else
Set noteForm = db.GetForm(sName)
End If

Call nc.Add(noteForm)
noteID = nc.GetFirstNoteId
Set noteDocument = db.GetDocumentByID(noteID)
Call exporter.SetInput(noteDocument)

Case "view" :
Set noteView = db.GetView(sName)
Call nc.Add(noteView)
noteID = nc.GetFirstNoteId
Set noteDocument = db.GetDocumentByID(noteID)
Call exporter.SetInput(noteDocument)

Case "outline" :
Set noteOutline = db.GetOutline(sName)
Call nc.Add(noteOutline)
noteID = nc.GetFirstNoteId
Set noteDocument = db.GetDocumentByID(noteID)
Call exporter.SetInput(noteDocument)
Case Else :
Exit Sub
End Select

Call exporter.SetOutput(stream)
Call exporter.Process

' added by Jerry Carter 6/20/2007
Dim outBuff As String
outBuff = exporter.Export(noteDocument)

' done with the stream so close it
stream.Close

' if we created a file on dis then delete it
If useDisk = "true" Then
Kill FileName$
End If

'PATCH: moved these lines from above to ensure that any
' errors would have already been handled by now
' Send response
Print {content-type:text/xml}
Print outBuff

Exit Sub

ErrHandler:
'PATCH: added this next line to send the correct status code in the case of an error
' Send the HTTP Status code for an error
Print {Status: 500 Internal Server Error}

REM tell the browser it is getting xml
Print {content-type:text/xml;charset=utf-8}
Print "<?xml version='1.0' encoding='utf-8'?>"
Print "<error>" + "<line>" + Cstr(Erl) + "</line>" + "<number>" + Cstr(Err) + "</number>" + "<message>" + Error$ + "</message>" + "</error>"
Exit Sub

End Sub

I did not investigate any other agents but it may be worth bearing in mind.

Keep up the great work guys...

Cheers,
Peter Herrmann