Refactoring and Removing Dependency with SystemJSON and Mapper

This commit is contained in:
Ezequiel Juliano Müller 2017-03-13 16:52:11 -03:00
parent d0f3961bed
commit 5f34de36b1
9 changed files with 2339 additions and 2790 deletions

View File

@ -30,16 +30,19 @@ interface
uses
System.SysUtils, Generics.Collections, MVCFramework.TypesAliases,
System.Generics.Collections, MVCFramework.Session, LoggerPro,
System.SyncObjs;
System.SysUtils,
System.SyncObjs,
System.Generics.Collections,
MVCFramework.TypesAliases,
MVCFramework.Session,
LoggerPro;
{$I dmvcframeworkbuildconsts.inc}
type
TMVCHTTPMethodType = (httpGET, httpPOST, httpPUT, httpDELETE, httpHEAD,
httpOPTIONS, httpPATCH, httpTRACE);
TMVCHTTPMethodType = (httpGET, httpPOST, httpPUT, httpDELETE, httpHEAD, httpOPTIONS, httpPATCH, httpTRACE);
TMVCHTTPMethods = set of TMVCHTTPMethodType;
TMVCPair<TKey, TVal> = class
@ -64,7 +67,7 @@ type
property Val3: TVal3 read FVal3;
end;
TMVCMimeType = class sealed
TMVCMimeType = record
public const
APPLICATION_JSON = 'application/json';
TEXT_HTML = 'text/html';
@ -79,7 +82,7 @@ type
TEXT_EVENTSTREAM = 'text/event-stream';
end deprecated 'use TMVCMediaType';
TMVCMediaType = class sealed
TMVCMediaType = record
public const
APPLICATION_ATOM_XML = 'application/atom+xml';
APPLICATION_FORM_URLENCODED = 'application/x-www-form-urlencoded';
@ -88,6 +91,7 @@ type
APPLICATION_SVG_XML = 'application/svg+xml';
APPLICATION_XHTML_XML = 'application/xhtml+xml';
APPLICATION_XML = 'application/xml';
APPLICATION_OCTETSTREAM = 'application/octet-stream';
MEDIA_TYPE_WILDCARD = '*';
MULTIPART_FORM_DATA = 'multipart/form-data';
TEXT_HTML = 'text/html';
@ -97,13 +101,13 @@ type
TEXT_JAVASCRIPT = 'text/javascript';
TEXT_CACHEMANIFEST = 'text/cache-manifest';
TEXT_EVENTSTREAM = 'text/event-stream';
TEXT_CSV = 'text/csv'; // https://tools.ietf.org/html/rfc7111
TEXT_CSV = 'text/csv';
IMAGE_JPEG = 'image/jpeg';
IMAGE_PNG = 'image/x-png';
WILDCARD = '*/*';
end;
TMVCCharSet = class sealed
TMVCCharSet = record
public const
US_ASCII = 'US-ASCII';
WINDOWS_1250 = 'windows-1250';
@ -127,7 +131,7 @@ type
UTF_16LE = 'UTF-16LE';
end;
TMVCConstants = class sealed
TMVCConstants = record
public const
SESSION_TOKEN_NAME = 'dtsessionid';
DEFAULT_CONTENT_CHARSET = 'UTF-8';
@ -136,6 +140,28 @@ type
LAST_AUTHORIZATION_HEADER_VALUE = '__DMVC_LAST_AUTHORIZATION_HEADER_VALUE_';
end;
TMVCConfigKey = record
public const
SessionTimeout = 'sessiontimeout';
DocumentRoot = 'document_root';
ViewPath = 'view_path';
DefaultContentType = 'default_content_type';
DefaultContentCharset = 'default_content_charset';
DefaultViewFileExtension = 'default_view_file_extension';
ISAPIPath = 'isapi_path';
StompServer = 'stompserver';
StompServerPort = 'stompserverport';
StompUsername = 'stompusername';
StompPassword = 'stomppassword';
Messaging = 'messaging';
AllowUnhandledAction = 'allow_unhandled_action';
ServerName = 'server_name';
ExposeServerSignature = 'server_signature';
IndexDocument = 'index_document';
SessionType = 'session_type';
FallbackResource = 'fallback_resource';
end;
EMVCException = class(Exception)
private
FHTTPErrorCode: UInt16;
@ -361,7 +387,7 @@ type
HTTPVersionNotSupported = 505;
end;
{$SCOPEDENUMS ON}
{$SCOPEDENUMS ON}
type
@ -391,9 +417,13 @@ uses
idGlobal,
System.StrUtils,
idCoderMIME
{$IFDEF SYSTEMJSON}
, System.JSON //just to allow inline
{$ENDIF}
{$IFDEF SYSTEMJSON}
, System.JSON // just to allow inline
{$ENDIF}
;
const

