Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTEASY-1695] Add GRAMMARS into RESTEasy WADL #1649

Merged
merged 3 commits into from
Oct 30, 2018

Conversation

liweinan
Copy link
Collaborator

@liweinan liweinan commented Aug 6, 2018

No description provided.

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 6, 2018

work in progress

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 6, 2018

Design:

image

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 6, 2018

External grammar can be included now:

image

The left tasks is allow server to serve the external schema.

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 6, 2018

ResteasyWadlGrammar is the class for grammar storing(both included grammars or generated grammars):

class diagram3

And it's designed to be thread-safe.

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 6, 2018

Sample included schema output:

image

The usage should be simple:

contextBuilder.getDeployment().getActualResourceClasses().add(ResteasyWadlExtendedResource.class);
ResteasyWadlGrammar.includeGrammars("application-grammars.xml");

As shown above, the included grammars should be put into classpath and set via includeGrammars method.

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 7, 2018

Now the schema generation is working:

image

It will scan the resource methods and its parameters(also return types), and collect all @XmlRootElement annotated classes, and generate schemas for them.

To use the generation function, just enable it:

ResteasyWadlGrammar.enableSchemaGeneration();

And it will be automatically included in grammars section:

image

@liweinan
Copy link
Collaborator Author

liweinan commented Aug 7, 2018

The next step is to cleanup the code and add the tests. Then I'll move this PR for review.

@liweinan liweinan force-pushed the RESTEASY-1695 branch 2 times, most recently from 804654d to 3f193ad Compare August 8, 2018 07:32
@liweinan
Copy link
Collaborator Author

liweinan commented Aug 8, 2018

Final design:

image

tests added. code cleanup done.

@rsvoboda
Copy link
Member

Just scanned the PR a bit, no deep dive.

  • please remove System.out.println
  • hard-coded http://127.0.0.1 can bring issues in the future
    • HttpServer is used instead of ARQ in resteasy-wadl module

@liweinan
Copy link
Collaborator Author

@rsvoboda thanks for checking!

@liweinan
Copy link
Collaborator Author

@rsvoboda @asoldano To move tests to integration-tests and using ARQ instead of the current standalone tests, we have to add resteasy-wadl module into jboss-modules:

image

wdyt?

Copy link
Member

@asoldano asoldano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liweinan , thanks for the PR. Now, besides for the minor comment below, I think that my main concern is with the lifecycle and and visibility of the new ResteasyWadlGrammar class. Is there a reason for having such a singleton? Can't we simply keep track of the relevant data in each deployment/classloader?

private static AtomicBoolean generateSchema = new AtomicBoolean(false);


