web services: encapsulation, reusability, and simplicity

62
Web Services: Encapsulation, Reusability, and Simplicity #CSUC14 Presented by Wing Ming Chan, September 16, 2014 Upstate Medical University [email protected] 1

Upload: hannonhill

Post on 12-Jun-2015

276 views

Category:

Software


1 download

DESCRIPTION

#CSUC14

TRANSCRIPT

Page 1: Web Services: Encapsulation, Reusability, and Simplicity

Web Services:Encapsulation, Reusability, and Simplicity

#CSUC14

Presented by

Wing Ming Chan, September 16, 2014

Upstate Medical University

[email protected]

1

Page 2: Web Services: Encapsulation, Reusability, and Simplicity

Topics

Encapsulation: data and data manipulation hiding Usernames and passwords Creating phantom entities in Cascade Fully qualified identifiers

Reusability: code ready to use without modification AssetOperationHandlerService Asset and Property classes

Simplicity: lines of code Cascade and AssetTree classes CascadeInstances and Report classes

2

Page 3: Web Services: Encapsulation, Reusability, and Simplicity

Part 0: Three Questions

3

Page 4: Web Services: Encapsulation, Reusability, and Simplicity

Purposes

Look for positive answers Reveal the problems Highlight the main issues

4

Page 5: Web Services: Encapsulation, Reusability, and Simplicity

Question 1

How many of you can write a web service program, doing something, anything, without hard-coding the username and password anywhere in your code?

Main concern Security and encapsulation of sensitive data Access restrictions

5

Page 6: Web Services: Encapsulation, Reusability, and Simplicity

Question 2

How many of you can work with the first node of the second multiple field in a data definition block, without looking at the read dump of the block? Can you hard-code the location of the node in you program?

Main concern Encapsulation and reusability

6

Page 7: Web Services: Encapsulation, Reusability, and Simplicity

Locating Nodes

7

Page 8: Web Services: Encapsulation, Reusability, and Simplicity

Question 3

Besides the use of require_once or include_once, how many of you can use a single semi-colon in your code to do the following: Associate metadata set 1 with all folders in a site

(including Base Folder) Associate metadata set 2 with all files in the site Associate metadata set 3 with all symlinks in the site

Requirements: no program-specific data allowed in the required/included files; all information related to metadata sets, folders, files, and symlinks must show up in your code

Main concerns Reusability and simplicity

8

Page 9: Web Services: Encapsulation, Reusability, and Simplicity

Part 1: Encapsulation

9

Page 10: Web Services: Encapsulation, Reusability, and Simplicity

The Typical Way of Starting a Program

10

Page 11: Web Services: Encapsulation, Reusability, and Simplicity

Problems?

Programs are tied with usernames, passwords, and WSDL URL’s People can come and go Changes of passwords URL’s can change too (different instances)

Passwords exposed Security

A single program used by multiple users Usernames and passwords change depending on

the users using the same program Access restrictions

11

Page 12: Web Services: Encapsulation, Reusability, and Simplicity

Phantom: Episode I

Detachment of structuredData from dataDefinition in web services Created by web services Only affects data definition blocks (including

those plugged into pages) Can be created in pages, but haven’t figure out a

way to reveal the hidden content

12

Page 13: Web Services: Encapsulation, Reusability, and Simplicity

Data Definitions

13

Page 14: Web Services: Encapsulation, Reusability, and Simplicity

Data Definition Blocks

14

Page 15: Web Services: Encapsulation, Reusability, and Simplicity

REPLACING STRUCTUREDDATA

15

Page 16: Web Services: Encapsulation, Reusability, and Simplicity

Results

16

Page 17: Web Services: Encapsulation, Reusability, and Simplicity

Hidden Multiple Nodes

17

Page 18: Web Services: Encapsulation, Reusability, and Simplicity

Results

18

Page 19: Web Services: Encapsulation, Reusability, and Simplicity

The Other Side of the Same Coin I

19

Page 20: Web Services: Encapsulation, Reusability, and Simplicity

The Other Side of the Same Coin II

20

Page 21: Web Services: Encapsulation, Reusability, and Simplicity

Creating a Java Error

21

