Permanent REST web service url with many and long parameters - java

Since I found it very difficult to explain my problem in the heading I'm going to explain it a little further:
I want to /I'm writing a JAX-RS web service (Jersey/Servlet3.0) and the corresponding JS library for a geographic use case. The input of the web service are two lists (source & target points) of geographic points (latitude, longitude) and each point having a list of parameters. Since there is basically no limit on the number of points I don't know how to combine the URL length limit and the unlimitedness of the parameter list.
Here are the restrictions again:
Easy to share URL (so POST probably wont quite cut it?) for social media sharing and of course easy debugging
An example configuration can be seen here please note that there can be nested sets of parameters (point 1 has parameters of it's own)
Needs to able to be integrated in external website (with bookmarkable url)
Not all of the parameters are mandatory, what is the best way to deal with defaults/missing values?
What I thought of so far is:
create a boatload of parameters
jsonify the configuration and send it to server via url parameter
But I don't really like these options. Am I missing something?
Sorry for this rather vague question.
Daniel

Ok to your points
for easy sharing why not just implement a tinyURl or bit.ly style sharing system - obviously you can't have both an easy to pass-in url (a URL that makes it easy to give the server detailed information) that is easy to share (human friendly and short) - but you could very easily save the results (or inputs and calculate each time) to a database and link that to a tinyURl.
as an aside POST will be the only way to handle this due to the amount of data.
Just pass as JSON - easy to nest paramaeters that way
Don't quite get this part - for an external site to use this they could post the data and you return the answers - or using point 1 method of 'tinyurl / bit.ly style system it could call this in an iframe?
You would deal with missing parameters / defaults at server side - create a function for each parameter - if parameter is expected then throw error - if parameter has a default include this in your function and if parameter is not included then don't run the function.
Hope that makes sense?

Related

How to get the parameter from the URL after '/'

https://localhost:8080/invoice_creation/1/
Here /invoice_creation is my resource location and /1 is my invoice number which denotes my database. I want to parse the value 1 alone in my java servlet when I pass this URL in my postman. I've used request.getParamerter() method..but it doesn't help me.. please help me to parse the value 1 in my java servlet page
getParameter is for the x and y in for example: http://localhost:8080/invoice_creation/1?x=5&y=hello.
A webserver listens on port 8080, receives the 'location' that the visitor wishes to visit (such as /invoice_creation/1?x=6&y=hello), and needs to then route this by finding the 'handler' that is supposed to deal with this. It then calls that handler.
Java has a ton of web frameworks, and most cooked up their own way of routing.
The 'original' java web servers uses the servlets API. It's a crappy API that you shouldn't be using; it's a pain to use. But, given that you tagged this question with servlets, you are, evidently, using it. I suggest you look around; perhaps Jersey/JaxRS is nicer, especially if you're attempting to set up a REST API.
At any rate, if you insist on using the servlet API, you've somehow set up routing such that /invoice_creation/_anything goes here_ ends up at some servlet and now you wish the 'anything goes here' part. Exactly how to do that depends on a few factors, but usually its one of these:
req.getPathInfo(). Depending on your Web Server / servlet container you're using, this will either return /1 or returns /invoice_creation/1. You'll have to 'extract' the 1 from this, using either regular expressions (java.util.regexp.Pattern and friends), or basic string manipulation such as str.substring).
req.getTranslatedPath().
req.getRequestURI() - this definitely returns the whole thing (including /invoice_creation/).

String Translation from API call

I have a found a free API to get some data I need for my app. The thing is the values that I get are in English.
I wanted to know if there is some way to translate this strings in my language before showing them to the users.
You can translate text but it will require another API call. Not only that, but you will have to create an appropriate request object and parse a response object from your chosen API.
You have various API providers to choose from, the top Google hits being:
Google: https://cloud.google.com/translate/
Yandex: https://tech.yandex.com/translate/
But beware! Machine translation is patchy at best. The likelihood of getting odd sounding or outright wrong results, particularly for anything other than simple text, is very high.

Trouble with understanding routing priority

