Vanity URLs with Playframework - java

I would like to create Vanity URLs for web application built ontop of Playframework.
I have tried to add a parameter for index method in main page Application, this will work if i call http://localhost:9000/?vanity_url and not for http://localhost:9000/vanity_url.
How would i achieve this? I would like users to create their own urls for the solution has to be dynamic, just like on Facebook

In the route you would put
GET /{<[a-z]+>fanPage}/? Page.showFanPage
With this one you could obtain:
http://localhost:9000/facebook
I also recommend using the slugify method provided by play framework, usually you would both check if the slug exists in the database, and look up for slug to grab the data (title of the fanpage/who's the owner of the fan page/how many views/etc etc)
In the tables:
Name: fanpage
pageID integer or bigint
title varchar
slug varchar unique
owner integer
content text
In code:
public static void showFanPage(String fanPage) {
// use models to look for the slug
// grab the data
// do what you need to do
}
I'm going on examples here on how to create the urls since I don't know what app you are building:
GET /post/slug/? Page.createPage
POST /post/slug/? Page.processPage
public static void createPage() {
// remember to create the template
render();
}
public static void processPage(String slugName) {
// slugName refers to the input field
// check if the slug exists and throw error if it does
// process
}
(note this is just an example, I don't know what kind of application you are building)
I hope this helps, let me know if this is what you meant

Related

Firestore - Use update method for document but with Object (Java)

I want to update a document with a User object that I have, but I do not want the document to be created if it does not exist, and therefore I cannot use "DocumentReference.set" with "SetOptions.Merge()" (to my understanding).
However, according to this post (Difference between set with {merge: true} and update), "update" is actually the command I need. My problem is, it doesn't seem like update accepts a Java object.
I do not want to check whether or not the document exists myself, as this will result in an unnecessary read.
Is there any way around this?
Here is my code (I have removed success and failure listeners for simplicity):
public void saveUser(User user)
{
CollectionReference collection = db.collection("users");
String id = user.getId();
if (id.equals(""))
{
collection.add(user);
}
else
{
// I need to ensure that the ID variable for my user corresponds
// with an existing ID, as I do not want a new ID to be generated by
// my Java code (all IDs should be generated by Firestore auto-ID)
collection.document(ID).set(user);
}
}
It sounds like you:
Want to update an existing document
Are unsure if it already exists
Are unwilling to read the document to see if it exists
If this is the case, simply call update() and let it fail if the document doesn't exist. It won't crash your app. Simply attach an error listener to the task it returns, and decide what you want to do if it fails.
However you will need to construct a Map of fields and values to update using the source object you have. There are no workarounds for that.

automated testing webdrive webpage translation

I am trying to write an app using automated testing with webdriver in Java (I am really new to this), I can already log in and crawl the data I need from a website, the problem is that the page is in chinese and I am trying to display it in English in my app. I have found information about using right click but only on a WebElement, is there anyway I can right click on the page and translate to English or any other method to achieve this?
Thanks
Personally I would continue to retrieve the information in Chinese.
Store each type for example an id as a String. Then use an external library such as the Google Cloud Translate and then pass that id as the following:
public static void translateText(id, String sourceLang, String targetLang, PrintStream out)
{
Translate trans = createTranslateService();
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption targLang = TranslateOption.sourceLanguage(targetLang);
TranslateOption model = TranslateOption.model("nmt");
Translation translation = translate.translate(id, srcLang, targLang, model);
translation.getTranslatedText());
// Then you can save this into a new variable and pass it onto your website as you need to.
}

Populating a table dynamically with the java web-service returns

I'm trying to implement a basic web application from the values that we are getting from web-service, it will include two datatables, each of them need to be populated in server-side.
For example, web-service have a structure like this ( Let's say these are books)
Firstly i am getting the string GUID value for the objects that i want to get an information, after that i am sending a request with the parameter of this GUIDs to service to get information XML for these book objects that includes name, page and author of them.
But as an important information, my servlet needs to get these values dynamically as soon as the page of datatable is changed, if this datatable will include 30 book ( i will get the 30 guid firstly so i can clarify that ) after that, send one request for 10 of them to show them on first page of datatable, if user clicked on page two, server behind needs to send request for the other group of ten and returns me result to show on the table.
I tried to implement the structure below :
http://www.codeproject.com/Articles/359750/jQuery-DataTables-in-Java-Web-Applications#ServerSideProcessing
but it populates a table with the DataRepository ones with all of them, so with this point of view i can't use it dynamically as i totally requested.
The main need for this, XML return for many objects needs so long time.
So do you know any example link or tutorial such a need for this ?
Thank you for informations in advance!
#Hayra, thanks for providing the Code Project link to the JQuery-DataTable example, it is very helpful. This is something that I might implement soon.
What I understood from the example, the JQuery-DataTable keeps track for you specific parameters that will allow you to return the exact number of records. The specific parameters that you need are "iDisplayStart" and the iDisplayLength". The "iDisplayLength" is set when the user specifies 10 records per page and the iDisplayStart, will is set when the page number changes.
So look at the code in the Code Project example doGet Method, this section of the code returns only the subset of records back to your table.
JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
if(companies.size()< param.iDisplayStart + param.iDisplayLength) {
companies = companies.subList(param.iDisplayStart, companies.size());
} else {
companies = companies.subList(param.iDisplayStart, param.iDisplayStart + param.iDisplayLength);
}
try {
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("sEcho", sEcho);
jsonResponse.addProperty("iTotalRecords", iTotalRecords);
jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
for(Company c : companies){
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(c.getName()));
row.add(new JsonPrimitive(c.getAddress()));
row.add(new JsonPrimitive(c.getTown()));
data.add(row);
}
jsonResponse.add("aaData", data);
response.setContentType("application/Json");
response.getWriter().print(jsonResponse.toString());
} catch (JsonIOException e) {
e.printStackTrace();
response.setContentType("text/html");
response.getWriter().print(e.getMessage());
}
I hope this help

make function to change password

I am a beginner in web programming and I need to create function to change password like in social network. I'm doing it first time and don't know how to do it. I don't know how to create architecture. I'm using backbone.js in user side, I will create userModel (this is backbone model). In server side I'm using Java. I have one idea that: add to UserClass (this is java class) new fields which named
#JsonIgnore
String oldPassword;
#JsonIgnore
String newPassword;
JsonIgnore make field invisible on user side. I will send the fields with userModel from user side,so I check in server side. I think, the idea is not good.
If You know any ways, please, tell me about it !
EDIT
I know how to make html-form. I don't know how to send the filds to server. If I do that:
var val1 // old_pass
var val2 // new_pass
this.model.save({password: val1,new_password: val2});
then model password change to val1, it is not correct, password do not set in user side, because user side haven't model password
I don't know how your server authentication process work but maybe you can try something like this. Create a new View with a user model inside to edit the user attributes. Inside that view render a form that displays the user attributes including the passwords. On the form the user will be able to change his information. Have a button called "Save" or something like that to store the changes. When the button is clicked create a function that grabs the values from the form and using the model save method. This method makes Backbone run a PUT command back to the server. On the server you should be able to handle this request and change the password. A very simple function you can write to save the changes in the view could be something like this:
changePassword = function() {
var attributes;
attributes = {
password: $('#password').val(),
confirm_password: $('#confirm_password').val()
};
this.model.save(attributes);
};
This functions will create an attributes object filled with the password fields and then it sends it back to the server. If you want to understand a little bit more about how the save method work you should check the Backbone documentation. Hope this helps!

simple example of native app passing string to website?

Can somebody give a simple example for java code of a native app passing a string to a website?
For example: when a string has the value Hello everybody, the text Hello everybody should get pasted into the Google search field.
For the most simple use, you can try:
public static void browseURL(Activity activity, String url)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
}
catch (Exception e)
{
message(activity, "Sorry, failed to view the desired page.");
}
}
and then call:
browseURL("http://www.google.com/search?q=Hello+World")
Do you want to fill the fields and submit them? If so, just do the request with the request parameters filled, and parse the response given by the server. Look into Apache HttpClient.
You don't actually have to add text to the Google search field explicitly. You can send a URL with a query string.
Depending on the website the query string will always be different. For Google it is http://www.google.ca/search?q=something . Anything after a ? is considered a query string which any good web developer will include in a webpage. That query string takes custom commands in the form of ?command=query for command&command2=query for command 2.
Since this is tagged blackberry, I assume you want to implement a blackberry app, and you don't explicitly explain what you want to do, so you have two options,
Invoke the browser
On that page it describes how to open a browser session. So within the
browserSession.displayPage("http://http://www.google.ca/search?q=searching%20for%20something");
If you need a class for URL encoding, let me know and I'll send one your way.
Http Request to pull the html of the webpage into the code. To do that, you'll have to look at my blog this week as I'll be posting a full in code network class either tomorrow or tuesday, which I'll edit this post to contain a link to.
OR you can send me a message if you need it NOW and I can email the non-cleaned up code to you.

Categories

Resources