Call js webscript from Java webscript in Alfresco - java

I have a problem. I need execute js webscript from Java webscript. I know, how do it:
req.getRuntime().getContainer().getRegistry().getWebScript("com/home/testJs/testJs.get").execute(req, res)
, but how to construct the new WebScriptRequest object? I need do it for rewrite request path. It's a really problem for me.
Thank you.

In general, you should use WebScriptRequestURLImpl. Without any other detail, it's hard go any deeper.
That said, it's in general a bad idea to go through yet another HTTP call to yourself to fix your problem, it's basically an indication of poor modularization or lack of code reuse.
I'd rather move the piece of code that's common in both the JS and Java web scripts flows to be an Action, which you could invoke from both places without having to repackage the input parameters, or worse send them via HTTP.

Of course skuro is correct saying it is a bad idea going through the HTTP layer twice.
But in fact, executing both, a script controller and a java method is supported by Alfresco right out of the box - without ugly hacks, and without passing the entire HTTP layer twice.
You may do this:
Put your script code in the corresponding .js file.
Make sure your Java class is a subclass of DeclarativeWebScript, override executeImpl and put your custom logic there.
Sure, you can still argue that having two controllers is bad style. :)

Related

About basicLTI java implementation (basiclti-util-java), ¿documentation?

I've as objective to create a simple "Hello World" as an LTI app. To do so, I would like to use JSP, so I look for a Java implementation of LTI to make it.
After looking for it, I found this github repository: https://github.com/IMSGlobal/basiclti-util-java
Where they've created some utils implementating LTI 1.0. The only problem about it, it's that I can't found any example nor documentation about how to use it.
The only way I think I can understand a bit about how to use it, is looking the Test classes they've created, but this only helps me in the methods way, and leaves me still without knowing how must I create the "app" (servlet? JSP? any special methods? What should be used first?
I'm a bit lost with all this. Can anyone give my a piece of advice or a way to start?
Thank you in advance
(PS: tried to tag it as LTI, but I've no reputation enough..)
I'm one of the maintainers of that project, and I'm glad you're interested in LTI!
There are many different aspects of LTI, but the main thing people are referencing when they talk about LTI is the oauth launch. You can read more about that here.
Here's a sample java lti app I wrote with the spring framework which uses basiclti-util-java, which might get you started.

Porting Java classes to the web whilst maintaining all functionality

So I have written a few Java classes that take input via the main method as passed arguments and then print to the screen. Is there any way I can 'port' this to something I can include in the HTML of a web document? There are multiple classes that work together so I would need some way of including all of these and forcing one specific file to run. For example, if I have Class1.java and Class2.java I would want to run Class1.java but somehow the second class must be included as the first relies on it.
I understand that this is probably not as simple as I'm making it, for example there may be complications with changing stdout to use the application screen etc, but anything that can get me started would be helpful.
Thanks!
The closest fit to what you are asking for is something called CGI, which is a technology that was popular 15 years ago, but is rarely used now. The next best fit would be to wrap your classes in something that can run in a java servlet container.
Simply put, instead of taking arguments in on the main method, you would accept them as URL parameters. Instead of printing to stdout, you would print to a special stream that sends the content over a socket to a web browser. There are many fine tutorials on the web for creating servlets. I like this one: http://helloworldprograms.blogspot.com/2010/08/servlet-hello-world.html

Calling Java class's method from javascript in a freemarker template

I have a freemarker template with javascript in it, and I'm using spring mvc to pass in a java object "emailer".
Somehow, in this freemarker template, I want to call the emailer object's "sendEmail(params, ..)" method from javascript within the freemarker template. I know how to call java methods from freemarker (the regular way - for example: How do I call java methods on an object from a FreeMarker template?) , but I don't know how to do it from within the javascript.
Is this even possible? If so, how?
And if it's not, what are some alternatives?
The overall goal was to get a value from a dropdown list (using javascript), and then using that value in the java method that gets called when a button is pressed.
Thanks in advance! If more info is needed, I'd be happy to provide it.
It's not the only possible combination of how these technologies would work together, but in the usual flow of things, what you're looking forward wouldn't be possible:
A Java call (mediated with Spring) renders the FreeMarker, allowing calls back into Java code as it processes.
This rendered string (which might happen to contain some Javascript) is shipped over HTTP to the client browser. At this point the Java execution has completed.
In the browser, the generated text is parsed, and the Javascript is run. Here there is no direct knowledge of the server, and there is no way to call back that completed thread of control.
So unless you're doing something more unusual, no you can't do what you're suggesting.
There are tools to allow client-side Javascript to call back to the server and to interact with Java there.; so you can rig something up. But you will not simply call the Java directly without more work.

How to reuse/extend closed java webapp (.ear)?

I've got webapp project written in java (.ear).
It is running under glassfish-3 server.
I really have to extend its functonality. but there is no way to change even a line of it.
Is it possible to wrap that webapp or anything else (it has no API, only html for user)?
I know it's very generic, but i cannot provide a code or other useful things.
A similar situation to mine would be (expect my case needs much SECURITY because of sensitive data)...
Let's have a blog application (this would be the java closed webapp), which allows everything You expect from an blog app, BUT one instance handles one user.
You would like to allow new user to create blog (new instances of blog app?). The instances should be isolated each other.. but it's simple to achieve. Different instance different db and so on..
I do not expect solution (because of my generic description) but direction where to go.
I'm java novice, but i can read and reason so.. :)
.ear is just a zipped file, see doc http://en.wikipedia.org/wiki/EAR_(file_format). You can always unzip it and get hold of all files. JSP and config-files can be altered but not the java-classes (unless you decompile them).
This not a complete solution but hopefully a hint in some direction...

how to give dynamic nature to my jsp pages?

Question is pretty lame I know. And the answer is pretty simple - AJAX (I think, I'm not sure).
But before proceeding further, I would like to know if there's a better way.
My problem:
I have a web application, that takes in one statement from the user, and then in response to it,sends back another statement. The whole process goes on like this.
So how can I give a dynamic nature to my JSP then? If I keep reloading the JSP, my old responses will get deleted. So, that's not how it works.
I could use RequestDispatcher 's include() method. I find that as one solution. So, I can keep including the new pages onto my old JSPs. But again, I think this will have a limitation. Haven't tested it yet though.
Is there a better way around?
Thanks for your help. :-)
I'm in love with AJAX consuming Restful Webservices so that is what I would suggest. In addition, if I understand you correctly, I don't think include() will help you in this scenario unless you manage to keep streamming your responses in 1 open connection which isn't really ideal in most Web page scenarios.

Categories