-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathejscreenRESTbroker.aspx.vb
323 lines (263 loc) · 13 KB
/
ejscreenRESTbroker.aspx.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Imports System.Net
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Text
Imports Newtonsoft.Json
Partial Class ejscreenRESTbroker
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'check for invalid chars in URL, send error as JSON and exit
'Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
'Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim z As Integer
Dim st As String
Dim pcounter As Integer = Context.Request.Params.Count
'start new invalid url code - 10/31/23
Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim method_get As Boolean = (Request.ServerVariables("Request_Method").ToUpper = "GET")
Dim currentContext As HttpContext = HttpContext.Current
Try
If method_get Then
Dim requestUrlCurrent As Uri = currentContext.Request.Url
Dim requestUrlString As String = requestUrlCurrent.ToString()
If RegexObj.IsMatch(requestUrlString) Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
Else
ValidateForInvalidCharacters(requestUrlString)
End If
Else
Dim postParams As NameValueCollection = currentContext.Request.Form
Dim queryStringBuilder As New StringBuilder()
For Each key As String In postParams.AllKeys
Dim value As String = postParams(key)
If queryStringBuilder.Length > 0 Then
queryStringBuilder.Append("&")
End If
queryStringBuilder.AppendFormat("{0}={1}", key, value)
Next
If RegexObj.IsMatch(queryStringBuilder.ToString()) Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
Else
ValidateForInvalidCharacters(queryStringBuilder.ToString())
End If
' ValidateForInvalidCharacters(queryStringBuilder.ToString())
End If
Catch ex As HttpRequestValidationException
Response.Write("Invalid URL Detected: " & ex.ToString())
Response.Write(vbNewLine)
Response.End()
End Try
'end new invalid url code
'old method
' Dim method_get As Boolean = (Request.ServerVariables("Request_Method").ToUpper = "GET")
' If method_get Then
' pcounter = Context.Request.QueryString.Count
' Else
' pcounter = Context.Request.Form.Count
' End If
' For z = 0 To pcounter - 1
' If method_get Then
' st = Context.Request.QueryString(z)
' Else
' st = Context.Request.Form(z)
' End If
' If RegexObj.IsMatch(st) Then
' Dim jsonStr As String = ""
' Dim errorObj As New Newtonsoft.Json.Linq.JObject
' errorObj.Add("error", "Invalid characters in URL")
' jsonStr = JsonConvert.SerializeObject(errorObj, Newtonsoft.Json.Formatting.Indented)
' Response.Clear()
' Response.ContentType = "application/json; charset=utf-8"
' Response.Write(jsonStr)
' Response.End()
' End If
' Next
Try
Dim scriptname As String = Request.ServerVariables("SCRIPT_NAME")
Dim googleid As String = System.Configuration.ConfigurationManager.AppSettings("googleAnalyticID")
Dim baseParams As String = "v=1&tid=" & googleid & "&cid=1750562792.1452199610&t=pageview&dt=EJScreen Report API&dl=" & scriptname
Dim requesturl As String = "https://www.google-analytics.com/collect"
Dim webreq As HttpWebRequest = CType(WebRequest.Create(requesturl), HttpWebRequest)
webreq.ContentType = "application/x-www-form-urlencoded"
webreq.Method = "POST"
Dim PostData() As Byte = System.Text.Encoding.ASCII.GetBytes(baseParams)
webreq.ContentLength = PostData.Length
Dim tempStream As Stream = webreq.GetRequestStream()
'write the data to be posted to the Request Stream
tempStream.Write(PostData, 0, PostData.Length)
tempStream.Close()
Dim webres As WebResponse = webreq.GetResponse()
Dim sr As New StreamReader(webres.GetResponseStream)
'convert the stream to a string
Dim s As String = sr.ReadToEnd
s = s.Trim
sr.Close()
callREST()
Catch ex As Exception
'Response.Write("ERROR" & ex.Message)
End Try
End Sub
Private Sub callREST()
'obj from main EJ call
Dim mainEJJsonObj As New Newtonsoft.Json.Linq.JObject
'main object to build and send back as JSON string
Dim dataJsonObj As New Newtonsoft.Json.Linq.JObject
Dim jsonStr As String = String.Empty
Try
Dim baseParams As String = Request.ServerVariables("QUERY_STRING")
Dim mainejurl As String = System.Configuration.ConfigurationManager.AppSettings("ejendpointMAIN")
'process MAIN service call
Dim sMain As String
sMain = getbrokerresponse(mainejurl, baseParams)
mainEJJsonObj = JsonConvert.DeserializeObject(sMain)
'remove needed properties
Dim fieldDelete() As String = {
"RAW_D_DEMOGIDX5",
"RAW_D_LIFEEXP",
"S_D_LIFEEXP",
"S_D_DEMOGIDX5ST",
"S_D_LIFEEXP_PER",
"S_D_DEMOGIDX5ST_PER",
"N_D_LIFEEXP",
"N_D_DEMOGIDX5",
"N_D_LIFEEXP_PER",
"N_D_DEMOGIDX5_PER",
"N_P5_DIESEL",
"N_P5_CANCER",
"N_P5_RESP",
"N_P5_TRAFFIC",
"N_P5_NPDES",
"N_P5_NPL",
"N_P5_RMP",
"N_P5_TSDF",
"N_P5_O3",
"N_P5_PM25",
"N_P5_UST",
"N_P5_LEAD",
"N_P5_RSEI_AIR",
"S_P5_O3",
"S_P5_PM25",
"S_P5_TSDF",
"S_P5_LEAD",
"S_P5_DIESEL",
"S_P5_CANCER",
"S_P5_RESP",
"S_P5_TRAFFIC",
"S_P5_NPDES",
"S_P5_NPL",
"S_P5_RMP",
"S_P5_UST",
"S_P5_RSEI_AIR",
"NUM_WATERDIS",
"NUM_AIRPOLL",
"NUM_BROWNFIELD",
"NUM_TRI",
"NUM_SCHOOL",
"NUM_HOSPITAL",
"NUM_CHURCH",
"YESNO_TRIBAL",
"YESNO_CEJSTDIS",
"YESNO_IRADIS",
"YESNO_AIRNONATT",
"YESNO_IMPWATERS",
"YESNO_HOUSEBURDEN",
"YESNO_TRANSDIS",
"YESNO_FOODDESERT",
"centroidX",
"centroidY",
"stateName",
"stateAbbr"
}
'sample remove if needed
'joMain.Property("RAW_D_INCOME").Remove()
For Each item As String In fieldDelete
'mainEJJsonObj.Property(item).Remove()
If mainEJJsonObj.Property(item) IsNot Nothing Then
mainEJJsonObj.Property(item).Remove()
End If
Next
Dim rDict As Dictionary(Of String, String)
Dim remapJSON As String = "{'RAW_D_PEOPCOLOR':'RAW_D_MINOR','RAW_D_DEMOGIDX2':'RAW_D_INDEX','S_D_PEOPCOLOR':'S_D_MINOR','S_D_DEMOGIDX2':'S_D_INDEX','S_D_PEOPCOLOR_PER':'S_D_MINOR_PER','S_D_DEMOGIDX2_PER':'S_D_INDEX_PER','S_P2_LEAD':'S_P_LEAD','S_P2_DIESEL':'S_P_DIESEL','S_P2_CANCER':'S_P_CANCER','S_P2_RESP':'S_P_RESP','S_P2_TRAFFIC':'S_P_TRAFFIC','S_P2_NPDES':'S_P_NPDES','S_P2_NPL':'S_P_NPL','S_P2_RMP':'S_P_RMP','S_P2_TSDF':'S_P_TSDF','S_P2_O3':'S_P_O3','S_P2_PM25':'S_P_PM25','S_P2_UST':'S_P_UST','S_P2_RSEI_AIR':'S_P_RSEI_AIR','N_D_PEOPCOLOR':'N_D_MINOR','N_D_DEMOGIDX2':'N_D_INDEX','N_D_PEOPCOLOR_PER':'N_D_MINOR_PER','N_D_DEMOGIDX2_PER':'N_D_INDEX_PER','N_P2_LEAD':'N_P_LEAD','N_P2_DIESEL':'N_P_DIESEL','N_P2_CANCER':'N_P_CANCER','N_P2_RESP':'N_P_RESP','N_P2_TRAFFIC':'N_P_TRAFFIC','N_P2_NPDES':'N_P_NPDES','N_P2_NPL':'N_P_NPL','N_P2_RMP':'N_P_RMP','N_P2_TSDF':'N_P_TSDF','N_P2_O3':'N_P_O3','N_P2_PM25':'N_P_PM25','N_P2_UST':'N_P_UST','N_P2_RSEI_AIR':'N_P_RSEI_AIR'}"
rDict = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(remapJSON)
'build new obj, use remap name if exists
For Each prop As Newtonsoft.Json.Linq.JProperty In mainEJJsonObj.Properties
Dim newKey As String = prop.Name
If rDict.ContainsKey(prop.Name) Then
newKey = rDict.Item(prop.Name)
End If
dataJsonObj.Add(newKey, prop.Value)
Next
'if pjson, indent for pretty-print, if json, send raw minified json. Anything else send back raw json
If Request.QueryString.Get("f").ToLower = "pjson" Then
jsonStr = JsonConvert.SerializeObject(dataJsonObj, Newtonsoft.Json.Formatting.Indented)
ElseIf Request.QueryString.Get("f").ToLower = "json" Then
jsonStr = JsonConvert.SerializeObject(dataJsonObj)
Else
jsonStr = JsonConvert.SerializeObject(dataJsonObj)
End If
Response.Clear()
Response.ContentType = "application/json; charset=utf-8"
Response.Write(jsonStr)
Catch ex As Exception
'Response.Write("ERROR" & ex.Message)
Dim errorObj As New Newtonsoft.Json.Linq.JObject
errorObj.Add("error", ex.Message)
jsonStr = JsonConvert.SerializeObject(errorObj, Newtonsoft.Json.Formatting.Indented)
Response.Clear()
Response.ContentType = "application/json; charset=utf-8"
Response.Write(jsonStr)
End Try
Response.End()
End Sub
Public Function getbrokerresponse(ByVal requesturl As String,ByVal baseParams As String ) As String
Dim responsestr As String = ""
Try
Dim webreq As HttpWebRequest = CType(WebRequest.Create(requesturl), HttpWebRequest)
webreq.ContentType = "application/x-www-form-urlencoded"
webreq.Method = "POST"
Dim PostData() As Byte = System.Text.Encoding.ASCII.GetBytes(baseParams)
webreq.ContentLength = PostData.Length
Dim tempStream As Stream = webreq.GetRequestStream()
'write the data to be posted to the Request Stream
tempStream.Write(PostData, 0, PostData.Length)
tempStream.Close()
Dim webres As WebResponse = webreq.GetResponse()
Dim sr As New StreamReader(webres.GetResponseStream)
'convert the stream to a string
responsestr = sr.ReadToEnd
responsestr = responsestr.Trim
Catch ex As Exception
' responsestr
End Try
Return responsestr
End Function
'start new validation methods 10/31/23
Private Sub ValidateForInvalidCharacters(requestUrlString As String)
GetDecodedUrl(requestUrlString)
End Sub
Sub GetDecodedUrl(ByVal url As String)
Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim firstDecodedUrl As String = HttpUtility.UrlDecode(url)
Dim secondDecodedUrl As String = HttpUtility.UrlDecode(firstDecodedUrl)
'Return secondDecodedUrl = firstDecodedUrl ' Returns True if the URL was not encoded, False otherwise
If firstDecodedUrl = secondDecodedUrl Then
If RegexObj.IsMatch(url) Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
End If
Else
Context.Response.Write("Invalid URL!")
Context.Response.Flush()
Response.End()
End If
End Sub
'end new validation methods
End Class