Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix for GRAILS-6271 "Improve support for proxies, with multiple named…

… locations"
  • Loading branch information...
commit e168c95fe9b092e8617c96f551927a0c34dd3b91 1 parent 3a85061
@graemerocher graemerocher authored
View
46 scripts/AddProxy_.groovy
@@ -0,0 +1,46 @@
+
+/**
+ * @author Graeme Rocher
+ * @since 1.2.3
+ */
+
+includeTargets << grailsScript("_GrailsArgParsing")
+target(default:"Adds a proxy configuration") {
+ depends(parseArguments)
+
+
+ if(!argsMap.params) {
+ println msg()
+ exit 1
+ }
+ else {
+ if(argsMap.host && argsMap.port) {
+ def settingsFile = grailsSettings.proxySettingsFile
+ config = grailsSettings.proxySettings
+
+ config[argsMap.params[0]] = ['http.proxyHost':argsMap.host,
+ 'http.proxyPort':argsMap.port,
+ "http.proxyUserName": argsMap.username?: '',
+ "http.proxyPassword": argsMap.password?: '']
+
+ settingsFile.withWriter { w ->
+ config.writeTo(w)
+ }
+
+ println "Added proxy ${argsMap.params[0]} to ${settingsFile}"
+ }
+ else {
+ println msg()
+ exit 1
+ }
+ }
+}
+
+String msg() {
+ return '''\
+Usage: grails add-proxy [name] --host=[server] --port=[port] --username=[username]* --password=[password]*
+Example: grails add-proxy client --host=proxy-server --port=4300 --username=guest --password=guest
+
+* Optional
+'''
+}
View
18 scripts/ClearProxy_.groovy
@@ -0,0 +1,18 @@
+
+/**
+ * @author Graeme Rocher
+ * @since 1.2.3
+ */
+
+target(default:"Clears a proxy configuration") {
+ def settingsFile = grailsSettings.proxySettingsFile
+ config = grailsSettings.proxySettings
+ config.remove('currentProxy')
+
+ settingsFile.withWriter { w ->
+ config.writeTo(w)
+ }
+
+ println "Cleared proxy settings."
+}
+
View
35 scripts/RemoveProxy_.groovy
@@ -0,0 +1,35 @@
+
+/**
+ * @author Graeme Rocher
+ * @since 1.2.3
+ */
+
+includeTargets << grailsScript("_GrailsArgParsing")
+target(default:"Removes a proxy configuration") {
+ depends(parseArguments)
+
+
+ if(!argsMap.params) {
+ println msg()
+ exit 1
+ }
+ else {
+ def settingsFile = grailsSettings.proxySettingsFile
+ config = grailsSettings.proxySettings
+ def name = argsMap.params[0]
+ config.remove(name)
+
+ settingsFile.withWriter { w ->
+ config.writeTo(w)
+ }
+
+ println "Removed proxy configuration [${name}]."
+ }
+}
+
+String msg() {
+ return '''\
+Usage: grails remove-proxy [name]
+Example: grails remove-proxy client
+'''
+}
View
85 scripts/SetProxy_.groovy
@@ -1,54 +1,57 @@
+import grails.util.BuildSettings
+
/*
- * Copyright 2004-2005 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+* Copyright 2004-2005 the original author or authors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
/**
* Gant script for setting HTTP proxy-settings.
*
- * @author Sergey Nebolsin
+ * @author Graeme Rocher
*
- * @since 0.5.5
+ * @since 1.2.3
*/
-includeTargets << grailsScript("_GrailsEvents")
-includeTargets << grailsScript("_GrailsProxy")
+includeTargets << grailsScript("_GrailsArgParsing")
target ( "default" : "Sets HTTP proxy configuration for Grails") {
- depends(configureProxy)
- setProxy()
-}
+ depends(parseArguments)
+
+ if(argsMap.params) {
+ def name = argsMap.params[0]
+
+ BuildSettings settings = grailsSettings
+ ConfigObject proxySettings = settings.proxySettings
+ if(proxySettings[name]) {
+ proxySettings.currentProxy = name
+ settings.proxySettingsFile.withWriter { w ->
+ proxySettings.writeTo(w)
+ }
+ println "Proxy set to [$name]"
+ }
+ else {
+ println "No proxy configuration found for name: $name. Please add a proxy with add-proxy first."
+ exit 1
+ }
+ }
+ else {
+ println '''\
+Usage: grails set-proxy [name]
-target(setProxy:"Implementation target") {
- ant.mkdir( dir:"${userHome}/.grails/scripts" )
- def scriptFile = new File("${userHome}/.grails/scripts/ProxyConfig.groovy")
- ant.input(addProperty:"proxy.use", message:"Do you wish to use HTTP proxy?",validargs:'y,n',defaultvalue:'y')
- if( ant.antProject.properties."proxy.use" == 'n' ) {
- scriptFile.delete()
- event("StatusFinal", [ "Grails is configured to not use HTTP proxy"])
- } else {
- def proxyHost = System.getProperty("http.proxyHost") ? System.getProperty("http.proxyHost") : 'localhost'
- def proxyPort = System.getProperty("http.proxyPort") ? System.getProperty("http.proxyPort") : '80'
- def proxyUser = System.getProperty("http.proxyUserName") ? System.getProperty("http.proxyUserName") : ''
- def proxyPassword = System.getProperty("http.proxyPassword") ? System.getProperty("http.proxyPassword") : ''
- ant.input(addProperty:"proxy.host", message:"Enter HTTP proxy host [${proxyHost}]: ",defaultvalue:proxyHost)
- ant.input(addProperty:"proxy.port", message:"Enter HTTP proxy port [${proxyPort}]: ",defaultvalue:proxyPort)
- ant.input(addProperty:"proxy.user", message:"Enter HTTP proxy username [${proxyUser}]: ",defaultvalue:proxyUser)
- ant.input(addProperty:"proxy.password", message:"Enter HTTP proxy password: ")
- scriptFile.delete()
- scriptFile << "// This file is generated automatically with 'grails set-proxy' command\n"
- scriptFile << "proxyConfig = [proxyHost:'${ant.antProject.properties.'proxy.host'}',proxyPort:'${ant.antProject.properties.'proxy.port'}',proxyUser:'${ant.antProject.properties.'proxy.user'}',proxyPassword:'${ant.antProject.properties.'proxy.password'}']"
- event("StatusFinal", [ "Grails is configured to use HTTP proxy"])
+Sets a named proxy. Use "grails add-proxy" to add a named proxy.
+'''
+ exit 1
}
}
View
36 src/java/grails/util/BuildSettings.groovy
@@ -230,6 +230,16 @@ class BuildSettings {
/** The settings stored in the project's BuildConfig.groovy file if there is one. */
ConfigObject config = new ConfigObject()
+ /**
+ * The settings used to establish the HTTP proxy to use for dependency resolution etc.
+ */
+ ConfigObject proxySettings = new ConfigObject()
+
+ /**
+ * The file containing the proxy settings
+ */
+ File proxySettingsFile;
+
/** Implementation of the "grailsScript()" method used in Grails scripts. */
Closure grailsScriptClosure
@@ -689,14 +699,36 @@ class BuildSettings {
protected ConfigObject loadSettingsFile() {
if (!settingsFileLoaded) {
def settingsFile = new File("$userHome/.grails/settings.groovy")
+ def gcl = obtainGroovyClassLoader()
+ def slurper = createConfigSlurper()
if (settingsFile.exists()) {
- def gcl = obtainGroovyClassLoader()
- def slurper = createConfigSlurper()
Script script = gcl.parseClass(settingsFile)?.newInstance()
if (script) {
config = slurper.parse(script)
}
}
+
+ this.proxySettingsFile = new File("$userHome/.grails/ProxySettings.groovy")
+ if(proxySettingsFile.exists()) {
+ slurper = createConfigSlurper()
+ try {
+ Script script = gcl.parseClass(proxySettingsFile)?.newInstance()
+ if (script) {
+ proxySettings = slurper.parse(script)
+ def current = proxySettings.currentProxy
+ if(current) {
+ proxySettings[current]?.each { key, value ->
+ System.setProperty(key, value)
+ }
+ }
+ }
+ }
+ catch (e) {
+ println "WARNING: Error configuring proxy settings: ${e.message}"
+ }
+
+ }
+
settingsFileLoaded = true
}
config
Please sign in to comment.
Something went wrong with that request. Please try again.