View File

@ -133,8 +133,8 @@ begin
raise EMVCException.Create('Invalid or empty topic');
if not CTX.Request.ThereIsRequestBody then
raise EMVCException.Create('Body request required');
EnqueueMessageOnTopicOrQueue(queuetype = 'queue', '/' + queuetype + '/' + topicname,
CTX.Request.BodyAsJSONObject.Clone as TJSONObject, true);
// EnqueueMessageOnTopicOrQueue(queuetype = 'queue', '/' + queuetype + '/' + topicname,
// CTX.Request.BodyAsJSONObject.Clone as TJSONObject, true);
// EnqueueMessage('/queue/' + topicname, CTX.Request.BodyAsJSONObject.Clone as TJSONObject, true);
Render(200, 'Message sent to topic ' + topicname);
end;
@ -224,12 +224,12 @@ begin
if LTimeOut then
begin
res.AddPair('_timeout', TJSONTrue.Create);
Render(http_status.RequestTimeout, res);
//Render(http_status.RequestTimeout, res);
end
else
begin
res.AddPair('_timeout', TJSONFalse.Create);
Render(http_status.OK, res);
//Render(http_status.OK, res);
end;
finally

View File

@ -6,6 +6,8 @@
//
// https://github.com/danieleteti/delphimvcframework
//
// Collaborators on this file: Ezequiel Juliano Müller (ezequieljuliano@gmail.com)
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -27,172 +29,192 @@ unit MVCFramework.Router;
interface
uses
Web.HTTPApp,
MVCFramework.RTTIUtils,
MVCFramework.Commons,
System.RTTI,
System.Rtti,
System.SysUtils,
System.Generics.Collections,
System.RegularExpressions,
System.AnsiStrings,
MVCFramework,
System.Generics.Collections;
MVCFramework.Commons,
IdURI;
type
TMVCRouter = class
private
FCTX: TRttiContext;
FMethodToCall: TRTTIMethod;
FMVCControllerClass: TMVCControllerClass;
FMVCControllerDelegate: TMVCControllerDelegate;
FMVCConfig: TMVCConfig;
function IsHTTPContentTypeCompatible(AWebRequestMethodType: TMVCHTTPMethodType;
AContentType: string; AAttributes: TArray<TCustomAttribute>): Boolean;
function IsHTTPAcceptCompatible(AWebRequestMethodType: TMVCHTTPMethodType; AAccept: string;
AAttributes: TArray<TCustomAttribute>): Boolean;
function GetFirstMimeType(const AContentType: string): string;
protected
function IsHTTPMethodCompatible(AMethodType: TMVCHTTPMethodType;
AAttributes: TArray<TCustomAttribute>): Boolean; virtual;
function IsCompatiblePath(AMVCPath: string; APath: string; var AParams: TMVCRequestParamsTable)
: Boolean; virtual;
function GetAttribute<T: TCustomAttribute>(AAttributes: TArray<TCustomAttribute>): T;
FRttiContext: TRttiContext;
FConfig: TMVCConfig;
FMethodToCall: TRttiMethod;
FControllerClazz: TMVCControllerClazz;
FControllerCreateAction: TMVCControllerCreateAction;
function GetAttribute<T: TCustomAttribute>(const AAttributes: TArray<TCustomAttribute>): T;
function GetFirstMediaType(const AContentType: string): string;
function IsHTTPContentTypeCompatible(
const ARequestMethodType: TMVCHTTPMethodType;
var AContentType: string;
const AAttributes: TArray<TCustomAttribute>): Boolean;
function IsHTTPAcceptCompatible(
const ARequestMethodType: TMVCHTTPMethodType;
var AAccept: string;
const AAttributes: TArray<TCustomAttribute>): Boolean;
function IsHTTPMethodCompatible(
const AMethodType: TMVCHTTPMethodType;
const AAttributes: TArray<TCustomAttribute>): Boolean;
function IsCompatiblePath(
const AMVCPath: string;
const APath: string;
var AParams: TMVCRequestParamsTable): Boolean;
protected
{ protected declarations }
public
class function StringMethodToHTTPMetod(const Value: AnsiString): TMVCHTTPMethodType;
constructor Create(AMVCConfig: TMVCConfig);
function ExecuteRouting(const AWebRequestPathInfo: AnsiString;
AWebRequestMethodType: TMVCHTTPMethodType; const AWebRequestContentType: AnsiString;
const AWebRequestAccept: AnsiString; AMVCControllers: TObjectList<TMVCControllerRoutable>;
const ADefaultContentType: string; const ADefaultContentCharset: string;
var AMVCRequestParams: TMVCRequestParamsTable; out AResponseContentType: string;
out AResponseContentEncoding: string): Boolean; overload;
property MethodToCall: TRTTIMethod read FMethodToCall;
property MVCControllerClass: TMVCControllerClass read FMVCControllerClass;
property MVCControllerDelegate: TMVCControllerDelegate read FMVCControllerDelegate;
class function StringMethodToHTTPMetod(const AValue: string): TMVCHTTPMethodType; static;
public
constructor Create(const AConfig: TMVCConfig);
destructor Destroy; override;
function ExecuteRouting(
const ARequestPathInfo: string;
const ARequestMethodType: TMVCHTTPMethodType;
const ARequestContentType: string;
const ARequestAccept: string;
const AControllers: TObjectList<TMVCControllerDelegate>;
const ADefaultContentType: string;
const ADefaultContentCharset: string;
var ARequestParams: TMVCRequestParamsTable;
out AResponseContentType: string;
out AResponseContentEncoding: string): Boolean;
property MethodToCall: TRttiMethod read FMethodToCall;
property ControllerClazz: TMVCControllerClazz read FControllerClazz;
property ControllerCreateAction: TMVCControllerCreateAction read FControllerCreateAction;
end;
implementation
uses
System.AnsiStrings,
System.StrUtils,
System.RegularExpressions,
System.SysUtils,
idURI;
{ TMVCRouter }
constructor TMVCRouter.Create(AMVCConfig: TMVCConfig);
constructor TMVCRouter.Create(const AConfig: TMVCConfig);
begin
inherited Create;
FMVCConfig := AMVCConfig;
FRttiContext := TRttiContext.Create;
FConfig := AConfig;
FMethodToCall := nil;
FControllerClazz := nil;
FControllerCreateAction := nil;
end;
function TMVCRouter.ExecuteRouting(const AWebRequestPathInfo: AnsiString;
AWebRequestMethodType: TMVCHTTPMethodType; const AWebRequestContentType: AnsiString;
const AWebRequestAccept: AnsiString; AMVCControllers: TObjectList<TMVCControllerRoutable>;
const ADefaultContentType, ADefaultContentCharset: string;
var AMVCRequestParams: TMVCRequestParamsTable; out AResponseContentType: string;
destructor TMVCRouter.Destroy;
begin
FRttiContext.Free;
inherited Destroy;
end;
function TMVCRouter.ExecuteRouting(const ARequestPathInfo: string;
const ARequestMethodType: TMVCHTTPMethodType;
const ARequestContentType, ARequestAccept: string;
const AControllers: TObjectList<TMVCControllerDelegate>;
const ADefaultContentType: string;
const ADefaultContentCharset: string;
var ARequestParams: TMVCRequestParamsTable;
out AResponseContentType: string;
out AResponseContentEncoding: string): Boolean;
var
controllerRoutable: TMVCControllerRoutable;
_type: TRttiType;
_methods: TArray<TRTTIMethod>;
_method: TRTTIMethod;
_attribute: TCustomAttribute;
_attributes: TArray<TCustomAttribute>;
i: Integer;
ControllerMappedPath: string;
MethodPathAttribute: string;
MVCProduceAttr: MVCProducesAttribute;
Found: Boolean;
LWebRequestPathInfo: string;
LWebRequestAccept: string;
LRequestPathInfo: string;
LRequestAccept: string;
LRequestContentType: string;
LControllerMappedPath: string;
LControllerDelegate: TMVCControllerDelegate;
LAttributes: TArray<TCustomAttribute>;
LAtt: TCustomAttribute;
LRttiType: TRttiType;
LMethods: TArray<TRTTIMethod>;
LMethod: TRTTIMethod;
LFound: Boolean;
LMethodPath: string;
LProduceAttribute: MVCProducesAttribute;
begin
FMethodToCall := nil;
FMVCControllerClass := nil;
FMVCControllerDelegate := nil;
LWebRequestAccept := string(AWebRequestAccept);
Result := False;
LWebRequestPathInfo := string(AWebRequestPathInfo);
if Trim(LWebRequestPathInfo) = EmptyStr then
LWebRequestPathInfo := '/'
FMethodToCall := nil;
FControllerClazz := nil;
FControllerCreateAction := nil;
LRequestAccept := ARequestAccept;
LRequestContentType := ARequestContentType;
LRequestPathInfo := ARequestPathInfo;
if (Trim(LRequestPathInfo) = EmptyStr) then
LRequestPathInfo := '/'
else
begin
if LWebRequestPathInfo[1] <> '/' then
LWebRequestPathInfo := '/' + LWebRequestPathInfo;
if LRequestPathInfo[1] <> '/' then
LRequestPathInfo := '/' + LRequestPathInfo;
end;
// FIX https://github.com/danieleteti/delphimvcframework/issues/17
LWebRequestPathInfo := TIdURI.PathEncode(LWebRequestPathInfo);
LRequestPathInfo := TIdURI.PathEncode(LRequestPathInfo);
{ ISAPI CHANGE THE REQUEST PATH INFO START }
if IsLibrary then
begin
if string(LWebRequestPathInfo).StartsWith(FMVCConfig.Value[TMVCConfigKey.ISAPIPath]) then
LWebRequestPathInfo := LWebRequestPathInfo.Remove(0,
FMVCConfig.Value[TMVCConfigKey.ISAPIPath].Length);
if Length(LWebRequestPathInfo) = 0 then
LWebRequestPathInfo := '/';
if string(LRequestPathInfo).StartsWith(FConfig.Value[TMVCConfigKey.ISAPIPath]) then
LRequestPathInfo := LRequestPathInfo.Remove(0, FConfig.Value[TMVCConfigKey.ISAPIPath].Length);
if Length(LRequestPathInfo) = 0 then
LRequestPathInfo := '/';
end;
{ ISAPI CHANGE THE REQUEST PATH INFO END }
TMonitor.Enter(Lock); // start of lock
TMonitor.Enter(Lock);
try
Result := False;
ControllerMappedPath := '';
for controllerRoutable in AMVCControllers do
LControllerMappedPath := EmptyStr;
for LControllerDelegate in AControllers do
begin
SetLength(_attributes, 0);
_type := FCTX.GetType(controllerRoutable.&Class.ClassInfo);
_attributes := _type.GetAttributes;
if _attributes = nil then
SetLength(LAttributes, 0);
LRttiType := FRttiContext.GetType(LControllerDelegate.Clazz.ClassInfo);
LAttributes := LRttiType.GetAttributes;
if (LAttributes = nil) then
Continue;
Found := False;
for _attribute in _attributes do
if _attribute is MVCPathAttribute then
LFound := False;
for LAtt in LAttributes do
if LAtt is MVCPathAttribute then
begin
Found := True;
ControllerMappedPath := MVCPathAttribute(_attribute).Path;
LFound := True;
LControllerMappedPath := MVCPathAttribute(LAtt).Path;
Break;
end;
if not Found then
raise EMVCException.Create('Controller ' + _type.Name + ' doesn''t have MVCPath attribute');
if not LFound then
raise EMVCException.CreateFmt('Controller %s does not have MVCPath attribute', [LRttiType.Name]);
if ControllerMappedPath = '/' then // WE WANT TO AVOID '//' AS MVCPATH
ControllerMappedPath := '';
if (LControllerMappedPath = '/') then
LControllerMappedPath := '';
if (not ControllerMappedPath.IsEmpty) and (Pos(ControllerMappedPath, LWebRequestPathInfo) <> 1)
then
if (not LControllerMappedPath.IsEmpty) and (Pos(LControllerMappedPath, LRequestPathInfo) <> 1) then
Continue;
_methods := _type.GetMethods;
for _method in _methods do
LMethods := LRttiType.GetMethods;
for LMethod in LMethods do
begin
_attributes := _method.GetAttributes;
for i := 0 to Length(_attributes) - 1 do
begin
_attribute := _attributes[i];
if _attribute is MVCPathAttribute then
begin
if IsHTTPMethodCompatible(AWebRequestMethodType, _attributes) and
IsHTTPContentTypeCompatible(AWebRequestMethodType, string(AWebRequestContentType),
_attributes) and IsHTTPAcceptCompatible(AWebRequestMethodType, LWebRequestAccept,
_attributes) then
LAttributes := LMethod.GetAttributes;
for LAtt in LAttributes do
if LAtt is MVCPathAttribute then
if IsHTTPMethodCompatible(ARequestMethodType, LAttributes) and
IsHTTPContentTypeCompatible(ARequestMethodType, LRequestContentType, LAttributes) and
IsHTTPAcceptCompatible(ARequestMethodType, LRequestAccept, LAttributes) then
begin
MethodPathAttribute := MVCPathAttribute(_attribute).Path;
if IsCompatiblePath(ControllerMappedPath + MethodPathAttribute, LWebRequestPathInfo,
AMVCRequestParams) then
LMethodPath := MVCPathAttribute(LAtt).Path;
if IsCompatiblePath(LControllerMappedPath + LMethodPath, LRequestPathInfo, ARequestParams) then
begin
FMethodToCall := _method;
FMVCControllerClass := controllerRoutable.&Class;
FMVCControllerDelegate := controllerRoutable.Delegate;
// getting the default contenttype using MVCProduceAttribute
MVCProduceAttr := GetAttribute<MVCProducesAttribute>(_attributes);
if MVCProduceAttr <> nil then
FMethodToCall := LMethod;
FControllerClazz := LControllerDelegate.Clazz;
FControllerCreateAction := LControllerDelegate.CreateAction;
LProduceAttribute := GetAttribute<MVCProducesAttribute>(LAttributes);
if Assigned(LProduceAttribute) then
begin
AResponseContentType := MVCProduceAttr.Value;
AResponseContentEncoding := MVCProduceAttr.ProduceEncoding;
AResponseContentType := LProduceAttribute.Value;
AResponseContentEncoding := LProduceAttribute.Encoding;
end
else
begin
@ -200,194 +222,193 @@ begin
AResponseContentEncoding := ADefaultContentCharset;
end;
Exit(True);
end; // if is compatible path
end; // if is compatible method, contenttype and accept
end; // if attribute is mvcpath
end; // for each attributes on method
end; // for each methods
end; // for each controllers
end;
end;
end;
end;
finally
TMonitor.Exit(Lock);
end;
end;
function TMVCRouter.GetAttribute<T>(AAttributes: TArray<TCustomAttribute>): T;
function TMVCRouter.GetAttribute<T>(const AAttributes: TArray<TCustomAttribute>): T;
var
a: TCustomAttribute;
Att: TCustomAttribute;
begin
Result := nil;
for a in AAttributes do
if a is T then
Exit(T(a));
for Att in AAttributes do
if Att is T then
Exit(T(Att));
end;
function TMVCRouter.GetFirstMimeType(const AContentType: string): string;
function TMVCRouter.GetFirstMediaType(const AContentType: string): string;
begin
Result := AContentType;
while Pos(',', Result) > 0 do
Result := Copy(Result, 1, Pos(',', Result) - 1);
while Pos(';', Result) > 0 do
Result := Copy(Result, 1, Pos(';', Result) - 1);
// application/json;charset=UTF-8 {daniele}
end;
function TMVCRouter.IsCompatiblePath(AMVCPath: string; APath: string;
function TMVCRouter.IsCompatiblePath(
const AMVCPath: string;
const APath: string;
var AParams: TMVCRequestParamsTable): Boolean;
function ToPattern(const V: string; Names: TList<string>): string;
var
s: string;
S: string;
begin
Result := V;
for s in Names do
Result := StringReplace(Result, '($' + s + ')', '([ àèéùòì@\.\_\,%\w\d\x2D\x3A]*)',
[rfReplaceAll]);
for S in Names do
Result := StringReplace(Result, '($' + S + ')', '([ àèéùòì@\.\_\,%\w\d\x2D\x3A]*)', [rfReplaceAll]);
end;
function GetParametersNames(const V: string): TList<string>;
var
s: string;
matches: TMatchCollection;
match: TMatch;
i: Integer;
S: string;
Matches: TMatchCollection;
M: TMatch;
I: Integer;
begin
Result := TList<string>.Create;
s := '\(\$([A-Za-z0-9\_]+)\)';
// dt 2/08/2016 added "_" as allowed character in the parameter name
matches := TRegEx.matches(V, s, [roIgnoreCase, roCompiled, roSingleLine]);
for match in matches do
for i := 0 to match.Groups.Count - 1 do
S := '\(\$([A-Za-z0-9\_]+)\)';
Matches := TRegEx.Matches(V, S, [roIgnoreCase, roCompiled, roSingleLine]);
for M in Matches do
for I := 0 to M.Groups.Count - 1 do
begin
s := match.Groups[i].Value;
if (Length(s) > 0) and (s[1] <> '(') then
S := M.Groups[I].Value;
if (Length(S) > 0) and (S[1] <> '(') then
begin
Result.Add(s);
Result.Add(S);
Break;
end;
end;
end;
var
re: TRegEx;
m: TMatch;
pattern: string;
i: Integer;
RegEx: TRegEx;
Macth: TMatch;
Pattern: string;
I: Integer;
Names: TList<string>;
begin
Names := GetParametersNames(AMVCPath);
try
pattern := ToPattern(AMVCPath, Names);
if APath = AMVCPath then
Pattern := ToPattern(AMVCPath, Names);
if (APath = AMVCPath) then
Exit(True)
else
begin
re := TRegEx.Create('^' + pattern + '$', [roIgnoreCase, roCompiled, roSingleLine]);
m := re.match(APath);
Result := m.Success;
RegEx := TRegEx.Create('^' + Pattern + '$', [roIgnoreCase, roCompiled, roSingleLine]);
Macth := RegEx.match(APath);
Result := Macth.Success;
if Result then
for i := 1 to pred(m.Groups.Count) do
AParams.Add(Names[i - 1], TIdURI.URLDecode(m.Groups[i].Value));
for I := 1 to pred(Macth.Groups.Count) do
AParams.Add(Names[I - 1], TIdURI.URLDecode(Macth.Groups[I].Value));
end;
finally
Names.Free;
end;
end;
function TMVCRouter.IsHTTPAcceptCompatible(AWebRequestMethodType: TMVCHTTPMethodType;
AAccept: string; AAttributes: TArray<TCustomAttribute>): Boolean;
function TMVCRouter.IsHTTPAcceptCompatible(
const ARequestMethodType: TMVCHTTPMethodType;
var AAccept: string;
const AAttributes: TArray<TCustomAttribute>): Boolean;
var
i: Integer;
I: Integer;
MethodAccept: string;
FoundOneAttribProduces: Boolean;
FoundOneAttProduces: Boolean;
begin
Result := False;
FoundOneAttribProduces := False;
for i := 0 to high(AAttributes) do
begin
if AAttributes[i] is MVCProducesAttribute then
begin
FoundOneAttribProduces := True;
MethodAccept := MVCProducesAttribute(AAttributes[i]).Value;
AAccept := GetFirstMimeType(AAccept);
// while Pos(',', AAccept) > 0 do
// AAccept := Copy(AAccept, 1, Pos(',', AAccept) - 1);
FoundOneAttProduces := False;
for I := 0 to High(AAttributes) do
if AAttributes[I] is MVCProducesAttribute then
begin
FoundOneAttProduces := True;
MethodAccept := MVCProducesAttribute(AAttributes[I]).Value;
AAccept := GetFirstMediaType(AAccept);
Result := SameText(AAccept, MethodAccept, loInvariantLocale);
if Result then
Break;
end;
end;
Result := (not FoundOneAttribProduces) or (FoundOneAttribProduces and Result);
Result := (not FoundOneAttProduces) or (FoundOneAttProduces and Result);
end;
function TMVCRouter.IsHTTPContentTypeCompatible(AWebRequestMethodType: TMVCHTTPMethodType;
AContentType: string; AAttributes: TArray<TCustomAttribute>): Boolean;
function TMVCRouter.IsHTTPContentTypeCompatible(
const ARequestMethodType: TMVCHTTPMethodType;
var AContentType: string;
const AAttributes: TArray<TCustomAttribute>): Boolean;
var
i: Integer;
I: Integer;
MethodContentType: string;
FoundOneAttribConsumes: Boolean;
FoundOneAttConsumes: Boolean;
begin
// content type is applicable only for PUT, POST and PATCH
if AWebRequestMethodType in [httpGET, httpDELETE, httpHEAD, httpOPTIONS] then
if ARequestMethodType in [httpGET, httpDELETE, httpHEAD, httpOPTIONS] then
Exit(True);
Result := False;
FoundOneAttribConsumes := False;
for i := 0 to high(AAttributes) do
begin
if AAttributes[i] is MVCConsumesAttribute then
FoundOneAttConsumes := False;
for I := 0 to High(AAttributes) do
if AAttributes[I] is MVCConsumesAttribute then
begin
FoundOneAttribConsumes := True;
MethodContentType := MVCConsumesAttribute(AAttributes[i]).Value;
AContentType := GetFirstMimeType(AContentType);
FoundOneAttConsumes := True;
MethodContentType := MVCConsumesAttribute(AAttributes[I]).Value;
AContentType := GetFirstMediaType(AContentType);
Result := SameText(AContentType, MethodContentType, loInvariantLocale);
if Result then
Break;
end;
end;
Result := (not FoundOneAttribConsumes) or (FoundOneAttribConsumes and Result);
Result := (not FoundOneAttConsumes) or (FoundOneAttConsumes and Result);
end;
function TMVCRouter.IsHTTPMethodCompatible(AMethodType: TMVCHTTPMethodType;
AAttributes: TArray<TCustomAttribute>): Boolean;
function TMVCRouter.IsHTTPMethodCompatible(
const AMethodType: TMVCHTTPMethodType;
const AAttributes: TArray<TCustomAttribute>): Boolean;
var
i: Integer;
I: Integer;
MustBeCompatible: Boolean;
CompatibleMethods: TMVCHTTPMethods;
begin
Result := False;
// if there aren't MVCHTTPMethod attributes defined, the action is compatibile with all methods
MustBeCompatible := False;
for i := 0 to high(AAttributes) do
begin
if AAttributes[i] is MVCHTTPMethodAttribute then
for I := 0 to High(AAttributes) do
if AAttributes[I] is MVCHTTPMethodAttribute then
begin
MustBeCompatible := True;
CompatibleMethods := MVCHTTPMethodAttribute(AAttributes[i]).MVCHTTPMethods;
CompatibleMethods := MVCHTTPMethodAttribute(AAttributes[I]).MVCHTTPMethods;
Result := (AMethodType in CompatibleMethods);
end;
end;
Result := (not MustBeCompatible) or (MustBeCompatible and Result);
end;
class function TMVCRouter.StringMethodToHTTPMetod(const Value: AnsiString): TMVCHTTPMethodType;
class function TMVCRouter.StringMethodToHTTPMetod(const AValue: string): TMVCHTTPMethodType;
begin
if Value = 'GET' then
if AValue = 'GET' then
Exit(httpGET);
if Value = 'POST' then
if AValue = 'POST' then
Exit(httpPOST);
if Value = 'DELETE' then
if AValue = 'DELETE' then
Exit(httpDELETE);
if Value = 'PUT' then
if AValue = 'PUT' then
Exit(httpPUT);
if Value = 'HEAD' then
if AValue = 'HEAD' then
Exit(httpHEAD);
if Value = 'OPTIONS' then
if AValue = 'OPTIONS' then
Exit(httpOPTIONS);
if Value = 'PATCH' then
if AValue = 'PATCH' then
Exit(httpPATCH);
if Value = 'TRACE' then
if AValue = 'TRACE' then
Exit(httpTRACE);
raise EMVCException.CreateFmt('Unknown HTTP method [%s]', [Value]);
raise EMVCException.CreateFmt('Unknown HTTP method [%s]', [AValue]);
end;
end.

View File

@ -67,7 +67,7 @@ type
destructor Destroy; override;
end;
TMVCSerializerHelpful = class sealed
TMVCSerializerHelpful = record
public
class function GetKeyName(const AField: TRttiField; const AType: TRttiType): string; overload; static;
class function GetKeyName(const AProperty: TRttiProperty; const AType: TRttiType): string; overload; static;

View File

@ -61,6 +61,9 @@ type
function SerializeDataSet(const ADataSet: TDataSet): string; overload;
function SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
@ -70,6 +73,7 @@ type
procedure DeserializeCollection(const ASerializedList: string; const AList: TObject; const AClazz: TClass; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
procedure DeserializeDataSet(const ASerializedDataSet: string; const ADataSet: TDataSet);
procedure DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet);
end;
implementation

View File

@ -93,6 +93,9 @@ type
function SerializeDataSet(const ADataSet: TDataSet): string; overload;
function SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
@ -102,6 +105,7 @@ type
procedure DeserializeCollection(const ASerializedList: string; const AList: TObject; const AClazz: TClass; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
procedure DeserializeDataSet(const ASerializedDataSet: string; const ADataSet: TDataSet);
procedure DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet);
public
procedure AfterConstruction; override;
end;
@ -329,6 +333,11 @@ begin
raise EMVCSerializationException.Create('Method TMVCJSONSerializer.DeserializeDataSet not implemented.');
end;
procedure TMVCJSONSerializer.DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet);
begin
raise EMVCSerializationException.Create('Method TMVCJSONSerializer.DeserializeDataSetRecord not implemented.');
end;
procedure TMVCJSONSerializer.DeserializeObject(
const ASerializedObject: string; const AObject: TObject;
const AType: TMVCSerializationType);
@ -582,6 +591,16 @@ begin
raise EMVCSerializationException.Create('Method TMVCJSONSerializer.SerializeDataSet not implemented.');
end;
function TMVCJSONSerializer.SerializeDataSetRecord(const ADataSet: TDataSet): string;
begin
Result := SerializeDataSetRecord(ADataSet, []);
end;
function TMVCJSONSerializer.SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: array of string): string;
begin
raise EMVCSerializationException.Create('Method TMVCJSONSerializer.SerializeDataSetRecord not implemented.');
end;
function TMVCJSONSerializer.SerializeDataSet(
const ADataSet: TDataSet): string;
begin

