|
| 1 | +Class %zpkg.isc.sc.git.Socket Extends %CSP.WebSocket |
| 2 | +{ |
| 3 | + |
| 4 | +Parameter CSPURL = "/isc/studio/usertemplates/gitsourcecontrol/%zpkg.isc.sc.git.Socket.cls"; |
| 5 | + |
| 6 | +Property OriginallyRedirected; |
| 7 | + |
| 8 | +Property OriginalMnemonic; |
| 9 | + |
| 10 | +Property OriginalDevice; |
| 11 | + |
| 12 | +ClassMethod Run() |
| 13 | +{ |
| 14 | + If %request.Get("method") = "preview" { |
| 15 | + Do ##class(SourceControl.Git.API).Pull(1) |
| 16 | + } ElseIf %request.Get("method") = "pull" { |
| 17 | + Do ##class(SourceControl.Git.API).Pull() |
| 18 | + } ElseIf %request.Get("method") = "init" { |
| 19 | + Do ##class(SourceControl.Git.Utils).Init() |
| 20 | + Write !,"Done." |
| 21 | + } ElseIf %request.Get("method") = "clone" { |
| 22 | + Set remote = %request.Get("remote") |
| 23 | + Do ##class(SourceControl.Git.Utils).Clone(remote) |
| 24 | + Write !,"Done." |
| 25 | + } ElseIf %request.Get("method") = "sshkeygen" { |
| 26 | + Do ##class(SourceControl.Git.Utils).GenerateSSHKeyPair() |
| 27 | + Write !,"Done." |
| 28 | + } Else { |
| 29 | + Write !!,"Invalid method selected.",!! |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +Method OnPreServer() As %Status |
| 34 | +{ |
| 35 | + If '$System.Security.Check("%Development","USE") { |
| 36 | + Quit $$$ERROR($$$AccessDenied) |
| 37 | + } |
| 38 | + If (%request.Get("$NAMESPACE") '= "") { |
| 39 | + Set $Namespace = %request.Get("$NAMESPACE") |
| 40 | + } |
| 41 | + Quit $$$OK |
| 42 | +} |
| 43 | + |
| 44 | +Method Server() As %Status |
| 45 | +{ |
| 46 | + New %server |
| 47 | + Set tSC = $$$OK |
| 48 | + Set tRedirected = 0 |
| 49 | + Try { |
| 50 | + $$$ThrowOnError(..StartOutputCapture()) |
| 51 | + Set tRedirected = 1 |
| 52 | + |
| 53 | + // In subclasses: Do Something that produces output to the current device. |
| 54 | + // It will be sent back to the client, Base64-encoded, over the web socket connection. |
| 55 | + Do ..Run() |
| 56 | + } Catch e { |
| 57 | + Do e.Log() |
| 58 | + Set tSC = e.AsStatus() |
| 59 | + } |
| 60 | + |
| 61 | + // Cleanup |
| 62 | + If tRedirected { |
| 63 | + Do ..EndOutputCapture() |
| 64 | + } |
| 65 | + Do ..EndServer() |
| 66 | + Quit tSC |
| 67 | +} |
| 68 | + |
| 69 | +Method StartOutputCapture() [ ProcedureBlock = 0 ] |
| 70 | +{ |
| 71 | + New tSC, tRedirected |
| 72 | + #dim ex As %Exception.AbstractException |
| 73 | + #dim tSC As %Status = $$$OK |
| 74 | + #dim tRedirected As %Boolean = 0 |
| 75 | + Try { |
| 76 | + Set %server = $this |
| 77 | + Set ..OriginallyRedirected = 0 |
| 78 | + Set ..OriginalMnemonic = "" |
| 79 | + Set ..OriginalDevice = $IO |
| 80 | + |
| 81 | + Set ..OriginallyRedirected = ##class(%Library.Device).ReDirectIO() |
| 82 | + Set ..OriginalMnemonic = ##class(%Library.Device).GetMnemonicRoutine() |
| 83 | + Use ..OriginalDevice::("^"_$ZNAME) |
| 84 | + Set tRedirected = 1 |
| 85 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 86 | + } Catch ex { |
| 87 | + Set tSC = ex.AsStatus() |
| 88 | + |
| 89 | + // In case of exception, clean up. |
| 90 | + If tRedirected && ##class(%Library.Device).ReDirectIO(0) { |
| 91 | + Use ..OriginalDevice |
| 92 | + } |
| 93 | + If (..OriginalMnemonic '= "") { |
| 94 | + Use ..OriginalDevice::("^"_..OriginalMnemonic) |
| 95 | + } |
| 96 | + If ..OriginallyRedirected { |
| 97 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Quit tSC |
| 102 | + |
| 103 | + #; Public entry points for I/O redirection |
| 104 | +wstr(s) Do write(s) |
| 105 | + Quit |
| 106 | +wchr(a) Do write($char(a)) |
| 107 | + Quit |
| 108 | +wnl Do write($$$EOL) |
| 109 | + Set $X = 0 |
| 110 | + Quit |
| 111 | +wff Do wnl Quit |
| 112 | +wtab(n) New tTab |
| 113 | + Set tTab = $J("",$S(n>$X:n-$X,1:0)) |
| 114 | + Do write(tTab) |
| 115 | + Quit |
| 116 | +write(str) |
| 117 | + // If there was an argumentless NEW, cache the output and leave it at that. |
| 118 | + // This will be output next time there's a write with %server in scope. |
| 119 | + If '$IsObject($Get(%server)) { |
| 120 | + Set ^||OutputCapture.Cache($i(^||OutputCapture.Cache)) = str |
| 121 | + Quit |
| 122 | + } |
| 123 | + |
| 124 | + // Restore previous I/O redirection settings. |
| 125 | + New tOriginalDevice,i |
| 126 | + Set tOriginalDevice = $io |
| 127 | + If ##class(%Library.Device).ReDirectIO(0) { |
| 128 | + Use tOriginalDevice |
| 129 | + } |
| 130 | + If (%server.OriginalMnemonic '= "") { |
| 131 | + Use tOriginalDevice::("^"_%server.OriginalMnemonic) |
| 132 | + } |
| 133 | + If %server.OriginallyRedirected { |
| 134 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 135 | + } |
| 136 | + |
| 137 | + If $Data(^||OutputCapture.Cache) { |
| 138 | + For i=1:1:$Get(^||OutputCapture.Cache) { |
| 139 | + Do reallywrite(^||OutputCapture.Cache(i)) |
| 140 | + } |
| 141 | + Kill ^||OutputCapture.Cache |
| 142 | + } |
| 143 | + |
| 144 | + // Write out Base64-Encoded string |
| 145 | + Do reallywrite(str) |
| 146 | + |
| 147 | + // Turn I/O redirection back on. |
| 148 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 149 | + Use tOriginalDevice::("^"_$ZNAME) |
| 150 | + Quit |
| 151 | +reallywrite(pString) |
| 152 | + New tMsg |
| 153 | + Set tMsg = {"content":(pString)} // This is handy because it handles escaping of newlines, etc. |
| 154 | + Do %server.Write($System.Encryption.Base64Encode(tMsg.%ToJSON())) |
| 155 | + Quit |
| 156 | +rstr(len, time) Quit "" |
| 157 | +rchr(time) Quit "" |
| 158 | +} |
| 159 | + |
| 160 | +Method EndOutputCapture() |
| 161 | +{ |
| 162 | + Set tSC = $$$OK |
| 163 | + Try { |
| 164 | + If (..OriginalMnemonic '= "") { |
| 165 | + Use ..OriginalDevice::("^"_..OriginalMnemonic) |
| 166 | + } |
| 167 | + If ..OriginallyRedirected { |
| 168 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 169 | + } |
| 170 | + } Catch e { |
| 171 | + Set tSC = e.AsStatus() |
| 172 | + } |
| 173 | + Quit tSC |
| 174 | +} |
| 175 | + |
| 176 | +Method SendJSON(pObject As %DynamicAbstractObject) |
| 177 | +{ |
| 178 | + Set tOriginalDevice = $io |
| 179 | + If ##class(%Library.Device).ReDirectIO(0) { |
| 180 | + Use tOriginalDevice |
| 181 | + } |
| 182 | + If (..OriginalMnemonic '= "") { |
| 183 | + Use tOriginalDevice::("^"_..OriginalMnemonic) |
| 184 | + } |
| 185 | + If ..OriginallyRedirected { |
| 186 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 187 | + } |
| 188 | + Do ..Write($System.Encryption.Base64Encode(pObject.%ToJSON())) |
| 189 | + Do ##class(%Library.Device).ReDirectIO(1) |
| 190 | + Use tOriginalDevice::("^"_$ZNAME) |
| 191 | +} |
| 192 | + |
| 193 | +} |
0 commit comments