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());
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class WadlGeneratorResourceDocSupportTest method wadlIsGeneratedWithUnknownCustomParameterAnnotation.
@Test
public void wadlIsGeneratedWithUnknownCustomParameterAnnotation() throws JAXBException {
/* Set up a ClassDocType that has something for a custom-annotated parameter */
ClassDocType cdt = new ClassDocType();
cdt.setClassName(TestResource.class.getName());
MethodDocType mdt = new MethodDocType();
mdt.setMethodName("method");
cdt.getMethodDocs().add(mdt);
ParamDocType pdt = new ParamDocType("x", "comment about x");
mdt.getParamDocs().add(pdt);
AnnotationDocType adt = new AnnotationDocType();
adt.setAnnotationTypeName(CustomParam.class.getName());
adt.getAttributeDocs().add(new NamedValueType("value", "x"));
pdt.getAnnotationDocs().add(adt);
ResourceDocType rdt = new ResourceDocType();
rdt.getDocs().add(cdt);
/* Generate WADL for that class */
WadlGenerator wg = new WadlGeneratorResourceDocSupport(new WadlGeneratorImpl(), rdt);
WadlBuilder wb = new WadlBuilder(wg, false, null);
Resource resource = Resource.from(TestResource.class);
ApplicationDescription app = wb.generate(Collections.singletonList(resource));
/* Confirm that it can be marshalled without error */
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Application.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(app.getApplication(), sw);
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class ChildResourceTest method createApplication.
private ApplicationHandler createApplication() {
final Resource.Builder rootBuilder = Resource.builder("root");
rootBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {
@Override
public String apply(ContainerRequestContext requestContext) {
return "root-get";
}
});
rootBuilder.addChildResource("child").addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {
@Override
public String apply(ContainerRequestContext requestContext) {
return "sub-get";
}
}).build();
Resource.Builder anotherChildBuilder = Resource.builder("another-child");
anotherChildBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {
@Override
public String apply(ContainerRequestContext requestContext) {
return "another-child-get";
}
});
rootBuilder.addChildResource(anotherChildBuilder.build());
Resource resource = rootBuilder.build();
ResourceConfig resourceConfig = new ResourceConfig().registerResources(resource);
return new ApplicationHandler(resourceConfig);
}
use of org.glassfish.jersey.server.model.Resource in project jersey by jersey.
the class App method createProgrammaticClipboardApp.
/**
* Create example application resource configuration.
*
* @return initialized resource configuration of the example application.
*/
public static ResourceConfig createProgrammaticClipboardApp() {
final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH);
final Clipboard clipboard = new Clipboard();
resourceBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
final String content = clipboard.content();
if (content.isEmpty()) {
return Response.noContent().build();
}
return Response.ok(content).build();
}
});
resourceBuilder.addMethod("PUT").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
if (data != null) {
clipboard.setContent(((ContainerRequest) data).readEntity(String.class));
}
return Response.noContent().build();
}
});
resourceBuilder.addMethod("POST").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
String newContent = (data != null) ? clipboard.append(((ContainerRequest) data).readEntity(String.class)) : "";
return Response.ok(newContent).build();
}
});
resourceBuilder.addMethod("DELETE").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(ContainerRequestContext data) {
clipboard.clear();
return Response.noContent().build();
}
});
return new ResourceConfig().registerResources(resourceBuilder.build());
}
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;
}
});
}
Aggregations