Skip to content

Commit 78768d4

Browse files
committed
CORS support
Fixes #24
1 parent 7ea3008 commit 78768d4

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

MDX2JSON/AbstractREST.cls.xml

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ Add param Namespace to a request to execute MDX in desired namespace.]]></Descri
1717
#dim %response As %CSP.Response
1818
#dim %request As %CSP.Request
1919
#dim %session As %CSP.Session
20-
21-
set %response.ContentType="text/json"
22-
set %session.Language=$$MatchLanguage^%occMessages(%request.GetCgiEnv("HTTP_ACCEPT_LANGUAGE"),"%CSP","+s") // Localization support
23-
set %response.Language=%session.Language
2420
25-
//do %response.SetHeader("Access-Control-Allow-Origin","*")
26-
//do %response.SetHeader("Access-Control-Allow-Headers","Authorization")
27-
//return:($ZCVT(pMethod,"U") = "OPTIONS") $$$OK
21+
Do %response.SetHeader("Access-Control-Allow-Origin","*")
22+
Do %response.SetHeader("Access-Control-Allow-Methods","GET, PUT, POST, DELETE, OPTIONS")
23+
Do %response.SetHeader("Access-Control-Max-Age","10000")
24+
Do %response.SetHeader("Access-Control-Allow-Headers","Content-Type, Authorization, Accept-Language")
25+
26+
Set %response.CharSet = "utf-8"
27+
Set %response.ContentType="application/json"
28+
29+
Set %session.Language=$$MatchLanguage^%occMessages(%request.GetCgiEnv("HTTP_ACCEPT_LANGUAGE"),"%CSP","+s") // Localization support
30+
Set %response.Language=%session.Language
2831
2932
#dim tSC As %Status = $$$OK
3033
#dim e As %Exception.AbstractException
@@ -38,8 +41,8 @@ Add param Namespace to a request to execute MDX in desired namespace.]]></Descri
3841
3942
Try {
4043
// Desired execusion namespace
41-
set Namespace = $get(%request.Data("Namespace",1))
42-
set st = ..CheckNamespace(.Namespace)
44+
Set Namespace = $get(%request.Data("Namespace",1))
45+
Set st = ..CheckNamespace(.Namespace)
4346
If $$$ISERR(st) Do ..Http500(st) Quit
4447
4548
#; Check that the effective user ( could be unknown user ) can access this resource
@@ -105,18 +108,18 @@ Add param Namespace to a request to execute MDX in desired namespace.]]></Descri
105108
If tTarget[":" Set tClass=$Piece(tTarget,":"),tTarget=$Piece(tTarget,":",2)
106109
107110
#; Validate input
108-
set tBody = $List(tMapEntry,4)
109-
set tSC = ..ValidateRequest(pMethod,tBody)
111+
Set tBody = $List(tMapEntry,4)
112+
Set tSC = ..ValidateRequest(pMethod,tBody)
110113
If $$$ISERR(tSC) Do ..Http500(tSC) Quit
111114
112115
#; Dispatch
113-
if (Namespace'=$Namespace) {
114-
set oldNS = $Namespace
116+
If (Namespace'=$Namespace) {
117+
Set oldNS = $Namespace
115118
zn Namespace
116-
set tSC=$zobjclassmethod(tClass,tTarget,tArgs...)
119+
Set tSC=$zobjclassmethod(tClass,tTarget,tArgs...)
117120
zn oldNS
118-
} else {
119-
set tSC=$zobjclassmethod(tClass,tTarget,tArgs...)
121+
} Else {
122+
Set tSC=$zobjclassmethod(tClass,tTarget,tArgs...)
120123
}
121124
If $$$ISERR(tSC) Do ..Http500(tSC)
122125
@@ -154,6 +157,30 @@ Converts request into UTF8. Converts request into proxyObject. ]]></Description>
154157
]]></Implementation>
155158
</Method>
156159

160+
<Method name="Login">
161+
<Description>
162+
Called for a REST page in the event of a login being required</Description>
163+
<ClassMethod>1</ClassMethod>
164+
<FormalSpec>skipheader:%Boolean=1</FormalSpec>
165+
<ReturnType>%Status</ReturnType>
166+
<Implementation><![CDATA[
167+
Do %response.SetHeader("Access-Control-Allow-Origin","*")
168+
Do %response.SetHeader("Access-Control-Allow-Methods","GET, PUT, POST, DELETE, OPTIONS")
169+
Do %response.SetHeader("Access-Control-Max-Age","10000")
170+
Do %response.SetHeader("Access-Control-Allow-Headers","Content-Type, Authorization, Accept-Language")
171+
172+
Set %response.CharSet = "utf-8"
173+
Set %response.ContentType="application/json"
174+
175+
If (%request.GetCgiEnv("REQUEST_METHOD") = "OPTIONS"){
176+
Do %response.WriteHTTPHeader()
177+
Return $$$OK
178+
}
179+
180+
Quit ##super(skipheader)
181+
]]></Implementation>
182+
</Method>
183+
157184
<Method name="Http500">
158185
<Description><![CDATA[
159186
Issue an '500' error and give some indication as to what occurred.<br>
@@ -166,7 +193,7 @@ Issue an '500' error and give some indication as to what occurred.<br>
166193
Set %response.Status="500 Internal Server Error"
167194
168195
#; Return a helpful error string
169-
Write "{Error:"_$$$ZENJSSTR($System.Status.GetErrorText(pStatus,%session.Language))_"}"
196+
Write "{""Error"":"_$$$ZENJSSTR($System.Status.GetErrorText(pStatus,%session.Language))_"}"
170197
]]></Implementation>
171198
</Method>
172199

@@ -278,7 +305,7 @@ This project creates the DispatchMethod used to dispatch the Url and Method to t
278305
}
279306
$$$GENERATE(" Quit """"")
280307
281-
} else {
308+
} Else {
282309
283310
#; The specified class must have an XDATA Block named UrlMap
284311
Set tSC=$$$ERROR($$$XDataBlockMissing,tClassName,"UrlMap")

MDX2JSON/Installer.cls.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ todo add optional parameter WEBAPP to Alter web app name if it needs to have ano
1414
<Log Text="Creating namespace ${Namespace}" Level="0"/>
1515
<Namespace Name="${Namespace}" Create="yes" Code="${Namespace}" Data="${Namespace}">
1616
<Configuration>
17-
<Database Name="${Namespace}" Dir="${MGRDIR}/${Namespace}" Create="yes" MountRequired="true" MountAtStartup="true"/>
17+
<Database Name="${Namespace}" Dir="${MGRDIR}/${Namespace}" Create="yes" MountRequired="true" Resource="%DB_${Namespace}" PublicPermissions="RW" MountAtStartup="true"/>
1818
</Configuration>
1919
</Namespace>
2020
<Log Text="End Creating namespace ${Namespace}" Level="0"/>

MDX2JSON/REST.cls.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Test method, outputs <b>%request</b>, <b>%response</b> and <b>%session</b> objec
108108
<ClassMethod>1</ClassMethod>
109109
<ReturnType>%Status</ReturnType>
110110
<Implementation><![CDATA[
111-
write "{Status: ""OK""}"
111+
write "{""Status"": ""OK""}"
112112
return $$$OK
113113
]]></Implementation>
114114
</Method>

0 commit comments

Comments
 (0)