Design REST Services with CXF JAX- RS implementation: best practices and lessons learned



Similar documents
Distribution and Integration Technologies

REST API Development. B. Mason Netapp E-Series

Designing RESTful Web Applications

How to secure your Apache Camel deployment

Secure Services withapache CXF

Contents. 2 Alfresco API Version 1.0

02267: Software Development of Web Services

Session 6 Patterns and best practices in SOA/REST

Improving performance for security enabled web services. - Dr. Colm Ó héigeartaigh

REST and SOAP Services with Apache CXF

REST vs. SOAP: Making the Right Architectural Decision

Application layer Web 2.0

Security As A Service Leveraged by Apache Projects. Oliver Wulff, Talend

Commerce Services Documentation

Securing JAX-RS RESTful services. Miroslav Fuksa (software developer) Michal Gajdoš (software developer)

SOA and Virtualization Technologies (ENCS 691K Chapter 2)

Apache CXF Web Services

02267: Software Development of Web Services

Fairsail REST API: Guide for Developers

JAX-WS Developer's Guide

WEB SERVICES. Revised 9/29/2015

CERTIFIED MULESOFT DEVELOPER EXAM. Preparation Guide

Apache. Version 1.0. Developer Guide

A Comparison of Service-oriented, Resource-oriented, and Object-oriented Architecture Styles

Accessing Data with ADOBE FLEX 4.6

Integrating Complementary Tools with PopMedNet TM

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

SOA Fundamentals For Java Developers. Alexander Ulanov, System Architect Odessa, 30 September 2008

Web Services Tutorial

Interoperable Web Services for Building Automation Integrating KNX. KNX Scientific Conference 2006

A Comparison of Service-oriented, Resource-oriented, and Object-oriented Architecture Styles

rpafi/jl open source Apache Axis2 Web Services 2nd Edition using Apache Axis2 Deepal Jayasinghe Create secure, reliable, and easy-to-use web services

REST web services. Representational State Transfer Author: Nemanja Kojic

Spring Data JDBC Extensions Reference Documentation

Systems Integration in the Cloud Era with Apache Camel. Kai Wähner, Principal Consultant

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

FUSE-ESB4 An open-source OSGi based platform for EAI and SOA

REST services in Domino - Domino Access Services

Some REST Design Patterns (and Anti-Patterns)

T Network Application Frameworks and XML Web Services and WSDL Tancred Lindholm

IETF 84 SCIM System for Cross-domain Identity Management. Kelly Grizzle

Cloud Storage Standards Overview and Research Ideas Brainstorm

Service for checking whether an is operative or not. Validate ids in your databases.

MarkLogic Server. Java Application Developer s Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

Enabling REST Services with SAP PI. Michael Le Peter Ha

Cloud Elements! Marketing Hub Provisioning and Usage Guide!

An Oracle White Paper June RESTful Web Services for the Oracle Database Cloud - Multitenant Edition

Apache Sling A REST-based Web Application Framework Carsten Ziegeler cziegeler@apache.org ApacheCon NA 2014

70-487: Developing Windows Azure and Web Services

GlassFish. Developing an Application Server in Open Source

How To Script Administrative Tasks In Marklogic Server

JBoss SOAP Web Services User Guide. Version: M5

Business Process Execution Language for Web Services

Perceptive Integration Server

Integrating VoltDB with Hadoop

Next-Generation ESB. Kevin Conner SOA Platform Architect, Red Hat Keith Babo JBoss ESB Project Lead, Red Hat. June 23rd, 2010

Setting the World on FHIR

IBM SPSS Collaboration and Deployment Services Version 6 Release 0. Single Sign-On Services Developer's Guide

Message Containers and API Framework

Intel Rack Scale Architecture Storage Services

CST6445: Web Services Development with Java and XML Lesson 1 Introduction To Web Services Skilltop Technology Limited. All rights reserved.

The end. Carl Nettelblad

JASPERREPORTS SERVER WEB SERVICES GUIDE

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

1 What Are Web Services?

How to consume a Domino Web Services from Visual Studio under Security

Web-Based Hazus-MH. A Conceptual Approach. Mourad Bouhafs, AVP ATKINS Atlanta mourad.bouhafs@atkinsglobal.com

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Going Native With Apache Cassandra. QCon London, 2014

An Introduction to Job Server In Sage SalesLogix v8.0. Sage SalesLogix White Paper

Leveraging Cloud Storage Through Mobile Applications Using Mezeo Cloud Storage Platform REST API. John Eastman Mezeo

Aspect Oriented Programming. with. Spring

Talend Open Studio for ESB. Release Notes 5.2.1

