leaningtech / cheerpj-meta

Run Java 8 applications, libraries, applets, Java Web Start, and Oracle Forms on the web without legacy plugins.
https://labs.leaningtech.com/cheerpj
445 stars 21 forks source link

I can't get REST to work #133

Closed jannegus closed 2 years ago

jannegus commented 2 years ago

I have used CheerpJ to convert a JavaEE Swing application. And I was astonished how fast I could convert it and make it run in the CheerpJ environment. But it originally uses RMI to communicate with the server, So I implemented a REST communication with the server. This worked very well in java. But I couldn't get it to work with CheerPJ.

So I built a much smaller application to just test the REST part. And I have spent some weeks now trying different approaches, The application just tries to get some text data (Json) with a GET operation. This is what the java application looks like: image

The right pane shows the java class path and the left shows the Json received from the server.

This is the CheerpJ result: image

The right pane shows the CheerpJ class path and the left the exception returned.

A NullPointerException occurs. I have been trying to debug the javascript code. But that is not so easy. But I have got the impression that it tries to load a class javax.xml.transform.stream.StreamSource. But I'm not sure that it has something to do with the error.

This is the REST call:

**  client = ClientBuilder.newClient();
    basetarget = client.target(baseaddress);

        jsonPersonList = basetarget.
            path(path).
            request(mediatype).
            get(String.class);**

where mediatype is text/plain

I found the _cheerpjInit({javaProperties:["java.protocol.handler.pkgs=com.leaningtech.handlers"]});_option, but that didn't help. Since I don't understand what it does i tried to:

So what does this handler do?

This is my build script:

FOR %%f IN (aopalliance-repackaged-2.6.1.jar hk2-api-2.6.1.jar hk2-locator-2.6.1.jar hk2-utils-2.6.1.jar jakarta.annotation-api-1.3.5.jar jakarta.inject-2.6.1.jar jakarta.ws.rs-api-2.1.6.jar javassist-3.25.0-GA.jar jersey-client-2.35.jar jersey-common-2.35.jar jersey-hk2-2.35.jar osgi-resource-locator-1.0.3.jar WebServiceTest.jar) DO py /progfiles/cheerpj_2.2/cheerpjfy.py --work-dir WorkDir --deps aopalliance-repackaged-2.6.1.jar:hk2-api-2.6.1.jar:hk2-locator-2.6.1.jar:hk2-utils-2.6.1.jar:jakarta.annotation-api-1.3.5.jar:jakarta.inject-2.6.1.jar:jakarta.ws.rs-api-2.1.6.jar:javassist-3.25.0-GA.jar:jersey-client-2.35.jar:jersey-common-2.35.jar:jersey-hk2-2.35.jar:osgi-resource-locator-1.0.3.jar:WebServiceTest.jar %%f

It compiles with no errors.

This is the main part of the html file (loaded from the Weblogic server): _

_

I'm running CheerpJ on a virtual WIndows Server (could that be a problem?)

I do clear the browser cache each time, so that is not the problem. I use Microsoft Edge, but have also tried with Chrome and Firefox.

Except for trying to build the environment on my PC I do not know what else to do. Any suggestions?

One thing I of cource could try is to use another set (or another version) of the REST jars. But it would be very helpful if you could publish a set of REST jars (names and versions) that you know work with CheerpJ in other environments. For people, like me, who are not used to working with REST programming (or Maven) it really is a challange to find a group of jars to start with.

jannegus commented 2 years ago

To answer my own question about Windows server. I have now tested on Windows 10. And the same thing happens. Except that when running the java client i get the following warning:

org.glassfish.jersey.message.internal.MessagingBinders$EnabledProvidersBinder bindToBinder VARNING: A class javax.activation.DataSource for a default provider MessageBodyWriter was not found. The provider is not available.

On windows server I use java 8 and on WIndows 10 I use java 9. After some googling I found that I should add a --add-modules switch to the java command to get rid of the warning:

java --add-modules java.xml.bind ...

java.xml.bind is not any longer, starting with java 9, a module that is loaded by default.

Can this have something to do with my problem? Which java runtime version is CheerpJ based on?

marsicplusplus commented 2 years ago

Hi @jannegus

Which java runtime version is CheerpJ based on?

