Not able to access web-app folder files in Grails 3 - grails

I am not able to access web-app folder files in Grails 3.
I have robots.txt in the web-app folder and in Grails 2 I was able to access it directly at http://localhost:8080/robots.txt. After migrating to Grails 3 I am not able to access it anymore at http://localhost:8080/robots.txt.
How can I make these files accessible again?

See https://github.com/grails/grails-core/releases/tag/v3.0.12 and part Location of static resources
In Grails 3 you can store your files under src/main/resources. You can access them by filename preceded with static for example http://localhost:8080/static/robots.txt.
This path can be changed using config option as defined in attached URL

I faced a similar problem in Grails 3.3.1. I had the necessity to access a file template to use it with a plugin (excel-export plugin). After reading the documentation, I put the file under src/main/resources. It worked with OK in development mode (grails run-app), but I received an error in production environment (grails war). After a lot of reading, I've found the way to make it work. I've let the file in the same directory (src/main/resources), and then, in my controller:
def template = this.class.classLoader.getResource('myExcelFile.xlsx')
def path = template.file //will give you the real path to your file
Having the path, you can then open a stream or do what you need to do. In my case, use it whith the plugin:
new WebXlsxExporter(path).with {
setResponseHeaders(response)
add(products, withProperties)
save(response.outputStream)
}

Related

Grails 3 how to access favicon, icon and tile files from app root

I have an Grails app that runs on http://localhost:8092
Different browsers look for certain files in the root path (http://localhost:8092/), for instance: favicon.ico, browserconfig.xml (looks for ms tiles on Windows), site.webmanifest (looks for icons + app name on Android that is used when adding the web app to home screen), safari-pinned-tab.svg (for Safari), favicon-32x32.png/favicon-16x16.png (for alternative size favicons), etc.
What I can make work is favicon.ico, adding it to grails-app/assets/images and using the asset pipeline in the GSP:
But the rest doesn't work. Also site.webmanifest and browserconfig.xml files have paths to certain images to be used as Android icons or Windows tiles respectively. I can change those paths to be under /assets/... but those files should be available under the Grails app root.
Looked in the docs and tried to find examples online but couldn't find anything for Grails 3.
How can I configure Grails to allow those files to be accessed from the app's root path?
BTW I'm using Grails 3.2.9
Basing on the release notes:
In order to resolve an issue around how POST requests are treated for
REST applications on non-existent resources, static resources located
in src/main/resources/public are now resolved under the /static/** URI
by default, instead of the base URI /**. If you wish to restore the
previous behaviour add the following configuration:
grails.resources.pattern = '/**'
It's also resolving issue (even for rest-api profile app) with:
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'grailsDispatcherServlet'
Put all of them in src/main/resources/public
https://gsp.grails.org/latest/guide/resources.html

Where is the default login page for the spring security core plugin?

I have installed the spring security core plugin. I need to modify the login page to look like my existing website. I have searched the entire project and cannot find it. I am running grails 2.4 and spring-security-core:2.0-RC5. Where can this pesky little file be? Can someone who is not a complete greenhorn help a fellow out?
As #Abs points out, the file is at target/work/plugins/spring-security-core-2.0-RC5/grails-app/views/login/auth.gsp but you shouldn't edit plugin files. Other developers on your team won't have access to the modified files and if you delete the target directory you'll lose your changes since the target directory is only a temporary work location.
Instead, copy the file to the same relative location in your application and make changes there. Create grails-app/views/login and copy the file there and make whatever changes you want.
This technique works for most plugin files, not just GSPs. The compilation order and classpath are configured such that application files and classes override plugin files if they're in the same location/package.
You can find the default login page here
targt->work->plugins->spring-security-core-2.0-RC5->grails-app->views->login>auth.gsp

Grails 2.5.0: way to fill database automatically

I have a business web application made with Grails 2.5.0.
In this application, I use a GSP form to upload files and to fill a database.
In fact, I upload httpd conf files from a directory and I extract informations from them.
Now, I want to find a solution to do the same automatically. But I didn't find solution.
I tried Spring Batch plugin but it does work with Grails 2.5.0.Other Batch plugin seems too old to work.
Other way, it perhaps to import files at startup but I don't know if you can read files from a directory at startup ( e.g : from /var/myapp/conf ).
If you want to execute something when the application starts add it to BootStrap.groovy

Spring Web Service Message Dispatcher overrriding

I am currently working on creating a web service using Spring -WS.
I want to make the request reach my own Message Dispatcher Class. So I made the necessary configuration changes to web.xml and also my sping-congfig.xml file.
I am seeing an error when spring loads beans for my Message Dispatcher,
it tries to look for a properties file in my package which has the Dispatcher Class. for Example if my class is AccountMessageDispatcher, it looks for AccountMessageDispatcher.properties file in the package where I have created the class, I can get it running by keeping the properties file there, but I want to keep the properties file under my resources directory which has other property files needed by my application.
Can any one help me or point me in the right direction as to what I am doing wrong?
If we take a look to the default MessageDispatcher infrastructure, we'll that it reads appropriate proerties file - org.springframework.ws.server.MessageDispatcher.properties.
As you see this file is located at the same package as the original MessageDispatcher class.
According to your concern, you are right: that file should be located at the resources dir for sources. But if you use normal build system like Maven or Gradle, all your resources are packaged to the target jar alongside with classes.
To achieve your requirements you just need to create the same dir tree in the resources as your original AccountMessageDispatcher.
Actually any Java package is a dir in the end jar.

How to access files in the Project Directory with Grails

I needed some templates to render some code for users to paste. I put these into
/project-dir/grails-app/resources/templates/quickInstallCode.html
Then I tried accessing them using their relative path (grails-app/resources/templates/quickInstallCode.html), and it worked great.
When we then deployed the application to a Tomcat Server, using a .war file, the paths began pointing to a wrong location.
ERROR call, Template file /var/lib/tomcat6/grails-app/resources/templates/quickInstallCode.html not found.
I assumed, that Grails, giving good defaults for everything would handle this mess for me, but it seems like it does not.
I also tried this call, and it seemed to work great, but when deployed, the BuildSettingsHolder did not contain build Settings, which resulted in a fatal error.
BuildSettingsHolder.settings.baseDir.toString()
http://grails.org/doc/latest/api/grails/util/BuildSettingsHolder.html
http://grails.org/doc/latest/api/grails/util/BuildSettings.html
I am pretty frustrated that I cannot get this easy task to work, but the reason that this is so complicated seems to be that all Files are encapsuled in a WAR and not unpacked on the Server.
So the Questions are:
Where in your Project would you put
Files like this?
How to get a
reliable and stable way to access
this files? I just need a stable path to a base directory, without having to hardcode something in the configuration ... This cannot be so hard.
I have 2 solution to propose for this situation:
Save the template in the database, in a setting table. This way guarantees that nothing can go wrong.
You can consider using the resource folder like Sachin & Nirmal has proposed. About security, I think you can configure SpringSecurity Plugin to protect the specific resources, so that it can only be accessed by the site user.
Take a look at this link and try to use the getResource that spring provides. Its way more flexible and configurable.
def filePath = "resources/file.txt"
def appHolder=ApplicationHolder.application.parentContext.getResource("classpath:$filePath")
By the way Conf is on the root of the classpath, you can stick the files in src/java or src/groovy.
I keep my static resources in web-app folder and access them like this
ApplicationHolder.application.parentContext.servletContext.getRealPath("quickInstallCode.html")
// quickInstallCode.html should be in web-app folder.

Resources