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

Feature #47: Support for @ApplicationPath in JAX-RS doclet #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doclets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<docletArtifact>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1</version>
<version>1.1.1</version>
</docletArtifact>
</docletArtifacts>
<destDir>jpadocs</destDir>
Expand Down Expand Up @@ -203,7 +203,7 @@
<docletArtifact>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1</version>
<version>1.1.1</version>
</docletArtifact>
<docletArtifact>
<groupId>org.jboss.resteasy</groupId>
Expand All @@ -220,7 +220,7 @@
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1</version>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.LinkedList;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Path;

import com.lunatech.doclets.jax.JAXDoclet;
Expand All @@ -32,6 +33,7 @@
import com.lunatech.doclets.jax.jaxrs.tags.*;
import com.lunatech.doclets.jax.jaxrs.writers.IndexWriter;
import com.lunatech.doclets.jax.jaxrs.writers.SummaryWriter;
import com.sun.javadoc.AnnotationDesc;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.LanguageVersion;
Expand All @@ -43,10 +45,12 @@
import com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet;

public class JAXRSDoclet extends JAXDoclet<JAXRSConfiguration> {
public static final String JAX_RS_APP_CLASS = "javax.ws.rs.core.Application";

public final HtmlDoclet htmlDoclet = new HtmlDoclet();

private static final Class<?>[] jaxrsAnnotations = new Class<?>[] { Path.class };
private String rootPath;

public static int optionLength(final String option) {
if ("-jaxrscontext".equals(option)) {
Expand Down Expand Up @@ -92,6 +96,7 @@ protected JAXRSConfiguration makeConfiguration(ConfigurationImpl configuration)

public void start() {
final ClassDoc[] classes = conf.parentConfiguration.root.classes();
findApplicationRoot(classes);
for (final ClassDoc klass : classes) {
if (Utils.findAnnotatedClass(klass, jaxrsAnnotations) != null) {
handleJAXRSClass(klass);
Expand All @@ -105,8 +110,36 @@ public void start() {
Utils.copyResources(conf);
}

private void findApplicationRoot(ClassDoc[] classes) {
List<ClassDoc> applicationClasses = new LinkedList<ClassDoc>();

for (final ClassDoc klass : classes) {
if (Utils.findSuperTypeFromClass(klass, JAX_RS_APP_CLASS) != null) {
applicationClasses.add(klass);
}
}

switch (applicationClasses.size()) {
case 0:
getRootDoc().printNotice("JAX-RS root will be set by servlet-mapping for " + JAX_RS_APP_CLASS + ".");
break;

case 1:
AnnotationDesc annotation = Utils.findAnnotation(applicationClasses.get(0), ApplicationPath.class);
rootPath = (String) Utils.getAnnotationValue(annotation);
break;

default:
getRootDoc().printWarning("Ambiguous JAX-RS " + JAX_RS_APP_CLASS + " def:");
for (final ClassDoc klass : classes) {
getRootDoc().printWarning(klass.position(), "");
}
break;
}
}

private void handleJAXRSClass(final ClassDoc klass) {
jaxrsMethods.addAll(new ResourceClass(klass, null).getMethods());
jaxrsMethods.addAll(new ResourceClass(klass, null, rootPath).getMethods());
}

public void warn(String warning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ResourceClass {

private ClassDoc declaringClass;

private String applicationRootPath;

private AnnotationDesc rootPathAnnotation;

private AnnotationDesc rootProducesAnnotation;
Expand All @@ -55,7 +57,12 @@ public class ResourceClass {
private Map<String, String> regexFragments = new HashMap<String, String>();

public ResourceClass(ClassDoc resourceClass, ResourceMethod methodLocator) {
this(resourceClass, methodLocator, null);
}

public ResourceClass(ClassDoc resourceClass, ResourceMethod methodLocator, String applicationRootPath) {
this.parentMethod = methodLocator;
this.applicationRootPath = applicationRootPath;
// find the annotated class or interface
declaringClass = Utils.findAnnotatedClass(resourceClass, Path.class);
// sub-resources may not have a path, but they're still resources
Expand Down Expand Up @@ -104,6 +111,8 @@ private void setupPath() {
path = null;
if (parentMethod != null)
path = Utils.appendURLFragments(parentMethod.getPath(), path);
if (parentMethod == null && applicationRootPath != null)
path = Utils.appendURLFragments("/", applicationRootPath, path);
}

public ClassDoc getDeclaringClass() {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1</version>
<version>1.1.1</version>
<type>jar</type>
</dependency>
<dependency>
Expand Down