Accessing resources in Private Grails Plugin web-app - file

I have a main grails application that use a private Grails plugin mySearch.
the plugin Search has a file in Search\web-app\filters.json.
when the application is run with run-app : the ressource file can be accessed with :
def ressource = new File( org.codehaus.groovy.grails.plugins.GrailsPluginUtils.getPluginDirForName('mySearch')?.file?.absolutePath
+"/web-app/filters.json").text
but It doesn't work when the app is deployed in tomcat7.
I'have tried using pluginManager.getGrailsPlugin('mySearch')
but I can't access the obsolute path of the resource.

After many attempts to resolve it, I found this workaround.
It looks messy but I didn't found anything else shorter and sweeter :
// work only for a plugin installed in app deployed from a war.
String getRessourceFile(String relativePath) throws FileNotFoundException{
def pluginDir = pluginManager.getGrailsPlugin('MyPlugin')?.getPluginPath()
def pluginFileSystemName = pluginManager.getGrailsPlugin('MyPlugin')?.getFileSystemName()
def basedir = grailsApplication.parentContext.getResource("/")?.file
if(basedir.toString().endsWith("web-app")){
basedir = basedir.parent
}
ressourcefile = "$basedir$pluginDir/$relativePath"
ressource = new File(ressourcefile)?.text
}

Related

Configuring Webpack build for file:// use in CEF

I have to develop a webapp for a CEF-Browser environment. There is no HTTP server available, everything will be served over file:// protocol.
When developing a Webapp nowadays one doesn't get round working with a framework like react/vue for frontend. The standard webpack build scripts of those build a bundle which only works served over HTTP.
Is it possible to configure webpacks build bundle to work on file:// or is there another way to use react or vue via file://?
I'm suggest read CEF wiki more carefully. You are especially interested in https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-request-handling
In short:
You can register custom scheme handler to serve resources over http+custom fake domain.
You can pack resources in zip for example if you like, or leave them at file system as is (but in that case you can expect that some funny users can edit your files, and then report back unexisting errors back to you).
Important helpers already done (but you can write own when need.)
You can... many other things.
Main thing that "file" scheme are more restricted, and for example you can't do XHR requests. But for custom handler you can. Even if dynamic loader for some reason use XHR instead DOM-based loading it will work again same as on http without touching network.
cefclient itself also has usage of custom schemes. Check URL of Tests->Other... in menu. :)
PS: Sorry that my answer doesnt have direct answer for your question. But, custom resource handling in CEF is so common, that i'm just should say about.
fddima is right - you don't need to configure your webpack (although it would be theoretically possible). Instead you can use custom scheme handler in CEF. I made it work with angular at work.
I wrote blog post on how to serve web application via 'file' protocol in CEF.
What you want to add is your scheme handler and its factory:
using System;
using System.IO;
using CefSharp;
namespace MyProject.CustomProtocol
{
public class CustomProtocolSchemeHandler : ResourceHandler
{
// Specifies where you bundled app resides.
// Basically path to your index.html
private string frontendFolderPath;
public CustomProtocolSchemeHandler()
{
frontendFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./bundle/");
}
// Process request and craft response.
public override bool ProcessRequestAsync(IRequest request, ICallback callback)
{
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
var requestedFilePath = frontendFolderPath + fileName;
if (File.Exists(requestedFilePath))
{
byte[] bytes = File.ReadAllBytes(requestedFilePath);
Stream = new MemoryStream(bytes);
var fileExtension = Path.GetExtension(fileName);
MimeType = GetMimeType(fileExtension);
callback.Continue();
return true;
}
callback.Dispose();
return false;
}
}
public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "customFileProtocol";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
return new CustomProtocolSchemeHandler();
}
}
}
And then register it before calling Cef.Initialize:
var settings = new CefSettings
{
BrowserSubprocessPath = GetCefExecutablePath()
};
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory()
});

MSF4J: Serving static content

