java ee 8 - what’s new on the web front
TRANSCRIPT
Java EE 8 What’s new on the Web front
David Delabassee -‐ @delabassee Oracle
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
1
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Safe Harbor Statement
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Agenda
Context
Web Front update
Call to Action
1
2
3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Agenda
Context
Web Front update
1
2
Context
Web Front update
Call to Action 3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Java EE 8
Clou
d
Mobile
HTTP/2
SECURITYReactive Programming
User Experience
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Community-‐Prioritized Features
http://glassfish.org/survey
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Java EE 8 Themes
• HTML5 / Web Tier Enhancements • Ease of Development • Infrastructure for running in the Cloud
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Agenda
Context
Web Front update
1
2
3
Context
Web Front update
Call to Action
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSRs Updated
• Servlet • JAX-‐RS • JavaServer Faces • JSON-‐P
New • JSON-‐B • MVC
10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON• Java API for JSON Processing
• Process JSON
• Java API for JSON Binding • Unmarshall JSON to Java
• Marshall Java to JSON
11
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1Java API for JSON Processing
• Keep JSON-‐P spec up-‐to-‐date • Track new standards • Add editing operations to JsonObject and JsonArray • Java SE 8 • JSON Big Data
12
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1
• String syntax for referencing a JSON value "/0/phone/mobile"
JSON-‐Pointer – IETF RFC 6901
13
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1JsonArray contacts = … JsonPointer pointer = new JsonPointer("/0/name"); JsonValue value = pointer.getValue(contacts);
[ { "name":"Duke", "gender":"M", "phones":{ "home":"650-‐123-‐4567", "mobile":"650-‐234-‐5678"}}, { "name":"Jane", "gender":"F", "phones":{ "mobile":"707-‐555-‐9999"}} ]
14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1
• String syntax for referencing a JSON value “/0/name"
• Methods – getValue() – JSON operations: add(), replace(), remove()
JSON-‐Pointer – IETF RFC 6901
JsonPointer pointer = new JsonPointer("/0/name"); JsonArray result = pointer.replace(contacts, Json.createValue("Alex"));
15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1
• Patch is a JSON document – Array of objects / operations for modifying a JSON document – add, replace, remove, move, copy, test
JSON-‐Patch – IETF RFC 6902
[ {"op":"replace","path":"/0/phones/mobile","value":"650-‐111-‐222"}, {"op":"remove","path":"/1"} ]
16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1[ { "op":"replace", "path":"/0/phones/mobile", "value":"650-‐111-‐2222"}, { "op":"remove", "path":"/1"} ]
[ { "name":"Duke", "gender":"M", "phones":{ "home":"650-‐123-‐4567", "mobile":"650-‐234-‐5678"}}, { "name":"Jane", "gender":"F", "phones":{ "mobile":"707-‐555-‐9999"}} ]
17
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1[ { "op":"replace", "path":"/0/phones/mobile", "value":"650-‐111-‐2222"}, { "op":"remove", "path":"/1"} ]
[ { "name":"Duke", "gender":"M", "phones":{ "home":"650-‐123-‐4567", "mobile":"650-‐111-‐2222"}}, { "name":"Jane", "gender":"F", "phones":{ "mobile":"707-‐555-‐9999"}} ]
18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1[ { "op":"replace", "path":"/0/phones/mobile", "value":"650-‐111-‐2222"}, { "op":"remove", "path":"/1"} ]
[ { "name":"Duke", "gender":"M", "phones":{ "home":"650-‐123-‐4567", "mobile":"650-‐111-‐2222"}} ]
19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JsonArray target = …; JsonArray patch = …; JsonPatch jsonPatch = new JsonPatch(patch); JsonArray result = jsonPatch.apply(target);
JSON-‐P 1.1
JsonPatchBuilder builder = new JsonPatchBuilder(); JsonArray result = builder.add("/Joe/phones/office", "1234-‐567") .remove("/Amy/age") .apply(contacts);
20
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1JSON-‐Merge Patch – IETF RFC 7386
Orignal Patch Result
{"a":"b"} {"a":"c"} {"a":"c"}
{"a":"b"} {"b":"c"} {"a":"b", "b":"c"} {"a":"b"} {"a":null} { }
{"a":"b", {"a":null} {"b":"c"} "b":"c"}
{ } {"a": {"a": {"bb": {"bb": {"ccc": {}}} null}}}
21
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1 { "title": "Hello!", "author": { "familyName": null }, "tags": [ "example" ], "phoneNumber": "+01-‐123-‐456-‐7890" }
{ "title": "Goodbye!", "author": { "givenName": "John", "familyName": "Doe" }, "tags": [ "example", "sample" ], "content": "This will be unchanged" }
{ "title": "Hello!", "author": { "givenName": "John" }, "tags": [ "example" ], "content": "This will be unchanged", "phoneNumber": "+01-‐123-‐456-‐7890" }
JSON-‐Merge Patch – IETF RFC 7386
22
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JsonObject contact = …; // The target to be patched JsonObject patch = …; JsonValue output = JsonMergePatch.mergePatch(target, patch);
JSON-‐P 1.1JSON-‐Merge Patch – IETF RFC 7386
23
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1
• Reverse operation – Given two objects returns the patch to apply (diff)
JSON-‐Patch & JSON-‐Merge Patch Diff
JsonValue output = JsonMergePatch.diff(original, target); JsonArray diff = JsonPatch.diff(original, target);
24
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1JSON Query using Lambda Operations
JsonArray cars = ...; List<String> audis = cars.getValuesAs(JsonObject.class).stream() .filter(x-‐>"Audi".equals(x.getString("brand"))) .map(x-‐>(x.getString("serial")) .collect(Collectors.toList());
25
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1JSON Query collecting results in JsonArray
JsonArray cars = ...; JsonArray audis = cars.getValuesAs(JsonObject.class).stream() .filter(x-‐>"Audi".equals(x.getString("brand"))) .map(x-‐>(x.getString("serial")) .collect(JsonCollectors.toJsonArray());
26
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐P 1.1
• Keep JSON-‐P spec up-‐to-‐date • Track new standards • Add editing operations to JsonObject and JsonArray • Java SE 8 • JSON Big Data
JSR 374
27
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐B
• API to marshal/unmarshal Java objects to/from JSON – Similar to JAXB runtime API in XML world
• Default mapping of classes to JSON – Annotations to customize the default mappings – JsonProperty, JsonTransient, JsonNillable, JsonValue, …
Java API for JSON Binding
28
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐B
• Draw from best practices of existing JSON binding implementations – Jackson, Genson, EclipseLink MOXy, Fleece, JSON-‐lib, Gson, Flexjson, Json-‐io, JSONiJ, Johnzon, Xstream, etc.
• Switch JSON binding providers • Implementations compete on common ground
Standard API
29
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JSON-‐B// from default JsonbBuilder instance of the default JsonbProviderJsonb jsonb = JsonbBuilder.create();
JsonbProvider myProvider = JsonbProvider.provider("foo.bar.ProviderImpl");Jsonb jsonb = JsonbBuilder.newBuilder(myProvider);
// UnMarshallBook book = jsonb.fromJson(new File("myBook.json"), Book.class);
// Marshalljsonb.toJson(myObject, new File(“foo.json”));jsonb.toJson(myObject, new PrintWriter(System.out));
30
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Java API for RESTful Web Services
31
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Server-‐sent Events
• Part of HTML5 standardization • Server-‐to-‐client streaming of text data • Media type: “text/event-‐stream” • Long-‐lived HTTP connection
– Client establishes connection – Server pushes update notifications to client
32
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Server-‐sent Events
• Servlet, WebSocket, JAX-‐RS or standalone API? • JAX-‐RS deemed most natural fit
– Streaming HTTP resources already supported – Small extension
• Server API: new media type; EventOutput • Client API: new handler for server side events
– Convenience of mixing with other HTTP operations; new media type – Jersey already supports SSE
33
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Server-‐sent Events
@Path("tickers") public class StockTicker { … @Get @Produces("text/event-‐stream") public EventOutput getQuotes() { EventOutput eo = new EventOutput(); new StockThread(eo).start() return eo; } }
JAX-‐RS resource class
34
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JAX-‐RS StockThread class
class StockThread extends Thread {
private EventOutput eo; … @Override public void run() {
try { …
eo.send(new StockQuote("...")); } catch (IOException e) { … } } }
Server-‐sent Events
35
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
WebTarget target = client.target("http://example.com/tickers");
EventSource eventSource = new EventSource(target) {
@Override
public void onEvent(InboundEvent inboundEvent) {
StockQuote sq = inboundEvent.readData(StockQuote.class);
// ...
}
};
eventSource.open();
JAX-‐RS Client
Server-‐sent Events
36
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JAX-‐RS 2.1
37
• Server-‐sent Events • Improving integration with CDI • JSON-‐B Integration • Building upon the hypermedia API • NIO support in providers (filters, interceptors, …) • Reactive API • JAX-‐RS resource classes as MVC controllers • …
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HTTP 1.1 circa 1999
• HoLB • HTTP uses TCP poorly
- HTTP flows are short and bursty
- TCP was built for long-‐lived flows
• Solutions - File concatenations, Assets Inlining, Domain sharding, etc.
Problems Vs Solutions
39
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HTTP/2
• HTTP/2 – Hypertext Transfer Protocol version 2 -‐ RFC 7540
– HPACK -‐ Header Compression for HTTP/2 -‐ RFC 7541
• Reduce latency • Address the HOL blocking problem • Support parallelism • Retain semantics of HTTP 1.1 • Define interaction with HTTP 1.x
Address the Limitations of HTTP 1.x
40
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HTTP/2
• One TCP Connection • Request -‐> Stream
–Multiplexed – Prioritised
• Binary Framing Layer – Prioritisation – Flow Control – Server Push
• Header Compression
Multiplexed Binary Frames POST /upload HTTP/1.1 Host: www.test.com Content-‐Type: application/json Content-‐Length: 15
{“name”:“duke”}
HTTP 1.1 HTTP/2
HEADERS frame
DATA frame
41
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Stream Prioritization
• Stream Dependency in HEADERS Frame • PRIORITY frame type • An additional 40 bytes
- Stream id (31) - Weight (8): [1, 256] - Exclusive bit (1)
• Only an advice
A
B C
4 12
A
B CD
4 16 12
exclusive = 0
A
B C
D
4 12
16
exclusive = 1
42
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HTTP/2Server Push
/index.html : stream 1 /style.css : stream 2 /logo.png : stream 4
Client Server
stream 1 HEADERS
stream 1 FRAME x
stream 2 PROMISE
stream 4 PROMISE
43
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Servlet 4.0
• Request/response multiplexing – Servlet Request as HTTP/2 message
• Stream prioritization – Add stream priority to HttpServletRequest
• Server push • Binary framing • Upgrade from HTTP 1.1
HTTP/2 Features in Servlet API
45
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Push
PushBuilder builder = baseRequest.getPushBuilder();
builder.addHeader(“X-Pusher", …);
builder.path(aResource) .etag(associated._etag) .lastModified(associated._lastModified) .push();
46
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Stream Prioritization
47
• New Priority class – boolean exclusive – int streamId – int weight
• New methods to HttpServletRequest – int getStreamId() – Priority getPriority()
• New methods to HttpServletResponse – int getStreamId() – Priority getPriority() – void setPriority(Priority p)
• Dependency?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Model-‐View-‐Controller
53
• Pattern used to implement a User Interface • Consists of 3 major components
–Model – View – Controller
• Each of the components has a distinct responsibility
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Component-‐based MVC• A specific style of MVC made popular by component frameworks • Controller provided by the framework • Examples
– JavaServer Faces –Wicket – Tapestry – Seam (discontinued) – Apache Click (retired)
54
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Action-‐based MVC• Controller(s) defined by the application • Examples
– Struts 1 (end of life), Struts 2 – Spring MVC
• No standard Java EE implementation – Good news we are creating one! – Targeted for inclusion in Java EE 8
55
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Model-‐View-‐Controller (MVC 1.0) Specification
• “Action-‐based” MVC 1.0 JSR • Why?
– UI landscape is not one size fits all – Java EE 8 Community Survey – Talking to our customers, etc.
JSR 371
56
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
MVC 1.0
• Action-‐based MVC • Glues together key Java EE technologies
–Model : CDI, Bean Validation, JPA – View : Facelets, JSP, more? – Controller : Invent new technology Vs. Leverage existing technologies
57
Principles
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Controller• Combine data models and views to produce web application pages
@Path("hello") public class HelloController {
@GET@Controllerpublic String hello() {
// do somethingreturn "hello.jsp";
} }
58
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Controller• Class/method decorated with @Controller
@Path(“hello") @Controller public class HelloController {
@GET@View(“hello.jsp")public void hello() {
// do business stuff }
}
59
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Controller
@Path(“hello") @Controller public class HelloController {
@GETpublic Viewable hello() {
// some other stuff return new Viewable("hello.jsp");
} }
60
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Controller
… @GET @Controller public Response getById(@PathParam("id") String id) { if (id.length() == 0) { return Response.status(Response.Status.BAD_REQUEST) .entity(“error.jsp") .build(); } }
61
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Model• “Refers to the data” • CDI @Named bean • javax.mvc.Models interface
62
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Model @Named(“greeting”) @RequestScoped public class Greeting {
private String message;
public String getMessage() { return message;
} public void setMessage(String message) { this.message = message; }}
63
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Model
@Path(“hello”) public class HelloController {
@Inject private Models models;
@GET @Controller public String hello() { models.set(“greeting”, new Greeting(“Salut”)); return “hello.jsp”; } }
64
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
View• Define the structure of the output • Can refer to model(s) • Rendered by a View Engine
– JSP – Facelets
• Not accessible as static resources – /WEB-‐INF/views/
65
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
View
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %> <html> <head> <title>Home</title> </head> <body> <p>${greeting.message}</p> </body> </html>
66
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
View
<html> <head> <title>FreeMarker</title> </head> <body> <p>Hello ${user}</p> … </body> </html>
67
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
View Engine• CDI based extension mechanism • javax.mvc.engine.ViewEngine interface
boolean supports(String view);
void processView(ViewEngineContext context)
throws ViewEngineException;
• Ozark – JSP & Facelets – FreeMarker, Velocity, Thymeleaf, Mustache, Handlebars, Jade, AsciiDoc, …
68
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Miscellaneous• Bootstrap via javax.ws.rs.core.Application • Validation • Exception Mapping Providers • New @RedirectScoped scope • Security • @Produces • CDI Events…
69
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
MVC• New annotations
–@Controller, @View, @csrf, @RedirectScoped
• Doing MVC now is not a bad idea – Leveraging CDI, JAX-‐RS, Facelets, BV –More? Servlet 4.0’s ServerPush – Delivering an easy migration path if you are using JSPs
• Offers more choices
70
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
JavaServer Faces
71
• CDI Alignment –@Inject FacesContext, ExternalContext, etc.
– CDI managed versions of Validator and Converter
– Rely on CDI for EL resolving
– Invoking CDI managed bean methods directly from Ajax, etc.
• “Adjustments” for MVC • Misc.
–Multi-‐field validation, etc.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Agenda
Context
Web Front update
1
2
72
3
Context
Web Front update
Call to Action
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Java EE 8
• Java EE 8 Platform (JSR 366) • CDI 2.0 (JSR 365) • JSON Binding 1.0 (JSR 367) • JMS 2.1 (JSR 368) • Servlet 4.0 (JSR 369) • JAX-‐RS 2.1 (JSR 370)
• MVC 1.0 (JSR 371) • JSF 2.3 (JSR 372) • Java EE Management 2.0 (JSR 373) • JSON-‐P 1.1 (JSR 374) • Java EE Security 1.0 (JSR 375)
So far…..
73
Early Draft Review
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Roadmap• Q4 2015 Early Draft • Q1 2016 Public Review • Q3 2016 Proposed Final Draft • H1 2017 Final Release
74
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Contribute!• Join the JCP • “Adopt-‐A-‐JSR”
– http://glassfish.java.net/adoptajsr
• Get involved – For any Spec, join users mailing list – Test SNAPSHOT and milestone builds of RIs – Tweet, blog, socialize to raise awareness – Fill issues!
• The Aquarium Blog – https://blogs.oracle.com/theaquarium/
75