I am building some sort of discussion board, where you can ask questions and write answers. Now I ran into a problem with routing and seem to be understanding them wrong.
I have a homepage called index, from there you can click a button "Ask question" or another button "Write Answer". Each button leads to another webpage (askQuestion.scala.html or writeAnswer.scala.html) where you can write a question or answer. After hitting submit you come back to the index-page, where the new question or answer is put into the view. In the background, the question / answer gets put into a DB.
My routes.conf:
# Home page
GET / controllers.Application.index()
#Questions
GET /FrageStellen controllers.Application.askQuestion()
POST / controllers.Application.sendQuestion()
#Answers
GET /AntwortGeben controllers.Application.writeAnswer()
POST / controllers.Application.sendAnswer()
But when I enter an answer, it gets written into the question-table in the DB! This is due to the fact that the question-route is higher up in the routing table and therefore it seems to get selected first.
This is against my understanding of routes, I thought a route consists of a 3-tuple: HTTP-Method, Request Path, Call definition ... and as the call definitions differ (sendQuestion() and sendAnswer()), the correct one should be used?
Why is this not the case?
I've read about routing in the documentation of the play framework and googled, but still dont understand.
I am also aware how to fix my problem, but I want to understand what's happening here.
Fix 1:
Change this
POST / controllers.Application.sendAnswer()
to this
POST /Antwort controllers.Application.sendAnswer()
Disadvantage: This is not the homepage (index) anymore. Seems weird.
Fix 2:
Write a combined method for sending stuff from the form to the index.
Disadvantage: I want to keep the methods separate, in order to maintain a better structure in my project. Also I would have to see if a question gets asked or an answer written and based on that omit one of the fields or use another one (answer has an extra questionID-field to link an answer to a question).
So, why is this happening in the routes and whats the best way to deal with it?
In Play each route is combination of route method (GET/POST) and path (determined by static parts and params types) so if you have two routes with the same type and path only first will be resolvable, other will be ignored even if you'll use other name of param (but same param type).
In this case the bar(String bar) method won't be resolved ever:
GET /foo/:foo controllers.Application.foo(foo: String)
GET /foo/:bar controllers.Application.bar(bar: String)
The safest way to make sure that you won't mishmash the routes is using unique paths always in sets:
GET / controllers.Application.index()
GET /FrageStellen controllers.Application.askQuestion()
POST /FrageStellen controllers.Application.sendQuestion()
GET /AntwortGeben controllers.Application.writeAnswer()
POST /AntwortGeben controllers.Application.sendAnswer()
Finally if you want to go to the root page after post you can return a redirect() instead of ok()
You should post to an action with different request path and then redirect to index. It is a recommended approach in web development.
http://en.wikipedia.org/wiki/Post/Redirect/Get

Generating Dynamic URLs

I have a list of users across various companies who are using one of the functionality that our website provides. Whenever they contact our business group , we need to send a url via email to the requestor in order for them to upload some data. All these external users do not have any dedicated account. However we do not want a static link to be provided to them as this can be accessed by anyone over the internet. We want dynamic links to be generated. Is this something that is usually done? Is there an industry accepted way of doing this? Should we ensure that the dynamic link expires after a certain amount of time - if so , are there any design options?
Thanks a lot!
Usually, parameters to urls and not the actual urls are what's dynamic. Basically you generate params that are stored somewhere, typically on the database, and send email with the url and the parameter(s). This url is valid for only a limited period of time and possibly only for one request.
Answers to questions:
yes, this is something that is quite commonly used in, for example, unsubscribing from a mailing list or validating an account with a working email address
I'm not aware of any single way that is "industry accepted", there are many ways of doing it, but the idea is not that complex - you just need to decide on a suitable token format
normally you should ensure that the link expires after a certain amount of time. Depending on the use case that can be some days, a week or something else. In practice, you'd remove or disable the generated parameters in your database. However, if this data is something that might be needed for extended periods of time, you might want to think up a functionality so that it can be retrieved later on.
You may have a static URL taking a token as parameter. Eg. http://www.mycompany.com/exchange/<UUID> or http://www.mycompany.com/exchange?token=<UUID>.
The UUID could have a validity in a time range or be limited to a single use (one access or one upload).
Other variant is to use exists cookies on that site in web browser (of course, if they are).
But there are some drawbacks in this solution:
User can open link in different machine, different browser. User can clean all cookies or they can expire after it was visited your site last time when user try to go on granted URL. In these cases user won't access your page.

expand Tiny url in java

I want to write a code in java that takes a url identify whether it is tiny url or not. if yes then it will identify the url is malicious or not. if not malicious print the url...
Please can any body help me....
You can use HttpClient to detect whether the URL is redirected to another location. After that it's a simple case of:
if (!isMalicious(redirectTargetURL))
{
System.out.println(redirectTargetURL);
}
The isMalicious(...) implementation is left as an excercise for the reader.
If you trust google to implement isMalicious(...) then they have done so with their Safe Browsing API.
So 2 main things you want:
Identify if it's a tinyurl
Identify if the URL is malicious
The answer to part 1 is easy. Just check if the URL belongs to the domain 'tinyurl.com'. Should be straightforward to either test raw URL string, or the host part returned by the getHost() method of a java.net.URL object.
Part 2 is more difficult to code up from scratch...
First you will need your code to figure out where the tinyurl redirects to.
The next bit really depends on how you want to define 'malicious'. Detecting deceptive URLs will require a bit of work (e.g. finding the difference between something like www.stackoverflow.com and www.stack0verf10w.com), or comparing the target URL with a malicous URL list (there's sites that publish them). There's also checking for multiple redirects, popups, and the list of criteria could go on and on.

Categories

Resources