Page 22: Web Services: Encapsulation, Reusability, and Simplicity

A NIGHTMARE…

22

Pretend that I am you enemy What I can do to you:

Detachment of data definitions from content types

Assigning an empty string to a checkbox in data definition blocks

Data definition manipulation with phantom nodes How long will it take to solve the problem:

Days? Weeks?

Page 23: Web Services: Encapsulation, Reusability, and Simplicity

Encapsulating Usernames and Passwords: a Two-Step Approach

Step 1: move usernames and passwords out of programs to authentication files

Step 2: hide usernames and passwords from the system altogether

23

Page 24: Web Services: Encapsulation, Reusability, and Simplicity

Moving Usernames and PasswordsOut of Programs: Using Authentication Files

24

Page 25: Web Services: Encapsulation, Reusability, and Simplicity

Authentication File Example

25

Page 26: Web Services: Encapsulation, Reusability, and Simplicity

Password-Protected Page

26

Page 27: Web Services: Encapsulation, Reusability, and Simplicity

Using Session

27

Page 28: Web Services: Encapsulation, Reusability, and Simplicity

Using Command-Prompt Arguments

28

Page 29: Web Services: Encapsulation, Reusability, and Simplicity

Authentication File: Entry Point to Code Library

29

Page 30: Web Services: Encapsulation, Reusability, and Simplicity

The AssetOperationHandlerService Class: Encapsulating Operations

Encapsulation Username and password URL The SoapClient object and all operations All parameters required for operations

The instance $service: Instantiated in the authentication files Passed from client code to code library Performs all basic operations in code library

30

Page 31: Web Services: Encapsulation, Reusability, and Simplicity

Fully Qualified Identifiers

When working with data definition blocks or pages, we need to work with nodes

A fully qualified identifier of a node is the full path of the node, including all the information about ancestors and absolute position

Example: pie;1;pie-is-sliced

31

Page 32: Web Services: Encapsulation, Reusability, and Simplicity

What Is Special About Fully Qualified Identifiers

Example: pie;1;pie-is-sliced The ;1 part following an identifier indicates

that the field is a multiple field ;0 means that the node bearing this

identifier is the first node in the set; ;1 means that the node is the second node of the set

Unlike array indexes, the fully qualified identifier of a node never changes, no matter how many cousins and siblings it has

A more complicated example: main-field;group;2;sub-group;1;text;3

32

Page 33: Web Services: Encapsulation, Reusability, and Simplicity

Why Fully Qualified Identifiers?

They never change: can be hard-coded in programs

Easy node look-up Possible to use for and foreach loops Encapsulation of arrays and stdClass

objects

33

Page 34: Web Services: Encapsulation, Reusability, and Simplicity

DATADEFINITIONBLOCK::GETIDENTIFIERS

34

Page 35: Web Services: Encapsulation, Reusability, and Simplicity

Part 2: Reusability

35

Page 36: Web Services: Encapsulation, Reusability, and Simplicity

Why OOP?

Encapsulation Hiding complicated program logic Hiding data and raw data structures

Reusability The entire library consists of reusable code Easy object retrieval Providing look-up mechanism Setting and editing data and metadata, and

preventing unacceptable input Error checking and exception handling Inheritance

Problems with web services36

Page 37: Web Services: Encapsulation, Reusability, and Simplicity

Problems with Web Services I

It is possible to delete the value of a required field using web services (e.g. in a metadata set)

Cascade won’t complain when an unacceptable boolean value is assigned to a field (e.g. fals instead of false) An unacceptable boolean value is ignored Unlike dateTime (which is represented by an object)

Cascade won’t complain when an unacceptable string value is assigned to a dropdown An undefined item is introduced An empty string will also work

37

Page 38: Web Services: Encapsulation, Reusability, and Simplicity

Problems with Web Services II

When a single-item checkbox is assigned an empty string, Cascade won’t complain

However, the associated data definition block cannot be viewed (a Java error)

The field should have been assigned the string ‘::CONTENT-XML-CHECKBOX::’ instead

Phantoms we have met

38

Page 39: Web Services: Encapsulation, Reusability, and Simplicity

Problems with Web Services III

Assignment of NULL vs. unset When modifying temporal settings for scheduled

publishing (for a site, a publish set, or a destination), assignments of the NULL value to properties won’t work

Instead, we must unset the properties

39

Page 40: Web Services: Encapsulation, Reusability, and Simplicity

ASSET AND PROPERTY

Asset: an abstract class Other abstract subclasses: ContainedAsset, Container, Block, Format, etc.

42 concrete classes corresponding to 42 asset types defined in the WSDL

All methods have returned values Method chaining possible

stdClass objects within an asset are represented by subclasses of Property

40

Page 41: Web Services: Encapsulation, Reusability, and Simplicity

Inheritance

Reusability An asset class can be reused to deal with any

assets of the same type Metadata-related classes can be reused in File, Page, Block (and its subclasses), Format (and its subclasses) and Symlink

Members in a parent class inherited by child classes, avoiding repetition of code

Separate code files Less code per file Easier to maintain

41

Page 42: Web Services: Encapsulation, Reusability, and Simplicity

Exception Handling

All classes throw exceptions of various types Client code is expected to deal with

exceptions Two modes of execution: strict and lenient To throw or not to throw? Cascade::getAsset vs. Cascade::getX (e.g. Cascade::getPage)

42

Page 43: Web Services: Encapsulation, Reusability, and Simplicity

Part 3: Simplicity

43

Page 44: Web Services: Encapsulation, Reusability, and Simplicity

Writing Client Code

Client code does not look like ‘normal’ Cascade web service code No username and password No arrays of raw data or stdClass objects Chained method calls Handling of exceptions A lot shorter: simplicity!

Normally, no use of new keyword Easy retrieval of asset objects Easy retrieval of associated objects of pages High level classes: Cascade and AssetTree 44

Page 45: Web Services: Encapsulation, Reusability, and Simplicity

Retrieving an Asset Object

45

Page 46: Web Services: Encapsulation, Reusability, and Simplicity

Dumping an Asset Object

46

Page 47: Web Services: Encapsulation, Reusability, and Simplicity

Working With A Data Definition Block

47

Page 48: Web Services: Encapsulation, Reusability, and Simplicity

Displaying Page-Related Information

48

Page 49: Web Services: Encapsulation, Reusability, and Simplicity

Setting Access Rights

49

Page 50: Web Services: Encapsulation, Reusability, and Simplicity

Setting Content Type

50

Page 51: Web Services: Encapsulation, Reusability, and Simplicity

Switch Content Type

51

Page 52: Web Services: Encapsulation, Reusability, and Simplicity

ASSETTREE

A tree structure consisting of: Root container Children Sub-trees

What it is good for: Creating reports Modifying assets of a certain type or types

The AssetTree::traverse method Visits the root, every children of the root, and recursively

all sub-trees (depth-first traversal) Accepts global functions, parameters, and results array

(passed in by reference) Global functions as filters 52

Page 53: Web Services: Encapsulation, Reusability, and Simplicity

ASSETTREE::TRAVERSE

53

Page 54: Web Services: Encapsulation, Reusability, and Simplicity

ASSETTREE::APPLYFUNCTIONSTOCHILD

54

Page 55: Web Services: Encapsulation, Reusability, and Simplicity

MetadataSet Associations

55

Page 56: Web Services: Encapsulation, Reusability, and Simplicity

Global Function Used

56

Page 57: Web Services: Encapsulation, Reusability, and Simplicity

Publishing Every Page in a Folder

57

Page 58: Web Services: Encapsulation, Reusability, and Simplicity

AssetTree Recipes

58

Page 59: Web Services: Encapsulation, Reusability, and Simplicity

Phantom: Episode II

Detachment of structuredData from dataDefinition in web services Created by changes of data definitions Affects web services only

59

Page 60: Web Services: Encapsulation, Reusability, and Simplicity

CascadeInstances:The Ultimate Test for Simplicity

60

Page 61: Web Services: Encapsulation, Reusability, and Simplicity

REPORT:ENCAPSULATING ASSETTREE

61

Page 62: Web Services: Encapsulation, Reusability, and Simplicity

Questions?

Tutorial: http://www.upstate.edu/cascade-admin/projects/web-services/

Email: [email protected] A web service user group?

62