SDN Controller Programming Guide

174
SwitchViewResource.java:
package com.hp.hm.ui.rs;
...
@Path("switches")
public class SwitchViewResource extends ControllerResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response hello() {
// SwitchService is used just to illustrate that
// services are available.
SwitchService service = get(SwitchService.class);
List<Switch> switches = service.find(null, null);
return ok("Hello from the HP SDN Controller: " +
switches.toString()).build();
}
}
Now update the web.xml placeholder added in Module Configuration on page 168 under the “UI
Module Web Application (web.xml)” listing. The Jersey Servlet [2] that handles HTTP requests and
dispatches to the right REST API based on the @Path annotations needs to be defined. Update hm-
ui/src/main/webapp/WEB-INF/web.xml file with the content from the following listing:
UI Module Web Application (web.xml) Defining Jersey Servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Health Monitor UI</display-name>
<servlet>
<servlet-name>GUI REST Services</servlet-name>
<servlet-
class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- Authentication Filter -->
<init-param>
<param-
name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.hp.util.rs.auth.AuthJerseyFilter</param-value>
</init-param>
<init-param>
<param-name>exclude-paths</param-name>
<param-value>^$</param-value>
</init-param>