Building	
  Web	
  Services	
  
Jussi	
  Pohjolainen	
  
Communica9on	
  between	
  Apps	
  
•  Implemen9ng	
  RPCs	
  can	
  be	
  a	
  difficult	
  task	
  
•  DCOM,	
  CORBA,	
  ...
Web	
  Service	
  
•  Communica9on	
  between	
  devices	
  over	
  Web	
  
– W3C	
  defines	
  "Web	
  Service"	
  as	
  a...
XML	
  Web	
  Services	
  
•  XML	
  Web	
  services	
  uses	
  XML	
  messages	
  that	
  
follow	
  SOAP	
  standard	
  ...
Web	
  API	
  (Rest)	
  
•  Emphasis	
  to	
  simpler	
  communica9on	
  
– Representa)onal	
  state	
  transfer	
  (REST)...
XML	
  WEB	
  SERVICE	
  
SOAP?	
  
•  SOAP	
  is	
  a	
  XML-­‐based	
  protocol	
  to	
  let	
  apps	
  
exchange	
  informa9on	
  over	
  HTTP	
 ...
SOAP	
  Building	
  Blocks	
  
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
s...
SOAP	
  Request	
  
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/s...
SOAP	
  Response	
  
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2...
Client	
  and	
  Server	
  
•  "Soap	
  Server"	
  provides	
  a	
  WSDL	
  –	
  file	
  to	
  client	
  
•  Client	
  open...
Java	
  6:	
  XML	
  &	
  Web	
  Services	
  
•  Easy	
  way	
  of	
  crea9ng	
  Web	
  Services	
  
•  Expose	
  web	
  s...
Web	
  Service	
  
