use of org.glassfish.jersey.server.model.Resource in project dropwizard by dropwizard.
the class DropwizardResourceConfig method getEndpointsInfo.
public String getEndpointsInfo() {
final StringBuilder msg = new StringBuilder(1024);
final Set<EndpointLogLine> endpointLogLines = new TreeSet<>(new EndpointComparator());
msg.append("The following paths were found for the configured resources:");
msg.append(NEWLINE).append(NEWLINE);
final Set<Class<?>> allResourcesClasses = new HashSet<>();
for (Class<?> clazz : allClasses()) {
if (!clazz.isInterface() && Resource.from(clazz) != null) {
allResourcesClasses.add(clazz);
}
}
for (Class<?> klass : allResourcesClasses) {
new EndpointLogger(urlPattern, klass).populate(endpointLogLines);
}
final Set<Resource> allResources = this.getResources();
for (Resource res : allResources) {
for (Resource childRes : res.getChildResources()) {
for (Class<?> childResHandlerClass : childRes.getHandlerClasses()) {
EndpointLogger epl = new EndpointLogger(urlPattern, childResHandlerClass);
epl.populate(cleanUpPath(res.getPath() + epl.rootPath), epl.klass, false, childRes, endpointLogLines);
}
}
}
if (!endpointLogLines.isEmpty()) {
for (EndpointLogLine line : endpointLogLines) {
msg.append(line).append(NEWLINE);
}
} else {
msg.append(" NONE").append(NEWLINE);
}
return msg.toString();
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class NaiveResourceMappingContext method buildMappings.
private void buildMappings() {
if (mappings != null) {
return;
}
mappings = new HashMap<>();
erc.getResourceModel().accept(new ResourceModelVisitor() {
Deque<PathPattern> stack = new LinkedList<>();
private void processComponents(final ResourceModelComponent component) {
final List<? extends ResourceModelComponent> components = component.getComponents();
if (components != null) {
for (final ResourceModelComponent rc : components) {
rc.accept(this);
}
}
}
@Override
public void visitInvocable(final Invocable invocable) {
processComponents(invocable);
}
@Override
public void visitRuntimeResource(final RuntimeResource runtimeResource) {
processComponents(runtimeResource);
}
@Override
public void visitResourceModel(final ResourceModel resourceModel) {
processComponents(resourceModel);
}
@Override
public void visitResourceHandlerConstructor(final HandlerConstructor handlerConstructor) {
processComponents(handlerConstructor);
}
@Override
public void visitMethodHandler(final MethodHandler methodHandler) {
processComponents(methodHandler);
}
@Override
public void visitChildResource(final Resource resource) {
visitResourceIntl(resource, false);
}
@Override
public void visitResource(final Resource resource) {
visitResourceIntl(resource, true);
}
private void visitResourceIntl(final Resource resource, final boolean isRoot) {
try {
stack.addLast(resource.getPathPattern());
processComponents(resource);
if (isRoot) {
Class likelyToBeRoot = null;
for (final Class next : resource.getHandlerClasses()) {
if (!(Inflector.class.isAssignableFrom(next))) {
likelyToBeRoot = next;
}
}
if (likelyToBeRoot != null) {
mappings.put(likelyToBeRoot, getMapping(getTemplate()));
}
}
} finally {
stack.removeLast();
}
}
@Override
public void visitResourceMethod(final ResourceMethod resourceMethod) {
if (resourceMethod.isExtended()) {
return;
}
if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) {
if (resourceMethod.getInvocable() != null) {
final Invocable i = resourceMethod.getInvocable();
final Type type = i.getResponseType();
final StringBuilder template = getTemplate();
mappings.put((Class) type, getMapping(template));
// Process sub resources ?
Resource.Builder builder = Resource.builder(i.getRawResponseType());
if (builder == null) {
// for example in the case the return type of the sub resource locator is Object
builder = Resource.builder().path(resourceMethod.getParent().getPath());
}
final Resource subResource = builder.build();
visitChildResource(subResource);
}
}
processComponents(resourceMethod);
}
private StringBuilder getTemplate() {
final StringBuilder template = new StringBuilder();
for (final PathPattern pp : stack) {
final String ppTemplate = pp.getTemplate().getTemplate();
final int tlength = template.length();
if (tlength > 0) {
if (template.charAt(tlength - 1) == '/') {
if (ppTemplate.startsWith("/")) {
template.append(ppTemplate, 1, ppTemplate.length());
} else {
template.append(ppTemplate);
}
} else {
if (ppTemplate.startsWith("/")) {
template.append(ppTemplate);
} else {
template.append("/");
template.append(ppTemplate);
}
}
} else {
template.append(ppTemplate);
}
}
return template;
}
});
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class MonitoringStatisticsTest method testUrisWithProgrammaticResourcesAndExecution.
@Test
public void testUrisWithProgrammaticResourcesAndExecution() {
final MonitoringStatisticsImpl.Builder statBuilder = getProgStats();
final Resource.Builder resourceBuilder = Resource.builder();
resourceBuilder.addMethod("GET").handledBy(MyInflector.class);
resourceBuilder.addMethod("POST").handledBy(MyInflector.class);
final Resource res = resourceBuilder.build();
ResourceMethod getMethod;
ResourceMethod postMethod;
if (res.getResourceMethods().get(0).getHttpMethod().equals("GET")) {
getMethod = res.getResourceMethods().get(0);
postMethod = res.getResourceMethods().get(1);
} else {
getMethod = res.getResourceMethods().get(1);
postMethod = res.getResourceMethods().get(0);
}
statBuilder.addExecution("/new/elefant", getMethod, 10, 5, 8, 8);
statBuilder.addExecution("/new/elefant", getMethod, 20, 12, 18, 10);
statBuilder.addExecution("/new/elefant", postMethod, 30, 2, 28, 4);
final MonitoringStatisticsImpl stat = statBuilder.build();
final Iterator<Map.Entry<String, ResourceStatistics>> it = stat.getUriStatistics().entrySet().iterator();
check(it, "/hello", 2);
check(it, "/hello/world", 1);
check(it, "/new/elefant", 2);
check(it, "/prog", 1);
check(it, "/test-resource", 1);
check(it, "/test-resource/child", 3);
check(it, "/test-resource/prog-child", 1);
final Map<ResourceMethod, ResourceMethodStatistics> resourceMethodStatistics = stat.getUriStatistics().get("/new/elefant").getResourceMethodStatistics();
for (ResourceMethodStatistics methodStatistics : resourceMethodStatistics.values()) {
final ResourceMethod method = methodStatistics.getResourceMethod();
final ExecutionStatistics st = methodStatistics.getMethodStatistics();
if (method.getHttpMethod().equals("GET")) {
Assert.assertEquals(20, st.getLastStartTime().getTime());
} else if (method.getHttpMethod().equals("POST")) {
Assert.assertEquals(30, st.getLastStartTime().getTime());
} else {
Assert.fail();
}
}
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class MonitoringUtilsTest method testGetMethodUniqueId.
@Test
public void testGetMethodUniqueId() {
final Resource resource = Resource.builder(MyResource.class).build();
Assert.assertEquals("[]|[]|GET|resource|get", MonitoringUtils.getMethodUniqueId(getMethod(resource, "get")));
Assert.assertEquals("[text/html]|[]|GET|resource.sub|subGet", MonitoringUtils.getMethodUniqueId(getMethod(resource, "subGet")));
Assert.assertEquals("[text/html]|[]|GET|resource.sub|subGet", MonitoringUtils.getMethodUniqueId(getMethod(resource, "subGet")));
Assert.assertEquals("[text/xml]|[text/plain]|POST|resource|post", MonitoringUtils.getMethodUniqueId(getMethod(resource, "post")));
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class App method create.
/**
* Create example application resource configuration.
*
* @return initialized resource configuration of the example application.
*/
public static ResourceConfig create() {
final ResourceConfig resourceConfig = new ResourceConfig(TracingResource.class);
final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC);
resourceBuilder.addMethod(TRACE.NAME).handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext request) {
if (request == null) {
return Response.noContent().build();
} else {
return Response.ok(Stringifier.stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build();
}
}
});
return resourceConfig.registerResources(resourceBuilder.build());
}
Aggregations