1
199 Summary 7.6.3 Best design practices When using any persistence solution with Restlet, you should try loading and storing the state of your resources each time a ServerResource instance is created in order to process a new call. The goal is to respect the stateless constraint of REST . This constraint doesn’t mean that your application can’t have state, but that the conversation between clients and servers shouldn’t be driven by out-of-band informa- tion such as cookie-based sessions promoted in the Servlet API. For efficiency reasons you should consider putting in place database connection pools and data caches in your Restlet application subclasses, accessing those mecha- nisms by casting the result of the ServerResource#getApplication() method. Another option to share state between several resource instances is via the parent Context instance, which contains a map of attributes. When dealing with transient data, such as a shopping cart or transactions running across several web pages, you should rethink your application design to turn this data into resources, such as a shopping cart resource with a specific URI. The state of those resources might be stored in a database like other resources, maybe with a shorter life- cycle but without special treatment. Far from being a backward step, this stateless constraint can even benefit your end users—they won’t experience the typical session expiration issue when taking too long to complete a purchase order, for example. 7.7 Summary In this chapter you learned best practices that, taken together, make the difference between a prototype Restlet application and a more mature one suitable for production. You first saw how to complete an application with the usual web elements manipu- lated in most web APIs and websites, such as HTML forms handling, static file serving, and cookie manipulation. Each feature has been carefully illustrated in the context of the ongoing mail server example. You also learned two complementary ways to handle web feeds in the Atom and RSS formats using two Restlet extensions, comparing the advantages and disadvan- tages of each of them. Then you learned how to redirect client calls, either manually by setting the proper HTTP statuses or more automatically and powerfully with the Redirector class. We also discussed performance and modularization aspects in order to deal with applications growing in size and complexity. We looked at HTTP built-in features such a conditional method processing, entity compression, and caching support. We also introduced Restlet-specific features such as the server dispatcher, the internal router, and the RIAP pseudoprotocol to communicate optimally inside a Restlet component. You’ll now continue exploring further Restlet Framework possibilities, such as deployment in cloud computing, web browsers, and smartphones—and innovative semantic web support. ZZZLWHERRNVLQIR ZZZLWHERRNVLQIR

dsfdsfdsff

  • Upload
    anduva8

  • View
    214

  • Download
    2

Embed Size (px)

DESCRIPTION

sdc

