45
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 1 今から始めるJava EE 7 セミナー WebSocket アプリケーショ ン開発 (JSR 356) 日本オラクル株式会社 Fusion Middleware事業統括本部 Cloud Application Foundation 松林

今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 1

今から始めるJava EE 7 セミナー

WebSocket アプリケーション開発 (JSR 356)

日本オラクル株式会社

Fusion Middleware事業統括本部

Cloud Application Foundation

松林 晶

Page 2: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 2

以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。

Oracleは、米国オラクルコーポレーション及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。

Page 3: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 3

Agenda

Primer on WebSocket

JSR 356: Java API for WebSocket

Page 4: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 4

Interactive Web Sites

HTTP is half-duplex

HTTP is verbose

Hacks for Server Push

– Polling

– Long Polling

– Comet/Ajax

Complex, Inefficient, Wasteful

Page 5: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 5

WebSocket to the Rescue

TCP based, bi-directional, full-duplex messaging

Originally proposed as part of HTML5

IETF-defined Protocol: RFC 6455

– Handshake

– Data Transfer

W3C defined JavaScript API

– Candidate Recommendation

Page 6: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 6

What’s the basic idea ?

Upgrade HTTP to upgrade to WebSocket

– Single TCP connection

– Transparent to proxies, firewalls, and routers

Send data frames in both direction (Bi-directional)

– No headers, cookies, authentication

– No security overhead

– “ping”/”pong” frames for keep-alive

Send message independent of each other (Full Duplex)

End the connection

Page 7: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 7

Establish a connection

Client

Handshake Request

Handshake Response

Server

Page 8: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 8

Handshake Request

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13

Page 9: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 9

Handshake Response

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat

Page 10: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 10

Server Client

Handshake Request

Handshake Response

Connected !

Establishing a Connection

Page 11: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 11

Peer

(server)

Peer

(client)

Connected !

open open

close

message

error

message

message message

message

Disconnected

WebSocket Lifecycle

Page 12: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 12

WebSocket API www.w3.org/TR/websockets/

Page 13: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 13 http://caniuse.com/websockets

Browser Support

Page 14: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 14

REST vs WebSocket

0

10000

20000

30000

40000

50000

60000

10 of 1 100 of 10

1000 of 100 5000 of 1000

REST WebSocket 10 messages of 1 byte

Page 15: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 15

Page 16: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 16

JSR 356 Specification

Standard Java API for creating WebSocket Applications

Transparent Expert Group

– jcp.org/en/jsr/detail?id=356

– java.net/projects/websocket-spec

FINAL: Part of Java EE 7

Page 17: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 17

JSR 356: Reference Implementation

Tyrus: java.net/projects/tyrus

GlassFish Server Open Source Edition 4.0 Download

– http://glassfish.java.net/download.html

Page 18: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 18

Java API for WebSocket Features

API for WebSocket Server and Client Endpoint

– Annotated: @ServerEndpoint, @ClientEndpoint

– Programmatic: Endpoint

WebSocket opening handshake negotiation

Integration with Java EE Web container

Page 19: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 19

Touring the APIs

Page 20: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 20

Hello World and Basics POJO

Page 21: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 21

Hello World

import javax.websocket.*; @ServerEndpoint("/hello") public class HelloBean { @OnMessage public String sayHello(String name) { return “Hello “ + name; } }

Page 22: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 22

Annotations

Annotation Level Purpose

@ServerEndpoint class Turns a POJO into a Server Endpoint

@ClientEndpoint class Turns a POJO into a Client Endpoint

@OnMessage method Intercepts WebSocket Message events

@PathParam method

parameter Flags a matched path segment of a URI-template

@OnOpen method Intercepts WebSocket Open events

@OnClose method Intercepts WebSocket Close events

@OnError method Intercepts errors during a conversation

Page 23: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 23

@ServerEndpoint attributes

value Relative URI or URI template

e.g. “/hello” or “/chat/{subscriber-level}”

decoders list of message decoder classnames

encoders list of message encoder classnames

subprotocols list of the names of the supported subprotocols

Page 24: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 24

Custom Payloads

@ServerEndpoint( value="/hello", decoders={MyMessageDecoder.class}, encoders={MyMessageEncoder.class} ) public class MyEndpoint { . . . }

Page 25: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 25

Custom Payloads – Text Decoder