Enterprise JavaBeans 3.1

Java EE 7: Back-End Server Application Development

Building SOA Applications with JAX-WS, JAX- RS, JAXB, and Ajax

MarkLogic Server. Reference Application Architecture Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

Spoilt for Choice Which Integration Framework to choose? Mule ESB. Integration. Kai Wähner

1 of 21 9/22/11 10:41 AM

SnapLogic Salesforce Snap Reference

vcommander will use SSL and session-based authentication to secure REST web services.

Junos Space. Junos Space Security Director Restful Web Services API Reference. Modified: Copyright 2016, Juniper Networks, Inc.

Ansible Tower API Guide

Reusing Existing * Java EE Applications from Oracle SOA Suite

Force.com REST API Developer's Guide

1 What Are Web Services?

EUR-Lex 2012 Data Extraction using Web Services

Copyright. Restricted Rights Legend. Trademarks or Service Marks. Copyright 2003 BEA Systems, Inc. All Rights Reserved.

ReSTful OSGi Web Applications Tutorial. Khawaja Shams & Jeff Norris

Configuring CQ Security

TIBCO JASPERREPORTS SERVER WEB SERVICES GUIDE

Introduction to Service Oriented Architectures (SOA)

RESTful web applications with Apache Sling

SOA Blueprints Concepts

Transcription:

Design REST Services with CXF JAX- RS implementation: best practices and lessons learned Andrei Shakirin, Talend ashakirin@talend.com ashakirin.blogspot.com

Agenda REST architectural style Design of REST API for Syncope domain Practical aspects of using CXF JAX-RS

AboutMe Software architect in Talend Team PMC and committer in Apache CXF and commiter in Apache Syncope projects Speaker for Java conferences

RepresentationalState Transfer Set of principals and restrictions HTTP isoneinstantiation ofthe REST The scope of REST architectural style: loosely coupled application

REST Principles 1. Everything has an ID 2. Using IDs to link things together: hypermedia and HATEOAS 3. Uniform interface 4. Interaction with resources through the representation 5. Communication is stateless

JAX-RS Specification and Java API Support in exposing a resource Java class (a POJO) as a web resource Versions: 1.0, 1.1, 2.0 (clientapi, asynchronous API, filters and interceptors) Implementations: Jersey, Apache CXF, Resteasy,

REST API Design

Apache Syncope

SyncopeDomain Model Users (name, password, dates, attributes) Roles(name, owners, attributes) Entitlements (TASK_DELETE, ROLE_CREATE) Connectors(ConnID bundle: DatabaseTable, SOAP) ExternalResources (name, connector, mode, mapping) Tasks (user/roletemplate, resource, action, status)

Resources anduris: Rules Resource is anything to be referenced Normally resources are the nouns Resources are coarse grained URIs: descriptive and well structured URIs: scoping information

Design Attempt /users/adduser /users/a1b2c3/verifypassword /roles/s8g3j8/updaterole /tasks/submittask Don tdo it!

Resources Types 1. Predefined top-level resources 2. Resource for collection of objects 3. Resource for every exposed objects 4. Resource representing results of algorithms

Top Level Resources Entry point to the API Home page or list of root entities(collections) URIs: http(s)://api.syncope.org/administration vs http(s)://api.syncope.org/administration/rest/jaxrs/c xf

Collection Resources /users /roles /connectors /policies /resources /tasks

Instance Resources /users/user123 /users/user123/status /roles/rolea /roles/rolea/parent /connectors/ldap /connectors/ldap/bundles No Hierarchy? /relationships/user123,rolea /color-blends/red;blue

Algorithm Resources /users?failedlogin=true /tasks?type=propagation&status=success FIQL (Feed Item Query Language): /tasks?_s=date=lt=2014-10-31;date=gt=2014-10-01;(type==sync)

SubsetofUniform Interface Will the client will fetch resource of this type? GET /users GET /users/a1b2c3

SubsetofUniform Interface Will the client delete resource of this type? DELETE /users/a1b2c3

SubsetofUniform Interface Will the client modify resource of this type? PUT /users/a1b2c3

SubsetofUniform Interface Will the client create resource of this type? Who isin chargeto determineuri: server/ client? Server: POST /users 201 Created, Location=/users/a1b2c3 Client: PUT /users/myuser

ResourceAPI: UserService

Representations: Media Types Data Format + Parsing rules

Representations: Media Types Standard: text/html application/json application/xml application/xhtml+xml application/x-www-form-urlencoded Custom: application/user+json application/vnd.mycompany-myformat Versioned: application/user+json&v2

