XMLHttpRequest is an alternative for HTTP calls from GWT client side and allows the control over all aspects of requests/responses. But how to use it?
javadoc address: http://www.gwtproject.org/javadoc/latest/com/google/gwt/xhr/client/class-use/XMLHttpRequest.html
You haven't mentioned what GWT version you use, so I assume the latest one. It means 2.8.2 or newer.
Elemental2 is the way to go
As it is mentioned in comments above, Elemental2 is the right way. I will explain it a bit.
If you think about future-proof implementation (being aware of GWT3/J2CL new approach), please do not use legacy GWT stuff. It means please use elemental2.dom.XMLHttpRequest instead of com.google.gwt.xhr.client.XMLHttpRequest (the one mentioned by you). Please do not use gwt-user dependency if possible, as it will be deprecated (if it is not already).
The Elemental2 is an opensource project available here: https://github.com/google/elemental2. It is kind of a base library for the "new GWT". For easier migration of existing GWT2.x projects to GWT3.x, a part of the "old" gwt-user is currently being ported to the new approach using JsInterop technique and mentioned Elemental2. So definitely Elemental2 is the way to go.
Elemental2 and JsInterop in general
The specification is not yet that rich if about the new JsInterop approach, but at the moment you will find some introduction at least: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html
Examples
Please find an example for XMLHttpRequest in this article:
http://www.g-widgets.com/2016/09/09/gwt-http-requests-alternatives/
If you look for examples, also a good way is to search this on the Github site this way: https://github.com/search?q=elemental2.dom.XMLHttpRequest&type=Code.
(To use Github search you need to be logged in, in other case you will see "Whoa there! You have triggered an abuse blah blah..." )
One of the results will lead you to the very interesting project (you have the preview of the future GWT now):
https://github.com/gwtproject/gwt-http. It is
a future-proof port of the legacy com.google.gwt.http.HTTP GWT module. It will help to migrate GWT2.x projects to GWT3.x.
When you look to the test package, you will find some examples: https://github.com/gwtproject/gwt-http/tree/master/src/test/java/org/gwtproject/http/client .
So this is finally the answer to your question: "how to use it?" :-)
An additional examples source for XMLHttpRequest (using Elemental2) from Gist: https://gist.github.com/search?utf8=%E2%9C%93&q=elemental2.dom.XMLHttpRequest. This is probably even better for start, as they are short and clear.
What Elemental2 is?
The Elemental2 gives you a type checked access to native browser's API. So if you are familiar with browser's API, you should be able to implement your stuff, even based on some native JavaScript example. Please think about the new GWT like about type safe JavaScript (in addition very performant and well optimized). With JsInterop you create bindings, so it is something similar to bindings for TypeScript. So in fact you have a possibility to deal directly with browser's API, without anything GWT specific.
Libraries? More examples...?
Dealing with XMLHttpRequest is a bit low level.
You have also a possibility to use the library. One of Github search results will lead you to this repository: https://github.com/ibaca/autorest-streaming-example which is an example for interesting REST library: https://github.com/intendia-oss/autorest. A modern and reactive one, works with Observables, RxJava and so on.
This library uses JsInterop and is also migrated to Elemental2 what makes it GWT3/J2CL ready, please see the change: https://github.com/intendia-oss/autorest/commit/58516802cd42134544e6e3787207b5431fae94b5 .
With the Github search query I provided you, now you are able to find even more code examples for XMLHttpRequest. So please just have a look and find the best one for your needs.
An alternative approach would be to use a framework, for instance Errai from RedHat: http://erraiframework.org/. It helps you to deal with many problems at a different abstraction level.
I think now you have some references to study.
On the other hand it's 2018, so why not the Fetch API?
When think about the modern web application, I would rather think about the Fetch API instead of XMLHttpRequest. All modern browsers now implement the fetch() function natively. Isn't it the best way for solving your issue then?
The fetch() is a Promise-based mechanism that allows you to make network requests similar to XMLHttpRequest. Promises and Fetch are handled by Elemental2. Then you can use it from your Java code more or less in similar way like in Mozilla's examples.
Read more about the Fetch API here:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
https://developers.google.com/web/updates/2015/03/introduction-to-fetch
https://codepen.io/aderaaij/post/fetching-data-with-fetch
https://fetch.spec.whatwg.org/
What more, this is nothing new as you see.
If about the older browsers a polyfill will emulate the missing function: https://github.com/github/fetch.
If about examples, I don't see that much on Github:
https://github.com/search?utf8=%E2%9C%93&q=elemental2.dom.DomGlobal+fetch&type=Code, but at least something.
The Fetch API seems to be the most current solution to the problem.
Please find a very simple fetch() example using Elemental2.
The imports section:
import static elemental2.dom.DomGlobal.fetch;
import static elemental2.dom.DomGlobal.console;
import elemental2.dom.Response;
Then use in your code:
fetch("https://randomuser.me/api/?gender=female&results=1")
.then(Response::json)
.then(data -> {
console.log(Global.JSON.stringify(data));
return null;
}).
catch_(error -> {
console.log(error);
return null;
});
As a result you should be able to see something like this:
{"results":[{"gender":"female","name":{"title":"mrs","first":"caroline","last":"coleman"},"location":{"street":"3703 new road","city":"swansea","state":"leicestershire","postcode":"ZH67 0YS","coordinates":{"latitude":"14.7870","longitude":"-107.8990"},"timezone":{"offset":"-6:00","description":"Central Time (US & Canada), Mexico City"}},"email":"caroline.coleman#example.com","login":{"uuid":"25357d90-cce4-4fe6-a3db-8ab77c0272ba","username":"smallpeacock582","password":"citizen","salt":"VX3s05Ah","md5":"84649cce1db8c6f2cbe33098221aa570","sha1":"005abf7d2ca0ff5b1a0bfd6dcee6d4860ef6e75d","sha256":"caadff0a16e27b0d9893aea483aedc7cf7c4707096c33a58acf44336bb2b54be"},"dob":{"date":"1978-03-14T15:47:16Z","age":40},"registered":{"date":"2013-08-10T19:09:41Z","age":5},"phone":"015396 74385","cell":"0726-723-103","id":{"name":"NINO","value":"JA 32 24 22 P"},"picture":{"large":"https://randomuser.me/api/portraits/women/45.jpg","medium":"https://randomuser.me/api/portraits/med/women/45.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/45.jpg"},"nat":"GB"}],"info":{"seed":"98f4f4a344470fbd","results":1,"page":1,"version":"1.2"}}
You can further convert the result to Java object using a technique called JsInterop DTOs. If you are interested, please find some information here: https://stackoverflow.com/a/50565283/5394086 .
Not recommended approach
If you sadly prefer to use the old GWT, so <= 2.7, then I think you can search for some examples on Github using similar search query, but for this legacy com.google.gwt.xhr.client.XMLHttpRequest. I this case I would also suggest you to not do stuff so low level, but use a library like https://github.com/reinert/requestor (which is unfortunately discontinued and development has stopped on GWT 2.7, but for this GWT version it is probably the best choice). But again please do not go this way and use GWT >= 2.8.2 with Elemental2/JsInterop approach instead.
Related
When it comes to write REST doc documentations, is it good to write all possible values of each request parameter? I think it's better even though there are many other ways to get the information like source codes. But I am not sure. Is there correct answer, or it's just a matter of team policy?
I think the answer to that question is depends on the project that you are working on. If this API will be used by many other developers and open to everyone, I think it would be better to put a few example code snippets by using a different language and explain each parameter like what is used for and the limits that should be considered. I think you don't need to put all possible combinations after simply explain each parameter itself. You may look at popular projects API documentation to get an idea; for example :
Github API Documentation
Twitter API Documentation
Dropbox API Documentation
These documentations are giving every detail that a developer needs. If this API is only for your small team and you don't need such detailed explanations you may use Swagger like tools to easily document your API.
Swagger
I completely agree with the above, just wanted to add another handy thing. You might want to have Postman Collection with all the test case scenarios. This will come handy when you will want to share it with the team or QA. There is a practice to keep the Postman Collections in source control and to update them as needed.
I'm trying to find an API so that I can avoid building something (in Java) that can convert MediaWiki syntax to HTML myself. This seems to be a sufficiently general problem that someone else should have solved it, but thus far I've been digging around on the internet with no luck.
My first pick was Mylyn Wikitext, which seems to work somewhat, but I'm using Maven, and the Maven repository version is still 0.94, and it seems to only support a subset of the full Mediawiki syntax -- in particular, it is missing the ability to replace {{quote|sample quote text}} with blockquotes.
I also tried Bliki v3.0.19, and it also seems to be missing blockquotes.
Based on a quick survey, many of the other available options look like they are either no longer maintained, still in alpha (e.g. Sweble), or like they are projects with a single contributor, so they may be less likely to be bug-free.
So, does anyone know of a Java library for parsing MediaWiki and generating HTML that meets the following criteria? (My intent is to be specific and concrete about what I'm looking for, so it's more a binary choice than a matter of opinion.)
Still maintained -- to be more specific, it's been updated within the last two years
Actually supports Mediawiki {{quote}} syntax in addition to more standard markup like bold/italics/lists/links
More than one contributor to source code and at least hundreds of users (as evidence that most bugs and performance issues are likely to have been worked out)
Latest versions are available from the central Maven repository.
Extra credit would go to an API where the parsing language is easily extensible, and where elements of the language can also be removed.
Thanks!
https://www.mediawiki.org/wiki/Alternative_parsers lists such attempts, but none can fulfill your requirements, i.e. being able to parse site-specific markup like an individual template.
The parsing API used via Java clients is likely to be your best chance. If for some reason you cannot rely on HTTP requests, please clarify your use case in the question.
I need to parse a String from HTML to Textile.
I've been looking at Textile4J, Textile-J, JTextile, PLextile.
But so far, none of them provide the functionality I'm looking for.
They do provide the reverse functionality (Textile to HTML).
Worst case scenario, I can use another programming language, but I have not really looked into that.
For now, I don't believe the functionality I want is available in any java Textile library.
I'll try and update this post if and when that changes.
Based on the libraries mentioned above, I have created my own (limited) functionality.
There are also several solutions available in python / ruby.
since version 10 guava offers com.google.common.eventbus.EventBus - a generic pub-sub facility. It is not marked as GWT-compatible in the source. I wonder whether it's possible to plug some custom code pre-processing into gwt compiler to map this (partially annotation/reflection-based) API into javascript.
AFAIK there're some other custom extensions added into GWT java-to-js compiler, like com.google.gwt.i18n.client.ConstantsWithLookup and quite likely some others. I'd seen some experimental compiler extensions to add partial support for reflection/annotations, but not sure they're required in case we handle a specific set of annotations and simple class->methods->annotation listing is required (not a complete reflection API).
Maybe someone knows whether GWT-enabling this component is planned by guava dev team?
Thanks,
Anton
This is probably more appropriate for guava-discuss than for StackOverflow, but Guava team member here: no, we're not planning on it.
simply already working (GWT server+client compatible) with this one: http://www.gwtproject.org/javadoc/latest/com/google/gwt/event/shared/EventBus.html
and here (Tutorial): http://www.gwtproject.org/articles/mvp-architecture.html#events
(answered also here: https://stackoverflow.com/a/28082823/1915920)
There are few open source projects/APIs/libraries that we use in our project (Spring, Struts, iBatis etc.) and I want to understand their design and how they work internally.
What is the best way to understand these projects? Note that I am already using these libraries in my project. And I know the input-output interaction/configurations for these libraries. What I don't understand is how these APIs/libraries work internally.
The problems I face is:
Finding the entry class of the library. Is there any way I can know the entry class for the library - something which is kicking the whole API?
Tools/Plugins to use in Eclipse to get an overview of the design of the library. Going through each and every class of the library, can be a very daunting task. Is there any tool you would like to recommend which can generate the class diagrams of the API in Eclipse.
Thanks in advance!!
UPDATE: I need some inputs on eclipse plugins which can help me in getting an overview/class diagram of the library
I always use the same strategy for this: I never try to "understand" the code base as a whole, and I usually try to follow the request flow. I read enough of the documentation to determine what is necessary to use the application, and I read that code (Keep all source code loaded in your IDE).
For example, in struts you'll be installing a servlet filter in web.xml. Start reading the filter and follow the path a single request takes through your stack.
Likewise for spring, there are two main entry points, the filter and "getBean", both of which are mentioned real early in the documentation. Read those two.
For both of these cases you'll find one or two classes that represent the "core" of the framework real quickly. Read those really well and let actual use cases & needs drive your further exploration.
Approaching "understanding" of an open source library (or any other code base for that matter) by trying to find all the pieces is usually not a very good way of approaching these things, it will usually just lead nowhere because a lot of these things contain too much code. When following the request flow I find making diagrams can also be quite distracting, it tends to draw attention/focus away from understanding (and since my understanding increases rapidly most of them are out-of-date even before they reach the printer).
Nice question!!!, what I've done, specially in the case of Spring, apart from consulting the Documentation and their API's is to attach the sources of the project to my project on Eclipse, that way I'm able to navigate through the source code, not just the API. Its been quite helpful specially in the case of the Spring-Security project, there were some concepts that I just couldn't understand until I inspected the source code.
That's one of the advantages of using Open Source libraries.
Regards.
Tools like Structure101 (http://www.headwaysoftware.com/products/structure101/index.php), and Lattix (http://www.lattix.com/) let you analyze code and produce architecture diagrams / dependency matrices.
This is not exactly class diagram - the main focus is on layering. So the entry point is usually the topmost layer.
But then again, as I specified above, you will notice that some libs are just a mess, and these tools will not be helpful enough.
See the S101 online demo: http://www.structure101.com/java/
This for example is the Sonar project architecture: http://www.structure101.com/java/tracker/sonar/1.11.1/arch.html
Your best bet for those three would be to consult the official documentation (make sure you are looking at the version you are using) or to get a book on the technology.
Most APIs don't have a class with a main method; they're running in the webserver called by the server itself. Unless they're running as their own server, they won't have a main method.