Can MSF4J application serve static content without using the Mustache template engine. I have developed a REST service which will be consumed by an already developed angular web app. Now I need to package the same angular app with the micro service so it will render in the browser and will consume the service via ajax calls.
MSF4J does not directly support serving static content. From your question what I understood is that you want to point the MSF4J server to a directory and serve resources in that directory by their relative path or something similar. In this case what you can do is to write an MSF4J service method with a wildcard path and serve the static content that matches the path of the request.
#Path("/")
public class FileServer {
private static final String BASE_PATH = "/your/www/dir";
#GET
#Path("/**")
public Response serveFiles(#Context Request request) {
String uri = request.getUri();
System.out.println("Requested: " + uri);
File file = Paths.get(BASE_PATH, uri).toFile();
if (file.exists()) {
return Response.ok().entity(file).build();
} else {
return Response.status(404).entity("<h1>Not Found</h1>").build();
}
}
}

How to list files under a tomcat 6 webapp

I have a spring boot project that is deployed on Tomcat 6. I have a list of files under project's resource directory and I am trying to list all files under that directory. I am able to get specific file if I specify classpath:abc.txt in the getResource() method. However I am wondering if there is a way to just list all files with a wild card or something.
You cannot do like this MyClass.class.getResource('*.txt'). The alternative to this is :
ServletContext sc = request.getSession().getServletContext();
String strPath = sc.getRealPath("/");
File[] allFiles = new File(strpath).listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
});
Refer this :Using File.listFiles with FileNameExtensionFilter

Will Prism OnDemand module loading work in an OOB scenerio?

Should the loading of OnDemand Prism modules work in an OOB scenerio? If so, I cannot seem to make it work. Everything is currently working in browser without any problems. Specifically I:
register my modules in code:
protected override IModuleCatalog GetModuleCatalog() {
var catalog = new ModuleCatalog();
Uri source;
if( Application.Current.IsRunningOutOfBrowser ) {
source = IsolatedStorageSettings.ApplicationSettings[SOURCEURI] as Uri;
}
else {
var src = Application.Current.Host.Source.ToString();
src = src.Substring( 0, src.LastIndexOf( '/' ) + 1 );
source = new Uri( src );
IsolatedStorageSettings.ApplicationSettings[SOURCEURI] = source;
IsolatedStorageSettings.ApplicationSettings.Save();
}
if( source != null ) {
var mod2 = new ModuleInfo { InitializationMode = InitializationMode.OnDemand,
ModuleName = ModuleNames.mod2,
ModuleType = "mod2.Module, mod2.Directory, '1.0.0.0', Culture=neutral, PublicKeyToken=null" ),
Ref = ( new Uri( source, "mod2.xap" )).AbsoluteUri };
catalog.AddModule( mod2 );
}
// per Jeremy Likeness - did not help.
Application.Current.RootVisual = new Grid();
return ( catalog );
}
later request for the module to be loaded is made:
mModuleManager.LoadModule( ModuleNames.mod2 );
and wait for a response to an event published during the initialization of that loaded module.
The module appears to never be loaded, and when the application is running under the debugger there will be a message box that states that the web server returned a 'not found' error. I can take the requesting url for the module and enter it into Firefox and download the module with no problem.
I have not been able to find any reference to this actually being workable, but it seems as though it should. The most I have found on the subject is a blog entry by Jeremy Likeness, which covers loading modules in MEF, but applying his knowledge here did not help.
The server is localhost (I have heard it mentioned that this might cause problems). The server has a clientaccesspolicy.xml file - although I don't expect that is needed.
I am using the client stack and register it during app construction:
WebRequest.RegisterPrefix( Current.Host.Source.GetComponents( UriComponents.SchemeAndServer, UriFormat.UriEscaped ), WebRequestCreator.ClientHttp );
Followup questions:
Can all of the xaps be installed to the client desktop in some manner - or only the main application xap? specify them in appmanifest.xml somehow??
Is it worth it make this work if only the application.xap is installed and the rest of the xaps must be downloaded anyway?
Once I worked on a similar scenario. The trick is having the modules stored in isolated storage and use a module loader that reads from isolated storage when working offline.
This is because otherwise, you can't get download the modules that are in a different .xap file than the Shell.
Thanks,
Damian
It is possible to hook custom module loaders into Prism if you're willing to tweak the Prism source and build it yourself. I was actually able to get this to work pretty easily - in our app, I look on disk first for the module, and if it's not found, I fall back to loading it from the server via a third-party commercial HTTP stack that supports client certificates.
To do this, download the Prism source code, and locate the Microsoft.Practices.Composite.Modularity.XapModuleTypeLoader class. This class uses another Prism class, Microsoft.Practices.Composite.Modularity.FileDownloader, to download the .xap content; but it instantiates it directly, giving you no chance to inject your own or whatever.
So - in XapModuleTypeLoader, I added a static property to set the type of the downloader:
public static Type DownloaderType { get; set; }
Then I modified the CreateDownloader() method to use the type specified above in preference to the default one:
protected virtual IFileDownloader CreateDownloader() {
if (_downloader == null) {
if (DownloaderType == null) {
_downloader = new FileDownloader();
} else {
_downloader = (IFileDownloader)Activator.CreateInstance(DownloaderType);
}
}
return _downloader;
}
When my app starts up, I set the property to my own downloader type:
XapModuleTypeLoader.DownloaderType = typeof(LocalFileDownloader);
Voila - now Prism calls your code to load its modules.
I can send you my LocalFileDownloader class as well as the class it falls back to to load the .xap from the web if you're interested... I suspect though that if you look at Prism's FileDownloader class you'll see that it's simple enough.
With regard to your other questions, the clientaccesspolicy.xml file is probably not needed if the URL the app is installed under is the same one you're talking to, or if you're in elevated trust.
The .xaps can definitely be pre-installed on the client, but it's a bit of work. What we did was write a launcher app that is a standalone .NET 2.0 desktop app. It downloads the main .xap plus certain modules* (checking for updates and downloading only as needed), then uninstalls/reinstalls the app if necessary, then launches the app. The last two are done via sllauncher.exe, which is installed as part of Silverlight. Here's a good intro to that: http://timheuer.com/blog/archive/2010/03/25/using-sllauncher-for-silent-install-silverlight-application.aspx.
Assuming you're running under elevated trust, it should also be possible to pre-fetch the module .xaps from within the SL client, but before they're actually requested due to user action. You'd just need to put them in a folder under My Documents somewhere, and then use the custom module loading approach described above to pull them from there.
*In our case, our main .xap is 2/3 of the application. The rest of our .xaps are small, so we download them on-the-fly, with the exception of some .xaps we created as containers for third-party components. We don't expect to update those very often, so we pre-install them.