package hello;
import javax.jws.WebService;
@WebService
public class CircleFunctions {
public double ge...
Server	
  
package hello;
import javax.xml.ws.Endpoint;
class Publish {
public static void main(String[] args) {
Endpoint....
Generate	
  Stub	
  Files	
  
•  Generate	
  stub	
  files:	
  
– wsgen –classpath . hello.CircleFunctions
PHP	
  Client	
  
<?php
$arguments = array (
"arg0" => "5.6",
);
// URI delivered to web service
$soapclient = new SoapCli...
REST	
  
REST	
  Constraints	
  
•  Client	
  Server	
  
–  Interface	
  separates	
  clients	
  from	
  servers.	
  Server	
  and	...
Guidelines	
  for	
  the	
  Interface	
  
•  Iden9fica9on	
  of	
  resources	
  
–  Resources	
  are	
  iden9fied	
  via	
  ...
Resources	
  
•  Resource	
  can	
  be	
  anything:	
  ar9cle,	
  comment,	
  
user	
  …	
  
•  Resources	
  are	
  access...
RESTful	
  Web	
  APIs	
  
PHP	
  REST	
  IMPLEMENTATION	
  
Rewrite	
  engine	
  
•  Rewrite	
  engine	
  is	
  a	
  sohware	
  that	
  modifies	
  
web	
  URL's	
  appearance	
  
– U...
.htaccess	
  
# Let's use the mod_rewrite module
RewriteEngine On
# Set's the base URL for per-directory rewrites
RewriteB...
XAMPP	
  windows	
  
•  Open	
  apache/conf/extra/hRpd-­‐xampp.conf	
  
– set	
  AllowOverride	
  All	
  
Gekng	
  info	
  about	
  Request	
  and	
  Path	
  
<?php
$requestMethod = $_SERVER['REQUEST_METHOD'];
print("Request met...
Modifica9on	
  
<?php
$requestMethod = $_SERVER['REQUEST_METHOD'];
$urlPaths = $_SERVER['REQUEST_URI'] ;
$paths = explode("...
Encoding	
  and	
  decoding	
  
•  Encode	
  data	
  to	
  and	
  decode	
  from	
  JSON	
  
– json_encode()	
  
– json_de...
Tes9ng:	
  Advanced	
  REST	
  Client	
  Chrome	
  App	
  
Tes9ng	
  
•  PHP	
  Script	
  that	
  makes	
  hRp	
  requests	
  
•  CURL	
  
•  Telnet…	
  
Building Web Services
Building Web Services
Building Web Services
Building Web Services
Building Web Services
Upcoming SlideShare
Loading in...5
×

Building Web Services

402

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
402
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Building Web Services

  1. 1. Building  Web  Services   Jussi  Pohjolainen  
  2. 2. Communica9on  between  Apps   •  Implemen9ng  RPCs  can  be  a  difficult  task   •  DCOM,  CORBA,  RMI  …  firewalls  and  proxy   servers  can  block  binary   •  Using  HTTP  for  RPCs  you  can  bypass  this   problem  
  3. 3. Web  Service   •  Communica9on  between  devices  over  Web   – W3C  defines  "Web  Service"  as  a  technology  that   uses  WSDL,  SOAP,  HTTP  and  XML  to  create  the   communica9on   •  Two  types   – XML  Web  Services   – Web  API  
  4. 4. XML  Web  Services   •  XML  Web  services  uses  XML  messages  that   follow  SOAP  standard  for  crea9ng  the   communica9on   •  Services  are  wriRen  using  WSDL   – Web  Services  Descrip9on  Language  (WSDL)   •  Web  Services  are  integrated  very  well  to  .NET   and  Java  (6  -­‐>)  
  5. 5. Web  API  (Rest)   •  Emphasis  to  simpler  communica9on   – Representa)onal  state  transfer  (REST)   •  Do  not  require  SOAP,  WSDL   •  Simple  Web  API   – hRp://www.something.com/twiRerthis.php? msg=hello!   •  If  the  Web  API  is  implemented  using  certain   constraints,  it's  rest  API   – hRp://www.something.com/clients/client17  
  6. 6. XML  WEB  SERVICE  
  7. 7. SOAP?   •  SOAP  is  a  XML-­‐based  protocol  to  let  apps   exchange  informa9on  over  HTTP   •  SOAP  is  language  independent  and  it's  W3C   recommenda9on   •  Since  SOAP  is  XML  and  it's  text,  it  can  be  send   easily  through  firewalls  
  8. 8. SOAP  Building  Blocks   <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> ... </soap:Header> <soap:Body> ... <soap:Fault> ... </soap:Fault> </soap:Body> </soap:Envelope>
  9. 9. SOAP  Request   <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/ envelope/" xmlns:ns1="http://hello/"> <SOAP-ENV:Body> <ns1:getArea><arg0>5.6</arg0></ns1:getArea> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
  10. 10. SOAP  Response   <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getAreaResponse xmlns:ns2="http://hello/"> <return>98.5203456165759</return> </ns2:getAreaResponse> </S:Body> </S:Envelope>
  11. 11. Client  and  Server   •  "Soap  Server"  provides  a  WSDL  –  file  to  client   •  Client  opens  the  WSDL  file  and  discovers  the   methods  and  arguments  of  given  service   •  Client  makes  a  invoca9on  to  server.  The   invoka9on  is  a  soap  message   •  Server  receives  the  soap  message,  parses  it   and  invocates  the  method.  Result  is  send  back   in  SOAP  envelope  
  12. 12. Java  6:  XML  &  Web  Services   •  Easy  way  of  crea9ng  Web  Services   •  Expose  web  service  with  a  simple  annota9on    
  13. 13. Web  Service   package hello; import javax.jws.WebService; @WebService public class CircleFunctions { public double getArea(double r) { return java.lang.Math.PI * (r * r); } public double getCircumference(double r) { return 2 * java.lang.Math.PI * r; } }
  14. 14. Server   package hello; import javax.xml.ws.Endpoint; class Publish { public static void main(String[] args) { Endpoint.publish( "http://localhost:8080/circlefunctions", new CircleFunctions()); } }
  15. 15. Generate  Stub  Files   •  Generate  stub  files:   – wsgen –classpath . hello.CircleFunctions
  16. 16. PHP  Client   <?php $arguments = array ( "arg0" => "5.6", ); // URI delivered to web service $soapclient = new SoapClient("http://localhost:8080/ circlefunctions?wsdl"); $result = $soapclient->getArea($arguments); print($result->return); ?>
  17. 17. REST  
  18. 18. REST  Constraints   •  Client  Server   –  Interface  separates  clients  from  servers.  Server  and  client  can  be   replaced  and  developed  independently   –  Client?  Browser,  PHP  script,  Desktop  app,  Command  line  …   •  Statelessness   –  Each  request  from  any  client  contains  all  the  informa9on  necessary   •  Cacheable   –  Clients  can  cache  responses.  Response  indicate  whether  it  is  cacheable   or  not   •  Uniform  interface   –  Uniform  interface  between  clients  and  servers.  Four  guiding  principles   •  Layered  System   –  Client  does  not  know  is  it  connected  directly  to  server  or  some   middleware  
  19. 19. Guidelines  for  the  Interface   •  Iden9fica9on  of  resources   –  Resources  are  iden9fied  via  request  using  URIs   –  Server  sends  to  client  HTML,  XML  or  JSON  as  result  (does  not  send  the   database)   •  Manipula9on  of  resources     –  Client  can  delete  or  modify  a  resource  when  it  holds  a  representa9on   of  the  resource   •  Self-­‐descrip9ve  messages   –  Message  includes  enough  informa9on  to  describe  how  to  process  the   message   •  Hypermedia  as  the  engine  of  applica9on  state   –  Client  enters  REST  app  using  simple  fixed  URL.  All  future  ac9ons  the   client  may  take  can  be  discovered  from  the  returned  representa9on  
  20. 20. Resources   •  Resource  can  be  anything:  ar9cle,  comment,   user  …   •  Resources  are  accessed  by  URI   – hRp://example.com/ar9cle/1   – hRp://example.com/comments/  
  21. 21. RESTful  Web  APIs  
  22. 22. PHP  REST  IMPLEMENTATION  
  23. 23. Rewrite  engine   •  Rewrite  engine  is  a  sohware  that  modifies   web  URL's  appearance   – URL  rewri9ng   •  Usage   – hRp://example.com/index.php?clien9d=123   •  Can  be  altered   – hRp://example.com/clients/client/123   •  Apache  HTTP  Server  has  URL  rewri9ng   provided  by  mod_rewrite  module  
  24. 24. .htaccess   # Let's use the mod_rewrite module RewriteEngine On # Set's the base URL for per-directory rewrites RewriteBase / # Defines a condition under which rewriting will # take place RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Make the rule RewriteRule ^(.*)$ /xampp/rest/index.php/$1 [L]
  25. 25. XAMPP  windows   •  Open  apache/conf/extra/hRpd-­‐xampp.conf   – set  AllowOverride  All  
  26. 26. Gekng  info  about  Request  and  Path   <?php $requestMethod = $_SERVER['REQUEST_METHOD']; print("Request method: " . $requestMethod . "nn"); $urlPaths = $_SERVER['REQUEST_URI'] ; print("Path: " . $urlPaths); ?>
  27. 27. Modifica9on   <?php $requestMethod = $_SERVER['REQUEST_METHOD']; $urlPaths = $_SERVER['REQUEST_URI'] ; $paths = explode("/", $urlPaths); $paths = array_splice($paths, 3); print_r($paths); ?>
  28. 28. Encoding  and  decoding   •  Encode  data  to  and  decode  from  JSON   – json_encode()   – json_decode()  
  29. 29. Tes9ng:  Advanced  REST  Client  Chrome  App  
  30. 30. Tes9ng   •  PHP  Script  that  makes  hRp  requests   •  CURL   •  Telnet…  
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×