-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-2177: Being able to create a new REST endpoint
- Loading branch information
Showing
6 changed files
with
411 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/rest/ui/AbstractRESTNewCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jboss.forge.addon.javaee.rest.ui; | ||
|
||
import static org.jboss.forge.addon.javaee.JavaEEPackageConstants.DEFAULT_REST_PACKAGE; | ||
|
||
import org.jboss.forge.addon.javaee.rest.RestFacet; | ||
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet; | ||
import org.jboss.forge.addon.parser.java.ui.AbstractJavaSourceCommand; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.ui.command.PrerequisiteCommandsProvider; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
import org.jboss.forge.addon.ui.result.NavigationResult; | ||
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder; | ||
import org.jboss.forge.addon.ui.util.Categories; | ||
import org.jboss.forge.addon.ui.util.Metadata; | ||
import org.jboss.forge.roaster.model.source.JavaSource; | ||
|
||
/** | ||
* @author <a href="antonio.goncalves@gmail.com">Antonio Goncalves</a> | ||
*/ | ||
public abstract class AbstractRestNewCommand<T extends JavaSource<?>> extends AbstractJavaSourceCommand<T> | ||
implements PrerequisiteCommandsProvider | ||
{ | ||
|
||
@Override | ||
public Metadata getMetadata(UIContext context) | ||
{ | ||
return Metadata.from(super.getMetadata(context), getClass()) | ||
.category(Categories.create(Categories.create("Java EE"), "REST")); | ||
} | ||
|
||
@Override | ||
protected boolean isProjectRequired() | ||
{ | ||
return true; | ||
} | ||
|
||
@Override | ||
protected String calculateDefaultPackage(UIContext context) | ||
{ | ||
return getSelectedProject(context).getFacet(JavaSourceFacet.class).getBasePackage() + "." | ||
+ DEFAULT_REST_PACKAGE; | ||
} | ||
|
||
@Override | ||
public NavigationResult getPrerequisiteCommands(UIContext context) | ||
{ | ||
NavigationResultBuilder builder = NavigationResultBuilder.create(); | ||
Project project = getSelectedProject(context); | ||
if (project != null) | ||
{ | ||
if (!project.hasFacet(RestFacet.class)) | ||
{ | ||
builder.add(RestSetupWizard.class); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/rest/ui/RestMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.rest.ui; | ||
|
||
/** | ||
* @author <a href="mailto:antonio.goncalves@gmail.com">Antonio Goncalves</a> | ||
*/ | ||
public enum RestMethod | ||
{ | ||
GET("doGet", javax.ws.rs.GET.class), | ||
POST("doPost", javax.ws.rs.POST.class), | ||
PUT("doPut", javax.ws.rs.PUT.class), | ||
DELETE("doDelete", javax.ws.rs.DELETE.class); | ||
|
||
private String methodName; | ||
|
||
private Class methodAnnotation; | ||
|
||
private RestMethod(String methodName, Class methodAnnotation) | ||
{ | ||
this.methodName = methodName; | ||
this.methodAnnotation = methodAnnotation; | ||
} | ||
|
||
public Class getMethodAnnotation() | ||
{ | ||
return methodAnnotation; | ||
} | ||
|
||
public String getMethodName() | ||
{ | ||
return methodName; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/rest/ui/RestNewEndpointCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.javaee.rest.ui; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.UriBuilder; | ||
|
||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.text.Inflector; | ||
import org.jboss.forge.addon.ui.context.UIBuilder; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
import org.jboss.forge.addon.ui.context.UIExecutionContext; | ||
import org.jboss.forge.addon.ui.input.UIInput; | ||
import org.jboss.forge.addon.ui.input.UISelectMany; | ||
import org.jboss.forge.addon.ui.metadata.WithAttributes; | ||
import org.jboss.forge.addon.ui.util.Metadata; | ||
import org.jboss.forge.roaster.model.source.JavaClassSource; | ||
import org.jboss.forge.roaster.model.source.MethodSource; | ||
|
||
/** | ||
* Creates a new REST Endpoint | ||
* | ||
* @author <a href="mailto:antonio.goncalves@gmail.com">Antonio Goncalves</a> | ||
*/ | ||
public class RestNewEndpointCommand extends AbstractRestNewCommand<JavaClassSource> | ||
{ | ||
@Inject | ||
@WithAttributes(label = "Methods", description = "REST methods to be defined") | ||
private UISelectMany<RestMethod> methods; | ||
|
||
@Inject | ||
@WithAttributes(label = "Path", description = "The root path of the endpoint") | ||
private UIInput<String> path; | ||
|
||
@Inject | ||
private Inflector inflector; | ||
|
||
@Override | ||
public Metadata getMetadata(UIContext context) | ||
{ | ||
return Metadata.from(super.getMetadata(context), getClass()) | ||
.name("REST: New Endpoint") | ||
.description("Creates a new REST Endpoint"); | ||
} | ||
|
||
@Override | ||
protected String getType() | ||
{ | ||
return "REST"; | ||
} | ||
|
||
@Override | ||
protected Class<JavaClassSource> getSourceType() | ||
{ | ||
return JavaClassSource.class; | ||
} | ||
|
||
@Override | ||
public void initializeUI(UIBuilder builder) throws Exception | ||
{ | ||
super.initializeUI(builder); | ||
builder.add(methods).add(path); | ||
} | ||
|
||
@Override | ||
public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source) | ||
throws Exception | ||
{ | ||
if (path.hasValue()) | ||
{ | ||
source.addAnnotation(Path.class).setStringValue("/" + path.getValue()); | ||
} | ||
else | ||
{ | ||
source.addAnnotation(Path.class).setStringValue("/" + inflector.lowerCamelCase(getNamed().getValue())); | ||
} | ||
|
||
for (RestMethod method : methods.getValue()) | ||
{ | ||
MethodSource<?> doGet = source.addMethod().setPublic().setName(method.getMethodName()) | ||
.setReturnType("javax.ws.rs.core.Response"); | ||
doGet.addAnnotation(method.getMethodAnnotation()); | ||
|
||
switch (method) | ||
{ | ||
case GET: | ||
doGet.addAnnotation(javax.ws.rs.Produces.class).setStringArrayValue(new String[] { MediaType.TEXT_PLAIN }); | ||
doGet.setBody("return Response.ok(\"method " + method.getMethodName() + " invoked\").build();"); | ||
break; | ||
case POST: | ||
source.addImport(UriBuilder.class); | ||
doGet.addAnnotation(javax.ws.rs.Consumes.class).setStringArrayValue( | ||
new String[] { MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }); | ||
doGet.addParameter(String.class, "entity"); | ||
doGet.setBody("return Response.created(UriBuilder.fromResource(" + getNamed().getValue() | ||
+ ".class).build()).build();"); | ||
break; | ||
case PUT: | ||
source.addImport(UriBuilder.class); | ||
doGet.addAnnotation(javax.ws.rs.Consumes.class).setStringArrayValue( | ||
new String[] { MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }); | ||
doGet.addParameter(String.class, "entity"); | ||
doGet.setBody("return Response.created(UriBuilder.fromResource(" + getNamed().getValue() | ||
+ ".class).build()).build();"); | ||
break; | ||
case DELETE: | ||
doGet.addAnnotation(javax.ws.rs.Path.class).setStringValue("/{id}"); | ||
doGet.addParameter(Long.class, "id").addAnnotation(PathParam.class).setStringValue("id"); | ||
doGet.setBody("return Response.noContent().build();"); | ||
break; | ||
} | ||
} | ||
|
||
return source; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.