public static boolean hasGrammars() {
Copy link
Member

@asoldano asoldano Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this hasGrammar() be syncronized too (under the current design)?

@asoldano
Copy link
Member

asoldano commented Sep 4, 2018

@liweinan , as for the resteasy-wadl new module, yes, that will have to be added

@liweinan
Copy link
Collaborator Author

liweinan commented Sep 5, 2018

thanks for reviewing everyone! Let me do the refactor work.

@liweinan
Copy link
Collaborator Author

liweinan commented Oct 19, 2018

class diagram1

Refactor

  • ResteasyWadlGrammar is no longer a singleton class and it's now inner class of ResteasyWadlWriter
  • ResteasyWadlResourceHelper is added to hold the singletion instance of ResteasyWadlWriter(the instance is moved out of ResteasyWadlDefaultResource).
  • ResteasyWadlDefaultResource and ResteasyWadlExtendedResource are provided for default WADL usages, and they use the writer inside ResteasyWadlResourceHelper.
  • ResteasyWadlWriter supports caching now(And it's turned off by default).

Besides, hard coded port numbers are cleaned up in tests.

Usage Becomes

Add ResteasyWadlDefaultResource and ResteasyWadlExtendedResource into deployment:

contextBuilder.getDeployment().getActualResourceClasses().add(ResteasyWadlDefaultResource.class);
contextBuilder.getDeployment().getActualResourceClasses().add(ResteasyWadlExtendedResource.class);

Actually the usages ofResteasyWadlDefaultResource, ResteasyWadlExtendedResource and ResteasyWadlResourceHelper are all optional. But it's convenient to use them because it helps users to setup almost everything. And it will provide two paths: /application.xml and /wadl-extended/{path} to the users.

To use grammar support, it's necessary to create an instance of ResteasyWadlGrammar. The recommended way is to put it into ResteasyWadlResourceHelper.apiWriter:

ResteasyWadlWriter.ResteasyWadlGrammar wadlGrammar = new ResteasyWadlWriter.ResteasyWadlGrammar();

// if you want to use your own handwritten grammar file:
wadlGrammar.includeGrammars("application-grammars.xml");

// if you'd like to enable automatic schema generation:
wadlGrammar.enableSchemaGeneration();

Put it into ResteasyWadlResourceHelper for future usage:

ResteasyWadlResourceHelper.getApiWriter().setWadlGrammar(wadlGrammar);

Then ResteasyWadlDefaultResource and ResteasyWadlExtendedResource can use it properly.

@liweinan liweinan force-pushed the RESTEASY-1695 branch 2 times, most recently from 96c7f67 to 35a0813 Compare October 19, 2018 06:40
@liweinan
Copy link
Collaborator Author

liweinan commented Oct 19, 2018

Relative PRs:

  • RESTEASY-1360 - Create RESTEasy WADL Feature pack for WildFly
  • RESTEASY-2043 - Update jboss-modules to include restasy-wadl module

@asoldano I have created separated JIRA tasks to work on jboss-module / wildfly integration of WADL. So this PR is complete now. Could you please help to review it? Thanks!

@asoldano
Copy link
Member

@liweinan please start by rebasing to resolve the conflicts and then removing the "requires rebase" label ;-)

@asoldano
Copy link
Member

@liweinan nevermind, the PR now shows no conflicts

Copy link
Member

@asoldano asoldano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liweinan I've checked the changes again. Honestly, I don't understand what has really changed wrt my previous comment. We have a single instance of ResteasyWadlWriter which relies on the latest and globally set grammar object. Is that really ok? what if 2 deployments runs in parallel? May be I really need to see an integration test using Arquillian to understand this. Can you clarify a bit?
Besides this, there're a couple of other things to solve, see comments below. Thanks!

import org.jboss.resteasy.wadl.jaxb.Resource;
import org.jboss.resteasy.wadl.jaxb.Resources;
import org.jboss.resteasy.wadl.jaxb.Response;
import org.jboss.resteasy.wadl.jaxb.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use * imports

} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something better for exception handling here? (at least use proper logging)

@liweinan
Copy link
Collaborator Author

@asoldano Thanks for checking! Let me:

  • Review the design and make necessary refactor
  • Provide the integration tests with Wildfly
  • Fix the imports

@rsvoboda
Copy link
Member

@liweinan I think the main item is to provide details and explanation for this:

We have a single instance of ResteasyWadlWriter which relies on the latest and globally set grammar object. Is that really ok? what if 2 deployments runs in parallel? ... Can you clarify a bit?

@liweinan
Copy link
Collaborator Author

@rsvoboda Yeah that's a problem. Let me redesign this part.

@liweinan
Copy link
Collaborator Author

Changes

  • Fix the original global instance design of wadl writers, and now its instance could be put into per-deployment scope.
  • Add wildfly integration test.
  • Deprecate ResteasyWadlServlet and ResteasyWadlServletWriter, because it doesn't support grammars.
  • Improve ResteasyWadlDefaultResource, for it can be used in both embedded container and servlet container.
  • Using logging error instead of runtime exceptions inside wadl writer.
  • Other minor fixes.

@liweinan
Copy link
Collaborator Author

@asoldano The refactor is done. Could you please help to review it? Thanks!

Copy link
Member

@asoldano asoldano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @liweinan , this looks better. I have a question and a minor change requirement, please see the other comments. Thanks.

// .setAsyncSupported(false)
// .setLoadOnStartup(1)
// .addMapping("/application.xml");
// di.addServlet(resteasyWadlServlet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please get rid of this and other commented out stuff in this PR please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir!

@liweinan liweinan dismissed asoldano’s stale review October 29, 2018 03:55

Hi Alessio, I have added comments and removed the outdated code. Could you please help to review? Thanks!

contextBuilder.bind(httpServer);
ResteasyWadlDefaultResource.getServices().put("/", ResteasyWadlGenerator.generateServiceRegistry(contextBuilder.getDeployment()));
// defaultResource.getServices().put("/", ResteasyWadlGenerator.generateServiceRegistry(contextBuilder.getDeployment()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also remove the commented out lines here above or do they have some special meaning that justify their presence?

@liweinan liweinan dismissed asoldano’s stale review October 30, 2018 02:52

Hi Alessio, I have added comments about TestApplication. Could you please help to check if it's okay? Thanks!

@asoldano asoldano merged commit 9207919 into resteasy:master Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants