119
#devoxxfr #ngjava @sebastienpertus @agoncal Enterprise Java MicroProfile TypeScript and Angular Sebastien Pertus Antonio Goncalves

When Enterprise Java Micro Profile meets Angular

Embed Size (px)

Citation preview

Page 1: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Enterprise Java MicroProfile

TypeScript and Angular

Sebastien Pertus

Antonio Goncalves

Page 2: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Agenda

• Enterprise Java MicroProfile

• TypeScript

• Docker

• Angular 2

• Break

• Exposing & consuming REST APIs with Angular 2

• Tips and tricks:

• APIs, Swagger, Cors, Hateoas, Etag, JWT, Scaling

Ask questions

Page 3: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Sebastien Pertus

• Microsoft Technical Evangelist

• OSS Lover

• Full Stack developer

• Node.JS & .Net Core advocate

• SQL Server man

• Develops on Windows (yeah yeah)

Page 4: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Antonio Goncalves

• Java Champion

• Loves back-end

• Hates front-end

• Develops on Mac

Page 5: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 6: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 7: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

The Conference App

Page 8: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

The Conference App

https://github.com/agoncal/agoncal-

application-conference

Page 9: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Optimizing Enterprise Javafor a Microservices Architecture

Page 10: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Fundamental Shifts in Computing

Cloud

Microservices

Reduce time to market

Address unpredictable loads

Pay as you go

Containerization

Deliver new features more quickly

Smaller, more agile teams

Deliver business features as discrete services

Scale services independently

Page 11: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

• Began as a of independent discussions

• Many “microservices” efforts exist in Java EE

• WildFly Swarm

• WebSphere Liberty

• Payara

• TomEE

• New features to address microservices architectures

• Java EE already being used for microservices…

• ...but we can do better

MicroProfile Background

Page 12: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Release Schedule

Sep 2016

MicroProfile

1.0

Q4 2016 2017 2017

Move to

Foundation

MicroProfile

1.1

MicroProfile

1.2

JAX-RS

CDI

JSON-P

Page 13: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

MicroProfile 1.1 Underway

Security: JWT Token Exchange

Health Check

Configuration

Fault Tolerance

Second Quarter

2017!

Page 14: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 15: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

WildFly Swarm

• Based on good old JBoss AS

• « Rightsize your services »

• Bundles several fractions

• Java EE

• Netflix OSS (Ribbon, Hystrix, RxJava)

• Spring

• Logstash

• Swagger

• ...

Page 16: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Setting up Swarm and MicroProfile

<profile>

<id>swarm</id>

<dependencies><dependency>

<groupId>org.wildfly.swarm</groupId>

<artifactId>microprofile</artifactId>

</dependency></dependencies>

<build>

<plugins><plugin>

<groupId>org.wildfly.swarm</groupId>

<artifactId>wildfly-swarm-plugin</artifactId>

</plugin></plugins>

</build>

</profile>

Page 17: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Skinny War

Fat Jar

Page 18: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

TypeScript:

JavaScript that

Scales

Page 19: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

BRENDAN EICH ANDERS HEJLSBERG

Do you know those guys ?

Javascript creator

CTO / CEO Mozilla Foundation

Brave Software CEO

C# creator

Technical Fellow @ Microsoft

TypeScript creator and lead team

Page 20: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 21: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

ES6 / EcmaScript 2015 / ES2015

ES6 is the most

important update

of JavaScript

Brendan Eich : - “We want to go faster. “

- “Every year, a new spec that will be shipped in nighty versions of moderns browsers”

Page 22: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Ecmascript evolution

ES 8

ES 7 (ES 2016)

ES 6(ES 2015)

ES 5

ES 3Core features

1997 ~~ 1999

new functions

strict mode, json

2009

class, promises, generators, arrow

functions, new syntax and

concepts …

2015

Exponential (**), array.includes,

2016

Page 23: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Rise of the transpilers: Typescript 2.0

Page 24: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

TRANSPILER COMPILER

Transpiler vs Compiler

is a specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction

TypeScript (subclass of JavaScript) to JavaScript

is the general term for taking source code written in one language and transforming into another

C# to IL

Java to ByteCode

"CoffeeScript is to Ruby as TypeScript is to Java/C#/C++." - Luke Hoban

Reference : Steve Fenton – 2012 - https://www.stevefenton.co.uk/2012/11/compiling-vs-transpiling

Page 25: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

A statically typed superset of

JavaScript

that compiles to plain JavaScript.

Oh wait … that transpiles

Page 26: When Enterprise Java Micro Profile meets Angular

B R O W S E R

H O S T

O S

Page 27: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Open Source

Page 28: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 29: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

The feature gap

Page 30: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

TypeScript IDE

Page 31: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

One year, four releases

1.5

1.6

1.7

1.8

Page 32: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

TypeScript 2.0

• Control flow based type analysis

• Non-nullable types

• Async/await downlevel support

• Readonly properties

• Private and protected Constructor

• Type “never”

Page 33: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

TypeScript 2.1, 2.2

• New JS language service in Visual Studio

• Better and more refactoring support

• Extensions methods

• Mixin classes

• Better .jsx react native support

Page 34: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Nullable types

number

stringboolean

Page 35: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Non-nullable types

number

stringboolean

Page 36: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Non-nullable types

number

stringboolean

undefined null

Page 37: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Non-nullable typesstring

undefined null

string | null | undefined

Page 38: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Non Nullables Types

Control Flow

Async / await

Page 39: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 40: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Docker

• « Build, Ship, Run »

• Containers, containers, containers

• Docker to run containers

• Docker-compose to compose several containers

• WildFly containers

• JRE container

Page 41: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Dockerfile Skinny War

FROM jboss/wildfly:10.1.0.Final

EXPOSE 8080

# Setting the Wildfly Admin console (user/pwd admin/admin)

RUN $JBOSS_HOME/bin/add-user.sh admin admin --silent

CMD $JBOSS_HOME/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0

COPY venue.war $JBOSS_HOME/standalone/deployments/

Page 42: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Dockerfile Far Jar

FROM openjdk:8-jre-alpine

EXPOSE 8080

COPY venue-swarm.jar /opt/venue-swarm.jar

ENTRYPOINT ["java", "-jar", "/opt/venue-swarm.jar"]

Page 43: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Dockerfile Angular Distribution

FROM nginx

EXPOSE 80

COPY ./dist /usr/share/nginx/html

Page 44: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Skinny War Image

Fat Jar Image

Page 45: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 46: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 47: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular : In a nutshell

Modules

Components

Services

Page 48: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular Component

Page 49: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular Module

Page 50: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular Dependency Injection

Page 51: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular & Webpack

Page 52: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

ES7 THEN ES8 PROPOSAL ALREADY IMPLEMENTED IN TS

Decorators

Pattern that allow us to extend /

modify the behavior of a class /

function / propery

As you can see ….

It’s used A LOT in Angular 2

Page 53: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Decorators

class Person {

public lastName: string;

public firstName: string;

constructor(ln: string, fn: string) {

this.lastName = ln;

this.firstName = fn;

}

@log(false)

public getFullName(fnFirst: boolean = true) {

if (fnFirst)

return this.firstName + " " + this.lastName;

else

return this.lastName + " " + this.firstName;

}

}

Page 54: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Decorators

function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {

var desc = {value: function (...args: any[]) {

// get the paramsvar params = args.map(arg => JSON.stringify(arg)).join();

// get the resultvar result = descriptor.value.apply(this, args);var resultString = JSON.stringify(result);

console.log(`function ${propertyKey} invoked. Params: ${params}. Result: ${resultString}`);

return result;}

}return desc;

}

Page 55: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Angular CLI

Angular Command Line Interface

Page 56: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Angular CLI

- Component

- Service