Representations: JAX-RS @Path("users") @Consumes("application/json", "application/xml") @Produces("application/json", "application/xml") public interface UserService { @GET @Produces("application/json;qs=1.0", "application/xml;qs=0.75") Collection<UserTO> list(); } @POST @Consumes("application/json;q=1.0", "application/xml;q=0.25") Response create(userto userto);

CXF: Entity Providers XML: JAXBElementProvider, Source application/xml, application/*+xml, text/xml JSON: JSONProvider(Jettison), Jenkins application/json, application/*+json Multipart: Attachments, MultipartBody multipart/mixed, multipart/related, BinaryData application/octet-stream, XSLTJaxb, Atom, Dom4J, XMLBeans,

JSON: JettisonvsJackson CXF JSONProvider (based on Jettison) AdoptXML structurestojson (mapped, BadgerFish) STaX API (XMLStreamWriter, XMLStreamReader) Flexible and simple (prefixes, root elements, array serialization, unwrapping, XSLT transformations) Using: for small payloads, if flexibility required <root><child>test</child><child>test</child></root> { "root" : { child : [ "test", "test" ] } }

JSON: JettisonvsJackson Jackson Not XML oriented Supports streaming, tree and data binding models Supports Jackson specific annotations Using: middle and large payloads, sophisticated class hierarchy @JsonTypeInfo(use=Id.CLASS, include=as.property, property="class")

Representation: Links HTML/XHTML <a href="http://mysyncope.com">syncope</a> XML <element xlink:href="http://mysyncope.com">syncope</element> JSON:?

Links in JSON JSON HAL (Mike Kelly, IETF draft) Siren(Kevin Swiber) Collection + JSON (Mike Amudsen) Custom format

GET /tasks JSON HAL

Relations: ManyTo Many User Role role 1 role 2 role N user1 user2 usern

Relations as Resources User Membership Role user1 role 2 createdby timestamp

Errors Choose appropriate HTTP status code Set short error code for automatic processing Provide informative and descriptive entitybodies Use JAX-RS ExceptionMappers

Errors: Code Mapping 1. Decide is it client or server problem (4xx/5xx) 2. Look into HTTP status codespecand select approprite one: Entity Not Found -> 404 Not Found Entity Already Exist -> 409 Conflict IllegalArgumentException -> 400 Bad Request

Errors: HTTP Response 404 Not found X-Application-Error-Code: EntityNotFound X-Application-Error-Info: entity=user,id=a1b2c3 { A user a1b2c3 isnot foundin Syncopestorage. Check if usernameiscorrect. Referfollowinglink forthedetails: https://cwiki.apache.org/confluence/pages/viewpage.acti on?pageid=30751185/ }

Errors: Batch Operations 207 Multi-Status X-Application-Error-Code: Composite { message : Multiple errors detected errors : [ { statuscode : 409 errorcode : EntityExists errormessage : User a1b2c3 already exists } { statuscode : 404 errorcode : NotFound errormessage : User d4e5f6 not found } ] }

Errors: ExceptionMappers @Provider public class RestServiceExceptionMapper implements ExceptionMapper<SyncopeClientException> { @Override public Response toresponse(final SyncopeClientException ex) { LOG.error("SyncopeClientException thrown by REST method: " + ex.getmessage(), ex); builder = ex.iscomposite()? getsyncopeclientcompositeexceptionresponse(ex.ascomposite()) : getsyncopeclientexceptionresponse(ex); } } return builder.build();

AsynchronousProcessing Model operations taking a long time Provide non-blocking calls on the client side Provide suspended responses on the server side

Asynchronous: Long Operations POST /tasks HTTP/1.1 { "propagationmode": "TWO_PHASES", "resource": { "href": "/resources/98712" } "status": "NONE", } 202 Accepted Location: /tasks/x7h3b4 GET tasks/x7h3b4 { "propagationmode": "TWO_PHASES", "resource": { "href": "/resources/98712" } "status": "IN_PROGRESS", }

Asynchronous: Client API InvocationCallback<Response> callback = new InvocationCallback { public void completed(response res) { } System.out.println("Request success!"); public void failed(clientexception e) { System.out.println("Request failed!"); } }; client.target("http://mysyncope.org/tasks").request().async().post(myentity, callback);

Asynchronous: Server API @Path("/connectors") public class AsyncResource { @GET public void asyncget(@suspended final AsyncResponse asyncresponse) { new Thread(new Runnable() { @Override public void run() { String result = readconnectors(); asyncresponse.resume(result); } } } private String readconnectors() { //... very expensive operation } }).start();

Transactions /tasks/f3g4n5 { userfilter : age<=16 } /tasks/l8b3n7 { userfilter : age>16 } Requirement: update age to 18 in both tasks in transaction

TransactionalView 1. Create transaction: POST /transactions/tasks-update 201 Created Location: /transactions/tasks-update/89d3 2. Update transaction resources: PUT /transactions/tasks-update/89d3/tasks/f3g4n5 { userfilter : age<=18 } PUT /transactions/tasks-update/l8b3n7/tasks/f3g4n5 { userfilter : age>18 }

Committed Transaction 3. Commit transaction: PUT /transactions/tasks-update/89d3 committed = true 200 OK { tasks : [ { ref : /tasks/f3g4n5 } { ref : /tasks/l8b3n7 } ] } GET /tasks/f3g4n5 { userfilter : age<=18 } GET /tasks/l8b3n7 { userfilter : age>18 }

Bean Validation: JAX-RS import javax.validation.constraints.min; import javax.validation.constraints.notnull; @Path("users") @Consumes("application/json", "application/xml") @Produces("application/json", "application/xml") public interface UserService { } @GET PagedResult<UserTO> list( @NotNull @Min(1) @QueryParam(PARAM_PAGE) Integer page, @NotNull @Min(1) @QueryParam(PARAM_SIZE) Integer size); @GET @Path("{email}") @Valid UserTO getuser(@email @PathParam("email") String email);

Conclusion Try to follow REST and RESTful HTTP principles by design your application Consider using JAX-RS 2.0 implementation for Java applications CXF is nice alternative with active, responsive and cooperative community

Links Apache CXF : http://cxf.apache.org/ http://cxf.apache.org/docs/jax-rs.html Apache Syncope: http://syncope.apache.org/ Blogs: http://sberyozkin.blogspot.com http://ashakirin.blogspot.de/ http://aredko.blogspot.de/

Validation JAX-RS 2.0: Bean Validation 1.1 Specification Implementation: Hibernate Validator (or Apache BVal) Exception mapper maps: a) Input parameter validation violation -> 400 Bad Request b) Return value validation violation -> 500 Internal Server Error

Relations as Resources GET users/a1b2c3 HTTP/1.1 200 OK Content Type: application/linked+json { "href": "/users/a1b2c3", "name": "testuser", "memberships": { "href": "/memberships?userid=a1b2c3" } }

OPTIONS Returns communication options of target resource OPTIONS /users Response: 200 OK Allow: OPTIONS,GET,POST

Algorithm Resources /users?failedlogin=true /tasks?type=propagation&status=success /role;name=myrole/entitlements;name=role_ CREATE/ FIQL (Feed Item Query Language): /tasks?_s=date=lt=2014-10-31;date=gt=2014-10-01;(type==sync)

Bean Validation: CXF <jaxrs:server address="/"> <jaxrs:ininterceptors> <ref bean="validationininterceptor" /> </jaxrs:ininterceptors> <jaxrs:outinterceptors> <ref bean="validationoutinterceptor" /> </jaxrs:outinterceptors> <jaxrs:servicebeans>... </jaxrs:servicebeans> <jaxrs:providers> <ref bean="exceptionmapper"/> </jaxrs:providers> </jaxrs:server> <bean id="exceptionmapper" class="org.apache.cxf.jaxrs.validation.validationexceptionmapper"/> <bean id="validationprovider" class="org.apache.cxf.validation.beanvalidationprovider" /> <bean id="validationininterceptor" class="org.apache.cxf.jaxrs.validation.jaxrsbeanvalidationininterceptor"> <property name="provider" ref="validationprovider" /> </bean> <bean id="validationoutinterceptor" class="org.apache.cxf.jaxrs.validation.jaxrsbeanvalidationoutinterceptor"> <property name="provider" ref="validationprovider" /> </bean>

GET, HEAD READ semantic Must besafe and idempotent Cacheable Can be conditional or partional GET /users/a1b2c3

DELETE DELETE semantic Not safe, Idempotent Resource doesn t have to removed immediately Can return the resource representation or other payload DELETE /users/a1b2c3

PUT Can beusedforupdate andforcreate Not safe, idempotent Not for partial updates PUT /users/a1b2c3 { username : testuser status : active }

POST Can beusedtodo anything (normally createor update) Neither safe, no idempotent POST /users { username : testuser status : active } Response: 201 Created, Location=/users/a1b2c3

Resources anduris: Rules Resource is anything to be referenced Normally resources are the nouns Resources are coarse grained URIs: descriptive and well structured URIs: scoping information

Representations: Media Types Data Format + Parsing rules

Representations Extension Mappings: /users/a1b2c3 /users/a1b2c3.json /users/a1b2c3.xml