How can I send Java object between client and server on Android and iOS? - gluon-mobile

I have a client and a server both written in Java and sharing Java classes that should be sent between each other. I'm not sure which libraries I can use for this on mobile because I don't know what Dalvik supports, what RoboVM supports etc. Not sure what Gluon Mobile can do for me in this case.
Specifically I have a file that looks like this:
class Data {
IntegerProperty int1 = new SimpleIntegerProperty(4);
ObjectProperty<Person> person = new SimpleObjectProperty();
ObservableList<Contact> contacts = FXCollections.observableArrayList();
// other properties
// also add the getters for the properties and the getters and setters for the values
}
Person and Contact are similar to the above - they contain mostly data properties and some methods for adding and removing from internal (private) lists etc. Basically they are like beans or POJOs only with properties wrappers. This is the data that needs to be sent between the server and the client but only the wrapped values are important - not the bindings. This leads me to the point about serialization: javaFX properties are not serializeable so it was suggested here to make such class as the above externalizeable and write and read the wrapped values.
Ultimately I don't care if i need to do this custom stuff (though it's a lot of work) or if there's a way around it. I need a method on the server like Data receiveDatafor(...) that the client can call, the server fetches the Data data and returns it. The client and server each have their own unrelated bindings to the Data object.
Currently we use RMI internally for desktop. I read that RMI isn't supported and it might not be that great of an option anyway but it does allow to just send java objects really easily. JavaEE has websockets which can transfer the binary form of the objects but it's JavaEE so I guess not supported. I'm not against JSONing and sending as Text but it seems more work than to just serialize - could be wrong. The communication method should support encryption for example when sending passwords. What are my options?

In terms of client-server communication you can have a look at Gluon Connect and Gluon CloudLink.
Gluon Connect
An open source library:
Gluon Connect is a client-side library that simplifies binding your data from any source and format to your JavaFX UI controls. It works by retrieving data from a data source and converting that data from a specific format into JavaFX observable lists and observable objects that can be used directly in JavaFX UI controls.
It is also part of the Charm dependencies, so you have it already included when you create a new Gluon project.
See the documentation on how to use to create a FileProvider or a RestProvider, and also the GluonConnectRestProvider sample.
As the doc already mentions: with a RestClient you can "convert" a REST endpoint into an ObservableList:
// create a RestClient to the specific URL
RestClient restClient = RestClient.create()
.requestMethod(RestClient.Method.GET)
.host("https://api.stackexchange.com")
.path("/2.2/errors");
// retrieve a list from the DataProvider
GluonObservableList<Error> errors = DataProvider
.retrieveList(restClient.buildListDataReader(Error.class));
// create a JavaFX ListView and populate it with the retrieved list
ListView<Error> lvErrors = new ListView<>(errors);
The Notes sample uses Gluon Connect for local storage of Notes and Settings.
Note that these samples make uses of JavaFX POJOs (i.e. Error, Note and Settings use properties).
Gluon CloudLink
Gluon CloudLink enables enterprise and mobile developers to easily connect their different services and applications together, enabling bi-directional communications between mobile apps, enterprise infrastructure, and cloud systems.
The data is stored in the cloud, and you (as administrator) can access to it through a Dashboard.
See documentation about it here.
Have a look at the PWS-GluonCloudLink-Whiteboard sample: a full demo of a back-end application (webapp-mobile) running on the cloud (Pivotal Web Services) and a mobile client (mobile app).
In the client side, once you get a valid GluonClient, you can retrieve an observable list of items:
public GluonObservableList<Item> retrieveItems() {
GluonObservableList<Item> items = DataProvider.retrieveList(gluonClient.createListDataReader("items", Item.class));
return items;
}
As you can see, in the client you don't deal with REST endpoints, json serializing... Everything is just JavaFX observables. The connection with the backend is set in CloudLink with the Dashboard application, defining a REST Connector.

Related

Java SOAP client very slow

I am building a client for a web service. I didn't want to the client downloading the wsdl everytime and got this answer.
But evaluating the source files of WSServiceDelegate,
URL url = wsdl.getSystemId()==null ? null : JAXWSUtils.getEncodedURL(wsdl.getSystemId());
WSDLModel model = parseWSDL(url, wsdl, serviceClass);
service = model.getService(this.serviceName);
if (service == null)
throw new WebServiceException(
ClientMessages.INVALID_SERVICE_NAME(this.serviceName,
buildNameList(model.getServices().keySet())));
// fill in statically known ports
for (WSDLPort port : service.getPorts())
ports.put(port.getName(), new PortInfo(this, port));
I see that it still parses the wsdl to get the service. How can I get around that. I provided the endpoint url using the context.
I need the client to be as fast and as small as possible, adding a huge wsdl in there is worst than downloading the wsdl.
For the operations you are interested in, you can build your own SOAP messages based on the wsdl's Request/Response messages and the xsd. You can use Jaxb tools to convert from XSD to Java classes. You then need to make post calls using Http Clients (like Spring RestTemplate) to post the POST body, soap based, to the endpoint address. This will make your calls faster but you have to code more in order to benefit.

