Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest")...
Transcript of Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest")...
![Page 1: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/1.jpg)
Felix HTTPPaving the road to the future
and Jan Willem Janssen Marcel Offermans
SSID: FelixPassword: felixdemo
Browse to: http://10.61.0.161:8080/
![Page 2: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/2.jpg)
Jan WillemJanssen
Software architect at Luminis TechnologiesCurrently working on PulseOn and AmdatuCommitter and PMC member at Apache Felix and Apache ACE
![Page 3: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/3.jpg)
MarcelOffermans
Director at Luminis Technologies, Fellow at LuminisCurrently working on AmdatuApache Member, Committer and PMC member at Apache ACE andApache Felix
![Page 4: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/4.jpg)
AgendaModular Web ApplicationsCurrent StateAvailable ExtensionsThe New SpecificationNew extensionsFuture WorkWrap Up
![Page 5: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/5.jpg)
Modular WebApplications
![Page 6: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/6.jpg)
ModularArchitectures
High Cohesion, Low CouplingSeparation of ConcernsMaintainable CodeReusable, Composable
![Page 7: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/7.jpg)
DeploymentsCompose modules into different deploymentsLow bandwidth by just sending changed modulesFast deployments by being able to update running applications
![Page 8: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/8.jpg)
Current State
![Page 9: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/9.jpg)
ExplicitRegistration
public interface HttpService { void registerServlet(String alias, Servlet servlet, Dictionary initParams, HttpContext httpContext);
void registerResources(String alias, String name, HttpContext httpContext);
void unregister(String alias);
HttpContext createDefaultHttpContext();}
![Page 10: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/10.jpg)
Servlets httpService.registerServlet("/hello", new HttpServlet() { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) { resp.setContentType("text/plain"); resp.getWriter().write("Hello ApacheCon world!"); }}, null /* initParams */, null /* httpContext */);
invoke the /hello servlet
...
![Page 11: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/11.jpg)
ResourceshttpService.registerResources("/site", "/resources", null /* httpContext */);
$ cat resources/helloHello ApacheCon world, I'm a static resource!
request a static resource called hello
...
![Page 12: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/12.jpg)
HttpContextProvides secure access to servlets and resources.Layer of abstraction for resource loading.
public interface HttpContext { boolean handleSecurity(HttpServletRequest request, HttpServletResponse response); URL getResource(String name); String getMimeType(String name);}
![Page 13: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/13.jpg)
Web ApplicationSpecificationPart of the OSGi Enterprise Specification, chapter 128.
Web Application Bundle (WAB) are extended Java EE WAR files.They have optional JSP support.Integration with OSGi BundleContext and service registry.
![Page 14: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/14.jpg)
AvailableExtensions
![Page 15: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/15.jpg)
Whiteboarddon't call us, we'll call you!
Register your Servlet in the service registry and add a property calledalias containing its endpoint.
For more information:http://www.osgi.org/wiki/uploads/Links/whiteboard.pdf
![Page 16: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/16.jpg)
FiltersJust like Servlets, these can be registered whiteboard styleor use explicit registration:
the ExtHttpService service from Felix HTTPthe WebContainer service from PAX Web
![Page 17: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/17.jpg)
Amdatu WebResources
of the open source project (Apache Licensed).Part Amdatu.orgX-Web-Resource-Version: 1.1X-Web-Resource: /amdatu-resources;resourcesX-Web-Resource-Default-Page: index.html,/doc=javadoc.html
Include-Resource: resources=resources/basic
![Page 18: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/18.jpg)
Demorequest a static resource called /amdatu‑resources
...
![Page 19: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/19.jpg)
Amdatu WebREST
Extensive support for based on industry standards.REST endpointsJAX-RS based annotation support.Includes support for Jackson mappings.Self-documenting endpoints with Swagger.
![Page 20: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/20.jpg)
Demo@Path("/rest")@Description("Provides a demo REST endpoint")public class DemoRestEndpoint { private String m_response = "Hello World!"; @GET @Produces(MediaType.TEXT_PLAIN) @Description("Gives a plain text response") public String getPlainResponse() { return m_response; } @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Description("Allows one to set the response to return") public void setResponse( @FormParam("response") @DefaultValue("Default response") String newResponse) { m_response = newResponse; }}
![Page 21: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/21.jpg)
Self-documentingEndpoints
Swagger is a library that creates documentation for endpoints based onJAX-RS annotations plus some extras.
Go to Swagger documentation
![Page 22: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/22.jpg)
The newspecification
![Page 23: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/23.jpg)
WhiteboardNo longer an extensionNo explicit registrationSpecify HttpService to be used
![Page 24: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/24.jpg)
ExampleDictionary props = new Hashtable();props.put("osgi.http.whiteboard.servlet.pattern", "/slidemgr");props.put("osgi.http.whiteboard.target", "(http.service=demo)");context.registerService(Servlet.class.getName(), new SlideManager(), props);
![Page 25: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/25.jpg)
(A)synchronousServlets
Servlet 3.0 APISupport wildcard one or more patternsProcess work outside the servlet lifecycle
![Page 26: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/26.jpg)
Example codeclass WorkerServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) { final AsyncContext asyncContext = req.startAsync(req, resp); asyncContext.start(new DeepThought(asyncContext)); }}//class DeepThought implements Runnable { private AsyncContext m_context = // ... public void run() { // ...do some hard work... TimeUnit.DAYS.sleep(356L * 7500000L);
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); response.setStatus(SC_OK); response.getWriter().printf("42"); asyncContext.complete(); }}
![Page 27: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/27.jpg)
Example registrationDictionary props = new Hashtable();props.put("osgi.http.whiteboard.servlet.pattern", "/worker/*");props.put("osgi.http.whiteboard.servlet.asyncSupported", "true");context.registerService(Servlet.class.getName(), new WorkerServlet(), props);
![Page 28: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/28.jpg)
FiltersFull support
![Page 29: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/29.jpg)
ExampleDictionary props = new Hashtable();props.put("osgi.http.whiteboard.filter.pattern", "/*");props.put("osgi.http.whiteboard.filter.dispatcher", new String[] {"REQUEST", "INCLUDE", "FORWARD"});context.registerService(Filter.class.getName(), new SecurityFilter(), props);
![Page 30: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/30.jpg)
ListenersFull supportAll events
![Page 31: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/31.jpg)
Examplefinal CountDownLatch latch = new CountDownLatch(1);ServletContextListener contextListener = new ServletContextListener() { public void contextDestroyed(ServletContextEvent event) {} public void contextInitialized(ServletContextEvent event) { latch.countDown(); } };Dictionary props = new Hashtable();props.put("osgi.http.whiteboard.context.select", "DEFAULT");context.registerService(ServletContextListener.class.getName(), contextListener, props);
assertTrue("HttpService not ready in time?!", latch.await(5, TimeUnit.SECONDS));// continue with your itest...
![Page 32: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/32.jpg)
Custom ErrorPages
By error codeBy exception
![Page 33: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/33.jpg)
ExampleDictionary props = new Hashtable();props.put("osgi.http.whiteboard.servlet.errorPage", new String[] {"500", "java.io.IOException"});context.registerService(Servlet.class.getName(), new MyErrorHandlingServlet(), props);
![Page 34: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/34.jpg)
New extensions
![Page 35: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/35.jpg)
WebSockets“Real-time”Binary or text-basedTwo-way communication
RFC 6455
![Page 36: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/36.jpg)
Example// Client-sidevar wsConn = new WebSocket("ws://" + window.location.host + "/servlet", [ "my-protocol" ]);wsConn.onmessage = function(event) { var data = event.data; // do something with data}
// Server-side, registered at "/servlet"class MyWebSocketServlet extends WebSocketServlet { public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) { if ("my-protocol".equals(protocol)) { return new WebSocket.OnTextMessage() { public void onOpen(Connection conn) {} public void onClose(int code, String reason) {} public void onMessage(String data) {} }; } return null; }}
![Page 38: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/38.jpg)
SPDYLet's make the web faster
Session layer on top of SSLReduce bandwidth & lower latencyPush multiple resources in a requestBasis of HTTP 2.0
![Page 39: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/39.jpg)
Application
Session
Presentation
Transport
How SPDY fits in the OSI layer model.
![Page 40: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/40.jpg)
SPDY vs WebSocketsSPDY WebSockets
Goal optimize HTTP 2-way communicationUpgradeability transparent needs works
Secure? ✔ (mandatory) ✔ (if needed)Two-way? ✔ / ✘ ✔
Multiplexed ✔ ✘
Prioritized ✔ ✘
![Page 42: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/42.jpg)
Future Work
![Page 43: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/43.jpg)
Finalize support for new HttpService specificationUpgrade to Jetty 9Allow “new style” WebSockets (JSR 356) to be usedImproved support for SPDY
![Page 44: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/44.jpg)
Wrap Up
![Page 45: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/45.jpg)
New/updated specificationsNew features, functionality & improvementsAvailable extensionsBuild modular web applications
![Page 46: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/46.jpg)
Questions?
![Page 47: Paving the road to the future - events.static.linuxfound.org...Demo @Path("/rest") @Description("Provides a demo REST endpoint") public class DemoRestEndpoint ... Swagger is a library](https://reader035.fdocuments.us/reader035/viewer/2022081616/5fe5f40124c78a241814418b/html5/thumbnails/47.jpg)
Linksfelix.apache.orgluminis-technologies.combndtools.orgamdatu.orgbitbucket.org/marrs/apachecon2014-felix-http