URLRewriteFilter in Development Environment? - grails

I am using UrlRewriteFilter to forward all requests to my domain to the "www" version for SEO.
I have a basic rule:
<rule>
<name>Domain Name Check</name>
<condition name="host" operator="notequal">www.mydomain.com</condition>
<from>/.*</from>
<to type="permanent-redirect">http://www.mydomain.com</to>
</rule>
This works great for production, but when I am running in development mode it changes my domain as well, from localhost:8080/mysite to www.mydomain.com.
How can I fix this up for development mode? I am using Grails and Tomcat, so everything is bundled in a .war that gets deployed to the server.
Thanks!

Can you try adding this in BuildConfig.groovy to exclude the dependency? (provided you have not added the jar in lib)
grails.project.dependency.resolution = {
inherits("global") {
if (Environment.current != Environment.PRODUCTION)
excludes "urlrewritefilter"
}
}
If that does not work then in my opinion the safest bet will be to fork/clone the plugin (which uses the latest version of urlrewritefilter:4.0.3) and modify UrlRewriteGrailsPlugin.groovy by adding the below elements:
def environments = ['prod'] //or 'production'
def scopes = [excludes:'run'] //If you normally use run-app in development
and use the plugin instead of adding the dependency.

You don't have to exclude the lib. Instead, you can define different web.xml files for development and for production. And you just include the UrlRewriteFilter in the production.

Related

Grails 2.5.0 endpoints located sans application context

I am upgrading an existing Grails 2.2.4 app to 2.5.0.
For some reason, endpoints whose URLs included the app context in 2.2.4 no longer include the app context in 2.5.0.
e.g., for app context = app, old endpoint in 2.2.4 was:
/app/a/b/c
but in 2.5.0 is now:
/a/b/c
I have tried many things to fix this, including:
in application.properties:
app.name=app
app.context=/app
in Config.groovy, for testing on localhost:8080:
grails.app.context = "/${appName}"
grails.serverURL = http://localhost:8080 and grails.serverURL = http://localhost:8080/${appName}, at different times
app.grails.serverURL = http://localhost:8080 and app.grails.serverURL = http://localhost:8080/${appName}, at different times
I've verified that "${appName}" == 'app'.
I have upgraded all of the plugins to the newest version, but I'm continuing to use the resources plugin instead of the asset-pipeline plugin.
I am not using scaffolding.
Any suggestions?
UrlMappings.groovy contained some mappings with (parseRequest:true), which is no longer supported by Grails. Removing that fixed it.

Grails: Conditionally Load Spring Security LDAP Plugin

I have an app that runs in multiple production environments. In one environment we want to authenticate with LDAP, in the other we do not. If the Spring Security LDAP plugin is included in BuildConfig.groovy, the non-LDAP environment fails to authenticate because LDAP is not configured.
I tried
environments {
devldap {
plugins {
compile ":spring-security-ldap:2.0-RC2"
}
}
}
but the LDAP plugin still builds with the non-LDAP environment and causes the non-LDAP environment (in this case development) to fail to authenticate if I don't include the LDAP configuration because it can't connect to LDAP.
I've tried
grails clean
grails refresh-dependencies
but the LDAP plugin only uninstalls if I completely comment it out.
How can I conditionally include/exclude a plugin in my build?
I see this question is a bit old now, however I do a similar thing with the Melody plugin. There is no value in this being installed during TEST - and can get in the way - so I do the following:
plugins {
// other plugins ...
if( Environment.current != Environment.TEST )
compile ":grails-melody:1.56.0"
// other plugins ...
}
So when I run 'test-app' I see the plugin 'uninstalled' and then when I do 'run-app' I see it installed and it's available.
NOTE: I recently got caught out by forgetting to also do an import grails.util.Environment. If you do that, you'll find that Environment.current == [:] as does Environment.TEST etc. I believe this is due to the builder behind the config file.

location of "web-app" folder in development phase

During my grails app development phase (when I compile and run using grails run-app), all my static files reside in web-app folder as usual.
Is there a way (using config or command line parameter) I can use a different directory, say web-app2 for my static files? Something like grails run-app -Dgrails.static.loc=~/web-app2 run-app?
I need this only for development time. For production and deployment, I'm fine with the default behavior.
in your config.groovy you can set environment-specific configurations,
sth like
environments {
development {
grails.resources.work.dir = '/webapp-2/'
grails.resources.uri.prefix = '/staticstuff/'
}
production {
grails.resources.work.dir = '/webapp/'
grails.resources.uri.prefix = '/static/'
}
}
you can start your application with
grails dev run-app // runs with the "development" data source
grails prod run-app // runs with the production data source
I tryed it out, works well!
have a look at http://www.grails.org/Environments and http://grails-plugins.github.io/grails-resources/guide/9.%20Configuration.html

Use of 'grails.logging.jul.usebridge' in grails config.groovy

I am a newbie for log4j and sl4j, I am using grails 2.0.4 and in config.groovy there is a line
grails.logging.jul.usebridge = false for prod
&
grails.logging.jul.usebridge = true for dev
I followed this article, As it says that use of grails.logging.jul.usebridge is to implement the swapping logic of logging frameworks such as
log4j
java.util.logging
commons logging
logback
Is this the only use of grails.logging.jul.usebridge in config.groovy, or is there any other uses
And one more question
which is the recommended logging framework to use in production environment
I definitely recommend you using Log4j. It has no dependencies and is tested (or even included) in various web app servers. You can configure it easily via DSL in Config.groovy or in separate config file in production environment.
Both commons-logging and SLF4J are wrappers for Log4j and use it underneath.
The grails.logging.jul.usebridge = true is used to put java.util.logging through SLF4J, as described here.

Customise behaviour of a Grails environment

I was wanting to run a custom environment called "local"...as in a local dev. I would use the config (eg DB connections) of this before a war would be deployed to the "shared" development server. But I noticed that it lacks the behavior of the standard Grails "development" Environment, such as Changes to a GSP were not available when you refresh the browser.
So this led me to wonder how do you change the behaviours of a custom environment? How can you copy all the settings for "development" to another environment?
You can enable reloading of modified gsp in a custom environment specifying the run-app flag:
-Dgrails.gsp.enable.reload=true
I don't think the automatic reloading is environment dependent. If you execute grails run-app, reloading will happen regardless of which environment you run under. In other words, automatic reloading will happen for all of
grails dev run-app
grails prod run-app
grails test run-app
On the other hand, reloading will not happen if you build a war using grails war, then deploy it. So reloading depends on how you run the app, not the environment. The easiest way to define a custom environment that is similar to dev, is to define a set of default configuration, then selectively override the settings for each environemnt, e.g.
//default config
myApp {
userRoleName = 'ROLE_USER'
adminRoleName = 'ROLE_ADMIN'
dateFormat = 'yyyy-MM-dd'
}
environments {
// config overrides for dev
development {
myApp.dateFormat = 'yyyy/MM/dd'
}
// config overrides for local
local {
myApp.dateFormat = 'MM/yy/dd'
}
}

Resources