Is there a way to use Swagger just for validation without using the whole framework?

Suppose I have an existing Java service implementing a JSON HTTP API, and I want to add a Swagger schema and automatically validate requests and responses against it without retooling the service to use the Swagger framework / code generation. Is there anything providing a Java API that I can tie into and pass info about the requests / responses to validate?
(Just using a JSON schema validator would mean manually implementing a lot of the additional features in Swagger.)
I don't think there's anything ready to do this alone, but you can easily do this by the following:
Grab the SchemaValidator from the Swagger Inflector project. You can use this to validate inbound and outbound payloads
Assign a schema portion to your request/response definitions. That means you'll need to assign a specific section of the JSON schema to your operations
Create a filter for your API to grab the payloads and use the schema
That will let you easily see if the payloads match the expected structure.
Of course, this is all done for you automatically with Inflector but there should be enough of the raw components to help you do this inside your own implementation

Export breeze entities in the server side to json

I am looking for a way to export breeze entities on the server side to a json string which a breezejs manager can import from the client side. I looked all over the breeze APIs (both public and internal source code) but I couldn't find an obvious way of achieving this. There is a possibility of getting the desired results by using BreezeSharp (a .NET breeze client) on the server side but I would like to see if this is achievable with using the breeze server APIs only.
First you need to determine the shape of the bundle to be imported, i.e. something that manager.importEntities will understand. I don't think the format is documented, but you can reverse-engineer it by using:
var exported = manager.exportEntities(['Customer', 'Product'], {asString:true, includeMetadata:false});
Then pretty-print the value of exported to see the data format. See EntityManager.exportEntities for more info.
Once you have that, you can re-create it on the server. In C#, you can build it up using Dictionary and List objects, then serialize it using Json.NET.
An alternative approach would be to have your webhook just tell the client to initiate a query to retrieve the data from the server.

Serving a webpage with Redstone

I am developing a web application with Dart using redstone and polymer
Because Dart allows for server and client side development, I wonder what a good pattern for a web application is (specifically to Dart)
Option 1:
Have a server, say, /bin/server.dart
1.1. get a request there and respond with json
#app.Route("/user/:id", methods: const [app.GET])
getUser(int id) { ... }
have a client, i.e. web/user.html and web/user.dart
2.1 in user.dart make a request to server
2.2 receive json and form a proper user.html
Option 2:
Have a server /bin/server
1.1 get a request there and respond with an html page, similar to
#app.Route("/")
helloWorld() => "Hello, World!";
If in the first case I more or less know (and understand) how to make things work, while i find it really frustrating that I do not take advantage of Dart's server-client code-sharing: I need to encode to and decode back json to get the same data. Is there a way to avoid it?
The second option is much less clear for me: how would I serve a web page in this way? How would I make Polymer do its work?
Answers on the questions in the text and a general explanation of a darty way to develop web apps are very much appreciated.
You can see a Redstone + Polymer application example here: https://github.com/luizmineo/io_2014_contacts_demo
Basically, it works as Option 1: The client and server communicates through a service API, and the data is encoded as JSON. Although, Redstone uses the shelf_static package to serve the client code to the browser as well.
If you prefer, it's also possible to use a server side template engine, such as mustache, to build html pages in the server, although, I think it would be really difficult to integrate that with Polymer.
And finally, you always have to encode the data someway when transferring data between client and server, but this doesn't means they can't share code. They can use the same domain classes, for example. Check out the sample application linked above for more details.
I don't think the option 2 is possible. Polymer depends on dart:html which is not allowed on server side.

Implementing django-socketio iOS client

I want to make a chat app using Django in iOS. The server-side socket communication method that I've chosen is django-socketio because it integrates well with django. So my problem is selecting a way to implement the client side on iOS. All the django-socketio client examples are in javascript e.g-
To subscribe to a channel client-side in JavaScript use the socket.subscribe method:
var socket = new io.Socket();
socket.connect();
socket.on('connect', function()
{
socket.subscribe('my channel');
});
I want to know how to implement such a code in my iOS client, as in how to implement the "subscribe()" channel function from it, and how to implement interactivity from iOS to the various other events defined by the django-socketio server like:
#on_connect
def my_message_handler(request, socket, context):
...
and #on_message, #on_subscribe, etc.
I'm currently trying to use NSStream and CFStream as shown here, but it's proving difficult for me to convert it in a way to make it talk with django-socketio server.
(Note: for all those who saw the last "here" link, yea I did go the way of using twisted first instead of django-socketio, but it doesn't have any well-defined concrete method of integration with django (Yes, I tried searching it everywhere). Maybe it will be my next question here.
https://github.com/pkyeck/socket.IO-objc
PS: Now it doesn't support socketio protocol 1.0, neither django-socketio.

Resources