URL mapping in Grails - grails

Suppose this is my URL:
https://stackoverflow.com/questions/ask/question1.xml
Currently in my UrlMappings.groovy I have `
"/$Question/$ask/$question1"(controller:"somecontroller")` to handle the request.
If my URL changes to:
https://stackoverflow.com/questions/ask/askAgain/question1.xml
my URL mapping cannot handle it.
Is there a way I can get ask/askAgain to be referenced by $ask in my urlmapping.groovy?

This is what I did to get around this.
I changed my URLMapping to
"/$Question/$ask**"(controller:"somecontroller")
If my URL is :
`http://stackoverflow.com/questions/ask/askAgain/question1.xml`
So now, $ask** = ask/askAgain/question1.xml
Then, If I want to remove question1.xml from that URL. I can use Regex to get rid of it.

You will have to provide 2 mappings:
"/$Question/$ask/$question1"(controller: "somecontroller")
"/$Question/$ask/$askAgain/$question1"(controller: "somecontroller")

Related

How to get the redirect url from deeplink in Swift

How to get the URL from the given redirect deeplink string?
For example, if I have a deeplink string as
myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
I would like to get the return result as
https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
I tried several methods from Apple documentation
Accessing the Parts of a URL,
the closest one was to combine path and query like below screenshot
Does anyone have a better way to do this?
I get your question, I think you want to get only the url starting from https://, please correct me.
If so I think I could remove the deeplink url scheme myapp:// and the host open-browser/ like
let url = "myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME"
let deeplinkBase = "myapp://open-browser/"
let newUrl = url.absoluteString.replacingOccurrences(of: deeplinkBase, with: "")
Then you could use the newUrl as you like since we got the expected result now
https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
My example for the deeplinkBase string is hardcoded, but I guess you could use enums or any handlers for it later.

How to get absolute url from the relative url in java

