How to call GQLQuery - java

Is it possible to call GQL query inside a gwt application in the server's implementation something like:
SELECT * FROM User order by score desc
To be able to retrieve a sorted list ???I tested the query in databaseviewer and it returned the sorted list I was wondering if I can sort the entities automatically after updating the entities so I can include the query in the application.

In your server part you can execute whatever you want. Of course that is true also for any server service provided in the GWT kit: RPC and RF.
But GWT is almost client side, hence the only way to call a remote procedure in your server, is using Ajax. So create a service method in your server side using RPC, RF, AutoBeans or any other JSON, XML, etc apprach. Then call that method from the client using Ajax (RPC, RF, RequestBuilder, etc).
The data transfered must be in a format understandable in client side, if you use RPC, RF or AutoBeans, that is almost out-of-the-box, for any other mechanism you are the responsible for the data-binding.
Said that, as you can see GWT uses the same architecture than any other JavaScript application. Except that in the case you use Java in server side you have some utilities for transfering data and calling remote methods.

Related

How to write a function to handle custom query in JPA repository in spring boot java?

Basically I am hosting a database on server side using spring boot. And I wanted to write custom query from client side which is developed in angular and call a function from server side for giving me the needed results.
The function for server side will look like this:
List<rows> func(String customQuery){
//fetch rows from table using this custom query and return those rows
//which I can use in client side.
}
Below are examples of customQuery which I need to send from client side:
select * from table;
select * from table where id>10;
select * from table where id>20 and id<30;
So far I searched in Internet I could not find any solution. Please help.
Sending SQL directly from the client would be a complete security failure, because a malicious user could easily figure out how it works and send a delete from table to kill your entire app.
Much worse, they might even be able to run create user ... to give themselves complete access to your entire database, get sensitive information, install malware, etc.
Instead you'll want to create a REST service in your application with methods such as
GET /table
GET /table?minId=10
GET /table?minId=20&maxId=30
Return as application/json or a similar data format and only return the information your angular app really needs.
Angular would then be responsible for selectively updating the display with your data.
Edit:
This is a guide I have found for creating a basic web app based on Spring Boot and Angular. Might be a good starting point for you:
https://www.baeldung.com/spring-boot-angular-web
The best way is to send only the parameters (minID and maxID in your case) from the client-side and then build the query dynamically on server-side using Spring JPA Specifications.

Is it possible to call Pubsub's subscription pull directly in javascript instead of calling java method to fetch the data?

I've been working in pubsub where I can successfully pull the data from a particular topic under a particular project in java. If I've to show these data in html, first I've to call the servlet method, then the servlet will call pubsub api to get the data, then I've to include that data in the response.
Since it involves an additional one layer (java) to access the data, is it possible to directly fecth the data in javascript call by skipping the java call..? Is there any api's available in google pubsub to serve for that purpose?
The api can be used in javascript (with ajax) since its just https calls,
https://cloud.google.com/pubsub/reference/rest/
however its a bad idea because you would need to expose your server account access token and the client could then abuse it.

JSP backend for ExtJS

This question is about a proper architecture using JSP as a controller for ExtJS.
I am fairly new to server side development but I am pretty familiar with ExtJS 4 and getting better with Java and SQL daily.
I am trying to create a JSP controller to write the data from stores in ExtJS. I have MSSQL database and Tomcat running on the server.
I successfully created a JSP (sqlData.jsp) that reads from the database and returns JSON data. I pass a query name to this JSP, it then looks up what the query is from a "query" table (columns: [query_id],[query_name],[query]). It then runs the query and returns the data in a JSON format - this is working fine to get data into ExtJS from a database.
To use this backend set-up I usually configure the store like this:
var store = Ext.create('Ext.data.Store', {
model: 'aModel',
proxy: {
type: 'ajax',
url: 'sqlData.jsp?queryName=aQueryName',
reader: 'json'
},
autoLoad: true
});
Somehow, I need this sqlData.jsp to also handle a store.save() call from the ExtJS framework. Which means the JSP needs to receive a POST request and then do an update based on a pile of JSON data (ExtJS sends read request as GET and write methods like store.save() are POST).
My plan was to add something in the Java to recognize whether it is a POST or GET request. Then, if it is a POST request, I would send it to a different Java method in the JSP to parse the JSON and write it to the database.
Of course I would have to change my "query" table to have another column for update/insert statements linked to the same queryName (i.e.: [query_id],[query_name],[select_query],[update_query]).
Does this backend implementation make any sense?
Anyone else use JSP and ExtJS to achieve this smoother?
I noticed that there is an api config option I can set in my proxy to specify different URLs for the different operations (READ, WRITE, DELETE, etc). Should I make a separate JSP and direct all write requests using this config instead?
Would it be wiser to add a writer: 'json' config on the proxy so that it parses before POSTING? I figured I would have to parse it in the JSP either way so I didn't think I should.
Any pointers will be much appreciated.
since your backend is Java, I would really recommend using Spring 3.0 MVC to code your backend.
JSP is not a good option for the stuff you are doing because:
the functions you write in there are not unit testable.
the functions you write in there are not reusable.
the code you write in JSP are functional in nature, not object oriented, you can't inject the services you need into your JSP.
Spring 3.0 MVC has really good synergy with ExtJS 4, namely the RESTful URL's and content negotiation.
This example shows how to integrate the two things together. http://java.dzone.com/articles/extjs-4-file-upload-spring-mvc
I would skip jsp and just go directly to servlets. i.e. implement the logic in the servlets for both returning json, and handling things like POST, PUT, etc.....
jsps are meant to be views. But in your case, your view layer is its own application running in the client. You only need the data.
The Servlet API puts allows you to handle requests, get the http method, and stream data to the response.
My advice is go with an MVC server side framework. My favorite is Grails which lets you work with JSON objects directly for both input and output. Its also super simple to with grails to read and write data to the database.

JSONP or other alternatives?

I a deveveloping a web site that comunicates with a custom made webserver by me in Java. The web site is made in PHP/JavaScript/JQuery running on Apache and i made a simple second webserver in Java to support some designed features by me, and this server runs under another port XXXXX. The problem is, i want to make requests in jQuery to second server the domain is diferent, the page runs on domain and the $.getJSON function calls domain:XXXXX wich is not allowed. I thought user $.getJSONP but im concerning concerned issues. The connections between two points is authed (i was think by passing a token beyond the callback generated by jquery). The two poins are supported by. Is there safe in this case use $.getJSONP or exists other alternatives thinking in browsers support(IE7+ and FF3+).
Sorry for my english :)
Best regards lealoureiro
JSONP should work for your needs, however your other option would be to have a proxy service on your second server that would make the request server side. Your client-side code could then access all the data natively via json instead of jsonp.

GWT interface to Solr index

I have a solr index on a remote server and need to create a search page interface. I am using GWT to code the pages and XML-HTTP to query the index and receive the response. The problem is the same-site origin security policy. It won't let JavaScript retrieve the remote XML data. Is there a workaround for this, without using JSON preferably.
A similar problem: Make GWT interact with ASP.NET Web Service
The answers there should also apply here.
Depending on the type of data that you want to send (that is, how "public" they are), JSONP might not be the best option - it's not the safest method of transport (pure JSON is safer, but to overcome SOP you need the padding).
If you have a Java server on the.. server side, I'd go with GWT <-> servlet (acting as proxy, on the same domain as your main app) <-> web service (any domain) - the safest and cleanest code, afaict.

Categories

Resources