Transcript
Page 1: Building Web Services

Building  Web  Services  

Jussi  Pohjolainen  

Page 2: Building Web Services

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  

Page 3: Building Web Services

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  

Page 4: Building Web Services

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  -­‐>)  

Page 5: Building Web Services

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  

Page 6: Building Web Services

XML  WEB  SERVICE  

Page 7: Building Web Services

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  

Page 8: Building Web Services

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>

Page 9: Building Web Services

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>

Page 10: Building Web Services

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>

Page 11: Building Web Services

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  

Page 12: Building Web Services
Page 13: Building Web Services

Java  6:  XML  &  Web  Services  

•  Easy  way  of  crea9ng  Web  Services  •  Expose  web  service  with  a  simple  annota9on    

Page 14: Building Web Services

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; } }

Page 15: Building Web Services

Server  package hello;

import javax.xml.ws.Endpoint; class Publish {

public static void main(String[] args) {

Endpoint.publish( "http://localhost:8080/circlefunctions", new CircleFunctions());

}

}

Page 16: Building Web Services

Generate  Stub  Files  

•  Generate  stub  files:  – wsgen –classpath . hello.CircleFunctions

Page 17: Building Web Services
Page 18: Building Web Services

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);

?>

Page 19: Building Web Services
Page 20: Building Web Services

REST  

Page 21: Building Web Services

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  

Page 22: Building Web Services

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  

Page 23: Building Web Services

Resources  

•  Resource  can  be  anything:  ar9cle,  comment,  user  …  

•  Resources  are  accessed  by  URI  – hRp://example.com/ar9cle/1  – hRp://example.com/comments/  

Page 24: Building Web Services

RESTful  Web  APIs  

Page 25: Building Web Services

PHP  REST  IMPLEMENTATION  

Page 26: Building Web Services

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  

Page 27: Building Web Services

.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]

Page 28: Building Web Services

XAMPP  windows  

•  Open  apache/conf/extra/hRpd-­‐xampp.conf  – set  AllowOverride  All  

Page 29: Building Web Services

Gekng  info  about  Request  and  Path  

<?php

$requestMethod = $_SERVER['REQUEST_METHOD'];

print("Request method: " . $requestMethod . "\n\n");

$urlPaths = $_SERVER['REQUEST_URI'] ;

print("Path: " . $urlPaths);

?>

Page 30: Building Web Services
Page 31: Building Web Services
Page 32: Building Web Services

Modifica9on  <?php

$requestMethod = $_SERVER['REQUEST_METHOD'];

$urlPaths = $_SERVER['REQUEST_URI'] ;

$paths = explode("/", $urlPaths);

$paths = array_splice($paths, 3);

print_r($paths);

?>

Page 33: Building Web Services

Encoding  and  decoding  

•  Encode  data  to  and  decode  from  JSON  –  json_encode()  –  json_decode()  

Page 34: Building Web Services

Tes9ng:  Advanced  REST  Client  Chrome  App  

Page 35: Building Web Services

Tes9ng  

•  PHP  Script  that  makes  hRp  requests  •  CURL  •  Telnet…  


Top Related