Skip to content

Commit

Permalink
Can configure @ExposedApplication for a package
Browse files Browse the repository at this point in the history
Reading https://jersey.java.net/documentation/latest/deployment.html we found that one can
deploy an application without an Application subclass.
In this case we expect @ExposedApplication (with a servletName) to be set on a package in package-info.java.
  • Loading branch information
jcgay committed Jan 26, 2015
1 parent 8f5263e commit 520ecc3
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 5 deletions.
72 changes: 72 additions & 0 deletions jax-rs-linker-integration-tests/no-configuration-webapp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>fr.vidal.oss</groupId>
<artifactId>jax-rs-linker-integration-tests</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>

<artifactId>no-configuration-webapp</artifactId>
<packaging>war</packaging>

<name>JAX RS Linker: webapp without JAXRS Configuration</name>

<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>port-allocator-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jax-rs-linker</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.vidal.oss.jax_rs_linker.it;


import fr.vidal.oss.jax_rs_linker.Linkers;
import fr.vidal.oss.jax_rs_linker.api.Self;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/brand")
public class BrandResource {

@Self
@Path("/{id}")
@GET
public String getById(@PathParam("id") int id) {
return Linkers.brandResourceLinker()
.self()
.replace(BrandResourcePathParameters.ID, String.valueOf(id))
.value();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@fr.vidal.oss.jax_rs_linker.api.ExposedApplication(servletName = "javax.ws.rs.core.Application")
package fr.vidal.oss.jax_rs_linker.it;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fr.vidal.oss.jax_rs_linker.it;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.junit.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

public class BrandIT {

@Test
public void self_link_with_context_path_and_servlet_name() throws Exception {
Response response = request("brand/23");

assertThat(response.body().string())
.isEqualTo("/it-tests/rest/brand/23");
}

private static Response request(String resource) throws IOException {
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url("http://localhost:" + port() + "/it-tests/rest/" + resource)
.build();

return client.newCall(request).execute();
}

private static String port() {
return System.getProperty("jetty.port", "8080");
}
}
1 change: 1 addition & 0 deletions jax-rs-linker-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@
<modules>
<module>no-xml-webapp</module>
<module>xml-webapp</module>
<module>no-configuration-webapp</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
import javax.ws.rs.ApplicationPath;
Expand All @@ -39,6 +40,7 @@
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Throwables.propagate;
import static fr.vidal.oss.jax_rs_linker.errors.CompilationError.INCONSISTENT_APPLICATION_MAPPING;
import static fr.vidal.oss.jax_rs_linker.errors.CompilationError.NO_APPLICATION_SERVLET_NAME;
import static fr.vidal.oss.jax_rs_linker.functions.JavaxElementToMappings.intoOptionalMapping;
import static fr.vidal.oss.jax_rs_linker.functions.MappingToPathParameters.TO_PATH_PARAMETERS;
import static fr.vidal.oss.jax_rs_linker.predicates.ElementHasKind.byKind;
Expand Down Expand Up @@ -152,15 +154,21 @@ private Optional<String> parseApplicationName(RoundEnvironment roundEnv) {
messager.printMessage(ERROR, CompilationError.ONE_APPLICATION_ONLY.text());
return absent();
}
Element application = applications.iterator().next();
return applicationName((TypeElement) application);
return applicationName(applications.iterator().next());
}

private Optional<String> applicationName(TypeElement application) {
ApplicationPath applicationPath = application.getAnnotation(ApplicationPath.class);
private Optional<String> applicationName(Element application) {
String servletName = application.getAnnotation(ExposedApplication.class).servletName();
String applicationName = String.valueOf(application.getQualifiedName());
if (application instanceof PackageElement) {

This comment has been minimized.

Copy link
@fbiville

fbiville Jan 27, 2015

Collaborator

Il vaut mieux utiliser un visiteur du type http://docs.oracle.com/javase/7/docs/api/javax/lang/model/util/ElementKindVisitor7.html

On les a peu utilisé jusqu'ici.

This comment has been minimized.

Copy link
@jcgay

jcgay Jan 27, 2015

Author Contributor

Merci, je regarde !

if (servletName.isEmpty()) {
messager.printMessage(ERROR, NO_APPLICATION_SERVLET_NAME.format(application), application);
return absent();
}
return Optional.of(servletName);
}

ApplicationPath applicationPath = application.getAnnotation(ApplicationPath.class);
String applicationName = String.valueOf(((TypeElement) application).getQualifiedName());
if (!(applicationPath != null ^ !servletName.isEmpty())) {
messager.printMessage(ERROR, INCONSISTENT_APPLICATION_MAPPING.format(applicationName), application);
return absent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public enum CompilationError {
),
NO_APPLICATION_FOUND(
"\n\tThere should be exactly one @ExposedApplication-annotated Jersey Application: none found."
),
NO_APPLICATION_SERVLET_NAME(
"\n\t@ExposedApplication servletName must not be empty when used on a package." +
"\n\tGiven package: <%s>"
);

private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,35 @@ public void fails_to_compile_ambiguous_configuration_class() {
)
.in(configuration);
}

@Test
public void generate_linkers_with_exposed_application_on_package() {
assert_().about(javaSources())
.that(ImmutableList.of(
forResource("package-info.java"),
forResource("BrandResource.java")
))
.processedWith(processor)
.compilesWithoutError()
.and()
.generatesSources(
forResource("BrandResourceLinker.java"),
forResource("linkers/LinkersPackageInfo.java")
);
}

@Test
public void fails_to_compile_without_jaxrs_configuration_and_servletName() {
JavaFileObject configuration = forResource("mispackage/package-info.java");

assert_().about(javaSource())
.that(configuration)
.processedWith(processor)
.failsToCompile()
.withErrorContaining(
"\n \t@ExposedApplication servletName must not be empty when used on a package." +
"\n \tGiven package: <mispackage>"
)
.in(configuration);
}
}
32 changes: 32 additions & 0 deletions jax-rs-linker/src/test/resources/linkers/LinkersPackageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

package fr.vidal.oss.jax_rs_linker;

import fr.vidal.oss.jax_rs_linker.servlet.ContextPaths;
import javax.annotation.Generated;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import fr.vidal.oss.jax_rs_linker.parser.BrandResourceLinker;

@WebListener
@Generated("fr.vidal.oss.jax_rs_linker.LinkerAnnotationProcessor")
public class Linkers
implements ServletContextListener {

private static String contextPath = "";

private static String applicationName = "my-super-application";

@Override
public void contextInitialized(ServletContextEvent sce) {
contextPath = ContextPaths.contextPath(sce.getServletContext(), applicationName);
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
}

public static BrandResourceLinker brandResourceLinker() {
return new BrandResourceLinker(contextPath);
}
}
2 changes: 2 additions & 0 deletions jax-rs-linker/src/test/resources/mispackage/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@fr.vidal.oss.jax_rs_linker.api.ExposedApplication
package mispackage;
2 changes: 2 additions & 0 deletions jax-rs-linker/src/test/resources/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@fr.vidal.oss.jax_rs_linker.api.ExposedApplication(servletName = "my-super-application")
package fr.vidal.oss.jax_rs_linker;

1 comment on commit 520ecc3

@fbiville
Copy link
Collaborator

Choose a reason for hiding this comment

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

nickel sinon.
va quand même falloir trouver un moyen de dégraisser LinkerAnnotationProcessor :/

Please sign in to comment.