Citation preview

  • 199

    Sum

    mar

    y

    7.6.

    3B

    est

    desi

    gn p

    ract

    ices

    Wh

    en u

    sin

    g an

    y pe

    rsis

    ten

    ce s

    olut

    ion

    wit

    h R

    estl

    et, y

    ou s

    hou

    ld t

    ry lo

    adin

    g an

    d st

    orin

    gth

    e st

    ate

    of y

    our

    reso

    urce

    s ea

    ch ti

    me

    a ServerResource in

    stan

    ce is

    cre

    ated

    in o

    rder

    topr

    oces

    s a

    new

    cal

    l. T

    he

    goal

    is to

    res

    pect

    the

    stat

    eles

    s co

    nst

    rain

    t of

    RE

    ST.

    Th

    is c

    onst

    rain

    t do

    esn

    t m

    ean

    th

    at y

    our

    appl

    icat

    ion

    can

    t h

    ave

    stat

    e, b

    ut t

    hat

    th

    eco

    nve

    rsat

    ion

    bet

    wee

    n c

    lien

    ts a

    nd

    serv

    ers

    shou

    ldn

    t be

    dri

    ven

    by

    out-o

    f-ban

    d in

    form

    a-ti

    on s

    uch

    as

    cook

    ie-b

    ased

    ses

    sion

    s pr

    omot

    ed in

    the

    Serv

    let A

    PI.

    For

    eff

    icie

    ncy

    rea

    son

    s yo

    u sh

    ould

    con

    side

    r pu

    ttin

    g in

    pla

    ce d

    atab

    ase

    con

    nec

    tion

    pool

    s an

    d da

    ta c

    ach

    es i

    n y

    our

    Res

    tlet

    app

    licat

    ion

    sub

    clas

    ses,

    acc

    essi

    ng

    thos

    e m

    ech

    a-ni

    sms

    by c

    astin

    g th

    e re

    sult

    of th

    e ServerResource#getApplication()

    met

    hod.

    Ano

    ther

    opti

    on t

    o sh

    are

    stat

    e be

    twee

    n s

    ever

    al r

    esou

    rce

    inst

    ance

    s is

    via

    th

    e pa

    ren

    t Context

    inst

    ance

    , wh

    ich

    con

    tain

    s a

    map

    of a

    ttri

    bute

    s. W

    hen

    dea

    ling

    wit

    h t

    ran

    sien

    t da

    ta, s

    uch

    as

    a sh

    oppi

    ng

    cart

    or

    tran

    sact

    ion

    s ru

    nn

    ing

    acro

    ss s

    ever

    al w

    eb p

    ages

    , you

    sh

    ould

    ret

    hin

    k yo

    ur a

    pplic

    atio

    n d

    esig

    n t

    o tu

    rn t

    his

    dat

    ain

    to r

    esou

    rces

    , suc

    h a

    s a

    shop

    pin

    g ca

    rt r

    esou

    rce

    wit

    h a

    spe

    cifi

    c U

    RI.

    Th

    e st

    ate

    of th

    ose

    reso

    urce

    s m

    igh

    t be

    stor

    ed in

    a d

    atab

    ase

    like

    oth

    er r

    esou

    rces

    , may

    be w

    ith

    a s

    hor

    ter

    life-

    cycl

    e bu

    t wit

    hou

    t spe

    cial

    trea

    tmen

    t. F

    ar fr

    om b

    ein

    g a

    back

    war

    d st

    ep, t

    his

    sta

    tele

    ss c

    onst

    rain

    t can

    eve

    n b

    enef

    it y

    our

    end

    user

    sth

    ey w

    ont

    expe

    rien

    ce th

    e ty

    pica

    l ses

    sion

    exp

    irat

    ion

    issu

    e w

    hen

    taki

    ng

    too

    lon

    gto

    com

    plet

    e a

    purc

    has

    e or

    der,

    for

    exam

    ple.

    7.7

    Sum

    mar

    yIn

    th

    is c

    hap

    ter

    you

    lear

    ned

    bes

    t pr

    actic

    es t

    hat

    , ta

    ken

    tog

    eth

    er,

    mak

    e th

    e di

    ffer

    ence

    betw

    een

    a p

    roto

    type

    Res

    tlet a

    pplic

    atio

    n a

    nd

    a m

    ore

    mat

    ure

    one

    suita

    ble

    for

    prod

    uctio

    n.

    You

    firs

    t saw

    how

    to c

    ompl

    ete

    an a

    pplic

    atio

    n w

    ith

    the

    usua

    l web

    ele

    men

    ts m

    anip

    u-la

    ted

    in m

    ost

    web

    API

    s an

    d w

    ebsi

    tes,

    suc

    h a

    s H

    TM

    L f

    orm

    s h

    andl

    ing,

    sta

    tic

    file

    ser

    vin

    g,an

    d co

    okie

    man

    ipul

    atio

    n. E

    ach

    feat

    ure

    has

    bee

    n c

    aref

    ully

    illu

    stra

    ted

    in th

    e co

    nte

    xt o

    fth

    e on

    goin

    g m

    ail s

    erve

    r ex

    ampl

    e. Y

    ou a

    lso

    lear

    ned

    tw

    o co

    mpl

    emen

    tary

    way

    s to

    han

    dle

    web

    fee

    ds i

    n t

    he

    Ato

    m a

    nd

    RSS

    for

    mat

    s us

    ing

    two

    Res

    tlet

    ext

    ensi

    ons,

    com

    pari

    ng

    the

    adva

    nta

    ges

    and

    disa

    dvan

    -ta

    ges

    of e

    ach

    of

    them

    . Th

    en y

    ou le

    arn

    ed h

    ow t

    o re

    dire

    ct c

    lien

    t ca

    lls, e

    ith

    er m

    anua

    llyby

    set

    tin

    g th

    e pr

    oper

    HT

    TP

    stat

    uses

    or

    mor

    e au

    tom

    atic

    ally

    an

    d po

    wer

    fully

    wit

    h t

    he

    Redirector c

    lass

    . W

    e al

    so d

    iscu

    ssed

    per

    form

    ance

    an

    d m

    odul

    ariz

    atio

    n a

    spec

    ts i

    n o

    rder

    to

    deal

    wit

    hap

    plic

    atio

    ns

    grow

    ing

    in s

    ize

    and

    com

    plex

    ity.

    We

    look

    ed a

    t H

    TT

    P bu

    ilt-in

    feat

    ures

    suc

    ha

    con

    diti

    onal

    met

    hod

    pro

    cess

    ing,

    en

    tity

    com

    pres

    sion

    , an

    d ca

    chin

    g su

    ppor

    t. W

    e al

    soin

    trod

    uced

    Res

    tlet

    -spe

    cifi

    c fe

    atur

    es s

    uch

    as

    the

    serv

    er d

    ispa

    tch

    er, t

    he

    inte

    rnal

    rou

    ter,

    and

    the

    RIA

    P ps

    eudo

    prot

    ocol

    to c

    omm

    unic

    ate

    opti

    mal

    ly in

    side

    a R

    estl

    et c

    ompo

    nen

    t. Y

    oull

    now

    con

    tin

    ue e

    xplo

    rin

    g fu

    rth

    er R

    estl

    et F

    ram

    ewor

    k po

    ssib

    iliti

    es,

    such

    as

    depl

    oym

    ent

    in c

    loud

    com

    puti

    ng,

    web

    bro

    wse

    rs,

    and

    smar

    tph

    ones

    an

    d in

    nov

    ativ

    ese

    man

    tic

    web

    sup

    port

    .