Page 57: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Agenda

• Enterprise Java MicroProfile

• TypeScript

• Docker

• Angular 2

• Break

• Exposing & consuming REST APIs with Angular 2

• Tips and tricks:

• APIs, Swagger, Cors, Hateoas, Etag, JWT, Scaling

Ask questions

Page 58: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 59: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Exposing REST Endpoints

Page 60: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Conference Micro Services

Page 61: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Exposing « beautiful » APIs

• JSon:API

• OData

• Jsend

• HAL

• CPHL

• SIREN

• Google’s JSon Style Guide

• Do it your own

Page 62: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

« Kind of » JSon:API

GET http://host/schedule/api/sessions

GET http://host/schedule/api/sessions?page=2

GET http://host/schedule/api/sessions?sort=title

GET http://host/schedule/api/sessions?sort=-title,date

POST http://host/schedule/api/sessions

GET http://host/schedule/api/sessions/abcd

REMOVE http://host/schedule/api/sessions/abcd

GET http://host/speaker/api/speakers/abcd

GET http://host/speaker/api/speakers/abcd?expand=false

Page 63: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 64: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Swagger

• Simple yet powerful representation of your RESTful API

• API documentation

• What do you call?

• What are the parameters?

• What are the status code?

• Contract written in JSon (or Yaml)

• Donated to the Open API Initiative

Page 65: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Swagger’s ecosystem

Page 66: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Swagger APIs

@Path("/speakers")

@Api(description = "Speakers REST Endpoint")

public class SpeakerEndpoint {

@POST

@ApiOperation(value = "Adds a new speaker to the conference")

@ApiResponses(value = {

@ApiResponse(code = 400, message = "Invalid input")})

public Response add(@NotNull Speaker speaker) { ... }

@GET @Path("/{id}")

@ApiOperation(value = "Finds a speaker by ID")

public Response retrieve(@PathParam("id") String id) { ... }

}

Page 67: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Swagger Maven Plugin

<plugin>

<groupId>com.github.kongchen</groupId>

<artifactId>swagger-maven-plugin</artifactId>

<configuration>

<apiSources><apiSource>

<locations>org.agoncal.conference.venue.rest</locations>

<schemes>http,https</schemes>

<host>localhost:8080</host>

<basePath>/api</basePath>

<info>

<title>Room</title>

<version>1.0.0</version>

<description>Rooms of the venue</description>

</info>

Page 68: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 69: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Swagger TypeScript for Angular generation

public add(room: models.Room): Observable<{}> {

return this.http.request(path, requestOptions).map((response: Response) => { … });

}

public retrieve(id: string, extraHttpRequestParams?: any): Observable<models.Room> {

return this.http.request(path, requestOptions).map((response: Response) => { … });

}

Page 70: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Generate Angular from Swagger

Page 71: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 72: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Proxy

Page 73: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

CORS

• Cross-Origin Resource Sharing