View File

@ -104,6 +104,9 @@ type
function SerializeDataSet(const ADataSet: TDataSet): string; overload;
function SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet): string; overload;
function SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: array of string): string; overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType); overload;
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
@ -113,6 +116,7 @@ type
procedure DeserializeCollection(const ASerializedList: string; const AList: TObject; const AClazz: TClass; const AType: TMVCSerializationType; const AIgnoredAttributes: array of string); overload;
procedure DeserializeDataSet(const ASerializedDataSet: string; const ADataSet: TDataSet);
procedure DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet);
public
procedure AfterConstruction; override;
end;
@ -343,12 +347,16 @@ begin
end;
end;
procedure TMVCJsonDataObjectsSerializer.DeserializeDataSet(
const ASerializedDataSet: string; const ADataSet: TDataSet);
procedure TMVCJsonDataObjectsSerializer.DeserializeDataSet(const ASerializedDataSet: string; const ADataSet: TDataSet);
begin
raise EMVCSerializationException.Create('Method TMVCJsonDataObjectsSerializer.DeserializeDataSet not implemented.');
end;
procedure TMVCJsonDataObjectsSerializer.DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet);
begin
raise EMVCSerializationException.Create('Method TMVCJsonDataObjectsSerializer.DeserializeDataSetRecord not implemented.');
end;
procedure TMVCJsonDataObjectsSerializer.DeserializeObject(
const ASerializedObject: string; const AObject: TObject;
const AType: TMVCSerializationType);
@ -599,6 +607,18 @@ begin
raise EMVCSerializationException.Create('Method TMVCJsonDataObjectsSerializer.SerializeDataSet not implemented.');
end;
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(
const ADataSet: TDataSet): string;
begin
Result := SerializeDataSetRecord(ADataSet, []);
end;
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(
const ADataSet: TDataSet; const AIgnoredFields: array of string): string;
begin
raise EMVCSerializationException.Create('Method TMVCJsonDataObjectsSerializer.SerializeDataSetRecord not implemented.');
end;
function TMVCJsonDataObjectsSerializer.SerializeDataSet(
const ADataSet: TDataSet): string;
begin