I am having a page like mypage.com/a/b/somePage.html, and one of the anchors href attribute is something like "a/b/anotherPage.html".
When i try to get the absolute url from by creating a new url object with page url and href value, i am getting the absolute url as mypage.com/a/b/a/b/anotherPage.html.
This is causing problem for me, but some how the browsers are handling this properly.
Any out of box things available to solve this problem. ( I can always add an exception in the code to handle this differently, but i don't want to do this.)
Using java.net.URL
URL baseUrl = new URL("http:www.google.com/someFolder/");
URL url = new URL( baseURL , "../test.html");

Yii url route with parameters

At the moment i have a glossar controller with an actionAnzeige()-method.
For this action i need GET-paramter named item.
Now i could use this url: www.xy.de/glossar/anzeigen?item=programming
But i want to use this: www.xy.de/glossar/programming
I've added this route to the rules:
'glossar/<item:\d+>'=>'glossar/anzeigen',
and now i can generate the url i want to use:
<?php echo Yii::app()->createUrl('glossar/anzeigen', array('item' => $glossarItem->Url)); ?>
But if i visit the created url, i get a 404 error.
You can use this, which accepts characters or numbers:
'glossar/<item:.+>'=>'glossar/anzeigen',
You have to use w+ instead of d+ since item takes letters instead of digits
'glossar/<item:\w+>'=>'glossar/anzeigen',

How to get the request URI in Grails?

I've got a simple Grails app with the following RESTful uri...
http://localhost:8080/kennis-api/funds/test/700
The mapping in my URIMappings is
"/funds/test/$fcode" (controller: "fundCache"){
action = [GET: "show"]
}
In my controller, I need to extract the request URI, in this case "/funds/test/700", but invoking request.uri or request.getRequestUri does not work. I tried using request.requestURL, but that gives me
http://localhost:8080/kennis-api/grails/fundCache/show.dispatch
Is there a special member or function from which to get the request uri?
Its Simple, You need the Original address, that is same as the one where your response will be forwarded, its simply stored in the Request, and can be retrieved by:
.
String originalURI = request.forwardURI
//Do this where request is available, ex: Controllers :)
// Everywhere else you can use RequestContextHolder
.
.
Hope that helps
Regards
Kushal

Facebook OAuth: custom callback_uri parameters

I'd like to have a dynamic redirect URL for my Facebook OAuth2 integration. For example, if my redirect URL is this in my Facebook app:
http://www.mysite.com/oauth_callback?foo=bar
I'd like the redirect URL for a specific request be something like this, so that on the server, I have some context about how to process the auth code:
http://www.mysite.com/oauth_callback?foo=bar&user=6234
My redirect gets invoked after the authorization dialog is submitted, and I get back an auth code, but when I try to get my access token, I'm getting an OAuthException error back from Facebook. My request looks like this (line breaks added for clarity):
https://graph.facebook.com/oauth/access_token
?client_id=MY_CLIENT_ID
&redirect_uri=http%3A%2F%2Fwww.mysite.com%2Foauth_callback%3Ffoo%3Dbar%26user%3D6234
&client_secret=MY_SECRET
&code=RECEIVED_CODE
All of my parameters are URL-encoded, and the code looks valid, so my only guess is that the problem parameter is my redirect_uri. I've tried setting redirect_uri to all of the following, to no avail:
The actual URL of the request to my site
The URL of the request to my site, minus the code parameter
The URL specified in my Facebook application's configuration
Are custom redirect URI parameters supported? If so, am I specifying them correctly? If not, will I be forced to set a cookie, or is there some better pattern for supplying context to my web site?
I figured out the answer; rather than adding additional parameters to the redirect URL, you can add a state parameter to the request to https://www.facebook.com/dialog/oauth:
https://www.facebook.com/dialog/oauth
?client_id=MY_CLIENT_ID
&scope=MY_SCOPE
&redirect_uri=http%3A%2F%2Fwww.mysite.com%2Foauth_callback%3Ffoo%3Dbar
&state=6234
That state parameter is then passed to the callback URL.
If, for any reason, you can't use the option that Jacob suggested as it's my case, you can urlencode your redirect_uri parameter before passing it and it will work, even with a complete querystring like foo=bar&morefoo=morebar in it.
I was trying to implement a Facebook login workflow against API v2.9 following this tutorial. I tried the solutions described above. Manuel's answer is sort of correct, but what I observed is url encoding is not needed. Plus, you can only pass one parameter. Only the first query parameter will be considered, the rest will be ignored. Here is an example,
Request a code via https://www.facebook.com/v2.9/dialog/oauth?client_id={app-id}&redirect_uri=http://{url}/login-redirect?myExtraParameter={some-value}
You'd get a callback for your url. It will look like http://{url}/login-redirect?code={code-from-facebook}&myExtraParameter={value-passed-in-step-1}. Note that facebook would make a callback with myExtraParameter. You can extract the value for myExtraParameter from callback url.
Then you can request access token with https://graph.facebook.com/v2.9/oauth/access_token?client_id={app-id}&client_secret={app-secret}&code={code-from-facebook}&redirect_uri=http://{url}/login-redirect?myExtraParameter={value-extracted-in-step-2}
Additional parameter passed in step 1 after the first query parameter will be ignored. Also make sure to not include any invalid characters in your query parameter (see this for more information).
You're best off specifying a unique callback for each oAuth provider, /oauth/facebook, /oauth/twitter etc.
If you really want the same file to respond to all oAuth requests, either include it in the individual files or setup a path that will call the same file on your server using .htaccess redirects or something similar: /oauth/* > oauth_callback.ext
You should set your custom state parameter using the login helper as such:
use Facebook\Facebook;
use Illuminate\Support\Str;
$fb = new Facebook([
'app_id' => env('FB_APP_ID'),
'app_secret' => env('FB_APP_SECRET'),
'default_graph_version' => env('FB_APP_VER'),
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = [
'public_profile',
'user_link',
'email',
'read_insights',
'pages_show_list',
'instagram_basic',
'instagram_manage_insights',
'manage_pages'
];
$random = Str::random(20);
$OAuth2Client = $fb->getOAuth2Client();
$redirectLoginHelper = $fb->getRedirectLoginHelper();
$persistentDataHandler = $redirectLoginHelper->getPersistentDataHandler();
$persistentDataHandler->set('state', $random);
$loginUrl = $OAuth2Client->getAuthorizationUrl(
url('/') . '/auth/facebook',
$random,
$permissions
);
Hey if you are using official facebook php skd then you can set custom state param like this
$helper = $fb->getRedirectLoginHelper();
$helper->getPersistentDataHandler()->set('state',"any_data");
$url = $helper->getLoginUrl($callback_url, $fb_permissions_array);

Resources