Skip to content

Commit cff0a91

Browse files
Improve the routing mechanism for GAM services (#974)
* Improve the routing mechanism for GAM services using the appropriate namespace for each service. * Refactored minor improvements * Store names in HttpHelper.GamServicesInternalName without extension. (cherry picked from commit 48cbde1) # Conflicts: # dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs # dotnet/src/dotnetframework/GxClasses/Middleware/HandlerFactory.cs
1 parent cf044c3 commit cff0a91

File tree

3 files changed

+95
-85
lines changed

3 files changed

+95
-85
lines changed

dotnet/src/dotnetcore/GxClasses.Web/Middleware/HandlerFactory.cs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,6 @@ public class HandlerFactory
3939
{"gxoauthuserinfo",typeof(GXOAuthUserInfo)},
4040
{"gxoauthaccesstoken",typeof(GXOAuthAccessToken)},
4141
{"gxmulticall",typeof(GXMultiCall)}};
42-
static Dictionary<string, string> _aspxRewrite = new Dictionary<string, string>(){
43-
{"oauth/access_token","gxoauthaccesstoken.aspx"},
44-
{"oauth/logout","gxoauthlogout.aspx"},
45-
{"oauth/userinfo","gxoauthuserinfo.aspx"},
46-
{"oauth/gam/signin","agamextauthinput.aspx"},
47-
{"oauth/gam/callback","agamextauthinput.aspx"},
48-
{"oauth/gam/access_token","agamoauth20getaccesstoken.aspx"},
49-
{"oauth/gam/userinfo","agamoauth20getuserinfo.aspx"},
50-
{"oauth/gam/signout","agamextauthinput.aspx"},
51-
{"saml/gam/signin","Saml2/SignIn"},
52-
{"saml/gam/callback","gamexternalauthenticationinputsaml20_ws.aspx"},
53-
{"saml/gam/signout","Saml2/Logout"},
54-
{"oauth/requesttokenservice","agamstsauthappgetaccesstoken.aspx"},
55-
{"oauth/queryaccesstoken","agamstsauthappvalidaccesstoken.aspx"},
56-
{"oauth/gam/v2.0/access_token","agamoauth20getaccesstoken_v20.aspx"},
57-
{"oauth/gam/v2.0/userinfo","agamoauth20getuserinfo_v20.aspx"},
58-
{"oauth/gam/v2.0/requesttokenanduserinfo","aGAMSSORestRequestTokenAndUserInfo_v20.aspx"}};
59-
private const string QUERYVIEWER_NAMESPACE = "QueryViewer.Services";
60-
private const string GXFLOW_NSPACE = "GXflow.Programs";
6142
private static List<string> GxNamespaces;
6243

