Created
July 5, 2018 22:20
-
-
Save cristianprofile/e1c2fdc03ee06f1508c097e37cc5201b to your computer and use it in GitHub Desktop.
jersey get all endpoints
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
/** | |
* | |
*/ | |
package com.technoactivity.mymo.resource.external; | |
import com.sun.jersey.api.model.AbstractResource; | |
import com.sun.jersey.api.model.AbstractResourceMethod; | |
import com.sun.jersey.api.model.AbstractSubResourceMethod; | |
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; | |
import com.technoactivity.mymo.resource.BaseResource; | |
import com.wordnik.swagger.annotations.Api; | |
import com.wordnik.swagger.annotations.ApiOperation; | |
import org.codehaus.jackson.node.ArrayNode; | |
import org.codehaus.jackson.node.JsonNodeFactory; | |
import org.codehaus.jackson.node.ObjectNode; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.Application; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
/** | |
* @author ajnavarro | |
* | |
*/ | |
@Path("/") | |
@Api(value = "/", description = "Transacion payment Order") | |
public class BaseResourceImpl extends BaseResource { | |
@GET | |
@Path("/") | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Produces(MediaType.APPLICATION_JSON) | |
@ApiOperation(value = "Get a payment order", authorizations = "ALL") | |
public Response showAll( @Context Application application) | |
{ | |
String basePath = "/"; | |
// http://stackoverflow.com/questions/13484350/find-a-list-of-all-jersey-resource-methods-in-my-app | |
ObjectNode root = JsonNodeFactory.instance.objectNode(); | |
ArrayNode resources = JsonNodeFactory.instance.arrayNode(); | |
root.put( "resources", resources ); | |
for ( Class<?> aClass : application.getClasses() ) | |
{ | |
if ( isAnnotatedResourceClass( aClass ) ) | |
{ | |
AbstractResource resource = IntrospectionModeller.createResource( aClass ); | |
ObjectNode resourceNode = JsonNodeFactory.instance.objectNode(); | |
String uriPrefix = resource.getPath().getValue(); | |
for ( AbstractSubResourceMethod srm : resource.getSubResourceMethods() ) | |
{ | |
String uri = uriPrefix + "/" + srm.getPath().getValue(); | |
addTo( resourceNode, uri, srm, joinUri(basePath, uri) ); | |
} | |
for ( AbstractResourceMethod srm : resource.getResourceMethods() ) | |
{ | |
addTo( resourceNode, uriPrefix, srm, joinUri( basePath, uriPrefix ) ); | |
} | |
resources.add( resourceNode ); | |
} | |
} | |
return Response.ok().entity( root ).build(); | |
} | |
private void addTo( ObjectNode resourceNode, String uriPrefix, AbstractResourceMethod srm, String path ) | |
{ | |
if ( resourceNode.get( uriPrefix ) == null ) | |
{ | |
ObjectNode inner = JsonNodeFactory.instance.objectNode(); | |
inner.put("path", path); | |
inner.put("verbs", JsonNodeFactory.instance.arrayNode()); | |
resourceNode.put( uriPrefix, inner ); | |
} | |
((ArrayNode) resourceNode.get( uriPrefix ).get("verbs")).add( srm.getHttpMethod() ); | |
} | |
public static String joinUri( String... parts ) | |
{ | |
StringBuilder result = new StringBuilder(); | |
for ( String part : parts ) | |
{ | |
if ( result.length() > 0 && result.charAt( result.length() - 1 ) == '/' ) | |
{ | |
result.setLength( result.length() - 1 ); | |
} | |
if ( result.length() > 0 && !part.startsWith( "/" ) ) | |
{ | |
result.append( '/' ); | |
} | |
result.append( part ); | |
} | |
return result.toString(); | |
} | |
private boolean isAnnotatedResourceClass( Class rc ) | |
{ | |
if ( rc.isAnnotationPresent( Path.class ) ) | |
{ | |
return true; | |
} | |
for ( Class i : rc.getInterfaces() ) | |
{ | |
if ( i.isAnnotationPresent( Path.class ) ) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment