developing web services with apache axis2 second edition

Upload: rajcuram

Post on 12-Apr-2018

245 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    1/262

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    2/262

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    3/262

    eveloping Web

    Services with

    Apache CXF and

    Axis2

    By

    Kent Ka Iok Tong

    Copyright 2005-2010

    TipTec Development

    Puli!her" TipTec Development

    #uthor$! email" %reemant2000&yahoo'com

    Book (e!ite" http"))((('agile!kill!2'org*otice" #ll right! re!erve+' *o part o% thi! pulication may e

    repro+uce+, !tore+ in a retrieval !y!tem or tran!mitte+, in any%orm or y any mean!, electronic, mechanical, photocopying,recor+ing, or other(i!e, (ithout the prior (ritten permi!!iono% the puli!her'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    4/262

    Developing e .ervice! (ith #pache C/ an+ #i!2

    I.B*" 34-3334-323-1-

    6+ition" Thir+ e+ition 7an 2010

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    5/262

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    6/262

    Developing e .ervice! (ith #pache C/ an+ #i!2 5

    Foreword

    Learn web services and Apache CXF and Axis2easily

    I% you$+ like to learn ho( to create (e !ervice! 8in particular, u!ing #pache

    C/ or #i!29 an+ make !ome !en!e o% variou! !tan+ar+! like 7#/-., 7#/-:., 7#/B, .;#P, .D

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    7/262

    Developing e .ervice! (ith #pache C/ an+ #i!2

    e+ite+ /=< %ile!' Ao(ever, you +o *;T nee+ to kno( the more a+vance+ /='''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''40.eeing the .;#P me!!age!''''''''''''''''''''''''''''''''''''''''''''''''''''40.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''4

    Chapter #ccepting multiple parameter!''''''''''''''''''''''''''''''''''''45hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''4.plitting the /=< element into multiple parameter!''''''''''''''''4!ing the (rappe+ !tyle in #i!2'''''''''''''''''''''''''''''''''''''''''''''1Interoperaility''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''2.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''2

    Chapter 5 .en+ing an+ receiving comple +ata !tructure!''''''''''hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Pro+uct Euery'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''.en+ing more +ata in a me!!age''''''''''''''''''''''''''''''''''''''''''''3:eturning %ault!''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''3:e%erring to ei!ting /=< element!'''''''''''''''''''''''''''''''''''''''105Doing it in #i!2''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''10.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''110

    Chapter .en+ing inary %ile!'''''''''''''''''''''''''''''''''''''''''''''''''''111hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''112Provi+ing the image o% a pro+uct'''''''''''''''''''''''''''''''''''''''''''1126naling =T;= in the !ervice'''''''''''''''''''''''''''''''''''''''''''''''113Doing it in #i!2''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''120Interoperaility''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''122.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''122

    Chapter 4 Invoking lengthy operation!'''''''''''''''''''''''''''''''''''''''12hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''12Invoking a time con!uming operation'''''''''''''''''''''''''''''''''''''12hat i% you can$t mo+i%y the .D< %ile>''''''''''''''''''''''''''''''''126tremely lengthy proce!!ing'''''''''''''''''''''''''''''''''''''''''''''''''123.peci%ying the reply a++re!!''''''''''''''''''''''''''''''''''''''''''''''''''1!ing an a!ynchronou! client in #i!2'''''''''''''''''''''''''''''''''''1.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''14

    Chapter .igning an+ encrypting .;#P me!!age!'''''''''''''''''13hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''10Private key an+ pulic key''''''''''''''''''''''''''''''''''''''''''''''''''''''10Digital !ignature''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''12.igning an+ encrypting'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''1Certi%icate an+ C#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''1Di!tingui!he+ name''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''15

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    10/262

    Developing e .ervice! (ith #pache C/ an+ #i!2 3

    Per%ormance i!!ue (ith a!ymmetric encryption'''''''''''''''''''''1Keeping key pair an+ certi%icate! in 7ava'''''''''''''''''''''''''''''''1Generating a key pair'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''14.etting up a C#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''150Importing the certi%icate into the key!tore''''''''''''''''''''''''''''''152.igning .;#P me!!age!'''''''''''''''''''''''''''''''''''''''''''''''''''''''15.upporting +igital !ignature! in the (e !ervice''''''''''''''''''''16ncrypting .;#P me!!age!''''''''''''''''''''''''''''''''''''''''''''''''''1.ecurity i!!ue! (hen per%orming oth !igning an+ encrypting''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''14.en+ing login in%ormation'''''''''''''''''''''''''''''''''''''''''''''''''''''''14In!talling :ampart into #i!2''''''''''''''''''''''''''''''''''''''''''''''''''12Creating a !ecure client in #i!2''''''''''''''''''''''''''''''''''''''''''''1Creating a !ecure !ervice in #i!2'''''''''''''''''''''''''''''''''''''''''1.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''132

    Chapter 3 Creating !calale (e !ervice! (ith :6.T'''''''''''''''13hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''13.calaility +i%%iculty (ith .;#P'''''''''''''''''''''''''''''''''''''''''''''''13!ing a generic proy'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''13Creating a :6.T%ul (e !ervice''''''''''''''''''''''''''''''''''''''''''''136naling caching y a proy'''''''''''''''''''''''''''''''''''''''''''''''''''201Fali+ating the cache+ re!pon!e a%ter epiry''''''''''''''''''''''''''20!ing other kin+! o% ver!ion!'''''''''''''''''''''''''''''''''''''''''''''''''203hat i% ook! can e up+ate+ at any time>'''''''''''''''''''''''''''211Per%orming an up+ate'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''211Implementing a++'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''21Implementing +elete'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''214'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''22Deploying the !imple !ervice''''''''''''''''''''''''''''''''''''''''''''''''''22In!talling Tomcat''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''2Invoking .pring ean! %rom your implementation o@ect'''''''25Deploying :6.T%ul (e !ervice!'''''''''''''''''''''''''''''''''''''''''''24

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    11/262

    10 Developing e .ervice! (ith #pache C/ an+ #i!2

    Invoking .pring ean! %rom your re!ource o@ect!''''''''''''''''2Deploying #i!2 (e !ervice!''''''''''''''''''''''''''''''''''''''''''''''''20!ing .pring (ith #i!2''''''''''''''''''''''''''''''''''''''''''''''''''''''''''21.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''2

    Chapter 11 nit te!ting your (e !ervice!'''''''''''''''''''''''''''''''''25hat$! in thi! chapter>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''2Di%%icultie! in te!ting a (e !ervice in a container''''''''''''''''''2Te!ting a (e !ervice out o% container, in i!olation'''''''''''''''2.ummary''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''251

    :e%erence!''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''25#lphaetical In+e''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''25

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    12/262

    11

    Chapter 1Chapter 1Designing the interface for

    a simple web service

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    13/262

    12 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    !hat"s in this chapter#In thi! chapter you$ll learn ho( to +e!ign the inter%ace %or a !imple (e !ervice'

    $roviding cross platorm operations across the%nternet

    .uppo!e that you$+ like to provi+e a !ervice to the pulic or to !ome u!ine!!partner!" They can !en+ you t(o !tring! an+ you (ill concatenate them an+return the !tring' ;% cour!e, in the real (orl+ you provi+e a more u!e%ul !ervice'

    There are !everal ma@or reEuirement!" ir!t, the u!er! may e u!ing +i%%erentlanguage! 87ava, CH an+ etc'9 an+ u!ing +i%%erent plat%orm! 8in+o(!,

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    14/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 1

    concat operation' .o, in a++ition to the name, you may +eclare that theconcat name aove i! in the name!pace o% http"))tt+ev'com)!! 8!ee the

    +iagram elo(9' # name!pace i! @u!t like a 7ava package, ut it i! not in a +ot%ormat like com'tt+ev'%ooJ it i! in the %ormat o% a :

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    15/262

    1 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    Ao(ever, (hat +oe! the aove !tring type mean> I! it the 7ava !tring type>*o, you can$t !ay that ecau!e it mu!t e language neutral' ortunately, the/=< !chema !peci%ication +e%ine! !ome a!ic +ata type! inclu+ing a !tring

    type' 6ach o% the!e +ata type! ha! a *ame a! it! i+' or eample"Data type Local name namespace

    !tring !tring http"))((('('org)2001)/=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    16/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 15

    hen you return, the output me!!age may e like"

    Thi! kin+ o% (e !ervice i! calle+ :PC !tyle (e !ervice 8:PC !tan+! %or

    &'oo:concat (mlns:'oo)*http://ttdev.com/ss*+&s1+a,c&/s1+&s2+123&/s2+

    &/'oo:concat+

    %oo i! a name!pace pre%i repre!entingthe http"))tt+ev'com)!! in the re!t o% thi!element inclu+ing it! chil+ren'

    The *ame o% thi! /=< elementi! eactly that o% the operation hei! trying to call

    There i! a chil+element %or eachpart' 6ach chil+element ha! the!ame name a!that part 8!1 inthi! ca!e9'

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: s1#$pe: string in http://www.w3.org/2001/XL!chema

    Part 2:Name: s2#$pe: string in http://www.w3.org/2001/XL!chema

    %utput message:Part 1:

    Name: return#$pe: string in http://www.w3.org/2001/XL!chema

    &'oo:concat (mlns:'oo)*http://ttdev.com/ss*+&return+a,c123&/return+

    &/'oo:concat+

    The *ame o% thi! /=< elementi! eactly that o% the operationeing calle+

    6ach chil+ elementha! the !ame namea! a part in theoutput me!!age8return in thi!

    ca!e9'

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: s1#$pe: string in http://www.w3.org/2001/XL!chema

    Part 2:Name: s2

    #$pe: string in http://www.w3.org/2001/XL!chema%utput message:Part 1:

    Name: return#$pe: string in http://www.w3.org/2001/XL!chema

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    17/262

    1 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    :emote Proce+ure Call9' That i!, the operation *ame an+ the name! o% thepart! are u!e+ to create the input an+ output me!!age!'

    'ocument style web service

    The aove (ay i! not the only (ay you +e!ign the inter%ace o% your (e !ervice'or eample, you may !ay that it! input me!!age only contain! a !ingle part8!ee the +iagram elo(9 (hich i! an element +e%ine+ in a !chema' In that!chema, it i! +e%ine+ a! an element name+ concat:eEue!t that contain! t(ochil+ element! L!1M an+ L!2M"

    *ote that the !chema i! inclu+e+ in the inter%ace o% your (e !ervice"

    #n operation

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: concatRe-uestlement:

    %utput message:...

    &(sd:schematargetNamespace)*http://ttdev.com/ss*(mlns:(sd)*http://www.w3.org/2001/XL!chema*+&(sd:element name)*concatRe-uest*+

    &(sd:comple(#$pe+&(sd:se-uence+

    &(sd:element name)*s1* t$pe)*(sd:string*/+&(sd:element name)*s2* t$pe)*(sd:string*/+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+&/(sd:schema+

    &'oo:concatRe-uest (mlns:'oo)*http://ttdev.com/ss*+&s1+a,c&/s1+&s2+123&/s2+

    &/'oo:concatRe-uest+

    Lconcat:eEue!tM i! a complet typeecau!e it contain! chil+ element!

    It contain! a!eEuence o% chil+element!' The %ir!ti! an L!1Melement, then i! anL!2M element'

    The element! +e%ine+ here are put intothi! name!pace

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    18/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 14

    #! you can !ee aove, a part may e +eclare+ a! a particular element8Lconcat:eEue!tM +e%ine+ in your !chema9 or a! any element having aparticular type 8!tring +e%ine+ in /=< !chema !peci%ication9' In either ca!e it i!i+enti%ie+ u!ing a *ame'

    hen !omeone call! thi! operation, he (ill !en+ you a Lconcat:eEue!tMelement a! the input me!!age like"

    .imilarly, %or the output me!!age, you may !peci%y that it contain! only one partan+ that part i! a Lconcat:e!pon!eM element"

    &'oo:concatRe-uest (mlns:'oo)*http://ttdev.com/ss*+

    &s1+a,c&/s1+&s2+123&/s2+

    &/'oo:concatRe-uest+

    #n operation

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: concatRe-uestlement: concatRe-uest in http://ttdev.com/ss

    %utput message:...

    &(sd:schematargetNamespace)*http://ttdev.com/ss*(mlns:(sd)*http://www.w3.org/2001/XL!chema*+&(sd:element name)*concatRe-uest*+

    &(sd:comple(#$pe+&(sd:se-uence+

    &(sd:element name)*s1* t$pe)*(sd:string*/+&(sd:element name)*s2* t$pe)*(sd:string*/+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+&/(sd:schema+

    # !chema

    # (e !ervice

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    19/262

    1 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    Thi! kin+ o% (e !ervice i! calle+ +ocument !tyle (e !ervice' That i!, theinput me!!age (ill contain a !ingle part only (hich i! (ell +e%ine+ in a !chema'The !ame i! true o% the output me!!age'

    I% you go ack to check the input me!!age %or the :PC !tyle !ervice, it !houl+e revi!e+ a!"

    #n operation

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: concatRe-uestlement: concatRe-uest in http://ttdev.com/ss

    %utput message:Part 1:

    Name: concatResponseElement: concatResponse in http://ttdev.com/ss

    &(sd:schematargetNamespace)*http://ttdev.com/ss*(mlns:(sd)*http://www.w3.org/2001/XL!chema*+&(sd:element name)*concatRe-uest*+

    &(sd:comple(#$pe+&(sd:se-uence+

    &(sd:element name)*s1* t$pe)*(sd:string*/+&(sd:element name)*s2* t$pe)*(sd:string*/+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+

    &/(sd:schema+

    # !chema

    # (e !ervice

    &'oo:concatResponse(mlns:'oo)*http://ttdev.com/ss*+a,c123&/'oo:concatResponse+

    Thi! Lconcat:e!pon!eM element i! a !impletype element, meaning that it ha! no attriutean+ can$t have element! in it! o+y 8!o only!imple !tring or numer in it! o+y9'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    20/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 13

    Thi! i! ecau!e L%oo"concatM, L!1M an+ L!2M are not +e%ine+ in any !chemaan+ there%ore you mu!t eplicitly !tate the /=< element type! o% the content o%L!1M an+ L!2M'

    *o(, let$! compare the input me!!age! o% the :PC !tyle (e !ervice an+ the+ocument !tyle (e !ervice"

    RPC style

    Document style

    *ot much +i%%erence, right> The !igni%icant +i%%erence i! that the %ormer can$t evali+ate+ (ith a !chema (hile the latter can' There%ore, +ocument !tyle (e!ervice i! ecoming the +ominant !tyle' #ccor+ing to an organiNation calle+.-I 8(e !ervice! interoperaility organiNation9, you !houl+ u!e +ocument!tyle (e !ervice! only'

    'etermining the operation or a document style webservice

    To call an operation in a +ocument !tyle (e !ervice, one (ill !en+ the !inglepart o% the input me!!age only' *ote that it +oe! *;T !en+ the operation namein any (ay' Then i% there are more than one operation! in the (e !ervice 8!eethe +iagram elo(9, ho( can it +etermine (hich one i! eing calle+> In that

    &'oo:concat+(mlns:'oo)*http://ttdev.com/ss*xmlns:xsd="http://.!.org/##1/$%&'chema"xmlns:xsi="http://.!.org/##1/$%&'chema()nstance"+&s1 xsi:type="xsd:string"+a,c&/s1+&s2 xsi:type="xsd:string"+123&/s2+

    &/'oo:concat+

    Thi! attriute i! u!e+ to eplicitly !tate the /=< +atatype o% the o+y o% an element 8ac here9' Thi! i!u!e%ul (hen the element 8L!1M9 it!el% i! not +e%ine+in a !chema' Thi! type attriute i! +e%ine+ in thehttp"))((('('org)2001)/=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    21/262

    20 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    ca!e, it (ill !ee i% the input me!!age i! a Lconcat:eEue!tM or aL!ome6lementM to +etermine' hat i% oth take a L!ome6lementM> Then it i!

    an error an+ it (on$t (ork"

    $ort type

    #ctually, a (e !ervice +oe!n$t +irectly contain a li!t o% operation!' In!tea+ 8!eethe +iagram elo(9, operation! are groupe+ into one or more port type!' #port type i! like a 7ava cla!! an+ each operation in it i! like a !tatic metho+' oreample, in the (e !ervice aove, you coul+ have a port type name+!tringtil containing operation! %or !tring!, (hile having another port typename+ +atetil containing operation! %or +ate!' The name o% a port type mu!t

    al!o e a *ame"

    #n operation

    Local name: ,arNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: ,arRe-uestlement: somelement in http://ttdev.com/ss

    %utput message:...

    ...

    # !chema

    # (e !ervice

    #n operation

    Local name: concatNamespace: http://ttdev.com/ss"nput message:

    Part 1:Name: concatRe-uestlement: concatRe-uest in http://ttdev.com/ss

    %utput message:...

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    22/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 21

    (inding

    #ctually, a port type may allo( you to acce!! it u!ing +i%%erent me!!age %ormat!'The me!!age %ormat that you have !een i! calle+ the .imple ;@ect #cce!!Protocol 8.;#P9 %ormat' It i! po!!ile that, !ay, the !tringtil port type mayal!o !upport a plain tet %ormat"

    In a++ition to the me!!age %ormat, a port type may allo( the me!!age to ecarrie+ 8tran!porte+9 in an ATTP P;.T reEue!t or in an email' 6ach !upporte+

    comination i! calle+ a in+ing"

    #n operation

    Local name: ,arNamespace: http://ttdev.com/ss...

    ...

    # !chema

    # (e !ervice

    #n operation

    Local name: concatNamespace: http://ttdev.com/ss...

    # port type

    Local name: stringtilNamespace: http://ttdev.com/ss

    # port type

    Local name: datetilNamespace: http://ttdev.com/ss

    #n operation

    Local name: ...Namespace: http://ttdev.com/ss...

    #n operation

    Local name: ...Namespace: http://ttdev.com/ss...

    concats1)a,c s2)1234

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    23/262

    22 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    hat in+ing! !houl+ your port type !upport> .;#POATTP i! the mo!tcommon comination' .o, you !houl+ proaly u!e thi! in+ing in practice'

    $ort

    .uppo!e that there are @u!t too many people u!ing your (e !ervice, you +eci+eto make it availale on more than one computer!' or eample 8!ee the+iagram elo(9, you may +eploy the aove in+ing 1 on computer! c1, c2 an+c an+ +eploy in+ing 2 on c' In that ca!e it i! !ai+ that you have %our port!'Three port! are u!ing in+ing 1 an+ one u!ing in+ing 2"

    concat...

    Port type" !tringtil

    P%!# /56!77/test/ts.php

    &concatRe-uest+ &s1+a,c&/s1+ &s2+123&/s2+&/concatRe-uest+

    8R%: 9entttdev.com#%: ...

    concats1)a,c s2)1234

    Name: ,inding1Port t$pe:8ormat: !%7P#ransport: ;##P

    Bin+ing

    Name: ,inding2Port t$pe:8ormat: #X##ransport: !#P

    Bin+ing

    ...

    # !chema

    # (e !ervice

    or eample or eample

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    24/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 2

    *ote that it +oe! *;T mean that the reEue!t! receive+ y the!e threecomputer! (ill e %or(ar+e+ to a computer hi+ing ehin+ %or proce!!ing'In!tea+, it mean! that there i! !ome !o%t(are implementing the port typein!talle+ on the!e three computer!' There i! no reEuirement that the !amepiece o% !o%t(are i! in!talle+ onto the +i%%erent computer!' or eample, on c1,

    port 1 may e (ritten in 7ava, (hile on c2, port 2 may e (ritten in CH' Theimportant point i! that they oth !upport the operation! !peci%ie+ in port type!tringtil an+ the me!!age %ormat an+ tran!port !peci%ie+ in the in+ing 1' Port mu!t al!o implement the !ame operation! too 8!ame port type9 ut theme!!age %ormat an+ tran!port are +i%%erent'

    To tell other! aout thi! arrangement, you inclu+e the!e port! in the inter%ace o%the (e !ervice"

    concat...

    Port type" !tringtil

    Name: ,inding1Port t$pe:8ormat: !%7P#ransport: ;##P

    Bin+ing

    Name: ,inding2Port t$pe:8ormat: #X##ransport: !#P

    Bin+ing

    ...

    # !chema

    # (e !ervice

    c1 c2 c

    Port 1 Port 2 Port

    Port

    Deploye+ to

    Deploye+ to

    Deploye+ toDeploye+ to

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    25/262

    2 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    Target namespace

    ?ou have een u!ing the !ame name!pace %or the operation name!, port typename! an+ etc' in thi! (e !ervice' Do they have to e in the !amename!pace> By +e%ault, thi! i! the ca!e" There i! a !ingle name!pace %or a (e!ervice to put the name! into' Thi! i! calle+ the target name!pace %or the (e!ervice"

    concat...

    Port type" !tringtil

    Name: ,inding1Port t$pe:8ormat: !%7P#ransport: ;##P

    Bin+ing

    Name: ,inding2Port t$pe:8ormat: #X##ransport: !#P

    Bin+ing

    ...

    # !chema

    # (e !ervice

    Name: port1

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    26/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 25

    ?ou$ve een u!ing http"))tt+ev'com)!! a! the target name!pace' I! it a goo+choice> Ba!ically a name!pace i! goo+ a! long a! it i! gloally uniEue' .o thi!one !houl+ e goo+' Ao(ever, people may try to +o(nloa+ a (e page %romthi! :

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    27/262

    2 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    Ao(ever, thi! (ill create a lot o% (orkloa+ on you an+ on I#*# 8one regi!trationper company9' #! you have alrea+y regi!tere+ the +omain name %oo'com, it i!

    unlikely that !omeone (ill u!e it in their :*$!' .o, you may (ant to go ahea+an+ u!e %oo'com, or, a! many people +o, %oo-com a! the o@ect type (ithoutregi!tration (ith I#*# an+ hope that there (on$t e any colli!ion'

    #n /=< name!pace mu!t e a :I' ?ou can u!e a :< or a :*' unctionallythere i! no +i%%erence at all' or eample, you may u!e !ay urn"tt+ev'com"!! a!the target name!pace %or your (e !ervice in!tea+ o% http"))tt+ev'com)!! (ithoutchanging any %unctionality'

    By the (ay, i% you are going to lookup re%erence! on :*, +o *;T try to %in+term! like o@ect type or o@ect i+' The o%%icial term! are"

    !)'LBy no( you have %ini!he+ +e!igning the inter%ace %or your (e !ervice"

    urn:is,n:123=>?@AB0

    :* name!pace i+enti%ier8*ID9' Thi! name!pace i! *;Tthe name!pace in /=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    28/262

    Chapter 1 De!igning the inter%ace %or a !imple (e !ervice 24

    It %ully +e!crie! your (e !ervice' Thi! +e!cription language 8term! an+concept!9 i! calle+ .D< 8e .ervice! De!cription

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    29/262

    2 Chapter 1 De!igning the inter%ace %or a !imple (e !ervice

    a *ame uniEuely i+enti%ying it' # *ame ha! a local part an+ an /=&/version+

    &/dependenc$+&/dependencies+

    apache(inc*+ating,pache )nc*+ating Repositoryhttp://people.apache.org/repo/m(inc*+ating(repository/

    &/proFect+

    hat you +i+ (a! to tell the =aven2 plugin to look %or package! in an a++itionalrepo!itory 8http"))people'apache'org)repo)m2-incuating-repo!itory9' .ave the%ile an+ the plugin (ill %ini!h the +o(nloa+ !hortly'

    Thi! i! calle+ the arti%act IDo% the package' It i+enti%ie! a!peci%ic pro+uct create+ ythat organiNation' Think o% ita! the cla!! name in 7ava'

    The key(or+Thi! i! calle+ the groupID o% the package' Itpre!ent! theorganiNation that create+the package' It i! likepackage name in 7ava'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    37/262

    Chapter 2 Implementing a (e !ervice

    !)'L ile or the web service

    .uppo!e that you$+ like to create a (e !ervice +e!crie+ in the previou!chapter"

    To (rite it u!ing the real .D< language, it !houl+ e"

    Name: ...Port t$pe:8ormat: !%7P#ransport: ;##P

    Bin+ing

    Name: ...%perations:

    Name: concat"nput msg:

    Part 1:

    Name: concatRe-uestlement: concatRe-uest element as de'ined in the schema%utput msg:

    Part 1:Name: concatRe-uestlement: concatResponse element as de'ined in the schema

    Port type

    Name: ...

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    38/262

    Chapter 2 Implementing a (e !ervice 4

    Thi! +e%ine! the !chema an+ the port type' To +e%ine the in+ing an+ the port"

    &H(ml version)*1.0* encoding)*#8A*H+&wsdl:de'initions (mlns:soap)*http://schemas.(mlsoap.org/wsdl/soap/*

    (mlns:tns)*http://ttdev.com/ss*(mlns:wsdl)*http://schemas.(mlsoap.org/wsdl/*(mlns:(sd)*http://www.w3.org/2001/XL!chema* name)*!imple!ervice*targetNamespace)*http://ttdev.com/ss*+&wsdl:t$pes+

    &(sd:schematargetNamespace)*http://ttdev.com/ss*(mlns:tns)*http://ttdev.com/ss*+

    &(sd:element name)*concatRe-uest*+&(sd:comple(#$pe+

    &(sd:se-uence+&(sd:element name)*s1* t$pe)*(sd:string*/+&(sd:element name)*s2* t$pe)*(sd:string*/+&/(sd:se-uence+

    &/(sd:comple(#$pe+&/(sd:element+&(sd:element name)*concatResponse* t$pe)*(sd:string*/+

    &/(sd:schema+&/wsdl:t$pes+&wsdl:message name)*concatRe-uest*+

    &wsdl:part name)*concatRe-uest* element)*tns:concatRe-uest* /+

    &/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part name)*concatResponse* element)*tns:concatResponse* /+&/wsdl:message+&wsdl:port#$pe name)*!imple!ervice*+

    &wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+...

    &/wsdl:de'initions+

    Put the !chemainto the Ltype!M

    !ection

    The input me!!agecontain! a !ingle part'The name o% the parti! unimportant'

    concat operation

    The name! o% the port type!, operation!,in+ing! an+ port! (ill e put into thi!name!pace

    #ll the element! an+ element type!+e%ine+ in the !chema (ill e put intothi! name!pace

    The output me!!agecontain! a !ingle part'The name o% the parti! unimportant'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    39/262

    Chapter 2 Implementing a (e !ervice

    In %act, in a .;#P in+ing, you nee+ to !peci%y !ome more +etail!"

    &H(ml version)*1.0* encoding)*#8A*H+&wsdl:de'initions (mlns:soap)*http://schemas.(mlsoap.org/wsdl/soap/*

    (mlns:tns)*http://ttdev.com/ss*(mlns:wsdl)*http://schemas.(mlsoap.org/wsdl/*(mlns:(sd)*http://www.w3.org/2001/XL!chema* name)*!imple!ervice*targetNamespace)*http://ttdev.com/ss*+&wsdl:t$pes+

    ...&/wsdl:t$pes+&wsdl:message name)*concatRe-uest*+

    &wsdl:part name)*concatRe-uest* element)*tns:concatRe-uest* /+&/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part name)*concatResponse* element)*tns:concatResponse* /+&/wsdl:message+

    &wsdl:port#$pe name)*!imple!ervice*+&wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+&wsdl:,inding name)*!imple!ervice!%7P* t$pe)*tns:!imple!ervice*+

    &soap:,inding st$le)*document*transport)*http://schemas.(mlsoap.org/soap/http* /+

    &/wsdl:,inding+&wsdl:service name)*!imple!ervice*+

    &wsdl:port ,inding)*tns:!imple!ervice!%7P*name)*p1*+&soap:address

    location)*http://localhost:A0A0/ss/p1* /+&/wsdl:port+

    &/wsdl:service+&/wsdl:de'initions+

    Thi! in+ingimplement! thi!port type

    The port

    The en+point o% the port

    The port !upport! thi! in+ing

    ?ou$ll +eploy it onyour o(ncomputer'

    The in+ing u!e! the .;#P %ormatan+ ATTP tran!port' .;#P!upport! :PC an+ +ocument !tyle!'Aere you u!e the +ocument !tyle'

    ?ou can u!e anything a! the path, ut it i! a goo+convention to inclu+e the !ervice name 8here a!horthan+ !! i! u!e+9 an+ the port name !o that,%or eample, you coul+ +eploy another port p2 %orthe !ame !ervice on the !ame ho!t 8)!!)p29 or+eploy a p1 port %or another !ervice 8)!2)p19'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    40/262

    Chapter 2 Implementing a (e !ervice 3

    &$C version o the web service

    I% the (e !ervice (a! a :PC !tyle !ervice, then the .D< %ile (oul+ e like"

    &wsdl:de'initions ...+...&wsdl:message name)*concatRe-uest*+

    &wsdl:part name)*concatRe-uest* element)*tns:concatRe-uest* /+&/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part name)*concatResponse* element)*tns:concatResponse * /+&/wsdl:message+...&wsdl:,inding name)*!imple!ervice!%7P* t$pe)*tns:!imple!ervice*+

    &soap:,inding st$le)*document*transport)*http://schemas.(mlsoap.org/soap/http* /+

    &wsdl:operation name)*concat*+&soap:operation

    soap7ction)*http://ttdev.com/ss/concat* /+&wsdl:input+

    &soap:,od$ parts)*concatRe-uest* use)*literal* /+&/wsdl:input+&wsdl:output+

    &soap:,od$ parts)*concatResponse* use)*literal* /+&/wsdl:output+

    &/wsdl:operation+&/wsdl:,inding+...

    &/wsdl:de'initions+

    The !oap action i! u!e+

    to tell the ATTP !erverthat it i! a .;#Pme!!age an+ it!purpo!e' It i! up to theATTP !erver tointerpret the actualmeaning' In your ca!e,it i! u!ele!! ecau!e#i! (ill han+le the.;#P me!!age, notTomcat'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    41/262

    0 Chapter 2 Implementing a (e !ervice

    #! :PC !tyle i! not goo+ %or interoperaility, you$ll continue to u!e the +ocument!tyle ver!ion'

    Creating the !)'L ile visually

    It may e error prone to manually create !uch a .D< %ile' In!tea+, you may

    &wsdl:de'initions ...+&wsdl:t$pes+

    &(sd:schema ...+&(sd:element name)*concatRe-uest*+

    &(sd:comple(#$pe+&(sd:se-uence+&(sd:element name)*s1* t$pe)*(sd:string*/+&(sd:element name)*s2* t$pe)*(sd:string*/+&/(sd:se-uence+

    &/(sd:comple(#$pe+&/(sd:element+&(sd:element name)*concatResponse* t$pe)*(sd:string*/+

    &/(sd:schema+&wsdl:t$pes/+&wsdl:message name)*concatRe-uest*+

    &/wsdl:message+&wsdl:message name)*concatResponse*+

    &/wsdl:message+&wsdl:port#$pe name)*!imple!ervice*+

    &wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+&wsdl:,inding name)*!imple!ervice!%7P* t$pe)*tns:!imple!ervice*+

    &soap:,inding st$le)*rpc*

    transport)*http://schemas.(mlsoap.org/soap/http* /+&wsdl:operation name)*concat*+&soap:operation

    soap7ction)*http://ttdev.com/ss/concat* /+&wsdl:input+

    &soap:,od$ parts)*s1 s* use)*literal* /+&/wsdl:input+&wsdl:output+

    &soap:,od$ parts)*ret*rn* use)*literal* /+&/wsdl:output+

    &/wsdl:operation+&/wsdl:,inding+...

    &/wsdl:de'initions+

    Don$t nee+ the!eany more

    The input me!!age ha! t(o part!'6ach part i! o% element type!+"!tring 8not element!9'

    :PC !tyle

    T(o me!!age part! are li!te+' .o, they(ill e inclu+e+ into the LBo+yM 8ut not+irectly9' #! it i! a :PC !tyle !ervice, thecaller mu!t create an element (ith the*ame o% the operation an+ then a++each me!!age part li!te+ here a! a chil+element' .o it !houl+ !till have a !ingleelement in the LBo+yM"

    &soapenv:nvelope(mlns:soapenv)*http://schemas.(mlsoap.org/soap/envelope/*+&soapenv:;eader+

    ...&/soapenv:;eader+&soapenv:

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    42/262

    Chapter 2 Implementing a (e !ervice 1

    u!e the 6clip!e to +o it' ir!t, create a ne( %ol+er !rc)main)re!ource! in the rooto% your pro@ect' *et, right click on that %ol+er an+ choo!e *e( R ;ther an+ then

    e .ervice! R .D

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    43/262

    2 Chapter 2 Implementing a (e !ervice

    Click *et' Then input a! !ho(n elo("

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    44/262

    Chapter 2 Implementing a (e !ervice

    Click ini!h' Then you (ill !ee !omething like"

    Target name!pace %or the .D< %ile

    :ememer, you$re u!ing the+ocument !tyle 8the only inputme!!age part i! the (holeme!!age9 an+ literal u!e %orthat part'

    !e the .;#P %ormat

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    45/262

    Chapter 2 Implementing a (e !ervice

    Thi! i! the .D< co+e' To e+it it vi!ually, click the De!ign ta at the ottom o%the e+itor (in+o(' Then you$ll !ee"

    Doule click on the en+point to change it to http"))localho!t"00)!!)p1"

    The !ervice

    # port' # !ervicemay contain oneor more port!'

    6n+point o% theport

    # in+ing 8.;#Pan+ ATTP9 Port type

    #n operation' # porttype may containone or moreoperation!'

    Part name /=< element nameor element type %orthat part

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    46/262

    Chapter 2 Implementing a (e !ervice 5

    Doule click on the name o% the port an+ change it to p1"

    Doule click on the name o% the operation an+ change it to concat"

    or the moment, the input part i! an LconcatM element' ?ou$+ like to change itto Lconcat:eEue!tM' But %or no(, put the cur!or on the arro( to it! right %ir!t'The arro( (ill turn into lue color' ait a couple o% !econ+! then a previe((in+o( (ill appear !ho(ing the +e%inition o% the LconcatM element"

    .et the name o% the operation'The /=< element name! %orthe input an+ output part! (ille change+ automatically"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    47/262

    Chapter 2 Implementing a (e !ervice

    Clicking any(here el!e (ill make that previe( (in+o( +i!appear' To e+it the!chema +e%inition, click on the lue arro(' # ne( e+itor (in+o( (ill appear"

    To e+it it vi!ually, click the De!ign ta at the ottom, you$ll !ee"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    48/262

    Chapter 2 Implementing a (e !ervice 4

    Doule click on in an+ change it to !1"

    :ight click it an+ choo!e In!ert 6lement R #%ter an+ !et the name to !2"

    By +e%ault the type i! alrea+y !et to !tring' I% you (ante+ it to e, !ay, an intin!tea+, you (oul+ +oule click on the type an+ it (oul+ ecome a como oan+ then you coul+ choo!e int"

    I% you (ante+ !2 to appear e%ore !1 in the !eEuence, you coul+ +rag it an+

    +rop it e%ore !1"

    But %or no(, make !ure it i! !1 %ir!t an+ then !2' *et, right click on theLconcatM element an+ choo!e :e%actor R :ename, then change it! name toconcat:eEue!t"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    49/262

    Chapter 2 Implementing a (e !ervice

    ?ou$re +one (ith the Lconcat:eEue!tM element' *o( return to the .D< e+itorto (ork on the re!pon!e me!!age' or the moment, the Lconcat:e!pon!eM i!like"

    That i!, it i! an element that contain! a !eEuence o% LoutM element"

    &'oo:concatResponse+&'oo:out+a,c&/'oo:out+

    &/'oo:concatResponse+

    Ao(ever, in your +e!ign, the re!pon!e i! !imple type element, not a comple

    type element"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    50/262

    Chapter 2 Implementing a (e !ervice 3

    To +o that, go into the !chema e+itor to e+it the Lconcat:e!pon!eM element"

    :ight click it an+ choo!e .et Type R Bro(!e"

    Choo!e !tring"

    Then it (ill e like"

    ?ou can al!o type ! !o that only tho!e !tarting (ith! (ill e li!te+

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    51/262

    50 Chapter 2 Implementing a (e !ervice

    That$! it' To revie( the (hole !chema, click on the icon at the upper le%t corner"

    Then you$ll !ee"

    Thi! look! %ine' *o(, !ave the %ile'

    -alidating the !)'L ile

    The net !tep i! to vali+ate the .D< %ile to make !ure it con%orm! to thevariou! (e !ervice! !tan+ar+!' To +o that, right click the .imple.ervice'(!+l

    %ile in 6clip!e an+ choo!e Fali+ate' I% there (ere anything (rong, they (oul+ ereporte+ in the Prolem! (in+o(' or eample, here I ha+ intro+uce+ an errorinto the %ile"

    Click it to !ee the (hole!chema

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    52/262

    Chapter 2 Implementing a (e !ervice 51

    *o(, correct the error an+ vali+ate it again' It !houl+ pa!! (ithout any error!'

    .enerating the service code

    #! mentione+ e%ore, a (e !ervice lirary !uch a! #pache C/ can create aconverter to convert an incoming .;#P me!!age into a 7ava o@ect to epa!!e+ a! a metho+ argument' To generate thi! co+e, create a !rc)main)@ava

    %ol+er an+ then right click the pro@ect root an+ choo!e =aven R p+ate Pro@ectCon%iguration' Thi! (ill turn that @ava %ol+er into a !ource %ol+er in 6clip!e 8!othe 7ava cla!! %ile! in it (ill e compile+9'

    *et, in that !rc)main)@ava %ol+er, create a 7ava cla!! a! !ho(n elo("

    The in+ing name! no longermatch'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    53/262

    52 Chapter 2 Implementing a (e !ervice

    :un it a! a 7ava application' I% you receive an error !uch a!@ava'lang'#!tract=etho+6rror in a cla!! !ome(here in!i+e theorg'apache'erce! package, it mean! that the /erce! lirary rought in y#pache C/ i! too ol+ %or your computer 8(hich ha! it! o(n ' To %i thi!

    prolem, a++ a ne( =aven +epen+ency"

    Group ID erce!

    #rti%act ID erce!Impl

    Fer!ion # recent ver!ion !uch a! 2'3'1

    #%ter running it !ucce!!%ully, right click the pro@ect an+ choo!e :e%re!h' ?ou!houl+ !ee a com'tt+ev'!! package ha! een create+ an+ that there are !ome%ile! in it"

    pac9age com.ttdevI

    import org.apache.c('.tools.wsdlto.6!5L#oJavaI

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 D

    6!5L#oJava.mainnew !tringM D*server**d* *src/main/Fava**src/main/resources/!imple!ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Thi! cla!! come! (ith #pacheC/' It can e u!e+ to convert a

    .D< %ile into 7ava co+e'

    It can e run a! a 7avaapplication y a u!er' Aere,call it! main89 metho+ %romyour o(n program'

    Generate 7ava co+e%or the !erver 8i'e',

    the !ervice9' I% you!peci%y -client, it (illgenerate 7ava co+e%or the client'

    Tell it to put the %ile! into the !rc)main)@ava%ol+er' Thi! i! a relative path' hen thi!program i! run in 6clip!e, the current %ol+er(ill e the pro@ect root' .o thi! relative pathi! correct'

    The mo!t important part" Tell the itthe path to the .D< %ile !o that itcan rea+ that %ile' #gain, thi! pathi! a relative path %rom the pro@ectroot'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    54/262

    Chapter 2 Implementing a (e !ervice 5

    The %ile! are marke+ a! in error ecau!e (hen you up+ate+ the pro@ectcon%iguration, the =aven 6clip!e plugin !et the pro@ect to u!e 7ava 1' (hich

    +oe!n$t !upport annotation!' To %i the prolem, mo+i%y the pom'ml %ile a!!ho(n elo("

    &proFect ...+...&dependencies+

    ...&/dependencies+&repositories+

    ...&/repositories+

    org.apache.maven.pl*ginsmaven(compiler(pl*gin

    1.1.

    &/proFect+

    Thi! tell! =aven to u!e 7ava 1' 8i'e', 7ava 9 %or thi! pro@ect' Then, up+ate thepro@ect con%iguration again an+ all the compile error! (ill e gone ecept %or the

    .imple.erviceSP1S.erver %ile, (hich i! a !imple cla!! to launch your (e!ervice"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    55/262

    5 Chapter 2 Implementing a (e !ervice

    It i! in error ecau!e there i! no !uch a .imple.erviceImpl cla!! yet' .o, createthi! cla!! in the com'tt+ev'!! package"

    pac9age com.ttdev.ssI

    import Fava(.(ml.ws.ndpointI

    pu,lic class !imple!erviceOP1O!erver D

    protected !imple!erviceOP1O!erver4 throws (ception D!$stem.out.println*!tarting !erver*4I%,Fect implementor ) new !imple!ervice"mpl4I!tring address ) *http://localhost:A0A0/ss/p1*Indpoint.pu,lishaddress implementor4I

    E

    pu,lic static void main!tring argsM4 throws (ception Dnew !imple!erviceOP1O!erver4I!$stem.out.println*!erver read$...*4I#hread.sleep> ?0 10004I!$stem.out.println*!erver e(iting*4I!$stem.e(it04I

    EE

    Thi! cla!! i! to e implemente+ yyou' It nee+! to implement theconcat89 metho+'

    The en+point a++re!!taken %rom the .D(!+l 8that i!, the en+point a++re!! (ith a Euery

    parameter name+ (!+l appen+e+9' Then it !houl+ return the .D< %ile to thero(!er"

    To en+ the !ervice, @u!t (ait 5 minute! or kill the application in 6clip!e yclicking the re+ utton in the con!ole (in+o('

    Creating a client

    To call thi! (e !ervice, again you can a!k #pache C/ to generate aconverter running on the client !i+e 8calle+ a !ervice !tu9' hen you call ametho+ on the !tu, it (ill convert your 7ava +ata)o@ect! into the right %ormat8/=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    59/262

    5 Chapter 2 Implementing a (e !ervice

    .impleClient' Then mo+i%y the Co+eGenerator cla!! !o that it convert! the.D< %ile to 7ava co+e %or the client, not %or the !ervice"

    pac9age com.ttdevI

    import org.apache.c('.tools.wsdlto.6!5L#oJavaI

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 D

    6!5L#oJava.mainnew !tringM D*client**d* *src/main/Fava**src/main/resources/!imple!ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Delete the (hole com'tt+ev'!! a! the client !houl+n$t have acce!! to the co+eimplementing the !ervice 8(hich coul+ have een (ritten in, !ay, CH9J all it ha!i! acce!! to the .D< %ile'

    *o(, run the Co+eGenerator cla!! an+ it !houl+ generate !ome %ile! into thecom'tt+ev'!! package 8re%re!h the pro@ect to !ee them9' #mong them, the.imple.erviceSP1SClient cla!! i! the client"

    *et, mo+i%y the co+e to create a Concat:eEue!t o@ect"

    pu,lic 'inal class !imple!erviceOP1OClient D

    pac9age com.ttdev.ssI...pu,lic 'inal class !imple!erviceOP1OClient D

    private static 'inal Name !RG"CON7 ) new Name*http://ttdev.com/ss**!imple!ervice*4I

    private !imple!erviceOP1OClient4 DEpu,lic static void main!tring argsM4 throws (ception D

    RL wsdlRL ) !imple!erviceO!ervice.6!5LOL%C7#"%NIi' args.length + 04 D

    8ile wsdl8ile ) new 8ileargs0M4Itr$ D

    i' wsdl8ile.e(ists44 DwsdlRL ) wsdl8ile.toR"4.toRL4I

    E else DwsdlRL ) new RLargs0M4I

    EE catch al'ormedRL(ception e4 De.print!tac9#race4I

    EE!imple!erviceO!ervice ss ) new !imple!erviceO!ervicewsdlRL !RG"CON74I!imple!ervice port ) ss.getP14ID

    !$stem.out.println*"nvo9ing concat...*4Icom.ttdev.ss.ConcatRe-uest OconcatOparameters ) nullIFava.lang.!tring OconcatOOreturn ) port.concatOconcatOparameters4I!$stem.out.println*concat.result)* Q OconcatOOreturn4I

    E!$stem.e(it04I

    EE

    Thi! co+e i! to allo( you to !peci%yanother .D< %ile on thecomman+ line'

    Create the !ervice !tu' It i! !imulatingthe !ervice on the client !i+e' Thi! i!the mo!t important thing'

    ?ou can !peci%y a path to the .D.1/repositor$/modules/scripting1.>.1.mar"N8%M 5eplo$ing module: soapmonitor1.>.1 'ile:/home/9ent/a(is2

    1.>.1/repositor$/modules/soapmonitor1.>.1.mar"N8%M 5eplo$ing module: addressing1.>.1 'ile:/home/9ent/a(is21.>.1/repositor$/modules/addressing1.>.1.mar"N8%M 5eplo$ing module: ping1.>.1 'ile:/home/9ent/a(is21.>.1/repositor$/modules/ping1.>.1.mar"N8%M 5eplo$ing module: metadata(change1.>.1 'ile:/home/9ent/a(is21.>.1/repositor$/modules/me(1.>.1.mar"N8%M 5eplo$ing module: metadata(change1.>.1 'ile:/home/9ent/a(is21.>.1/li,/me(1.>.1.Far"N8%M 5eplo$ing 6e, service: version.aar 'ile:/home/9ent/a(is21.>.1/repositor$/services/version.aar"N8%M !imple7(is!erverM !tarted!imple7(is!erverM !tarted"N8%M Listening on port A0A0

    To +eploy your (e !ervice, right click the pro@ect an+ choo!e :un #! R =avenpackage' It (ill create a %ile .imple.ervice-0'0'1-.*#P.A;T'@ar in the target%ol+er in your pro@ect' Thi! @ar %ile comine! the cla!! %ile! compile+ %rom!rc)main)@ava an+ the %ile! in !rc)main)re!ource! 8!ee elo(9'

    To +eploy thi! @ar %ile, copy it into the ai!)repo!itory)!ervice! %ol+er an+ renamerename it to have an 'aar eten!ion 8aar !tan+! %or #i!2 #:chive9, !uch a!.imple.ervice'aar' *ote that thi! can e +one (hile #i!2 !erver i! !till running8hot +eployment9' The #i!2 !erver (ill pick up your 'aar %ile an+ +eploy it"

    ...B)N@8C eploying 0e+ service: 'imple'ervice.aar ( -ile:/home/Dent/axis(1..1/repository/services/'imple'ervice.aar...

    To te!t it, open a ro(!er an+ acce!! http"))localho!t"00' ?ou !houl+ !ee"

    main

    com

    ttdev

    ss

    ...

    #7"N8

    com

    ttdev

    ss

    ...Compile the 7ava%ile! into 'cla!! %ile!'

    The @ar %ile

    ...

    ...

    Fava services.(ml

    !imple!ervice.wsdl

    resources

    services.(ml!imple!ervice.wsdl

    Copy thecon%iguration %ile!'

    !imple!ervice

    src

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    66/262

    Chapter 2 Implementing a (e !ervice 5

    To !ee it! .D< %ile, @u!t click the .imple.ervice link'

    Creating a client using Apache Axis2To create a client u!ing #pache #i!2, copy the #i!2.imple.ervice pro@ect an+pa!te it a! a ne( pro@ect name+ #i!2.impleClient' Then a++ t(o ne(+epen+encie! in pom'ml"

    &proFect ...+...&dependencies+

    &dependenc$+&group"d+(erces&/group"d+&arti'act"d+(erces"mpl&/arti'act"d+&version+2.B.1&/version+

    &/dependenc$+&dependenc$+&group"d+org.apache.a(is2&/group"d+&arti'act"d+a(is2codegen&/arti'act"d+&version+1.>.1&/version+

    &/dependenc$+&dependenc$+

    &group"d+org.apache.a(is2&/group"d+&arti'act"d+a(is2ad,codegen&/arti'act"d+&version+1.>.1&/version+

    &/dependenc$+

    org.apache.axisaxis(transport(http

    1..1

    org.apache.axisaxis(transport(local1..1

    &/dependencies+...

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    67/262

    Chapter 2 Implementing a (e !ervice

    &/proFect+

    Then mo+i%y the Co+eGenerator cla!! !o that it convert! the .D< %ile to 7ava

    co+e %or the client, not %or the !ervice"pac9age com.ttdevI

    import org.apache.a(is2.wsdl.6!5L2CodeI

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 throws (ception D

    6!5L2Code.mainnew !tringM D*ss**sd**!* *src/main/Fava**R* *src/main/resources/#7"N8**ns2p* *http://ttdev.com/ss)com.ttdev.ss*

    *uri* *src/main/resources/!imple!ervice.wsdl* E4I!$stem.out.println*5one*4IE

    E

    Delete the (hole com'tt+ev'!! an+ run the Co+eGenerator cla!!' It !houl+generate !ome %ile! into the com'tt+ev'!! package 8re%re!h the pro@ect to !eethem9' #mong them, the .imple.ervice.tu cla!! i! the client !tu'

    *et, create a .impleClient'@ava %ile in the com'tt+ev'!! package"

    :un it an+ it !houl+ print ac12 !ucce!!%ully'

    +ndeploying a web service rom Axis2

    I% you$+ like to un+eploy a (e !ervice %rom the #i!2 !erver, all you nee+ to +o

    pac9age com.ttdev.ssI...import com.ttdev.ss.!imple!ervice!tu,.ConcatRe-uestIimport com.ttdev.ss.!imple!ervice!tu,.ConcatResponseI

    pu,lic class !impleClient D

    pu,lic static void main!tringM args4 throws Remote(ception D!imple!ervice!tu, service ) new !imple!ervice!tu,

    *http://localhost:A0A0/a(is2/services/!imple!ervice*4IConcatRe-uest re-uest ) new ConcatRe-uest4I

    re-uest.set!1*a,c*4Ire-uest.set!2*123*4IConcatResponse response ) service.concatre-uest4I!$stem.out.printlnresponse.getConcatResponse44I

    EE *ote that thi! i! the en+point, not

    http"))localho!t"00)!!)p1' Thi! i!ecau!e your !ervice mu!t e runin!i+e the #i!2 !erver an+ it+etermine! the :< %or you'

    &wsdl:de'initions ...+...&wsdl:service name)*!imple!ervice*+

    &wsdl:port ,inding)*tns:!imple!ervice!%7P* name)*p1*+&soap:address

    location)*http://localhost:A0A0/ss/p1* /+&/wsdl:port+

    &/wsdl:service+&/wsdl:de'initions+

    Thi! i! the name o% the !ervice a!+e%ine+ in the .D< %ile'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    68/262

    Chapter 2 Implementing a (e !ervice 4

    i! to +elete the 'aar %ile' Thi! (ork! even (hen the #i!2 !erver i! running'

    )ummary

    =o!t u!ually your input me!!age or output me!!age i! !ent in a .;#Pme!!age' # .;#P me!!age i! al(ay! an L6nvelopeM element' It may contain aLAea+erM (hich contain! one or more hea+er entrie!)element!' TheL6nvelopeM mu!t contain a LBo+yM (hich may contain one or more o+yentrie!)element!' or a +ocument !tyle (e !ervice, the one an+ only inputme!!age part i! u!ually the !ingle o+y entry' or a :PC !tyle (e !ervice, theelement name+ a%ter the operation (ill u!ually contain all me!!age part! an+ i!

    then inclu+e+ a! the !ingle o+y entry'

    # (e !ervice lirary !uch a! #pache C/ an+ #i!2 (ill convert the /= Thi! (oul+ make it a little it ea!ier to (rite theconcat89 metho+ a! you (oul+n$t nee+ to call get.189 an+ get.289 anymore"

    To tell #pache C/ to +o that, you nee+ to make t(o change! to the .D< %ile"

    &'oo:concatRe-uest (mlns:'oo)*http://ttdev.com/ss*+ &s1+a,c&/s1+ &s2+123&/s2+&/'oo:concatRe-uest+

    .;#P me!!age

    ConcatResponse concatConcatRe-uest r4 D...

    E

    The (hole /=< elementi! converte+ into a !ingle

    7ava o@ect'

    &'oo:concatRe-uest (mlns:'oo)*http://ttdev.com/ss*+ &s1+a,c&/s1+ &s2+123&/s2+&/'oo:concatRe-uest+

    .;#P me!!age

    !tring concat!tring s1 !tring s24 D...

    E

    6ach chil+ element i!converte+ into a 7avao@ect'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    78/262

    Chapter #ccepting multiple parameter! 44

    .imilarly, %or the output me!!age, you hope to have #pache C/ (rap thereturn value a! a chil+ element"

    To +o that, in the .D< %ile, the element name o% the output me!!age mu!t ethe name o% the operation (ith the (or+ :e!pon!e appen+e+ an+ it mu!t e a!eEuence 8containing a !ingle chil+ element9"

    &H(ml version)*1.0* encoding)*#8A*H+&wsdl:de'initions ...+

    &wsdl:t$pes+&(sd:schema ...+

    &(sd:element name)*concatRe-uest concat*+&(sd:comple(#$pe+

    &(sd:se-uence+&(sd:element name)*s1* t$pe)*(sd:string* /+&(sd:element name)*s2* t$pe)*(sd:string* /+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+&(sd:element name)*concatResponse* t$pe)*(sd:string* /+

    &/(sd:schema+

    &/wsdl:t$pes+&wsdl:message name)*concatRe-uest*+&wsdl:part name)*parameters* element)*tns:concatRe-uest concat* /+

    &/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part name)*parameters* element)*tns:concatResponse* /+&/wsdl:message+&wsdl:port#$pe name)*!imple!ervice*+

    &wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+

    ...&/wsdl:de'initions+

    =ake !ure the elementname o% that !ingle part inthe input me!!age i! the!ame a! that o% the

    operation'

    The element mu!t e a !eEuence,(hich i! in+ee+ the ca!e here'

    &'oo:concatResponse (mlns:'oo)*http://ttdev.com/ss*+ &r+a,c123&/r+&/'oo:concatResponse+

    .;#P me!!age

    !tring concat!tring s1 !tring s24 D...

    E

    The return value i!converte+ into a chil+element'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    79/262

    4 Chapter #ccepting multiple parameter!

    Thi! !tyle o% parameter han+ling i! calle+ (rappe+ !tyle or (rapper !tyle' Incontra!t, pa!!ing the (hole /=< element a! the !ingle parameter i! calle+ theare !tyle'

    *ote that thi! !ervice +e!crie+ y thi! .D< %ile i! !till a 100 +ocument !tyle!ervice' The client! can !till call it the !ame (ay 8ecept that Lconcat:eEue!tMi! change+ to LconcatM9' The +i%%erence i! ho( the #pache C/ runtime call!your implementation an+ ho( it han+le! your return value' There i! no+i%%erence !een y the client'

    To implement thi! i+ea, mo+i%y the .imple.ervice'(!+l %ile 8in the!rc)main)re!ource! %ol+er9"

    &H(ml version)*1.0* encoding)*#8A* standalone)*no*H+&wsdl:de'initions (mlns:soap)*http://schemas.(mlsoap.org/wsdl/soap/*

    (mlns:tns)*http://ttdev.com/ss* (mlns:wsdl)*http://schemas.(mlsoap.org/wsdl/*(mlns:(sd)*http://www.w3.org/2001/XL!chema* name)*0rapped'ervice*targetNamespace)*http://ttdev.com/ss*+

    &wsdl:t$pes+&(sd:schema targetNamespace)*http://ttdev.com/ss*+

    &(sd:element name)*concat*+&(sd:comple(#$pe+

    &(sd:se-uence+&(sd:element name)*s1* t$pe)*(sd:string* /+&(sd:element name)*s2* t$pe)*(sd:string* /+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &H(ml version)*1.0* encoding)*#8A*H+&wsdl:de'initions ...+

    &wsdl:t$pes+&(sd:schema ...+

    &(sd:element name)*concat*+&(sd:comple(#$pe+

    &(sd:se-uence+&(sd:element name)*s1* t$pe)*(sd:string* /+&(sd:element name)*s2* t$pe)*(sd:string* /+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+

    &/(sd:schema+

    &/wsdl:t$pes+&wsdl:message name)*concatRe-uest*+

    &wsdl:part name)*parameters* element)*tns:concat* /+&/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part name)*parameters* element)*tns:concatResponse* /+&/wsdl:message+&wsdl:port#$pe name)*!imple!ervice*+

    &wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+...

    &/wsdl:de'initions+

    The element name mu!t econcat O :e!pon!e, (hichhappen! to e the ca!ealrea+y'

    It mu!t not e a !imple type!uch a! !tring' It mu!t e a!eEuence'

    The !eEuence mu!tcontain a !ingle element'The element name 8LrM

    here9 i! unimportant'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    80/262

    Chapter #ccepting multiple parameter! 43

    &/(sd:element+

    &/(sd:schema+

    &/wsdl:t$pes+&wsdl:message name)*concatRe-uest*+

    &wsdl:part element)*tns:concat* name)*parameters* /+&/wsdl:message+&wsdl:message name)*concatResponse*+

    &wsdl:part element)*tns:concatResponse* name)*parameters* /+&/wsdl:message+&wsdl:port#$pe name)*0rapped'ervice*+

    &wsdl:operation name)*concat*+&wsdl:input message)*tns:concatRe-uest* /+&wsdl:output message)*tns:concatResponse* /+

    &/wsdl:operation+&/wsdl:port#$pe+&wsdl:,inding name)*0rapped'ervice'8,P* t$pe)*tns:0rapped'ervice*+

    &soap:,inding st$le)*document*transport)*http://schemas.(mlsoap.org/soap/http* /+

    &wsdl:operation name)*concat*+&soap:operation soap7ction)*http://ttdev.com/ss/New%peration* /+&wsdl:input+

    &soap:,od$ use)*literal* /+

    &/wsdl:input+&wsdl:output+&soap:,od$ use)*literal* /+

    &/wsdl:output+&/wsdl:operation+

    &/wsdl:,inding+&wsdl:service name)*0rapped'ervice*+

    &wsdl:port ,inding)*tns:0rapped'ervice'8,P* name)*p1*+&soap:address location)*http://localhost:A0A0/ss/p1* /+

    &/wsdl:port+&/wsdl:service+

    &/wsdl:de'initions+

    Delete the (hole com'tt+ev'!! package an+ then run the Co+eGenerator

    program again' *ote the .6I generate+ 8the rappe+.ervice inter%ace9"To implement the !ervice, create a rappe+.erviceImpl cla!!"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    81/262

    0 Chapter #ccepting multiple parameter!

    6e,!erviceendpoint"nter'ace)*com.ttdev.ss.6rapped!ervice*4pu,lic class 6rapped!ervice"mpl implements 6rapped!ervice D

    %verridepu,lic !tring concat!tring s1 !tring s24 D

    return s1 Q s2IE

    E

    To create a client, copy the .impleClient pro@ect an+ pa!te it a! rappe+Client'Copy the .imple.ervice'(!+l %rom the rappe+.ervice pro@ect into therappe+Client pro@ect 8in the !rc)main)re!ource! %ol+er9' Delete the (holecom'tt+ev'!! package an+ then run the Co+eGenerator program again' =o+i%ythe client co+e in rappe+.erviceSP1SClient"

    pu,lic 'inal class 6rapped!erviceOP1OClient D...pu,lic static void main!tring argsM4 throws (ception D

    ...6rapped!erviceO!ervice ss ) new 6rapped!erviceO!ervicewsdlRL

    !RG"CON74I6rapped!ervice port ) ss.getP14ID

    !$stem.out.println*"nvo9ing concat...*4IFava.lang.!tring OconcatOs1 ) *a+c*IFava.lang.!tring OconcatOs2 ) *1!*IFava.lang.!tring OconcatOOreturn ) port.concatOconcatOs1

    OconcatOs24I!$stem.out.println*concat.result)* Q OconcatOOreturn4I

    E!$stem.e(it04I

    EE

    *ote that the concat89 metho+ in the !ervice !tu i! no( accepting t(o .tring!,not a comple +ata !tructure'

    inally, run the !ervice an+ then run the client' The client !houl+ print theac12 !ucce!!%ully'

    6e,!ervicetargetNamespace ) *http://ttdev.com/ss* name ) *6rapped!ervice*4Xml!ee7lso D %,Fect8actor$.class E4pu,lic inter'ace 6rapped!ervice D

    6e,Resultname ) *r* targetNamespace ) **4Re-uest6rapper

    localName ) *concat*targetNamespace ) *http://ttdev.com/ss*className ) *com.ttdev.ss.Concat*4

    Response6rapperlocalName ) *concatResponse*targetNamespace ) *http://ttdev.com/ss*className ) *com.ttdev.ss.ConcatResponse*4

    6e,ethodaction ) *http://ttdev.com/ss/New%peration*4pu,lic Fava.lang.!tring concat

    6e,Paramname ) *s1* targetNamespace ) **4 Fava.lang.!tring s1

    6e,Paramname ) *s2* targetNamespace ) **4 Fava.lang.!tring s24IE

    *o( the argument! are.tring!, not a comple+ata !tructure'

    It tell! C/ that (hen a LconcatMelement i! receive+, un(rap it to get it!chil+ element! an+ pa!! them to thi!metho+ a! in+ivi+ual argument!'

    .imilar to reEue!t han+ling, it tell!the C/ runtime to (rap the returnvalue into a Lconcat:e!pon!eMelement'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    82/262

    Chapter #ccepting multiple parameter! 1

    +sing the wrapped style in Axis2

    To +o it in #i!2, copy the #i!2.imple.ervice an+ pa!te it a!#i!2rappe+.ervice, copy the #i!2.impleClient an+ pa!te it a!#i!2rappe+Client' Copy the .imple.ervice'(!+l %ile %rom therappe+.ervice pro@ect into oth o% the!e t(o ne( pro@ect!'

    *et, mo+i%y the Co+eGenerator cla!! in the #i!2rappe+.ervice pro@ect toenale un(rapping"

    pu,lic static void main!tringM args4 throws (ception D6!5L2Code.mainnew !tringM D

    *ss**sd*"(*"*!* *src/main/Fava**R* *src/main/resources/#7"N8**ns2p* *http://ttdev.com/ss)com.ttdev.ss**uri* *src/main/resources/!imple!ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Do the !ame thing in the #i!2rappe+Client pro@ect"

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 throws (ception D

    6!5L2Code.mainnew !tringM D

    "(*"*!* *src/main/Fava**R* *src/main/resources/#7"N8**ns2p* *http://ttdev.com/ss)com.ttdev.ss**uri* *src/main/resources/!imple!ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Delete the com'tt+ev'!! package an+ the !rc)main)re!ource!)=6T#-I* %ol+erin oth pro@ect!' Then run Co+eGenerator in oth pro@ect!' ill in the co+e in the.imple.ervice.keleton cla!!"

    pu,lic class !imple!ervice!9eleton D

    pu,lic Fava.lang.!tring concatFava.lang.!tring s1 Fava.lang.!tring s24 Dreturn s1 Q s2I

    EE

    *ote that the concat89 metho+ i! no( taking t(o .tring! an+ returning a .tring':un the pro@ect a! =aven package' It (ill !till create a .imple.ervice-0'0'1-.*#P.A;T'@ar %ile in the target %ol+er ecau!e the pom'ml %ile !till u!e!.imple.ervice a! the arti%act ID"

    &proFect ...+&modelGersion+=.0.0&/modelGersion+

    &group"d+!imple!ervice&/group"d+&arti'act"d+!imple!ervice&/arti'act"d+&version+0.0.1!N7P!;%#&/version+...

    &/proFect+

    Copy it into the #i!2 !erver 8in the ai!2)repo!itory)!ervice! %ol+er9 a!rappe+.ervice'aar'

    Then, in the client pro@ect, create a rappe+Client cla!!"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    83/262

    2 Chapter #ccepting multiple parameter!

    pu,lic class 6rappedClient Dpu,lic static void main!tringM args4 throws Remote(ception D

    6rapped!ervice!tu, service ) new 6rapped!ervice!tu,

    *http://localhost:A0A0/a(is2/services/6rapped!ervice*4I!$stem.out.printlnservice.concat*a,c* *123*44I

    EE

    :un it an+ it !houl+ print ac12 !ucce!!%ully'

    %nteroperability

    The (rappe+ !tyle i! a goo+ i+ea' It i! the only kin+ o% (e !ervice !upporte+ ythe '*6T %rame(ork' ;viou!ly C/ an+ #i!2 have al!o implemente+ thi!

    !tyle' The goo+ ne(! i!, %rom the vie(point o% the caller, it i! @u!t a+ocumentOliteral !tyle !ervice' .o i% the caller +oe!n$t un+er!tan+ the (rappe+convention, it can !till acce!! it a! a regular +ocument !tyle !ervice'

    )ummary

    The (rappe+ parameter !tyle mean! that the (e !ervice runtime !houl+etract the chil+ /=< element! in the input me!!age an+ pa!! them a!in+ivi+ual argument! to your metho+' It +oe! the oppo!ite (hen it receive! the

    return value" (rap it a! a chil+ /=< element in the output me!!age'To allo( the (rappe+ !tyle, the /=< element in the input me!!age !houl+ e a!eEuence an+ !houl+ have the !ame name a! the operation' or the /=*/+&result"tem product"d)*p03* price)*A*/+

    &/'oo:productuer$Result+

    ?our (e!ervice

    Client

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    87/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    &(sd:schema targetNamespace)*http://'oo.com*+&(sd:element name)*productuer$* t$pe)*tns:productuer$Comple(#$pe*+&/(sd:element+

    &(sd:element name)*productuer$Result*+&(sd:comple(#$pe+

    &(sd:se-uence+&(sd:element name)*result"tem* ma(%ccurs)*un,ounded* min%ccurs)*1*+

    &(sd:comple(#$pe+&(sd:attri,ute name)*product"d* t$pe)*(sd:string* /+&(sd:attri,ute name)*price* t$pe)*(sd:int* /+

    &/(sd:comple(#$pe+&/(sd:element+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:element+&(sd:comple(#$pe name)*productuer$Comple(#$pe*+

    &(sd:se-uence+

    &(sd:element name)*-uer$"tem* ma(%ccurs)*un,ounded* min%ccurs)*1*+&(sd:comple(#$pe+

    &(sd:attri,ute name)*product"d* t$pe)*(sd:string*/+&(sd:attri,ute name)*-t$* t$pe)*(sd:int*/+

    &/(sd:comple(#$pe+&/(sd:element+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:schema+&/wsdl:t$pes+&wsdl:message name)*-uer$Re-uest*+

    &wsdl:part element)*tns:productuer$* name)*parameters* /+&/wsdl:message+&wsdl:message name)*-uer$Response*+

    &wsdl:part element)*tns:productuer$Result* name)*parameters* /+&/wsdl:message+&wsdl:port#$pe name)*

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    88/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 4

    *ote that the name o% it! type (ill e change+ %rom 8EueryType9 to8pro+uctueryType9 automatically'

    #l!o note the meaning o% the parenthe!e!" 8%ooType9 mean! that it i! the

    anonymou! type %or the L%ooM element"

    It i! po!!ile to name thi! type eplicitly' or eample, right click8pro+uctueryType9 an+ choo!e :e%actor R =ake #nonymou! Type Gloal"

    &(sd:element name)*'oo*+&(sd:comple(#$pe+

    ...&/(sd:comple(#$pe+

    &/(sd:element+

    Thi! i! the anonymou!type name+ 8%oo9'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    89/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    Then it (ill e given a name 8y +e%ault, pro+uctueryCompleType9 an+ the

    /=< !chema (ill ecome"

    #ny(ay, no(, :ename the in element to EueryItem"

    or the moment the LEueryItemM element i! a !tring' :ight click on it an+choo!e .et Type R *e("

    &(sd:schema targetNamespace)*http://'oo.com*+&(sd:element name)*productuer$*

    type="tns:prod*ctH*ery5omplex?ype"+&/(sd:element+

    ...

    &/(sd:schema+ The type no( ha! a

    name an+ i! anin+epen+ent entity'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    90/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 3

    Choo!e to create an anonymou! local comple type"

    It (ill e like"

    *et, you$+ like to e+it the 8EueryItemType9' But clicking on it (ill *;T allo( youto e+it it' In!tea+, it (ill only let you choo!e another type %or LEueryItemM"

    ?ou nee+ to e+it it net

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    91/262

    30 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    Thi! i! ecau!e 6clip!e (ill not allo( you to +irectly e+it !omething too +eep'In!tea+, it reEuire! you to +rill +o(n y one level' .o, +oule click onpro+uctueryCompleType 8*ote" *;T 8EueryItemType99 to +rill +o(n' ?ou$ll!ee that the 8EueryitemType9 i! availale %or e+iting"

    :ight click on 8EueryItemType9 an+ choo!e #++ #ttriute"

    :ename the attriute to pro+uctI+' The type i! y +e%ault !tring (hich i! (hatyou (ant"

    .imilarly, a++ another attriute Ety an+ !et it! type to int"

    *o( it i! availale %or e+iting'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    92/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 31

    To tell that there can e 1 to many LEueryItemM element!, right click theLEueryItemM element an+ choo!e .et =ultiplicity R 1''U 8;ne or =ore9"

    ?ou$ll !ee"

    *o(, it i! +one' To return to one level up, click the le%t arro( icon a! i% it (ere aro(!er"

    .imilarly, create the Lpro+uctuery:e!ultM element' #! u!ual, vali+ate it (henyou$re +one'

    *et, mo+i%y the Co+eGenerator cla!!"

    Go ack one !creen a! i% you (erein a ro(!er

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    93/262

    32 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 D

    6!5L#oJava.mainnew !tringM D

    *server*"(p" "http://-oo.com=com.ttdev.+i;"*d* *src/main/Fava**src/main/resources/9i;'ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Delete the (hole com'tt+ev'!! 8i% any9' :un the Co+eGenerator cla!!' *ote thattop level /=< type! have een converte+ to 7ava cla!!e!' I% the /=< type i!name+ !uch a! pro+uctueryCompleType, the name (ill e u!e+ a! the 7avacla!! name' I% an /=< type i! anonymou! !uch a! 8pro+uctuery:e!ult9, theelement name (ill e u!e+ a! the 7ava cla!! name"

    I% you in!pect the Pro+uctueryCompleType cla!!, you$ll note the mapping i!like thi!"

    pu,lic inter'ace

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    94/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 3

    .imilarly, in!pect the Pro+uctuery:e!ult cla!! to !ee ho( a top levelanonymou! /=< type i! mappe+ to a 7ava cla!!"

    &(sd:schema ...+...&(sd:comple(#$pe name)*productuer$Comple(#$pe*+

    &(sd:se-uence+&(sd:element name)*-uer$"tem*

    ma(%ccurs)*un,ounded*min%ccurs)*1*+&(sd:comple(#$pe+

    &(sd:attri,ute name)*product"d* t$pe)*(sd:string* /+&(sd:attri,ute name)*-t$* t$pe)*(sd:int* /+

    &/(sd:comple(#$pe+&/(sd:element+

    &/(sd:se-uence+&/(sd:comple(#$pe+

    &/(sd:schema+

    Xml7ccessor#$peXml7ccess#$pe.8"L54Xml#$pename ) *productuer$Comple(#$pe* prop%rder ) D *-uer$"tem* E4pu,lic class Productuer$Comple(#$pe D

    Xmllementre-uired ) true4protected List&Productuer$Comple(#$pe.uer$"tem+ -uer$"temI...Xml7ccessor#$peXml7ccess#$pe.8"L54Xml#$pename ) **4pu,lic static class uer$"tem D

    Xml7ttri,uteprotected !tring product"dIXml7ttri,uteprotected "nteger -t$I

    ...E

    E

    # name+ /=< type i!mappe+ to a 7avacla!!'

    6ach element in aL!eEuenceM i!

    mappe+ to a %iel+ inthe cla!!' I% thatelement can occurmultiple time! 8i'e',ma;ccur! M 19, thetype o% the %iel+ (ille a

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    95/262

    3 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    Thi! (ay o% mapping et(een /=< an+ 7ava an+ the annotation! like

    &/mlType or &/ml6lement are !tan+ar+iNe+ in the 7ava #rchitecture %or /=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    96/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 35

    Create a pro@ect %or the client an+ name it BiNClient' ill in the co+e in theBiN.erviceSBiN.ervice.;#PSClient cla!!"

    pu,lic 'inal class

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    97/262

    3 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    p01: 20p02: 20

    )ending more data in a message

    By the (ay, thi! Euery operation +emon!trate! a goo+ practice in (e !ervice!"?ou generally hope to !en+ more +ata in a me!!age' or eample, you may e!en+ing many Euery item! in a !ingle re!pon!e me!!age' Thi! i! more e%%icientthan !en+ing a !ingle Euery item o@ect in a me!!age' Thi! i! ecau!e there i!a certain overhea+ involve+ in !en+ing a me!!age, even i% it contain! no +ata"

    &eturning aults

    .uppo!e that a client i! calling your Euery operation ut a pro+uct i+ i! invali+8not @u!t out o% !tock, ut a!olutely unkno(n9 or the Euantity i! Nero ornegative' ?ou may (ant to thro( an eception' To return an eception to theclient, you !en+ a %ault me!!age, (hich i! very much like an output me!!age'To +o that, mo+i%y the .D< %ile"

    ;verhea+

    ;verhea+

    ;verhea+

    Kueryitem

    Kueryitem

    Kueryitem

    ;verhea+

    K

    ueryitem

    Kueryitem

    Kueryitem

    =e!!age 1 =e!!age 2 =e!!age

    # !ingle me!!age

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    98/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 34

    Ao( to inclu+e the %ault me!!age in a .;#P me!!age> It i! inclu+e+ in the.;#P o+y, ut not +irectly"

    &H(ml version)*1.0* encoding)*#8A* standalone)*no*H+&wsdl:de'initions ...+

    &wsdl:t$pes+&(sd:schema ...+

    ...

    &/(sd:schema+&/wsdl:t$pes+&wsdl:message name)*-uer$Re-uest*+

    &wsdl:part element)*tns:productuer$* name)*parameters* /+&/wsdl:message+&wsdl:message name)*-uer$Response*+

    &wsdl:part element)*tns:productuer$Result* name)*parameters* /+&/wsdl:message+

    &wsdl:port#$pe name)*

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    99/262

    3 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    The .;#P LaultM element tell! the caller that !omething i! (rong' TheL%aultco+eM i! a *ame acting a! an error co+e' The L%ault!tringM i! an errorme!!age %or human rea+ing' The L+etailM (ill contain any in%ormation that oth!i+e! agree on' In thi! ca!e, it contain! your %ault me!!age part'

    To make the aove change! to the .D< %ile vi!ually, right click the Eueryoperation an+ choo!e #++ ault"

    &wsdl:de'initions ...+...

    &wsdl:port#$pe name)*

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    100/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 33

    :ight click the %ault me!!age an+ choo!e .ho( propertie!' Then in thePropertie! (in+o(, !et it! name to %01"

    Choo!e to create a ne( me!!age"

    6nter the name %or the me!!age"

    .et the one an+ only part to a ne( /=< element Linvali+Pro+uctI+M' By +e%aultit !houl+ e o% type !+"!tring (hich i! (hat you (ant here' Create the !econ+%ault !imilarly' .et the me!!age name to EueryInvali+ty, !et the /=< elementto Linvali+tyM (ho!e type i! !+"int' inally it !houl+ e like"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    101/262

    100 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    ?ou$ve +one (ith the port type' *et, create the in+ing %or the t(o %ault!'

    Choo!e the in+ing an+ click Generate Bin+ing Content in the Propertie!(in+o("

    Check ;ver(rite ei!ting in+ing in%ormation an+ then click ini!h"

    Choo!e it' It repre!ent!the in+ing

    Click here

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    102/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 101

    Thi! (ill generate the in+ing portion"

    &wsdl:,inding name)*

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    103/262

    102 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    .imilarly, choo!e in+o( R .ho( Fie( R ;utline to !ho( the outline o% the.D< %ile a! !ho(n elo(' :ight click an+ +elete the unu!e+ me!!age! !ucha! EueryS%ault=!g an+ EueryS%ault=!g1"

    *o(, copy the .D< %ile to the BiNClient pro@ect' Then generate the !ervice an+client co+e again' The concat89 in the .6I (ill e +eclare+ a! thro(ing t(o type!o% eception!"

    6e,!ervicetargetNamespace ) *http://'oo.com* name ) *

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    104/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 10

    *o( mo+i%y your implementation co+e"

    6e,!erviceendpoint"nter'ace ) *com.ttdev.,iS.

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    105/262

    10 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    By the (ay, you !houl+ have note+ that the BiN.erviceSBiN.ervice.;#PSClientcla!! ha! een over(ritten (hen you ran the .D

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    106/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 105

    &eerring to existing X0L elements

    or the moment you$re +e%ining /=< element! !uch a! Lpro+uctueryM +irectlyin the .D< %ile' Ao(ever, in practice, mo!t likely !uch element! are +e%ine+ ya r+party !uch a! an in+u!trial con!ortium or neutral a!!ociation' .uppo!e thatthey are provi+e+ in a %ile purcha!ing'!+ !uch a! thi!"

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    107/262

    10 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    Ao( to re%er to tho!e /=< element! in your .D< %ile> ir!t, put thepurcha!ing'!+ %ile into the !ame %ol+er a! the .D< %ile 8i'e',!rc)main)re!ource!9' Then mo+i%y the .D< %ile"

    &H(ml version)*1.0* encoding)*#8A*H+&schema (mlns)*http://www.w3.org/2001/XL!chema*

    targetNamespace)*http://+ar.org/p*rchasing*(mlns:tns)*http://+ar.org/p*rchasing*element8orm5e'ault)*-uali'ied*+&(sd:element name)*productuer$* t$pe)*tns:productuer$Comple(#$pe* /+&(sd:element name)*productuer$Result*+

    &(sd:comple(#$pe+&(sd:se-uence+

    &(sd:element name)*result"tem* ma(%ccurs)*un,ounded*min%ccurs)*1*+&(sd:comple(#$pe+

    &(sd:attri,ute name)*product"d* t$pe)*(sd:string* /+

    &(sd:attri,ute name)*price* t$pe)*(sd:int* /+&/(sd:comple(#$pe+

    &/(sd:element+&/(sd:se-uence+

    &/(sd:comple(#$pe+&/(sd:element+&(sd:comple(#$pe name)*productuer$Comple(#$pe*+

    &(sd:se-uence+&(sd:element name)*-uer$"tem* ma(%ccurs)*un,ounded*

    min%ccurs)*1*+&(sd:comple(#$pe+

    &(sd:attri,ute name)*product"d* t$pe)*(sd:string* /+&(sd:attri,ute name)*-t$* t$pe)*(sd:int* /+

    &/(sd:comple(#$pe+&/(sd:element+

    &/(sd:se-uence+&/(sd:comple(#$pe+&(sd:element name)*invalidProduct"d* t$pe)*(sd:string* /+&(sd:element name)*invalidt$* t$pe)*(sd:int* /+

    &/schema+

    #! they are +e%ine+ y a r+party, it!houl+ u!e a +i%%erent targetname!pace'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    108/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 104

    =o+i%y the Co+eGenerator cla!! to !peci%y a 7ava package %or thehttp"))ar'org)purcha!ing name!pace 8not !trictly reEuire+' Do it only i% you +on$t(ant the +e%ault9"

    pu,lic class CodeKenerator D

    pu,lic static void main!tringM args4 D6!5L#oJava.mainnew !tringM D

    *server**p* *http://'oo.com)com.ttdev.,iS*"(p" "http://+ar.org/p*rchasing=com.ttdev.+i;.p*rchasing"*d* *src/main/Fava**src/main/resources/

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    109/262

    10 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    Copy the .D< an+ /.D %ile! to the BiNClient pro@ect' =o+i%y theCo+eGenerator cla!! to !peci%y the package mapping' Delete the generate+ %ile

    an+ generate the co+e again' :un the BiNClient an+ it !houl+ continue to (ork'

    'oing it in Axis2

    To +o it in #i!2, copy the #i!2.imple.ervice an+ pa!te it a! #i!2BiN.ervice,copy the #i!2.impleClient an+ pa!te it a! #i!2BiNClient' Copy the .D< %ilean+ /.D %ile %rom the BiN.ervice pro@ect into oth o% the!e t(o ne( pro@ect!8an+ +elete the ei!ting .D< %ile!9'

    *et, mo+i%y the Co+eGenerator cla!! in the #i!2BiN.ervice pro@ect a! !ho(nelo(' *ote that the t(o package mapping! are !eparate+ y a comma"

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 throws (ception D

    6!5L2Code.mainnew !tringM D*ss**sd**!* *src/main/Fava**R* *src/main/resources/#7"N8**ns2p*

    *http://-oo.com=com.ttdev.+i;http://+ar.org/p*rchasing=com.ttdev.+i;.p*rchasing**uri* *src/main/resources/9i;'ervice.wsdl* E4I

    !$stem.out.println*5one*4I

    EE

    Do the !ame thing in the #i!2BiNClient pro@ect"

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 throws (ception D

    6!5L2Code.mainnew !tringM D*!* *src/main/Fava**R* *src/main/resources/#7"N8**ns2p*

    "http://-oo.com=com.ttdev.+i;http://+ar.org/p*rchasing=com.ttdev.+i;.p*rchasing"*uri* *src/main/resources/9i;'ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Delete the com'tt+ev'!! package an+ the !rc)main)re!ource!)=6T#-I* %ol+erin oth pro@ect!' Then run Co+eGenerator in oth pro@ect!' ill in the co+e in theBiN.ervice.keleton cla!!"

    pu,lic class

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    110/262

    Chapter 5 .en+ing an+ receiving comple +ata !tructure! 103

    -a*lt%sg.set)nvalidHty3item.getHty344H*ery)nvalidHty e = ne H*ery)nvalidHty34e.set@a*lt%essage3-a*lt%sg4thro e

    Ai- 3item.getHty34

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    111/262

    110 Chapter 5 .en+ing an+ receiving comple +ata !tructure!

    EE

    E

    :un it an+ it !houl+ print Invali+ Ety" -200 !ucce!!%ully'

    )ummary

    ?ou can %reely u!e /=< !chema element! an+ /=< type! to epre!! comple+ata !tructure!' The co+e generator (ill tran!late them into 7ava type!'

    or etter per%ormance, you !houl+ +e!ign the inter%ace! o% your (e !erviceoperation! !o that more +ata i! !ent in a me!!age'

    To report an error %rom your operation, +e%ine a me!!age in the .D< %ile an+u!e it a! a %ault me!!age in the operation' Then a++ a corre!pon+ing chil+element in the .;#P in+ing to !tore it into the .;#P ault element' The %aultme!!age !houl+ contain one an+ only one part (hich i! an /=< element+e!criing the %ault' The co+e generator (ill map a %ault me!!age to a 7avaeception cla!! an+ the part a! a %iel+' The operation (ill e mappe+ to a 7avametho+ thro(ing that eception'

    I% you have ei!ting /=< element! in an /.D %ile that you$+ like to u!e in a.D< %ile, you can u!e LimportM to import them' ?ou can !peci%y the relative

    path to the /.D %ile !o that the .D< par!er can %in+ it'

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    112/262

    111

    Chapter 6Chapter 'Sending binar( files

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    113/262

    112 Chapter .en+ing inary %ile!

    !hat"s in this chapter#In thi! chapter you$ll learn ho( to receive an+ return inary %ile! in your (e!ervice'

    $roviding the image o a product

    .uppo!e that you$+ like to have a (e !ervice to allo( people to uploa+ theimage 8@peg9 o% a pro+uct 8i+enti%ie+ y a pro+uct i+9' The .;#P me!!age maye like elo(' But ho( to repre!ent the inary image +ata> The prolem i! that

    .;#P u!e! /=< an+ /=< u!e! tet to repre!ent the +ata"

    ;ne (ay to +o it i! to enco+e the inary +ata into a tet %ormat' or eample,one coul+ enco+e yte 0 a! character $#$, enco+e yte 1 a! character $B$ an+etc' ;ne o% !uch an enco+ing i! the a!e enco+ing' Then the .;#Pme!!age (ill e like"

    &nvelope+&

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    114/262

    Chapter .en+ing inary %ile! 11

    The key i! that no( the /=< +ocument 8repre!ente+ a! a =I=6 me!!age9 canhave tetual part! an+ inary part!' There%ore it can repre!ent inary +atae%%iciently'

    To implement thi! i+ea, create a ne( pro@ect name+ Image.ervice a! u!ual':ename the .D< %ile a! Image.ervice'(!+l an+ mo+i%y it a!"

    Content#$pe: ultipart/Related

    " ,oundar$Content#$pe: te(t/(ml

    &nvelope+&

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    115/262

    11 Chapter .en+ing inary %ile!

    #lthough thi! i! not reEuire+, it u!e! the (rappe+ convention' *et, up+ate theCo+eGenerator cla!!"

    pu,lic class CodeKenerator Dpu,lic static void main!tringM args4 D

    6!5L#oJava.mainnew !tringM D*server**d* *src/main/Fava**src/main/resources/)mage'ervice.wsdl* E4I

    !$stem.out.println*5one*4IE

    E

    Generate the co+e again' Check the .6I"

    6e,!ervicetargetNamespace ) *urn:ttdev.com:service/img* name ) *"mage!ervice*4Xml!ee7lso D %,Fect8actor$.class E4pu,lic inter'ace "mage!ervice D

    &H(ml version)*1.0* encoding)*#8A*H+&wsdl:de'initions (mlns:wsdl)*http://schemas.(mlsoap.org/wsdl/*

    (mlns:soap)*http://schemas.(mlsoap.org/wsdl/soap/*(mlns:tns)*urn:ttdev.com:service/img*(mlns:(sd)*http://www.w3.org/2001/XL!chema* name)*"mage!ervice*targetNamespace)*urn:ttdev.com:service/img*+&wsdl:t$pes+

    &(sd:schema targetNamespace)*urn:ttdev.com:service/img*(mlns:(sd)*http://www.w3.org/2001/XL!chema*+&(sd:element name)*upload"mage*+

    &(sd:comple(#$pe+&(sd:se-uence+

    &(sd:element name)*product"d* t$pe)*(sd:string* /+

    &(sd:element name)*image* t$pe)*(sd:,ase?=

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    116/262

    Chapter .en+ing inary %ile! 115

    %newa$Re-uest6rapper

    localName ) *upload"mage*

    targetNamespace ) *urn:ttdev.com:service/img*className ) *com.ttdev.service.img.pload"mage*4

    6e,ethodaction ) *urn:ttdev.com:service/img/upload"mage*4pu,lic void upload"mage

    6e,Paramname ) *product"d*targetNamespace ) **4 Fava.lang.!tring product"d

    6e,Paramname ) *image*targetNamespace ) **4+yteBC image4I

    E

    *ote that the inary image +ata i! pre!ente+ a! a yte array' ?ou are *;Tu!ing /;P yet' ?ou$re @u!t getting the !ervice up an+ running' Create aImage.erviceImpl cla!!"

    pac9age com.ttdev.service.imgI...6e,!erviceendpoint"nter'ace ) *com.ttdev.service.img."mage!ervice*4pu,lic class "mage!ervice"mpl implements "mage!ervice D

    %verridepu,lic void upload"mage!tring product"d ,$teM image4 D

    tr$ D8ile%utput!tream out ) new 8ile%utput!treamproduct"dQ*.Fpg*4Iout.writeimage4Iout.close4I

    E catch "%(ception e4 Dthrow new Runtime(ceptione4IE

    EE

    It !imply !ave! the image +ata into a p01'@pg %ile i% the pro+uct i! p01' *et,create an ImageClient pro+uct a! u!ual' Copy any '@pg %ile in your computer intothe !rc)main)re!ource! %ol+er a! !ample'@pg' Then mo+i%y theImage.erviceSP1SClient cla!!"

    pu,lic 'inal class "mage!erviceOP1OClient D...pu,lic static void main!tring argsM4 throws (ception D

    ..."mage!erviceO!ervice ss ) new "mage!erviceO!ervicewsdlRL !RG"CON74I"mage!ervice port ) ss.getP14ID

    !$stem.out.println*"nvo9ing upload"mage...*4Iava.lang.'tring 7*pload)mage7prod*ct)d = "p#1"@ile)np*t'tream in = ne @ile)np*t'tream3

    "src/main/reso*rces/sample.pg"49yte,rray8*tp*t'tream o*t = ne 9yte,rray8*tp*t'tream34+yteBC +*- = ne +yteB1#FC-or 34 I

    int no9ytesRead = in.read3+*-4i- 3no9ytesRead == (14 I

    +reaDAo*t.rite3+*- # no9ytesRead4

    Aport.*pload)mage37*pload)mage7prod*ct)d o*t.to9yte,rray344o*t.close34in.close34

    E!$stem.e(it04I

    E

  • 7/21/2019 Developing Web Services With Apache Axis2 Second Edition

    117/262

    11 Chapter .en+ing inary %ile!

    E

    :un the !ervice an+ then run the client' :e%re!h the Image.ervice pro@ect an+

    you !houl+ !ee a p01'@pg %ile there' ;pen it (ith a ro(!er to veri%y that it i! acopy o% your !ample'@pg'

    To veri%y that it i! *;T u!ing /;P, u!e the TCP =onitor an+ a+@u!t the client"

    pu,lic 'inal class "mage!erviceOP1OClient D...pu,lic static void main!tring argsM4 throws (ception D

    ..."mage!erviceO!ervice ss ) new "mage!erviceO!ervicewsdlRL !RG"CON74I"mage!ervice port ) ss.getP14ID

    9indingProvider +p = 39indingProvider4 port+p.getRe6*est5ontext34.p*t3

    9indingProvider.ENP8)N?7,RE''7PR8PER?"http://localhost:1!F/is/p1"4

    !$stem.out.println*"nvo9in