View File

@ -28,9 +28,9 @@ interface
{$I dmvcframework.inc}
uses
MVCFramework, MVCFramework.Commons;
MVCFramework,
MVCFramework.Commons;
type
@ -38,14 +38,12 @@ type
[MVCDoc('Built-in DelphiMVCFramework System controller')]
TMVCSystemController = class(TMVCController)
protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
var Handled: Boolean); override;
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string; var Handled: Boolean); override;
function GetUpTime: string;
public
[MVCPath('/describeserver.info')]
[MVCHTTPMethods([httpGET, httpPOST])]
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')
]
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')]
procedure DescribeServer(Context: TWebContext);
[MVCPath('/describeplatform.info')]
@ -65,11 +63,17 @@ uses
, System.Classes
, Winapi.Windows
, System.TypInfo
{$IFDEF SYSTEMJSON} // XE6
{$IFDEF SYSTEMJSON} // XE6
, System.JSON
{$ELSE}
{$ELSE}
, Data.DBXJSON
{$ENDIF}
{$ENDIF}
;
function MSecToTime(mSec: Int64): string;
@ -121,7 +125,7 @@ end;
procedure TMVCSystemController.DescribeServer(Context: TWebContext);
var
LJResp: TJSONObject;
LControllerRoutable: TMVCControllerRoutable;
LController: TMVCControllerDelegate;
ControllerInfo: TJSONObject;
LRTTIType: TRttiInstanceType;
LCTX: TRttiContext;
@ -141,13 +145,12 @@ begin
try
LJResp := TJSONObject.Create;
try
for LControllerRoutable in GetMVCEngine.RegisteredControllers do
for LController in Engine.Controllers do
begin
ControllerInfo := TJSONObject.Create;
LJResp.AddPair(LControllerRoutable.&Class.QualifiedClassName,
ControllerInfo);
LJResp.AddPair(LController.Clazz.QualifiedClassName, ControllerInfo);
LRTTIType := LCTX.GetType(LControllerRoutable.&Class)
LRTTIType := LCTX.GetType(LController.Clazz)
as TRttiInstanceType;
for LAttribute in LRTTIType.GetAttributes do
begin

File diff suppressed because it is too large Load Diff