route query for play framework 1.2.5 - playframework-1.x

I need to add a route for the following syntax:
http://www.testsite.com/select?term=query1
In my routes file, I tried using the following
GET /select/{term}
However, the above does not catch the URL - instead it goes to another handler in the config (placed beneath the handler for select/{term}:
GET /{auth}
Any thoughts on fixing or troubleshooting this would be most welcome. thanks

?term= means that term is a parameter - not part of the route you are trying to match
so you'd write
GET /select YourControllerClass.yourMethod
....
YourControllerClass extends Controller {
public static void yourMethod(String term){
Logger.debug("term=" + term);
}
}
If your URL was http://www.testsite.com/select/query1 then the route definition you provided above should work

Related

Nestjs: middleware for all routes except /auth

I am started with nestjs recently and i want apply a middleware for all routes, except the auth route.
In the documentation it says that I can add all the controllers or paths of the routes where I want the middleware, but I find this way quite expensive
.forRoutes(
SearchController,
Other,
Other,
Other
);
So I would like to know if there is a solution, or if you managed to use regex for something like this:
.forRoutes(
{path: 'All route except /auth', method: RequestMethod.ALL}
);
Nowadays we use the function exclude.
export class YourModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(YourMiddleware)
.exclude('auth')
.forRoutes('*');
}
}
For further: https://docs.nestjs.com/middleware#excluding-routes
DOC: https://docs.nestjs.com/middleware
TITLE: Excluding routes
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*')
consumer.apply(tokenVerify).exclude('/auth').forRoutes('*')
} }
Use /auth/(.*) in case you have routes like /auth/login, /auth/register, etc. .
Exclude route referring exact route path.If you have define global prefix at main.ts,It's also need to come in to that exclude path.
Eg :
main.ts
app.setGlobalPrefix('api/v1');
module.ts
export class YourModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(YourMiddleware)
.exclude({ path:'api/v1/auth',method: RequestMethod.ALL})
.forRoutes('*');
}
}

Is it possible to have dynamic routes in pion?

I'd like to use pion 5.0.6 as a small webserver in a VS2017 c++ project. For static routes I can use
add_resource("/my/static/route", <handler>)
I would need dynamic routes as well - like "/data/:id/info
How do I do this?
For those who may need it: I found a solution to add dynamic routing to the pion webserver. It requires smart router code I found at hxoht on github, and works the way that
all routes - static and dynamic - are set with httpd->add_resource(<url>, <handler);
a 404-handler has to be set with httpd->set_not_found_handler(<handler>); and is responsible for dispatching the dynamic routes to the handlers added above.
your webserver class must derive from pion::http::server in order to find the handler by name with httpd->find_request_handler(<url>, <handler>);
in your 404-handler, you use the Match::test(<dynamic-route>) method to detect a dynamic route - like in the following code fragment:
void handle_404(http::request_ptr& req, tcp::connection_ptr& con)
{
Route target;
Match dynamic = target.set(req->get_resource());
for (auto& route : dynamic_routes) // Our list of dynamic routes
{
if (dynamic.test(route)) // Does the url match the dynamic route pattern?
{
request_handler_t h;
if (find_request_handler(route, h))
{
auto name = get_param_name(route); // e.g. /a/:b -> "b"
value = dynamic.get(name); // Save value in string or map<name, value>
h(req, con); // Call original handler with value set properly
return;
}
}
}
// If no match then return a 404.
http::response_writer_ptr w(http::response_writer::create(con, *req,
boost::bind(&tcp::connection::finish, con)));
http::response& res = w->get_response();
res.set_status_code(http::types::RESPONSE_CODE_NOT_FOUND);
res.set_status_message(http::types::RESPONSE_MESSAGE_NOT_FOUND);
w->send();
}
For using the pion webserver in a multi-threaded way, I would store the parsed value inside the request object, which would be derived from pion::http::request.
This would work for Windows and Linux :)

play framework 2.2.2 routing multiple different query params, Ember - data redirect status