Alternatives to using WebClient.BaseAddress to get base url in Silverlight

In a Silverlight application I sometimes need to connect to the web site where the application is hosted. To avoid hard coding the web site in my Silverlight application I use code like this:
WebClient webClient = new WebClient();
Uri baseUri = new Uri(webClient.BaseAddress);
UriBuilder uriBuilder = new UriBuilder(baseUri.Scheme, baseUri.Host, baseUri.Port);
// Continue building the URL ...
It feels very clunky to create a WebClient instance just to get access to the URL of the XAP file. Are there any alternatives?
Application.Current.Host.Source retrieves the URI of the XAP.
I use,
Uri baseUri = new Uri(Application.Current.Host.Source, "/");
// Example results:
// http://www.example.com:42/
// or
// https://www.example.com/
No string parsing needed!
You can also use this method to create full Urls, for example,
Uri logoImageUri = new Uri(Application.Current.Host.Source, "/images/logo.jpg");
// Example result:
// http://www.example.com/images/logo.jpg
This will build the root url in ASP.NET. You would then need to pass in baseUrl via Silverlight's InitParams and add "ClientBin\silverlight.xap".
// assemble the root web site path
var baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd ('/') + '/';
In my case, I am not working in the main folder. I am working in h||p://localhost:1234/subfolder. That is no problem while working in Visual Studio IDE. But when moving to the server it fails. The following lines
Application.Current.Host.Source
can be replaced through a public function with result like this:
Public Sub AppPathWeb()
Res = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.LastIndexOf("/") + 1)
Return New Uri(Res)
End Sub
As Result, I can catch my files like this
MyImage = New Uri(AppPathWeb, "HelloWorld.jpg")
And the result is, that on server the url goes to h||p://mydomain.com/mysubfolder/HelloWorld.jpg"
Good luck
goldengel.ch

Resources