CheerpJ is currently based on Java8, hence modules are not supported, along all of the other features from java 9.

I'm running CheerpJ on a virtual Windows Server (could that be a problem?)

Most certainly not, being javascript code that run in the browser, the platform on which its running should not be a problem;

So what does this handler do?

The handler replaces the standard Java HTTP with a custom one to support non standard HTTP requests through the browser.

publish a set of REST jars (names and versions)

I'm not sure what you mean here? Are you asking for examples of working code that successfully performs a REST request after being compiled with CheerpJ?

It's also not entirely clear to me if you are trying to have a REST server through CheerpJ or just the client.

A client should just make an HTTP GET request to the server and read the results. This works and it's supported.

Could you send me a minimal example (both server and client) that reproduces the issue?

jannegus commented 2 years ago

Hi @marsicplusplus Thanks for answering so fast!

I have now prepared an example that I will send you!

Here are some answers to your follow up questions:

publish a set of REST jars (names and versions)

I'm not sure what you mean here? Are you asking for examples of working code that successfully performs a REST request after being compiled with CheerpJ?

I mean statements like this:

"We have seen that the following jars and versions works for REST calls (GET, PUT and POST ... ) : aopalliance-repackaged-2.6.1.jar hk2-api-2.6.1.jar hk2-locator-2.6.1.jar hk2-utils-2.6.1.jar jakarta.annotation-api-1.3.5.jar ... " That could help a lot, especially when things go wrong. Because in that case you don't know if you should test with other sets of jars or if you should concentrate on other things.

It's also not entirely clear to me if you are trying to have a REST server through CheerpJ or just the client.

The server side is all Java

jannegus commented 2 years ago

Hi Lorenzo!

I’m glad over your fast response! I needed some time to clean up the environment before coming back to you. I send you a very small example with both client and server. You also get a description of the different parts and how to build and run them.

Yesterday I tried to send you a version containing all the .jar.js files but this morning I realized that it had been rejected due to its size. So you will have to recreate everything with the .bat files included.

I answered some of your questions on Github.

Janne

@.***

marsicplusplus commented 2 years ago

I mean statements like this:

"We have seen that the following jars and versions works for REST calls (GET, PUT and POST ... ) : aopalliance-repackaged-2.6.1.jar hk2-api-2.6.1.jar hk2-locator-2.6.1.jar hk2-utils-2.6.1.jar jakarta.annotation-api-1.3.5.jar ... " That could help a lot, especially when things go wrong. Because in that case you don't know if you should test with other sets of jars or if you should concentrate on other things.

Unfortunately we cannot know which of all of the jars available on the internet work for REST calls. Furthermore, a REST call is just an HTTP request, requesting a jar or a png or a txt should be exactly the same thing.

I will be waiting for your example to debug your issue.

Best, Lorenzo

jannegus commented 2 years ago

Hi again Lorenzo!

I sent you an example yesterday, just after lunch, by email (to leaningtech/cheerpj-meta). Didn't you get that?

The mail I sent you is shown above starting with "Hi Lorenzo". Since I replied to your mail your previous answer was included and appeared in the wrong order here on Github which looked very confusing. So I edited and removed your part. This might have been a stupid thing to do. Perhaps I deleted the included files also?

I will resend it now :-)

Best, Janne

jannegus commented 2 years ago

So, this is a new try. I hope the files are included now.

Janne

marsicplusplus commented 2 years ago

Hi @jannegus,

Answering the email from the github notification doesn't work with attachments, you can send me an email containing the testcase at lorenzo@leaningtech.com

jannegus commented 2 years ago

Hi again Lorenzo!

On Thursday I tried to send you an email with a describing word document and a zip-file. But your mail server didn’t accept zip-files.

So I uploaded the files to Google Drive and invited you to share them with me. Have you seen that invitation? It was sent with my gmail address @.**@.>. The invitation might have reached you in Swedish, but I also wrote a message in English.

If you didn’t receive the invitation or if you can’t accept to download the zip-file, how do you suggest that we continue? I very much want to send you the jar-files that I used for REST services, since I think the problem has something to do with them. They work in the java environment, but not within the js environment. Of course I could list their names so you could download them from Maven. But I thought that would be more work for you.