I am using play 2.2.2. I have apis of the form
/users?token=abcd
/users?resetToken=abcd
I have configured my route as follows:
GET /users controllers.X.method(token: String)
GET /users controllers.Y.method(resetToken: String)
Now, when I make a call to the /users?resetToken=tokenvalue, I get the following error
For request 'GET /users?resetToken=tokenvalue' [Missing parameter: token]
I could have solved this by routing both the apis to the same method and then checking the query params inside the method. But I want to route the apis to two different methods because of the access restrictions on each of them. One of the apis can be accessed only after login while the other can be accessed with/without login.
Could you please help me resolve the issue?
(Adding more information:)
I tried the following:
GET /users controllers.A.genericMethod()
GET /usersByToken/:token controllers.X.method(token: String)
GET /usersByResetToken/:token controllers.Y.method(token: String)
In controllers.A,
public static Promise<Result> genericMethod(){
Map<String, String[]> queryParams = Context.current().request().queryString();
String[] tokens = queryParams.get("token");
String[] resetTokens = queryParams.get("resetToken");
if (tokens != null && tokens.length == 1) {
return Promise.pure((Result) redirect(controllers.routes.X.method(tokens[0])));
} else if (resetTokens != null && resetTokens.length == 1) {
return Promise.pure((Result) redirect(controllers.routes.Y.method(resetTokens[0])));
} else {
ObjectNode node = ControllerHelper.error(ResponseStatus.BAD_REQUEST,
"Required params not set!");
return Promise.pure((Result) badRequest(node));
}
}
In controllers.X
#SubjectPresent
public static Promise<Result> method(){
....
}
In controllers.Y
public static Promise<Result> method(){
....
}
This works from the play framework point of view.
But I am calling these apis from ember framework through ember-data. So, if I make a call to it from ember-data, say using
this.store.find('user', {token: "abcd"});
which forms the corresponding api url
/users?token=abcd
I get a response of "303, see other" and the required data is not returned.
You can't declare two routes with the same method and path, fortunately you don't have to, you have two solutions, first is declaring 'Optional parameters' (#see: doc) like
GET /users controllers.X.action(token ?= null, resetToken ?= null)
public static Result action(String token, String resetToken){
if (token==null) debug("No token given...");
// etc
}
Other solution is declaring rout w/out params, as you can still get arguments with DynamicForm
GET /users controllers.X.action()
public static Result action(){
DynamicForm dynamicForm = Form.form().bindFromRequest();
String token = dynamicForm.get("token");
if (token==null) debug("No token given...");
// etc
}
Second approach is better especially if your API has large amount of optional params.
Note: Written from top of my head
I understand that one api is protected, requiring a login, and another does not.
You need to have two separate routes, this is the best way.
GET /users controllers.X.method(token: String)
GET /public/users controllers.Y.method(resetToken: String)
Combining it into one route will not allow you to have separate access restrictions.
You need to break it into two routes, one with a private access and one with a public access.
This will change your apis obviously.

experiencing issue with route in play 1.2.5

I probably have misconfigured my route file for play-1.2.5 - below is the relevant route file part & the URL being used:
URL:
GET /application/autoComplete?term=mac
Route:
GET /autoComplete/{term} controllers.Application.AutoCompleteTerm
I also have the following route defined but its not getting picked up:
GET /autoComplete/ controllers.Application.AutoCompleteTerm
The route does not get hit - instead, I get a template not found exception:
play.exceptions.TemplateNotFoundException: Template not found
Any suggestions to help troubleshoot this will be quite welcome. thanks
The route:
GET /autoComplete/{term} controllers.Application.AutoCompleteTerm
...is wrong. It should be like this:
GET /autoComplete/{term} Application.autoCompleteTerm
This would correspond to the following URL:
GET http://127.0.0.1:9000/autoComplete/mac
The corresponding method in Application would look like this:
public static void autoCompleteTerm(String term) {
...
}
The URL:
GET http://127.0.0.1:9000/autoComplete?term=mac
...would need the following route:
GET /autoComplete Application.autoCompleteTerm
...and the same method as above:
public static void autoCompleteTerm(String term) {
...
}

URL routing problem in codeigniter

I am trying to route a URL using codeigniter URL routing.
I want to redirect a url like
/users/edit?email to userController/editemail
/users/edit?password to userController/editpassword
I tried using the following line in routes.php in config folder
$route["users/edit?(email|password)"] = "userController/edit$1";
This displays page not found. I am guessing that ? is being treated as a regular expression character. I tried escaping it but that didn't work either.
I don't want to set the config setting uri_protocol to PATH_INFO or QUERY_STRING, since this is just a pretty URL I want to setup, not pass anything to the action.
Can somebody help me out over here?
Regards
You should escape the ? like this, it should work. (not tested)
$route["users/edit\?(email|password)"] = "userController/edit$1";
Later edit:
This works as intended:
$route["users/edit(email|password)?"] = "userController/edit$1";
The userController looks like this
<?php
class UserController extends Controller {
function edit()
{
echo "general edit";
}
function editemail()
{
echo "edit email!";
}
function editpassword()
{
echo "edit password";
}
}
Router works like this:
if you go to http://sitename/index.php/users/editemail you see editemail() action.
if you go to http://sitename/index.php/users/editpassword you see editpassword() action.
if you go to http://sitename/index.php/users/edit you see the edit() action (the question mark makes optional the email/password field and you can do some other things in edit() action