• Specification (https://www.w3.org/TR/cors/)

• Access across domain-boundaries

• JavaScript and web programming has grown

• But the same-origin policy still remains

• Prevents JavaScript from making requests across domain

boundaries

Page 74: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

HTTP Header

Access-Control-Allow-Origin

Access-Control-Allow-Credentials

Access-Control-Expose-Headers

Access-Control-Max-Age

Access-Control-Allow-Methods

Access-Control-Allow-Headers

Access-Control-Request-Method

Access-Control-Request-Headers

Page 75: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

CORS@Provider

public class CORSFilter implements ContainerResponseFilter {

public void filter(ContainerRequestContext request,

ContainerResponseContext response) throws IOException {

response.getHeaders().add("Access-Control-Allow-Origin", "*");

response.getHeaders().add("Access-Control-Allow-Headers",

"origin, content-type, accept, authorization, Etag");

response.getHeaders().add("Access-Control-Allow-Credentials",

"true");

response.getHeaders().add("Access-Control-Allow-Methods",

"GET, POST, PUT, DELETE, OPTIONS, HEAD");

}

}

Page 76: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Page 77: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 78: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

HateOAS

• « Hypermedia as the engine of application state »

• At its core is the concept of « hypermedia »

• Or in other words: the idea of links

• Client application goes from one state to the next by following a link

• Runtime contract

• Nothing in Java EE, maybe in MicroProfile

• JAX-RS has a Link API

Page 79: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Links

links: {

self: "http://host/schedule/api/sessions?page=1",

first: "http://host/schedule/api/sessions?page=1",

last: "http://host/schedule/api/sessions?page=14",

next: "http://host/schedule/api/sessions?page=2",

monday: "http://host/schedule/api/sessions/monday",

tuesday: "http://host/schedule/api/sessions/tuesday"

},

data: [ {

links: {

self: "http://host/schedule/api/sessions/uni_room9_tuesd"

},

id: "uni_room9_tuesday_8_9h30_12h30",

title: ”Java EE and Angular 2",

Page 80: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

http://www.iana.org/assignments/link-relations/link-relations.xml

Page 81: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Links

@XmlType(name = "links")

public abstract class LinkableResource implements Identifiable {

private Map<String, URI> links;

public void addSelfLink(URI uri) {

addLink(SELF, uri);

}

public void addCollectionLink(URI uri) {

addLink(COLLECTION, uri);

}

}

Page 82: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Links

if (body.links) {this.links = {};for (let key in body.links) {

this.links[key] = body.links[key] !== undefined ? body.links[key] : null;}

}

Page 83: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

HateOAS

Page 84: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 85: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Caching

• HTTP has temporary storage (caching)

• Reduce bandwidth usage

• Reduce server load

• Reduce perceived lag

• ETags, or entity-tags: "conditional" requests.

• Checksum

• If-None-Match

Page 86: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Etag Generation

@GET @Path("/{id}")

public Response retrieve(@PathParam("id") String id,

@Context Request request) {

Talk talk = talkRepository.findById(id);

EntityTag etag = new EntityTag(talk.hashCode());

Response.ResponseBuilder preconditions =

request.evaluatePreconditions(etag);

if (preconditions == null) {

preconditions = Response.ok(talk).tag(etag);

}

return preconditions.build();

}

Page 87: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 88: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

ETag & caching

Page 89: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 90: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

JSon Web Token

• Lightweigh token

• Contains « some » data (claims)

• Base64

• Encrypted

• Passed in the HTTP Header

• Sent at each request

• Not in Java EE nor Microprofile (yet)

• Many librairies

Page 91: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

A Token

Bearer

eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZ29uY2FsIiwiaXNzIjoi

aHR0cDovL2NvbmZlcmVuY2UuZG9ja2VyLmxvY2FsaG9zd

Do5MC9jb25mZXJlbmNlLWF0dGVuZGVlL2FwaS9hdHRlbm

RlZXMvbG9naW4iLCJpYXQiOjE0Nzc0OTk3NTUsImV4cCI6

MTQ3NzUwMDY1NX0.aL0a_q5wC3cesBKhkXChg30zr3W

WOsYhFhpJ0lQ479LtLjrPvTQiDH0N_YnFuARuEuy299S4u

O0yXGmX0tSs-Q

Page 92: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

A Token

Bearer

eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZ29uY2FsIiwiaXNzIjoi

aHR0cDovL2NvbmZlcmVuY2UuZG9ja2VyLmxvY2FsaG9zd

Do5MC9jb25mZXJlbmNlLWF0dGVuZGVlL2FwaS9hdHRlbm

RlZXMvbG9naW4iLCJpYXQiOjE0Nzc0OTk3NTUsImV4cCI6

MTQ3NzUwMDY1NX0.aL0a_q5wC3cesBKhkXChg30zr3W

WOsYhFhpJ0lQ479LtLjrPvTQiDH0N_YnFuARuEuy299S4u

O0yXGmX0tSs-Q

Page 93: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

A Token

HEADER: { "alg": "HS512" }

PAYLOAD:{

"sub": "agoncal",

"iss": "http://host/attendee/api/login",

"iat": 1477499755,

"exp": 1477500655

}

VERIFY SIGNATURE HMACSHA256(...)

Page 94: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

JWT Generation

@Path("/attendees")

public class AttendeeEndpoint {

@POST @Path("/login")

@Consumes(APPLICATION_FORM_URLENCODED)

public Response auth(@FormParam("login") String login,

@FormParam("password") String password) {

// Authentication, security exception and so on...

return Response.ok().header(AUTHORIZATION, "Bearer " +

issueToken(login)).build();

}

}

Page 95: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Filter to Check the Token@JWTTokenNeeded

@Provider @Priority(Priorities.AUTHENTICATION)

public class JWTTokenNeededFilter implements ContainerRequestFilter{

public void filter(ContainerRequestContext ctx) {

String auth = ctx.getHeaderString(HttpHeaders.AUTHORIZATION);

if (auth == null || !authorizationHeader.startsWith("Bearer")) {

throw new NotAuthorizedException("No Bearer");

}

String token = auth.substring("Bearer".length()).trim();

Jwts.parser().setSigningKey(key).parseClaimsJws(token);

}

}

Page 96: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Check the Token@Path("/ratings")

public class RatingEndpoint {

@JWTTokenNeeded

@POST

public Response rate(...) {

}

@GET

public Response retrieve(...) {

}

}

Page 97: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

JWT Consumptionreturn this.http

.post(this.basePath, body, requestOptions)

.map((response: Response) => {if (response.status !== 200) {

return undefined;}this.jwt = response.headers.get('authorization');

if (!this.jwt)return undefined;

return this.jwt;

}).catch((error: any) => {

return undefined});

Page 98: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

@Injectable()export class AuthGuardService implements CanActivate {

constructor(private authService: AuthService, private router: Router) { }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean{

let url: string = state.url;

return this.checkLogin(url);}

checkLogin(url: string): boolean {if (this.authService.isLoggedIn) {

return true;}

this.router.navigate(['/login'], { queryParams: { redirectTo: url } });return false;

}}

Page 99: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Passing a token around

Page 100: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 101: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Scaling

• Stateless architecture

• No cookie

• No HTTP session

• No local cache

• Stateless scales better than statefull

• Clients can round robin

• Dynamic proxy

• Meet Traeffik

Page 102: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 103: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Proxy

Page 104: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Traeffik

Page 105: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 106: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Going in production with Angular

• By default, Angular (in dev mode) is about … 4 mb !

• Angular cli and webpack will:

• Uglify your JavaScript code

• Create a standalone bundle file

• Gzip compress

• Apply a tree shaking to delete unused code (it’s not dead code, btw !)

• Could use AOT compilation

Page 107: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

JIT vs AOT Compilation

Source Code

JIT Compilation

Code Generation

VM execution

Source Code

AOT Compilation

Code Generation

VM execution

BUILD

RUN

Page 108: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

With AOT, AN Angular project could be 60 – 70 % less code

http://slides.com/wassimchegham/demystifying-ahead-of-time-compilation-in-angular-2-aot-jit#/32

Page 109: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Demo Time !

Angular Production ready

Page 110: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Conclusion

• MicroProfile 1.1 will bring more MicroServices features

• Configuration

• Security: JWT Token Exchange

• Health Check

• Fault Tolerance

• More to come

Page 111: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 112: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Conclusion

• Angular 4

• Native Script

• TypeScript 2.x

Page 113: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 114: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 115: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 116: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 117: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 118: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Page 119: When Enterprise Java Micro Profile meets Angular

#devoxxfr #ngjava @sebastienpertus @agoncal

Enterprise Java MicroProfile

TypeScript and Angular

Sebastien Pertus

Antonio Goncalves

https://github.com/agoncal/agoncal-application-conference