I've create an app using the Playfrawork with Java.
And I'm using the SecureSocial plugin on it.
Inside my controller there is a method that one of the process of it is to execute the handle start reset password inside the SecureSocial.
But that method is written in Scala.
If I refer the method inside my Java source like this:
Registration.handleStartResetPassword();
Nothing happens! Same if I change to the following line:
Registration.handleStartResetPassword().apply();
Same as the following source code block:
play.api.libs.iteratee.Iteratee<byte[], SimpleResult> it = Registration.handleStartResetPassword().apply(ctx()._requestHeader());
Future<SimpleResult> future = it.run();
Option<Try<SimpleResult>> result = future.value();
SimpleResult res = result.get().get();
I removed all my code and just tried to execute the Secure Social code but nothing happens.
Here is my method:
public static Result resetPassword() {
Registration.handleStartResetPassword().apply();
return TODO;
}
My route is this one:
POST /reset controllers.ProfileController.resetPassword
Edited: Added another way that I've tried to run but just didm't got the method running.
When you call handleStartResetPassword() you get an instance of Action back, it doesn't actually do anything until you to feed it with a particular request by calling Action.apply(request) which will then actually run the logic for that controller action and return a Future<Result>.
Not entirely sure if you can bridge backwards and get a play Java async result out of that though.
Related
I had a situation where i am working in home.feature and need to call return.feature and gets the data. And internally return.feature is calling data.feature and collects all the data from it.
Source
directory1
home.feature
directory2
return.feature
data.feature
I have written * def response = call read('classpath:source/directory2/return.feature') in home.feature.
while calling this line, it is picking up the correct feature file, but while executing return.feature, it throws error while calling data.feature(javascript evaluation failed. java.io.FileNotFoundException).
Please guide what could i do in home.feature file, as i dont have access to change in directory2.
Thanks in advance
There is a this: prefix: https://github.com/intuit/karate#reading-files
So this should work when you are inside return.feature
* call read('this:data.feature')
I'm attempting to do a reverse lookup on a route I've created.
The route is defined as such in my routes file:
POST /login controllers.Web.Application.authenticate
However, when I try to do a reverse on it in a form I made, I can't seem to find it. I'm attempting to do a reverse like this:
#helper.form(action = routes.login())) {
rest of the form here...
}
However, the login appears in red in intellij and when attempting to run the program, I get the following error:
play.sbt.PlayExceptions$CompilationException: Compilation error[value login is not a member of object controllers.routes]
I've attempted recompiling the project multiple times and looking around the docs, but it's just not working... Any ideas?
So, an interesting thing happened recently and I found a way to point to the original structure properly. To do so, rather than writing:
routes.Web.Application.authenticate()
As Tyler suggested, I was supposed to write:
Web.routes.Application.authenticate()
Which is totally counter-intuitive and all, but that allows me to use the original package structure.
Edit:
As we discovered in the comments, the reverse router doesn't seem to like sub packages. You should remove the Web part of your package, and move everything up a level, then change the code to be this:
#helper.form(action = routes.Application.authenticate())) {
rest of the form here...
}
Original answer:
You need to refer to the controller function, and not the name of the route, so it should look something like this:
#helper.form(action = routes.Web.Application.authenticate())) {
rest of the form here...
}
I'm working on a Java project in the Play framework, version 2.2.1
I'm trying to implement a batch operation on my models, with a message being returned showing the status code of the query on each model instance. So that's one controller method calling another and getting the Result from it.
The thing is, I need to get that status code from that return, since it has to be in the message.
There doesn't seem to be a way to get this status code.
I tried this: check that a result is an ok playframework
But it won't compile, Java says the cast cannot be done.
So, how do I get that number out of the Result?
Once again, this is a Java project and all my Results are play.mvc.Result, not play.mvc.api.Result.
EDIT:
Let's say you have a route that does a DELETE:
DELETE /url/delete/:id/ myapp.mycontroller.delete(id: Long)
The controller method would look like:
public static Result delete(Long id) {
Mymodel m = MyModel.get(id);
if (m.hasDependencies) {
throw new CustomException(statusCode, message);
}
m.delete();
return ok();
}
So either it passes an ok() or an exception with a custom statuscode.
The batch functionality I am talking about would be another method in the same controller. This would receive a batch of IDs via a form element. The goal would then be to call the required function on all these ids.
The whole point is using the existing controller methods, as they contain other checks and fail-safes that need to be taken in to account for certain models.
So I have another method that receives this list of Ids and then calls the proper function in a loop, in a try/catch block.
The idea is that I want to find out what the statuscode of the Result is. That's it. Nothing more. I want the statuscode of the result.
play.mvc.Result is just a wrapper for play.mvc.api.Result so after all you will need to work with it.
public static Result batch() {
try {
Result result = delete(1L);
play.api.mvc.SimpleResult res = Await.result(result.getWrappedResult(), Duration.Inf());
int status = res.header().status();
//handle status
} catch (Exception e) {
//handle exception
}
}
Await and Duration are Scala classes
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
I'm looking for possibility to add anchor to url returned in controller:
public static Result open(String id) {
// here I want to add acnhor like #foo to the produced url
return redirect(routes.MyPage.show(id));
}
I found that it was possible in play 1 using addRef method, but I couldn't find any replacement of the method in play 2.
Of course I can use concatenation like:
public static Result open(String id) {
// here I want to add acnhor like #foo to the produced url
return redirect(routes.MyPage.show(id).url + "#foo");
}
But it seems ugly.
Thank you for any help! Have a good day!
Before trying to answer that question.
I should recommend you change whatever behavior you're currently setting.
Because, an URL fragment's purpose is client side only. Such fragment is never sent to the server, so that it's cumbersome to do the opposite.
However, here is the embryo of a (quite?) elegant solution that you could follow.
What I'll try to do is to leave the browser deal with the fragment, in order to keep potential behaviors (f.i. go to ID or even deal with history...).
To do so, you could add an implicit parameter to your main template which will define the fragment that the URL should have:
#(title: String)(content: Html)(urlFragment:Option[UrlFragment] = None)
As you can see I wrapped the parameter in an Option and default'ed it to None (in order to avoid AMAP pollution).
Also, it simply wraps a String but you could use String alone -- using a dedicated type will enforce the semantic. Here is the definition:
case class UrlFragment(hash:String)
Very simple.
Now here is how to tell the browser to deal with it. Right before the end of the head element, and the start of body, just add the following:
#urlFragment.map { f =>
<script>
$(function() {
//after everything is ready, so that other mechanism will be able to use the change hash event...
document.location.hash = "#Html(#f.hash)";
});
</script>
}
As you can see, using map (that is when the urlFragment is not None) we add a script block that will set the hash available in urlFragment.
This might be a start, however... Think about another solution for the whole scenario.
As of Play 2.4, it's possible to use Call.withFragment().
routes.Application.index.withFragment("some-id").absoluteURL(request)
This was added by PR #4152.
I'm writing an Eclipse plugin with a custom launch configuration, i.e. a launch() method inside a subclass of LaunchConfigurationDelegate. This method essentially just calls Runtime.exec(), but when I write to System.out from within launch() it goes to the console of the Eclipse instance which is debugging the plugin, rather than to the console of the plugin instance itself. I've analysed the ILaunchConfiguration and ILaunch arguments to the method but cannot find anywhere that they specify any output/error streams I can write to.
As is recommended in the tutorials, I have 2 separate plugins running together; one which handles the UI stuff (LaunchConfigurationTab,LaunchConfigurationTabGroup,LaunchShortcut,) and the other which contains the LaunchConfigurationDelegate itself.
I created a console in my UI plugin using this code, and I can write to it fine from within the UI code. But I cannot figure out how to direct output generated in my non-UI plugin to the console created in my UI plugin.
I've read this post and this one, but they do not specify how to "get ahold" of the output which is generated within the launch() method in the first place.
Any pointers would be really welcome, I am stuck!
Well I finally managed to get something working as follows:
In my LaunchConfigurationDelegate I introduced the following static method:
public static void setConsole(PrintStream ps) {
System.setOut(ps);
System.setErr(ps);
}
Then when creating my console in my UI plugin's PerspectiveFactory I call it as follows:
private void createConsole() {
console = new MessageConsole("My Console", null);
console.activate();
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
MessageConsoleStream stream = console.newMessageStream();
MyLaunchConfigurationDelegate.setConsole(new PrintStream(stream));
}
This works, except everytime I close down Eclipse and restart it the console disappears. However when I reset my perspective, the console appears again. So obviously I need that code to be called on startup, not in the PerspectiveFactory itself.
Hope this helps someone.. and if anybody has some input for this last problem (or about my approach in general) please do comment!