Skip to content

Commit

Permalink
Merge pull request #468 from ArcBees/cv_rest_service_from_path
Browse files Browse the repository at this point in the history
Generate REST services based on the presence of @path.

Former-commit-id: 15020ff
  • Loading branch information
Chris-V committed Apr 8, 2014
2 parents 0e6c6f7 + 35d6797 commit edd3d52
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 111 deletions.
Expand Up @@ -21,9 +21,8 @@

import com.gwtplatform.carstore.shared.dto.CarDto;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

public interface CarService extends RestService {
public interface CarService {
@GET
RestAction<CarDto> get();

Expand Down
Expand Up @@ -25,28 +25,30 @@
import javax.ws.rs.QueryParam;

import com.gwtplatform.carstore.shared.dto.CarDto;
import com.gwtplatform.carstore.shared.rest.PathParameter;
import com.gwtplatform.carstore.shared.rest.ResourcesPath;
import com.gwtplatform.carstore.shared.rest.RestParameter;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

@Path(ResourcesPath.CARS)
public interface CarsService extends RestService {
import static com.gwtplatform.carstore.shared.rest.PathParameter.PATH_ID;
import static com.gwtplatform.carstore.shared.rest.ResourcesPath.CARS;
import static com.gwtplatform.carstore.shared.rest.ResourcesPath.COUNT;
import static com.gwtplatform.carstore.shared.rest.RestParameter.ID;
import static com.gwtplatform.carstore.shared.rest.RestParameter.LIMIT;
import static com.gwtplatform.carstore.shared.rest.RestParameter.OFFSET;

@Path(CARS)
public interface CarsService {
@GET
RestAction<List<CarDto>> getCars();

@GET
RestAction<List<CarDto>> getCars(@QueryParam(RestParameter.OFFSET) int offset,
@QueryParam(RestParameter.LIMIT) int limit);
RestAction<List<CarDto>> getCars(@QueryParam(OFFSET) int offset, @QueryParam(LIMIT) int limit);

@GET
@Path(ResourcesPath.COUNT)
@Path(COUNT)
RestAction<Integer> getCarsCount();

@POST
RestAction<CarDto> saveOrCreate(CarDto carDto);

@Path(PathParameter.ID)
CarService car(@PathParam(RestParameter.ID) Long carId);
@Path(PATH_ID)
CarService car(@PathParam(ID) Long carId);
}
Expand Up @@ -26,27 +26,28 @@

import com.gwtplatform.carstore.shared.dto.ManufacturerDto;
import com.gwtplatform.carstore.shared.dto.ManufacturerRatingDto;
import com.gwtplatform.carstore.shared.rest.PathParameter;
import com.gwtplatform.carstore.shared.rest.ResourcesPath;
import com.gwtplatform.carstore.shared.rest.RestParameter;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

@Path(ResourcesPath.MANUFACTURER)
public interface ManufacturerService extends RestService {
import static com.gwtplatform.carstore.shared.rest.PathParameter.PATH_ID;
import static com.gwtplatform.carstore.shared.rest.ResourcesPath.MANUFACTURER;
import static com.gwtplatform.carstore.shared.rest.RestParameter.ID;

@Path(MANUFACTURER)
public interface ManufacturerService {
@GET
RestAction<List<ManufacturerDto>> getManufacturers();

@GET
@Path(PathParameter.ID)
RestAction<ManufacturerDto> get(@PathParam(RestParameter.ID) Long id);
@Path(PATH_ID)
RestAction<ManufacturerDto> get(@PathParam(ID) Long id);

@POST
RestAction<ManufacturerDto> saveOrCreate(ManufacturerDto manufacturerDto);

@DELETE
@Path(PathParameter.ID)
RestAction<Void> delete(@PathParam(RestParameter.ID) Long id);
@Path(PATH_ID)
RestAction<Void> delete(@PathParam(ID) Long id);

@GET
@Path(ResourcesPath.RATING)
Expand Down
Expand Up @@ -25,25 +25,25 @@
import javax.ws.rs.PathParam;

import com.gwtplatform.carstore.shared.dto.RatingDto;
import com.gwtplatform.carstore.shared.rest.PathParameter;
import com.gwtplatform.carstore.shared.rest.ResourcesPath;
import com.gwtplatform.carstore.shared.rest.RestParameter;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

@Path(ResourcesPath.RATING)
public interface RatingService extends RestService {
import static com.gwtplatform.carstore.shared.rest.PathParameter.PATH_ID;
import static com.gwtplatform.carstore.shared.rest.ResourcesPath.RATING;
import static com.gwtplatform.carstore.shared.rest.RestParameter.ID;

@Path(RATING)
public interface RatingService {
@GET
RestAction<List<RatingDto>> getRatings();

@GET
@Path(PathParameter.ID)
RestAction<RatingDto> get(@PathParam(RestParameter.ID) Long id);
@Path(PATH_ID)
RestAction<RatingDto> get(@PathParam(ID) Long id);

@POST
RestAction<RatingDto> saveOrCreate(RatingDto RatingDto);

@DELETE
@Path(PathParameter.ID)
RestAction<Void> delete(@PathParam(RestParameter.ID) Long id);
@Path(PATH_ID)
RestAction<Void> delete(@PathParam(ID) Long id);
}
Expand Up @@ -24,12 +24,12 @@
import com.gwtplatform.carstore.shared.dispatch.LogInRequest;
import com.gwtplatform.carstore.shared.dispatch.LogInResult;
import com.gwtplatform.carstore.shared.dto.CurrentUserDto;
import com.gwtplatform.carstore.shared.rest.ResourcesPath;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

@Path(ResourcesPath.SESSION)
public interface SessionService extends RestService {
import static com.gwtplatform.carstore.shared.rest.ResourcesPath.SESSION;

@Path(SESSION)
public interface SessionService {
@DELETE
RestAction<Void> logout();

Expand Down
Expand Up @@ -24,14 +24,13 @@

import com.gwtplatform.dispatch.rest.client.DateFormat;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestService;

import static com.gwtplatform.carstore.shared.rest.ResourcesPath.STATS;
import static com.gwtplatform.carstore.shared.rest.RestParameter.DATE;
import static com.gwtplatform.carstore.shared.rest.RestParameter.DATE_FORMAT;

@Path(STATS)
public interface StatisticsService extends RestService {
public interface StatisticsService {
@GET
RestAction<Integer> extractYearFromDate(@QueryParam(DATE) @DateFormat(DATE_FORMAT) Date date);
}
Expand Up @@ -92,7 +92,7 @@ public Response saveOrCreate(CarDto carDto) {
return Response.ok(Car.createDto(car)).build();
}

@Path(PathParameter.ID)
@Path(PathParameter.PATH_ID)
@DELETE
public Response delete(@PathParam(RestParameter.ID) Long id) {
carDao.delete(id);
Expand Down
Expand Up @@ -63,7 +63,7 @@ public Response getManufacturers() {
return Response.ok(manufacturerDtos).build();
}

@Path(PathParameter.ID)
@Path(PathParameter.PATH_ID)
@GET
public Response get(@PathParam(RestParameter.ID) Long id) {
ManufacturerDto manufacturerDto = Manufacturer.createDto(manufacturerDao.get(id));
Expand All @@ -78,7 +78,7 @@ public Response saveOrCreate(ManufacturerDto manufacturerDto) {
return Response.ok(Manufacturer.createDto(manufacturer)).build();
}

@Path(PathParameter.ID)
@Path(PathParameter.PATH_ID)
@DELETE
public Response delete(@PathParam(RestParameter.ID) Long id) {
manufacturerDao.delete(id);
Expand Down
Expand Up @@ -52,7 +52,7 @@ public Response getRatings() {
return Response.ok(ratingDtos).build();
}

@Path(PathParameter.ID)
@Path(PathParameter.PATH_ID)
@GET
public Response get(@PathParam(RestParameter.ID) Long id) {
RatingDto ratingDto = Rating.createDto(ratingDao.get(id));
Expand All @@ -67,7 +67,7 @@ public Response saveOrCreate(RatingDto ratingDto) {
return Response.ok(Rating.createDto(rating)).build();
}

@Path(PathParameter.ID)
@Path(PathParameter.PATH_ID)
@DELETE
public Response delete(@PathParam(RestParameter.ID) Long id) {
ratingDao.delete(id);
Expand Down
Expand Up @@ -17,5 +17,5 @@
package com.gwtplatform.carstore.shared.rest;

public class PathParameter {
public static final String ID = "/{" + RestParameter.ID + "}";
public static final String PATH_ID = "/{" + RestParameter.ID + "}";
}
Expand Up @@ -27,7 +27,7 @@

/**
* Provides a basic implementation of {@link RestAction} to inheritors. This is used by GWTP code-generator to create
* the actions defined by the {@link com.gwtplatform.dispatch.rest.shared.RestService} implementers.
* the actions defined by the services.
*
* @param <R> the result type
*/
Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.google.gwt.core.client.EntryPoint;

/**
* This interface is used to initiate the generation of {@link com.gwtplatform.dispatch.rest.shared.RestService}s and
* This interface is used to initiate the generation of services and
* {@link com.gwtplatform.dispatch.rest.shared.RestAction}s.
* <p/>
* Making it an {@link EntryPoint} makes sure GWT kicks off the generation as early as possible.
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.List;

import javax.inject.Provider;
import javax.ws.rs.Path;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
Expand All @@ -30,15 +31,14 @@
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.gwtplatform.dispatch.rest.rebind.type.ActionBinding;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceBinding;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceDefinitions;
import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil;
import com.gwtplatform.dispatch.rest.shared.RestAction;

public abstract class AbstractServiceGenerator extends AbstractVelocityGenerator {
protected static final String TEMPLATE = "com/gwtplatform/dispatch/rest/rebind/RestService.vm";

private final List<ActionBinding> actionBindings = Lists.newArrayList();
private final List<ServiceBinding> serviceBindings = Lists.newArrayList();
private final ServiceDefinitions serviceDefinitions;
private final GeneratorFactory generatorFactory;
private final JClassType service;

Expand All @@ -48,12 +48,10 @@ protected AbstractServiceGenerator(
Provider<VelocityContext> velocityContextProvider,
VelocityEngine velocityEngine,
GeneratorUtil generatorUtil,
ServiceDefinitions serviceDefinitions,
GeneratorFactory generatorFactory,
JClassType service) {
super(typeOracle, logger, velocityContextProvider, velocityEngine, generatorUtil);

this.serviceDefinitions = serviceDefinitions;
this.generatorFactory = generatorFactory;
this.service = service;
}
Expand All @@ -76,19 +74,33 @@ protected void generateMethods() throws UnableToCompleteException {
JMethod[] methods = service.getInheritableMethods();
if (methods != null) {
for (JMethod method : methods) {
if (isRestService(method)) {
generateChildRestService(method);
} else {
generateRestAction(method);
}
generateMethodHierarchy(method);
}
}
}

protected boolean isRestService(JMethod method) throws UnableToCompleteException {
protected void generateMethodHierarchy(JMethod method) throws UnableToCompleteException {
if (isAction(method)) {
generateRestAction(method);
} else if (isSubService(method)) {
generateChildRestService(method);
} else {
String methodName = method.getEnclosingType().getQualifiedSourceName() + "#" + method.getName() + "(...)";
getLogger().die(methodName + " should return either a RestAction<> or a Sub-Resource.");
}
}

protected boolean isAction(JMethod method) throws UnableToCompleteException {
JClassType actionClass = getTypeOracle().findType(RestAction.class.getName());
JClassType returnClass = method.getReturnType().isClassOrInterface();

return returnClass != null && returnClass.isAssignableTo(actionClass);
}

protected boolean isSubService(JMethod method) throws UnableToCompleteException {
JClassType returnInterface = method.getReturnType().isInterface();

return returnInterface != null && serviceDefinitions.isService(returnInterface);
return returnInterface != null && method.isAnnotationPresent(Path.class);
}

protected void generateChildRestService(JMethod method) throws UnableToCompleteException {
Expand Down
Expand Up @@ -37,7 +37,6 @@
import com.gwtplatform.dispatch.rest.client.NoXsrfHeader;
import com.gwtplatform.dispatch.rest.rebind.type.ChildServiceBinding;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceBinding;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceDefinitions;
import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil;

public class ChildServiceGenerator extends AbstractServiceGenerator {
Expand All @@ -50,17 +49,17 @@ public class ChildServiceGenerator extends AbstractServiceGenerator {
private ServiceBinding serviceBinding;

@Inject
ChildServiceGenerator(TypeOracle typeOracle,
Logger logger,
Provider<VelocityContext> velocityContextProvider,
VelocityEngine velocityEngine,
GeneratorUtil generatorUtil,
GeneratorFactory generatorFactory,
ServiceDefinitions serviceDefinitions,
@Assisted JMethod serviceMethod,
@Assisted ServiceBinding parent) {
super(typeOracle, logger, velocityContextProvider, velocityEngine, generatorUtil, serviceDefinitions,
generatorFactory, serviceMethod.getReturnType().isInterface());
ChildServiceGenerator(
TypeOracle typeOracle,
Logger logger,
Provider<VelocityContext> velocityContextProvider,
VelocityEngine velocityEngine,
GeneratorUtil generatorUtil,
GeneratorFactory generatorFactory,
@Assisted JMethod serviceMethod,
@Assisted ServiceBinding parent) {
super(typeOracle, logger, velocityContextProvider, velocityEngine, generatorUtil, generatorFactory,
serviceMethod.getReturnType().isInterface());

this.serviceMethod = serviceMethod;
this.parent = parent;
Expand Down Expand Up @@ -122,7 +121,7 @@ private void doGenerate(String implName, PrintWriter printWriter) throws UnableT

private boolean isSecured() {
return parent.isSecured()
&& !serviceMethod.isAnnotationPresent(NoXsrfHeader.class)
&& !service.isAnnotationPresent(NoXsrfHeader.class);
&& !serviceMethod.isAnnotationPresent(NoXsrfHeader.class)
&& !service.isAnnotationPresent(NoXsrfHeader.class);
}
}
Expand Up @@ -32,7 +32,6 @@
import com.google.inject.assistedinject.Assisted;
import com.gwtplatform.dispatch.rest.client.NoXsrfHeader;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceBinding;
import com.gwtplatform.dispatch.rest.rebind.type.ServiceDefinitions;
import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil;

public class ServiceGenerator extends AbstractServiceGenerator {
Expand All @@ -42,16 +41,15 @@ public class ServiceGenerator extends AbstractServiceGenerator {
private ServiceBinding serviceBinding;

@Inject
ServiceGenerator(TypeOracle typeOracle,
Logger logger,
Provider<VelocityContext> velocityContextProvider,
VelocityEngine velocityEngine,
GeneratorUtil generatorUtil,
ServiceDefinitions serviceDefinitions,
GeneratorFactory generatorFactory,
@Assisted JClassType service) {
super(typeOracle, logger, velocityContextProvider, velocityEngine, generatorUtil, serviceDefinitions,
generatorFactory, service);
ServiceGenerator(
TypeOracle typeOracle,
Logger logger,
Provider<VelocityContext> velocityContextProvider,
VelocityEngine velocityEngine,
GeneratorUtil generatorUtil,
GeneratorFactory generatorFactory,
@Assisted JClassType service) {
super(typeOracle, logger, velocityContextProvider, velocityEngine, generatorUtil, generatorFactory, service);

this.service = service;
path = extractPath(service);
Expand Down

0 comments on commit edd3d52

Please sign in to comment.