I could also include them all directly in the mail, but then we will lose the file structure of the example, and they are 3MB when zipped.

So, how do you suggest that we proceed?

Janne

@.***http://www.decerno.se/ Jan Gustafsson

Mobil 070 630 75 42 Electrum 234, 164 40 Kista Kistagången 16, 7 tr

Från: Lorenzo @.> Skickat: den 25 november 2021 22:29 Till: leaningtech/cheerpj-meta @.> Kopia: Jan Gustafsson @.>; Mention @.> Ämne: Re: [leaningtech/cheerpj-meta] I can't get REST to work (Issue #133)

Hi @jannegushttps://github.com/jannegus,

Answering the email from the github notification doesn't work with attachments, you can send me an email containing the testcase at @.**@.>

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/leaningtech/cheerpj-meta/issues/133#issuecomment-979476069, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AVLA2YB3NCHXUTI4BEMXMS3UN2TBPANCNFSM5IOHMKIQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

marsicplusplus commented 2 years ago

Hi @jannegus.

I have received and downloaded the files you sent me.

I started looking at it but I have yet to reach a conclusion.

jannegus commented 2 years ago

Hi Lorenzo!

It has gone a couple of weeks now since I sent you my example files. So I'm curious to know if you have come to any conclusions yet. Do you have any indications of what the problem might be?

Regards, Janne

marsicplusplus commented 2 years ago

Hi @jannegus,

It was a problem regarding the compilations of private methods on our side.

It should be fixed with the following runtime:

https://cjrtnc.leaningtech.com/20211227/loader.js

jannegus commented 2 years ago

Hi Lorenzo!

I’m sorry to say that this didn’t work! I modified WebServiceTest.html like this:

<!DOCTYPE html>

Web Service Test

… and made sure to disable the cache!

And I still get the null pointer exception:

@.***

Did you test the modified CherpJ against my example application?

Janne

marsicplusplus commented 2 years ago

I'm unable to start your backend java application, so I made a new server using python to simulate it. Are you getting the same nullpointer exception? I get a nullpointer exception because of CORS if I don't have the plugin that allows those to pass.

Could you send me the Netbeans or whatever IDE are you using of the project of the server itself so that I can build it and run it myself?

jannegus commented 2 years ago

Hi @marsicplusplus Sorry it took me so long to answer! I've been quite busy with other things. I have uploaded a new zip-file and word description to https://drive.google.com/drive/folders/1FVatNvCb-hIWO-yKpJv6wtbcJrCvVp-p?usp=sharing .

The previous version of the web service (HelloService) was created with Eclipse. It referenced WebLogic jars and was probably not very portable. The new version (.war file) is now built with an ant script, and it only uses some contained jersey server jars with dependencies. I have tested it with Glassfish 4, WebLogice 12.2.1 and TomEE 8.0.9. I believe that it now works with most JavaEE containers. But you can rebuild it with the ant script if you have to. Or you can download TomEE 8.0.9 and install it there as I have described in the word document.

I have also made some changes to the JavaClient application. I added a console pane so that I can display messages from the execution. But, unfortunately, you cannot be sure that the messages have been flushed to the console all the time. The client now calls the web service directly without having to press the Get Data button. This makes the stacktrace much smaller. And it now displays the base address of the web service call in the title bar so you can check the port number used. I have also added 'jakarta.activation-api-1.2.2.jar' to the client jars, but that didn't help.

I think, however, that you can use the old client if you don't want to regenerate all the JS code.

Are you getting the same nullpointer exception?

No, with the new version of the loader I get another null pointer exception. The new one looks like this.

image

alexp-sssup commented 2 years ago

@jannegus I see that you are trying to use the CheerpJ 2.2 AOT compiler with the new fixed runtime. That is not supported.

For the purpose of testing you can simply remove jar.js, the application will load more slowly but it will work.

You will need to wait for the next release (2.3) to generate jar.js files. As part of our support policies we cannot provide updated AOT builds for non-commercial customers.

jannegus commented 2 years ago

Thanks @alexp-sssup! Now it works! I didn't know that you could run the application without precompiling to javascript. Do you have any idea about when CheerpJ 2.3 will be released? A month? 6 month? A year? Regards, Janne

alexp-sssup commented 2 years ago

We expect to be able to make a release in a few weeks