Grails: Using the Resources plugin in a plugin - grails

I'm trying to use the Resources plugin (http://grails.org/plugin/resources) in a plugin that I'm developing, however, I can't seem to get it to work. I keep getting error messages that the module (defined in the ApplicationResources.groovy file) doesn't exist when it tries to render the gsp file. Only if I add the module to the Grails application's (that my plugin is being used in) ApplicationResources.groovy file does this error message go away. Clearly the Resources plugin is only looking at the application files, and not the plugin files. Is this a known issue? Is there a solution or work around to be able to use the Resources plugin in a plugin?

I simply renamed the ApplicationResources.groovy file to MyPluginResources.groovy (where MyPlugin is the name of the plugin) and the problem went away.

Related

NopCommerce Plugin Localization

I am working on a NopCommerce website and have quite a bit of site-wide customization so I have created a plugin to handle it all but not sure on how to handle the localization. I see there are a couple of ways of updating the Localization strings, one way I have found is in the Plugin's Install() method:
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.PayPalStandard.Fields.AdditionalFee", "Additional fee");
This looks like it only adds new resource strings for the plugin, is there a similar way to update the other resources via the Install() method like:
Admin.Catalog.Products.List.DownloadPDF
I found that there is a way to export the entire language to a language_pack.xml file, would it be better to just create an entire language pack instead? Is there a way to add a new language pack from the plugins Install() method?
I guess I could simply open the language_pack.xml file and add each resource found using the AddOrUpdatePluginLocaleResource, I was hoping that there was a built-in way of doing this using NopCommernce functionality.
Thanks!
As #Raphael suggested in a comment, provide a language pack along with plugin file to the end users, and give an option to upload required resource file within your plugin configuration page.
As per as I know, there is no inbuilt way to add language pack on plugin installation, but you can do some code on plugin install method to find language pack file(s) from plugin folder and install it, not quite sure, you can take reference of inbuilt methods.

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

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)
}

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.

Font Awesome Failed to load resource /font/fontawesome-webfont.woff?v=3.0.1

Everything looks fine on the site, but those two errors are showing up in the console. If I hit the url the font downloads. Been trying to track this down for a while, but I am getting two errors in the console:
Failed to load resource resource:/font/fontawesome-webfont.woff?v=3.0.1
Failed to load resource resource:/font/fontawesome-webfont.ttf?v=3.0.1
How can I get rid of this error?
I recently encountered and solved this problem. The solution is to add the 'font' directory, and any other directories you want resources to be processed to your adhoc includes, so it ends up looking something like this in config.groovy (add directories as needed if you have resources elsewhere).
grails.resources.adhoc.includes = ['/images/**', '/css/**', '/js/**', '/font/**']
More detail here:
URLs within CSS files broken with Grails resources plugin 1.2.7
The resource: prefix is an intermediary step in the grails resource plugin's css rewriting process. You appear to be tripping over a bug in either the ad-hoc resource processor or in the css rewriter.
I made an example application (grails 2.1.1, font-awesome 3.0.2, grails-resources 1.1.6) which upon initial load shows no errors. After modifying the font-awesome.css with the application running, the rewriter then throws errors and leaves the broken resource: urls in place.
If I perform the same request with ?_debugResources=true the errors then disappear again.
In my sample's case, leaving the font-awesome files alone after deployment OR using the font-awesome-resources plugin prevented the errors from showing up.
I had the same issue.
Try using-
src:url(asset-path('fontawesome-webfont.eot?v=3.2.1', font));
instead of a direct static path.
It worked for me on clearing up those specific errors.
This is problem of ?v=3.0.1 change your file name and refrence as well. it will work fine. use only fontawesome-webfont.woff & fontawesome-webfont.ttf file name

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