Skip to content

Commit

Permalink
add GtAutomaticNetworkProxySetter [feenkcom/gtoolkit#3704]
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajKubelka committed Mar 27, 2024
1 parent 37f384a commit 7f1ec73
Show file tree
Hide file tree
Showing 8 changed files with 445 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ BaselineOfGToolkitUtility >> baseline: spec [

package: 'GToolkit-Releaser-Logging-Analysis' with: [
spec requires: #('GToolkit-Utility-Logging' ). ];
package: 'GToolkit-Utility-ObjectConnections' ].
package: 'GToolkit-Utility-ObjectConnections';
package: 'GToolkit-Utility-Network' ].
]
245 changes: 245 additions & 0 deletions src/GToolkit-Utility-Network/GtAutomaticNetworkProxySetter.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
"
I configure {{gtClass:NetworkSystemSettings}} based on OS environment variables.
I configure it on {{gtMethod:GtAutomaticNetworkProxySetter class >>startUp: | label=startup}}.
Possible proxy environment variables are: {{gtExample: GtAutomaticNetworkProxySetter>>#itself | noCode | previewShow=#gtProxyVariablesFor: | previewHeight=150}}
Possible no-proxy environment variables are: {{gtExample: GtAutomaticNetworkProxySetter>>#itself | noCode | previewShow=#gtNoProxyVariablesFor: | previewHeight=100}}
"
Class {
#name : #GtAutomaticNetworkProxySetter,
#superclass : #Object,
#classVars : [
'IsAutomaticSetupEnabled'
],
#category : #'GToolkit-Utility-Network'
}

{ #category : #configuration }
GtAutomaticNetworkProxySetter class >> disable [
IsAutomaticSetupEnabled := false
]

{ #category : #configuration }
GtAutomaticNetworkProxySetter class >> enable [
IsAutomaticSetupEnabled := true
]

{ #category : #initialization }
GtAutomaticNetworkProxySetter class >> initialize [
"Get notified on session starts so the network proxy can be set"

SessionManager default registerUserClassNamed: self name
]

{ #category : #testing }
GtAutomaticNetworkProxySetter class >> isEnabled [
^ IsAutomaticSetupEnabled ifNil: [ true ]
]

{ #category : #accessing }
GtAutomaticNetworkProxySetter class >> noProxyVariablePragmas [
^ Pragma
allNamed: #noProxyVariables:
from: GtAutomaticNetworkProxySetter
to: self
sortedByArgument: 1
]

{ #category : #accessing }
GtAutomaticNetworkProxySetter class >> proxyVariablePragmas [
^ Pragma
allNamed: #proxyVariables:
from: GtAutomaticNetworkProxySetter
to: self
sortedByArgument: 1
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter class >> set [
self new set
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter class >> setIfEnabled [
self new setIfEnabled
]

{ #category : #'system startup' }
GtAutomaticNetworkProxySetter class >> startUp: resuming [
resuming ifFalse: [ ^ self ].
self set
]

{ #category : #variables }
GtAutomaticNetworkProxySetter >> httpProxyVariables [
<proxyVariables: 15>
^ #('HTTP_PROXY' 'http_proxy')
]

{ #category : #variables }
GtAutomaticNetworkProxySetter >> httpsProxyVariables [
<proxyVariables: 10>
^ #('HTTPS_PROXY' 'https_proxy')
]

{ #category : #testing }
GtAutomaticNetworkProxySetter >> isEnabled [
^ self class isEnabled
]

{ #category : #example }
GtAutomaticNetworkProxySetter >> itself [
<gtExample>
| isProxyEmpty isNoProxyEmpty |
isProxyEmpty := isNoProxyEmpty := true.
self proxyVariableNamesDo: [ :aName | isProxyEmpty := false ].
self noProxyVariableNamesDo: [ :aName | isNoProxyEmpty := false ].

self
assert: isProxyEmpty not
description: [ 'Possible proxy variable names are missing' ].
self
assert: isProxyEmpty not
description: [ 'Possible no-proxy variable names are missing' ].

^ self
]

{ #category : #building }
GtAutomaticNetworkProxySetter >> noProxyVariableNamesDo: aBlock [
"Iterate over all possible environment variable names."

self noProxyVariablePragmas
do: [ :aPragma |
| aCollection |
aCollection := self perform: aPragma methodSelector.
aCollection do: [ :aVariableName |
aBlock cull: aVariableName cull: aPragma ] ]
]

{ #category : #accessing }
GtAutomaticNetworkProxySetter >> noProxyVariablePragmas [
^ self class noProxyVariablePragmas
]

{ #category : #variables }
GtAutomaticNetworkProxySetter >> noProxyVariables [
<noProxyVariables: 10>
^ #('NO_PROXY' 'no_proxy')
]

{ #category : #building }
GtAutomaticNetworkProxySetter >> proxyVariableNamesDo: aBlock [
"Iterate over all possible environment variable names."

self proxyVariablePragmas
do: [ :aPragma |
| aCollection |
aCollection := self perform: aPragma methodSelector.
aCollection do: [ :aVariableName |
aBlock cull: aVariableName cull: aPragma ] ]
]

{ #category : #accessing }
GtAutomaticNetworkProxySetter >> proxyVariablePragmas [
^ self class proxyVariablePragmas
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter >> set [
self setProxy.
self setNoProxy
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter >> setIfEnabled [
self isEnabled ifFalse: [ ^ self ].
self set
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter >> setNoProxy [
self noProxyVariableNamesDo: [ :aName |
(self setNoProxyUsingVariableNamed: aName) ifTrue: [ ^ self ] ]
]

{ #category : #building }
GtAutomaticNetworkProxySetter >> setNoProxyUsingVariableNamed: aName [
"If environment variable exists, set network proxy exceptions and return true. If the environment variable does not exist, return false."

<return: #Boolean>
| aValue anAdoptedValue |
aValue := Smalltalk os environment
at: aName
ifAbsent: [
GtNetworkProxyUndefinedEnvironmentVariableSignal new
variableName: aName;
emit.
^ false ].

anAdoptedValue := [ aValue copyReplaceAll: ',' with: ';' ]
on: Error
do: [ :anException |
anException emit.
GtNetworkProxyInvalidEnvironmentVariableValueSignal new
exception: anException;
variableName: aName;
variableValue: aValue;
emit.
^ false ].

NetworkSystemSettings manageHttpProxyExceptions: anAdoptedValue.

GtNetworkNoProxySetSignal new
variableName: aName;
variableValue: aValue;
exceptionsValue: anAdoptedValue;
emit.
^ true
]

{ #category : #configuring }
GtAutomaticNetworkProxySetter >> setProxy [
self proxyVariableNamesDo: [ :aName |
(self setProxyUsingVariableNamed: aName) ifTrue: [ ^ self ] ]
]

{ #category : #building }
GtAutomaticNetworkProxySetter >> setProxyUsingVariableNamed: aName [
"If environment variable exists, set network proxy and return true. If the environment variable does not exist, return false."

<return: #Boolean>
| aValue anUrl |
aValue := Smalltalk os environment
at: aName
ifAbsent: [
GtNetworkProxyUndefinedEnvironmentVariableSignal new
variableName: aName;
emit.
^ false ].

anUrl := [ aValue asZnUrl ]
on: Error
do: [ :anException |
anException emit.
GtNetworkProxyInvalidEnvironmentVariableValueSignal new
exception: anException;
variableName: aName;
variableValue: aValue;
emit.
^ false ].

NetworkSystemSettings
useHTTPProxy: true;
httpProxyPort: anUrl port;
httpProxyServer: anUrl host.

GtNetworkProxySetSignal new
variableName: aName;
variableValue: aValue;
proxyUrl: anUrl;
proxyPort: anUrl port;
proxyServer: anUrl host;
emit.
^ true
]
48 changes: 48 additions & 0 deletions src/GToolkit-Utility-Network/GtNetworkNoProxySetSignal.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Class {
#name : #GtNetworkNoProxySetSignal,
#superclass : #GtNetworkProxySignal,
#instVars : [
'variableName',
'variableValue',
'exceptionsValue'
],
#category : #'GToolkit-Utility-Network'
}

{ #category : #accessing }
GtNetworkNoProxySetSignal >> exceptionsValue [
^ exceptionsValue
]

{ #category : #accessing }
GtNetworkNoProxySetSignal >> exceptionsValue: anObject [
exceptionsValue := anObject
]

{ #category : #printing }
GtNetworkNoProxySetSignal >> printOneLineContentsOn: stream [
stream
nextPutAll: self variableName asString;
nextPut: $=;
nextPutAll: self variableValue asString
]

{ #category : #accessing }
GtNetworkNoProxySetSignal >> variableName [
^ variableName
]

{ #category : #accessing }
GtNetworkNoProxySetSignal >> variableName: anObject [
variableName := anObject
]

{ #category : #accessing }
GtNetworkNoProxySetSignal >> variableValue [
^ variableValue
]

{ #category : #accessing }
GtNetworkNoProxySetSignal >> variableValue: anObject [
variableValue := anObject
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Class {
#name : #GtNetworkProxyInvalidEnvironmentVariableValueSignal,
#superclass : #GtNetworkProxySignal,
#instVars : [
'exception',
'variableName',
'variableValue'
],
#category : #'GToolkit-Utility-Network'
}

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> exception [
^ exception
]

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> exception: anObject [
exception := anObject
]

{ #category : #printing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> printOneLineContentsOn: stream [
stream
nextPutAll: self variableName asString;
nextPut: $=;
nextPutAll: self variableValue asString;
nextPutAll: ', ';
print: self exception
]

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> variableName [
^ variableName
]

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> variableName: anObject [
variableName := anObject
]

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> variableValue [
^ variableValue
]

{ #category : #accessing }
GtNetworkProxyInvalidEnvironmentVariableValueSignal >> variableValue: anObject [
variableValue := anObject
]

0 comments on commit 7f1ec73

Please sign in to comment.