6344
public HandlerFactory()
@@ -124,9 +105,9 @@ private static string ObjectUrl(string requestPath, string basePath)
124105
}
125106
lastSegment = CleanUploadUrlSuffix(lastSegment.TrimStart('/').ToLower());
126107
GXLogging.Debug(log, "ObjectUrl:", lastSegment);
127-
if (_aspxRewrite.ContainsKey(lastSegment))
108+
if (HttpHelper.GAMServices.ContainsKey(lastSegment))
128109
{
129-
return _aspxRewrite[lastSegment];
110+
return HttpHelper.GAMServices[lastSegment];
130111
}
131112
return lastSegment;
132113
}
@@ -169,11 +150,15 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
169150
string className;
170151
if (cname.StartsWith("agxpl_", StringComparison.OrdinalIgnoreCase) || cname.Equals("gxqueryviewerforsd", StringComparison.OrdinalIgnoreCase))
171152
{
172-
className = $"{QUERYVIEWER_NAMESPACE}.{cname}";
153+
className = $"{HttpHelper.QUERYVIEWER_NAMESPACE}.{cname}";
173154
}
174155
else if (Preferences.GxpmEnabled && (cname.StartsWith("awf", StringComparison.OrdinalIgnoreCase) || cname.StartsWith("wf", StringComparison.OrdinalIgnoreCase) || cname.StartsWith("apwf", StringComparison.OrdinalIgnoreCase)))
175156
{
176-
className = $"{GXFLOW_NSPACE}.{cname}";
157+
className = $"{HttpHelper.GXFLOW_NSPACE}.{cname}";
158+
}
159+
else if (HttpHelper.GamServicesInternalName.Contains(cname))
160+
{
161+
className = $"{HttpHelper.GAM_NSPACE}.{cname}";
177162
}
178163
else
179164
{

dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using Microsoft.Net.Http.Headers;
2929
using System.Net.Http;
3030
using System.Globalization;
31+
using System.Linq;
3132

3233
namespace GeneXus.Http
3334
{
@@ -77,6 +78,29 @@ public class WrappedJsonError
7778
public class HttpHelper
7879
{
7980
static readonly ILog log = log4net.LogManager.GetLogger(typeof(GeneXus.Http.HttpHelper));
81+
82+
internal static Dictionary<string, string> GAMServices = new Dictionary<string, string>(){
83+
{"oauth/access_token","gxoauthaccesstoken.aspx"},
84+
{"oauth/logout","gxoauthlogout.aspx"},
85+
{"oauth/userinfo","gxoauthuserinfo.aspx"},
86+
{"oauth/gam/signin","agamextauthinput.aspx"},
87+
{"oauth/gam/callback","agamextauthinput.aspx"},
88+
{"oauth/gam/access_token","agamoauth20getaccesstoken.aspx"},
89+
{"oauth/gam/userinfo","agamoauth20getuserinfo.aspx"},
90+
{"oauth/gam/signout","agamextauthinput.aspx"},
91+
{"saml/gam/signin","Saml2/SignIn"},
92+
{"saml/gam/callback","gamexternalauthenticationinputsaml20_ws.aspx"},
93+
{"saml/gam/signout","Saml2/Logout"},
94+
{"oauth/requesttokenservice","agamstsauthappgetaccesstoken.aspx"},
95+
{"oauth/queryaccesstoken","agamstsauthappvalidaccesstoken.aspx"},
96+
{"oauth/gam/v2.0/access_token","agamoauth20getaccesstoken_v20.aspx"},
97+
{"oauth/gam/v2.0/userinfo","agamoauth20getuserinfo_v20.aspx"},
98+
{"oauth/gam/v2.0/requesttokenanduserinfo","agamssorequesttokenanduserinfo_v20.aspx"}};
99+
internal static HashSet<string> GamServicesInternalName = new HashSet<string>(GAMServices.Values.Select(value => value.Replace(ASPX, string.Empty)));
100+
internal const string QUERYVIEWER_NAMESPACE = "QueryViewer.Services";
101+
internal const string GXFLOW_NSPACE = "GXflow.Programs";
102+
internal const string GAM_NSPACE = "GeneXus.Security.API";
103+
80104
/*
81105
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
82106
* Specifying no-cache or max-age=0 indicates that

dotnet/src/dotnetframework/GxClasses/Middleware/HandlerFactory.cs

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -61,88 +61,89 @@ class HandlerFactory : IHttpHandlerFactory
6161

6262
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
6363
{
64-
IHttpHandler handlerToReturn;
64+
IHttpHandler handlerToReturn;
6565

6666
string relativeURL = context.Request.AppRelativeCurrentExecutionFilePath;
6767
string fname = relativeURL.Substring(relativeURL.LastIndexOf('~') + 2);
68-
String cname1 = (fname.Contains(".")) ? fname.Substring(0, fname.LastIndexOf('.')) : fname;
68+
string cname1 = (fname.Contains(".")) ? fname.Substring(0, fname.LastIndexOf('.')) : fname;
6969
string cname0 = cname1.ToLower();
70-
string actualPath = "";
71-
70+
string mainNamespace;
71+
string assemblyName, cname;
72+
string actualPath;
7273
if (cname0 == "gxoauthlogout")
7374
{
74-
return new GeneXus.Http.GXOAuthLogout();
75+
return new GXOAuthLogout();
7576
}
7677
else if (cname0 == "gxoauthuserinfo")
7778
{
78-
return new GeneXus.Http.GXOAuthUserInfo();
79+
return new GXOAuthUserInfo();
7980
}
8081
else if (cname0 == "gxoauthaccesstoken")
8182
{
82-
return new GeneXus.Http.GXOAuthAccessToken();
83+
return new GXOAuthAccessToken();
8384
}
8485
else if (cname0 == "gxmulticall")
8586
{
86-
return new GeneXus.Http.GXMultiCall();
87+
return new GXMultiCall();
8788
}
88-
string assemblyName, cname;
89-
if (GXAPIModule.serviceInPath(pathTranslated, actualPath: out actualPath))
89+
else if (HttpHelper.GamServicesInternalName.Contains(cname0))
9090
{
91-
string nspace;
92-
Config.GetValueOf("AppMainNamespace", out nspace);
93-
String objClass = GXAPIModule.servicesBase[actualPath];
94-
//
95-
String objectName = GetObjFromPath(cname0, actualPath);
96-
String objectNameUp = GetObjFromPath(cname1, actualPath);
97-
//
98-
Dictionary<string, object> routeParms;
99-
if (GXAPIModule.servicesMapData.ContainsKey(actualPath) && GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out routeParms))
91+
mainNamespace = HttpHelper.GAM_NSPACE;
92+
}
93+
else
94+
{
95+
if (!Config.GetValueOf("AppMainNamespace", out mainNamespace))
96+
mainNamespace = "GeneXus.Programs.";
97+
98+
if (GXAPIModule.serviceInPath(pathTranslated, actualPath: out actualPath))
10099
{
101-
if (!String.IsNullOrEmpty(mapName) && GXAPIModule.servicesMap[actualPath].TryGetValue(mapName, out SingleMap value))
100+
string nspace;
101+
Config.GetValueOf("AppMainNamespace", out nspace);
102+
string objClass = GXAPIModule.servicesBase[actualPath];
103+
//
104+
string objectName = GetObjFromPath(cname0, actualPath);
105+
string objectNameUp = GetObjFromPath(cname1, actualPath);
106+
//
107+
Dictionary<string, object> routeParms;
108+
if (GXAPIModule.servicesMapData.ContainsKey(actualPath) && GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out routeParms))
102109
{
103-
String tmpController = objClass;
104-
String asssemblycontroller = tmpController;
105-
if (objClass.Contains("\\"))
110+
if (!String.IsNullOrEmpty(mapName) && GXAPIModule.servicesMap[actualPath].TryGetValue(mapName, out SingleMap value))
106111
{
107-
tmpController = objClass.Substring(objClass.LastIndexOf("\\") + 1);
108-
String addNspace = objClass.Substring(0, objClass.LastIndexOf("\\")).Replace("\\", ".");
109-
asssemblycontroller = addNspace + "." + tmpController;
110-
nspace += "." + addNspace;
111-
}
112-
GxContext gxContext = GxContext.CreateDefaultInstance();
113-
object handler = ClassLoader.FindInstance(asssemblycontroller, nspace, tmpController, new Object[] { gxContext }, null);
112+
String tmpController = objClass;
113+
String asssemblycontroller = tmpController;
114+
if (objClass.Contains("\\"))
115+
{
116+
tmpController = objClass.Substring(objClass.LastIndexOf("\\") + 1);
117+
String addNspace = objClass.Substring(0, objClass.LastIndexOf("\\")).Replace("\\", ".");
118+
asssemblycontroller = addNspace + "." + tmpController;
119+
nspace += "." + addNspace;
120+
}
121+
GxContext gxContext = GxContext.CreateDefaultInstance();
122+
object handler = ClassLoader.FindInstance(asssemblycontroller, nspace, tmpController, new Object[] { gxContext }, null);
114123

115-
gxContext.HttpContext = context;
116-
GxRestWrapper restWrapper = new Application.GxRestWrapper(handler as GXBaseObject, context, gxContext, value.ServiceMethod, value.VariableAlias, routeParms);
117-
return restWrapper;
124+
gxContext.HttpContext = context;
125+
GxRestWrapper restWrapper = new Application.GxRestWrapper(handler as GXBaseObject, context, gxContext, value.ServiceMethod, value.VariableAlias, routeParms);
126+
return restWrapper;
127+
}
118128
}
119-
}
120-
else
121-
{
122-
if ( requestType.Equals(HttpMethod.Options.Method) && !String.IsNullOrEmpty(actualPath) && GXAPIModule.servicesMapData.ContainsKey(actualPath))
129+
else
123130
{
124-
return new OptionsApiObjectRequestHandler(actualPath, objectName);
131+
if (requestType.Equals(HttpMethod.Options.Method) && !String.IsNullOrEmpty(actualPath) && GXAPIModule.servicesMapData.ContainsKey(actualPath))
132+
{
133+
return new OptionsApiObjectRequestHandler(actualPath, objectName);
134+
}
125135
}
136+
return null;
126137
}
127-
return null;
128138
}
129-
else
139+
assemblyName = cname0;
140+
cname = cname0;
141+
if (cname.EndsWith("_bc_ws"))
130142
{
131-
{
132-
assemblyName = cname0;
133-
cname = cname0;
134-
}
135-
if (cname.EndsWith("_bc_ws"))
136-
{
137-
cname = cname.Substring(0, cname.Length - 3);
138-
assemblyName = cname;
139-
}
143+
cname = cname.Substring(0, cname.Length - 3);
144+
assemblyName = cname;
140145
}
141-
string mainNamespace, className;
142-
if (Config.GetValueOf("AppMainNamespace", out mainNamespace))
143-
className = mainNamespace + "." + cname;
144-
else
145-
className = "GeneXus.Programs." + cname;
146+
string className = mainNamespace + "." + cname;
146147

147148
Type objType = GetHandlerType(assemblyName, className);
148149
if (objType == null)
@@ -160,9 +161,9 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
160161
}
161162
}
162163
}
163-
if (objType != null)
164-
{
165-
if (! typeof(IHttpHandler).IsAssignableFrom(objType))
164+
if (objType != null)
165+
{
166+
if (!typeof(IHttpHandler).IsAssignableFrom(objType))
166167
{
167168
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
168169
handlerToReturn = null;
@@ -183,17 +184,17 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
183184
}
184185
}
185186
else
186-
{
187-
handlerToReturn = (IHttpHandler)System.Web.UI.PageParser.GetCompiledPageInstance(url, pathTranslated, context);
188-
}
187+
{
188+
handlerToReturn = System.Web.UI.PageParser.GetCompiledPageInstance(url, pathTranslated, context);
189+
}
189190
return handlerToReturn;
190191
}
191192

192193
public string GetObjFromPath(string cname, string apath)
193194
{
194195
if (cname.LastIndexOf("/") == (cname.Length - 1))
195196
cname = cname.Substring(0, cname.Length - 1);
196-
String objectName = cname.Remove(0, apath.Length);
197+
string objectName = cname.Remove(0, apath.Length);
197198
return objectName;
198199
}
199200

@@ -290,7 +291,7 @@ internal static Type GetHandlerType(string assemblyName, string className)
290291
try
291292
{
292293

293-
objType = GeneXus.Metadata.ClassLoader.FindType(assemblyName, className, null);
294+
objType = ClassLoader.FindType(assemblyName, className, null);
294295
if (objType == null)
295296
objType = Assembly.Load(assemblyName).GetType(className);
296297
}

0 commit comments

Comments
 (0)