public class MyMessageDecoder implements Decoder.Text<MyMessage> {

public MyMessage decode(String s) { JsonObject jsonObject = Json.createReader(…).readObject(); return new MyMessage(jsonObject); }

public boolean willDecode(String string) { . . . return true; // Only if can process the payload }

. . . }

Page 26: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 26

Custom Payloads – Text Encoder

public class MyMessageEncoder implements Encoder.Text<MyMessage> {

public String encode(MyMessage myMessage) { return myMessage.jsonObject.toString(); }

. . . }

Page 27: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 27

Custom Payloads – Binary Decoder

public class MyMessageDecoder implements Decoder.Binary<MyMessage> { public MyMessage decode(byte[] bytes) { . . . return myMessage;

}

public boolean willDecode(byte[] bytes) { . . . return true; // Only if can process the payload }

. . . }

Page 28: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 28

Which methods can be @OnMessage ?

Exactly one of the following

– Text: String, Java primitive or equivalent class, String and boolean,

Reader, any type for which there is a decoder

– Binary: byte[], ByteBuffer, byte[] and boolean, ByteBuffer and

boolean, InptuStream, any type for which there is a decoder

– Pong messages: PongMessage

An optional Session parameter

0..n String parameters annotated with @PathParam

Return type: String, byte[], ByteBuffer, Java primitive or class

equivalent or any type for which there is a encoder

Page 29: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 29

Sample Messages

void m(String s);

void m(Float f, @PathParam(“id”)int id);

Product m(Reader reader, Session s);

void m(byte[] b); or void m(ByteBuffer b);

Book m(int i, Session s, @PathParam(“isbn”)String isbn, @PathParam(“store”)String store);

Page 30: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 30

Chat Server

@ServerEndpoint("/chat")

public class ChatBean {

static Set<Session> peers = Collections.synchronizedSet(…); @OnOpen public void onOpen(Session peer) { peers.add(peer); } @OnClose public void onClose(Session peer) { peers.remove(peer); } . . .

Page 31: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 31

Chat Server

. . . @OnMessage

public void message(String message, Session client) {

for (Session peer : peers) { peer.getBasicRemote().sendObject(message); } } }

Page 32: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 32

URI Template Matching

Level 1 only

@ServerEndpoint(“/orders/{order-id}”) public class MyEndpoint { @OnMessage public void processOrder( @PathParam(“order-id”)String orderId) { . . . } }

Page 33: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 33

WebSocket Client

@ClientEndpoint public class HelloClient { @OnMessage public void message(String message, Session session) { // process message from server } }

WebSocketContainer c = ContainerProvider.getWebSocketContainer(); c.connectToServer(HelloClient.class, “hello”);

Page 34: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 34

Packaging – Java EE Style

Client side

Classes + resources packaged as a JAR

Web Container

Only WAR packaging

Classes + resources packaged in WEB-INF/classes or WEB-INF/lib

Page 35: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 35

Hello World and Basics Non-POJO

Page 36: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 36

Interface-driven Endpoint public class MyEndpoint extends Endpoint { @Override public void onOpen(Session session) { session.addMessageHandler(new MessageHandler.Text() { public void onMessage(String name) { try { session.getBasicRemote().sendText(“Hello “ + name); } catch (IOException ex) { } } }); } }

Page 37: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 37

Interface-driven Endpoint

ServerEndpointConfiguration config = ServerEndpointConfigurationBuilder .create(MyEndpoint.class, “/foo”) .build();

Server Packaging

Page 38: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 38

Server and Client Configuration

Server

– URI matching algorithm

– Subprotocol and extension negotiation

– Message encoders and decoders

– Origin check

– Handshake response

Client

– Requested subprotocols and extensions

– Message encoders and decoders

– Request URI

Page 39: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 39

Relationship with Dependency Injection

Full Dependency Injection support required in endpoints

– Field, method, constructor injection

Interceptors permitted too

Page 40: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 40

Relationship with Servlet 3.1

Allows a portable way to upgrade HTTP request

New API

– HttpServletRequest.upgrade(ProtocolHandler handler)

Page 41: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 41

Security

Authenticates using Servlet security mechanism during opening

handshake

– Endpoint mapped by ws:// is protected using security model defined using

the corresponding http:// URI

Authorization defined using <security-constraint>

– TBD: Add/reuse security annotations

Transport Confidentiality using wss://

– Access allowed over encrypted connection only

Page 42: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 42

How to view WebSocket messages ? Capture traffic on loopback

Page 43: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 43

How to view WebSocket messages ? chrome://net-internals -> Sockets -> View live sockets

Page 44: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 44

Resources

Specification

– JSR: jcp.org/en/jsr/detail?id=356

– Mailing Lists, JIRA, Archive: java.net/projects/websocket-spec

– FINAL: Part of Java EE 7

Reference Implementation

– Tyrus: java.net/projects/tyrus

– Integrated in GlassFish 4 builds

Page 45: 今から始めるJava EE 7 セミナー WebSocket アプリ …...2013/08/22  · Java API for WebSocket Features API for WebSocket Server and Client Endpoint –Annotated: @ServerEndpoint,

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 45