9-6 rest developers guide

27
REST Developer’s Guide Version 9.6 April 2014

Upload: harish-bandi

Post on 16-Dec-2015

109 views

Category:

Documents


45 download

DESCRIPTION

Rest Developers Guide

TRANSCRIPT

  • REST Developers Guide

    Version 9.6

    April 2014

  • This document applies to webMethods Integration Server Version 9.6 and to all subsequent releases.

    Specications contained herein are subject to change and these changes will be reported in subsequent release notes or new editions.

    Copyright 2010-2014 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/orits aliates and/or their licensors.

    The name Software AG and all Software AG product names are either trademarks or registered trademarks of Software AG and/orSoftware AG USA Inc. and/or its subsidiaries and/or its aliates and/or their licensors. Other company and product names mentionedherein may be trademarks of their respective owners.

    Detailed information on trademarks and patents owned by Software AG and/or its subsidiaries is located athp://documentation.softwareag.com/legal/.

    Use of this software is subject to adherence to Software AG's licensing conditions and terms. These terms are part of the productdocumentation, located at hp://documentation.softwareag.com/legal/ and/or in the root installation directory of the licensed product(s).

    This software may include portions of third-party products. For third-party copyright notices and license terms, please refer to "LicenseTexts, Copyright Notices and Disclaimers of Third Party Products. This document is part of the product documentation, located athp://documentation.softwareag.com/legal/ and/or in the root installation directory of the licensed product(s).

    Document ID: IS-RS-DG-96-20140415

  • MTable of Contents

    REST Developers Guide Version 9.6 3

    Table of Contents

    About this Guide..............................................................................................................................5Document Conventions.............................................................................................................. 5Documentation Installation......................................................................................................... 6Online Information...................................................................................................................... 6

    About Integration Server REST Processing................................................................................. 9Overview................................................................................................................................... 10

    How REST Processing Works...................................................................................................... 13About REST Request Messages..............................................................................................14How webMethods Integration Server Processes REST Requests...........................................14Sending Responses to the REST Client..................................................................................16

    Status Line.........................................................................................................................16Header Fields.................................................................................................................... 17Message Body...................................................................................................................17

    Setting Up Your REST Application.............................................................................................. 19Setting Up a REST Application on Integration Server............................................................. 20

    Services............................................................................................................................. 20Configuration......................................................................................................................21

    Converting an Existing Application...........................................................................................22

    Documenting Your Rest Application........................................................................................... 23Providing Information About Your Application.......................................................................... 24

    General Information...........................................................................................................24Information About Each Request...................................................................................... 24Information About Responses........................................................................................... 26

    Index................................................................................................................................................ 27

  • MEven Header

    REST Developers Guide Version 9.6 4

  • MOdd Header

    REST Developers Guide Version 9.6 5

    About this Guide

    This guide is for developers using webMethods Integration Server to create RESTapplications. This guide assumes basic knowledge of REST concepts and HTTP requestprocessing and familiarity with Software AG Designer and webMethods IntegrationServer.

    Document Conventions

    Convention Description

    Bold Identies elements on a screen.

    Narrowfont Identies storage locations for services on webMethodsIntegration Server, using the convention folder.subfolder:service .

    UPPERCASE Identies keyboard keys. Keys you must press simultaneouslyare joined with a plus sign (+).

    Italic Identies variables for which you must supply values specic toyour own situation or environment. Identies new terms the rsttime they occur in the text.

    Monospace

    font

    Identies text you must type or messages displayed by thesystem.

    { } Indicates a set of choices from which you must choose one. Typeonly the information inside the curly braces. Do not type the { }symbols.

    | Separates two mutually exclusive choices in a syntax line. Typeone of these choices. Do not type the | symbol.

    [ ] Indicates one or more options. Type only the information insidethe square brackets. Do not type the [ ] symbols.

    ... Indicates that you can type multiple options of the same type.Type only the information. Do not type the ellipsis (...).

  • MEven Header

    REST Developers Guide Version 9.6 6

    Documentation InstallationYou can download the product documentation using the Software AG Installer. Thedocumentation is downloaded to a central directory named _documentation in the maininstallation directory (SoftwareAG by default).

    Online InformationYou can nd additional information about Software AG products at the locations listedbelow.

    If you want to... Go to...

    Access the latest version of productdocumentation.

    SoftwareAG Documentation website

    hp://documentation.softwareag.com

    Find information about product releasesand tools that you can use to resolveproblems.

    See the Knowledge Center to:

    Read technical articles and papers.

    Download xes and service packs (9.0SP1 and earlier).

    Learn about critical alerts.

    See the Products area to:

    Download products.

    Download certied samples.

    Get information about productavailability.

    Access older versions of productdocumentation.

    Submit feature/enhancement requests.

    Empower Product Support website

    hps://empower.softwareag.com

    Access additional articles, demos, andtutorials.

    SoftwareAG Developer Community forwebMethods

  • MOdd Header

    REST Developers Guide Version 9.6 7

    If you want to... Go to...

    Obtain technical information, usefulresources, and online discussion forums,moderated by Software AG professionals,to help you do more with Software AGtechnology.

    Use the online discussion forums toexchange best practices and chat withother experts.

    Expand your knowledge about productdocumentation, code samples, articles,online seminars, and tutorials.

    Link to external websites that discussopen standards and many webtechnology topics.

    See how other customers are streamliningtheir operations with technology fromSoftware AG.

    hp://communities.softwareag.com/

  • MEven Header

    REST Developers Guide Version 9.6 8

  • MOdd Header

    REST Developers Guide Version 9.6 9

    1 About Integration Server REST Processing Overview ....................................................................................................................................... 10

  • MEven Header

    REST Developers Guide Version 9.6 10

    OverviewRepresentational State Transfer (REST) is an architectural style used to build distributedhypermedia systems. The World Wide Web is the best known example of such a system.

    The focus of REST is on resources rather than services. A resource is a representation ofan object or information. A resource can represent:

    A single entity, like a coee pot you want to purchase from an online shopping site.

    A collection of entities, like records from a database.

    Dynamic information, like real-time status updates from a monitoring site.

    That is, resources are the entities or collections of entities in a distributed system thatyou want to post or retrieve or take action on. In a REST style system, each resource isidentied by a universal resource identier (URI).

    Development of REST systems is dened by a series of constraints:

    Clients and servers are separate.

    Communication between clients and servers is stateless.

    Clients can cache responses returned from servers.

    There may be intermediate layers between the client and server.

    Servers can supply code for the clients to execute.

    Clients and servers remain loosely coupled by communicating through a uniforminterface.

    The uniform interface is the key constraint that dierentiates REST from otherarchitectural approaches. The characteristics of this interface are:

    Requests identify resources.

    Responses contain representations of those resources.

    Clients manipulate resources through their representations.

    Messages are self-descriptive.

    The interface employs Hypermedia as the engine of application state (HATEOAS),which enables the client to nd other resources referenced in the response.

    One strength of REST is that it leverages the well understood methods supported byHTTP to describe what actions should be taken on a resource. To be REST-compliant,an application must support the HTTP GET, POST, PUT, and DELETE methods.Many applications use web browsers to interact with resources on the Internet. webbrowsers, however, typically support only the HTTP GET and HTTP POST methods.To get around this restriction, you can use Integration Server to build REST-compliantapplications that support all four methods.

  • MOdd Header

    REST Developers Guide Version 9.6 11

    Integration Server can be a REST server or a REST client. When Integration Serveracts as a REST server, it hosts an application that you write. The application includesservices that you write that instruct Integration Server to process some or all of theHTTP GET, POST, PUT, and DELETE methods in request messages against resources.When Integration Server acts as a REST client, it sends specially formaed requests tothe REST server.

  • MEven Header

    REST Developers Guide Version 9.6 12

  • MOdd Header

    REST Developers Guide Version 9.6 13

    2 How REST Processing Works About REST Request Messages ................................................................................................. 14

    How webMethods Integration Server Processes REST Requests .............................................. 14

    Sending Responses to the REST Client ..................................................................................... 16

  • MEven Header

    REST Developers Guide Version 9.6 14

    About REST Request MessagesREST clients send specially formaed requests to your REST application. Theformat of REST requests is determined by the webMethods Integration Server RESTimplementation and your specic application, but essentially it conveys the followinginformation, or tokens, to the REST server:

    The HTTP method to execute

    The directive

    The name of the resource

    A simple REST request looks like this:

    METHOD /directive/resource_type/resource_id HTTP/1.1

    Where... Is the...

    METHOD HTTP request method.

    directive The type of processing to perform.

    resource_type/resource_id

    Resource to act upon.

    More complex request messages can contain more explicit information about theresource.

    How webMethods Integration Server Processes RESTRequestsWhen Integration Server processes a REST request, it parses the tokens and identiesthe HTTP method to execute, locates the resource to act upon, and passes additionalinformation as input parameters to the services you wrote for your application. OnIntegration Server the resources of your application are represented as folders withina package. For each resource, you will write individual services for the HTTP methodsthat you want Integration Server to execute against the resource. Those services must benamed _get, _post, _put, and _delete, and they are stored in the folder for the resource.For more information, see"Services" on page 20.

    Consider a Discussion application that maintains a database of discussions aboutdierent topics. The following examples show how Integration Server would parse theseREST requests.

    Example 1

  • MOdd Header

    REST Developers Guide Version 9.6 15

    Here is a request to obtain a list of all topics contained in the database, and howIntegration Server would parse the request:

    GET /rest/discussion/topic HTTP/1.1

    Where... Is the...

    GET Type of HTTP method to perform. Integration Server mapsthis value to the corresponding service on Integration Server,in this case, the _get service.

    rest Type of processing to perform, in this case, Integration ServerREST processing.

    Note: For more information about directives, see webMethodsIntegration Server Administrators Guide.

    discussion/topic Location of the _get service for this resource on IntegrationServer. In this example, the _get service resides in the topicfolder in the discussion folder (discussion.topic).

    Example 2

    Here is a request to display information about topic number 3419, and how IntegrationServer would parse the request:

    GET /rest/discussion/topic/3419 HTTP/1.1

    Where... Is...

    3419 An instance of a resource passed into a service as the$resourceID variable. In the example, the $resourceID variablenarrows the focus of the GET request to topic 3419.

    Note: Integration Server assigns the rst token after the folder(s)to the $resourceID parameter. To determine whether a tokenrepresents a folder or the $resourceID , Integration Serverlooks in the current namespace for a folder that has the samename as the token. If it does not nd a folder with this name,Integration Server assigns the token to the $resourceID variable.In other words, the rst token (after the directive) that does notcorrespond to a folder becomes the $resourceID .

    Example 3

    Here is a request to display information about a particular comment, 17 for example,and how Integration Server would parse the request:

  • MEven Header

    REST Developers Guide Version 9.6 16

    GET /rest/discussion/topic/3419/comment/17 HTTP/1.1

    Where... Is...

    comment/17 Additional information that further narrows the informationabout the resource. This information is passed into a service asthe $path variable. In the example, comment/17 further narrowsthe focus of the GET request to comment 17.

    Example 4

    Here is a request to display information contributed by participant Robertson in 2009about topic 17, and how Integration Server would parse the request:

    GET /rest/discussion/topic/3419/comment/17?year=2009&name=Robertson HTTP/1.1

    Where... Are...

    year and name Input variables that are specic to your application. Tokensspecied after the ? must be entered as name/value pairs. Inthis example, year=2009 and name=Robertson narrow the focusof the GET request to entries that participant Robertson addedto comment 17 in 2009.

    Sending Responses to the REST ClientWhen Integration Server responds to an HTTP request, the response contains a statusline, header elds, and a message body.

    Status LineThe status line consists of the HTTP version followed by a numeric status code anda reason phrase. The reason phrase is a brief textual description of the status code.Integration Server will always set the HTTP version to match the version of the clientthat issued the request. You cannot change the HTTP version.

    You can, however, use the pub.flow:setResponseCode service to set the status code andreason phrase. For more information about this service, refer to the webMethodsIntegration Server Built-In Services Reference. If you do not explicitly set the statuscode, Integration Server will set it to 200 for successfully completed requests and anappropriate error code for unsuccessful requests.

    HTTP/1.1 denes all the legal status codes in Section hp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10. Examine these codes to determine which areappropriate for your application.

  • MOdd Header

    REST Developers Guide Version 9.6 17

    Header FieldsYou can communicate information about the request and the response through headerelds in the HTTP response. Integration Server will generate some header elds, such asSet-Cookie, WWW-Authenticate, Content-Type, Content-Length, and Connection. Youcan use the pub.flow:setResponseHeader to set Content-Type and other header elds. Formore information about this service, refer to the webMethods Integration Server Built-InServices Reference.

    HTTP/1.1 denes the header elds that can appear in a response in three sections of RFC2616: 4.5, 6.2, and 7.1. Examine these codes to determine which are appropriate for yourapplication.

    Message BodyThe message body usually contains a representation of the requested resource, one ormore URLs that satisfy the request, or both. In some cases, the message body should beempty, as specied in RFC 2616, Section 4.3

    You can use the pub.flow:setResponse service to explicitly set the message body. See thewebMethods Integration Server Built-In Services Reference for details. If you do not explicitlyset the message body, the output pipeline of the top-level service will be returned to theclient in the message body.

    For more information about how Integration Server builds HTTP responses, refer to thesection that describes how Integration Server chooses content handlers in webMethodsIntegration Server Administrators Guide.

  • MEven Header

    REST Developers Guide Version 9.6 18

  • MOdd Header

    REST Developers Guide Version 9.6 19

    3 Setting Up Your REST Application Setting Up a REST Application on Integration Server ................................................................. 20

    Converting an Existing Application .............................................................................................. 22

  • MEven Header

    REST Developers Guide Version 9.6 20

    Setting Up a REST Application on Integration ServerIntegration Server can act as a REST server or REST client. For Integration Server to actas a REST server, it must host services that perform the GET, PUT, POST, and DELETEmethods. These services, which you provide, perform processing that is specic to yourapplication. For example, the service you write to perform the GET method might accessyour application database, perform a query, and return a result.

    ServicesWhen you build a REST application on your Integration Server, you must includeservices that correspond to the HTTP methods you want to provide for each resource.These services must be named as follows:

    Service Description

    _get Performs the GET method.

    _put Performs the PUT method.

    _post Performs the POST method.

    _delete Performs the DELETE method.

    These services reside in folders on your Integration Server in a directory structure thatis specic to your application. For example, the Discussion application described abovemight have the following structure as viewed from Software AG Designer:

    In addition to the _get, _put, _post, and _delete services, you can also place a special servicenamed _default in one or more of the application folders. Integration Server executesthis service if a REST request species an HTTP method that is not represented by aservice in the folder. For example, suppose the folder contains the _get, _put, and _postservices, but no _delete service. If the client issues a DELETE request, Integration Serverwill execute the _default service, and pass DELETE to it in the $hpMethod variable.

  • MOdd Header

    REST Developers Guide Version 9.6 21

    If a request species an HTTP request method that is not represented by a service in thefolder and there is no _defaultservice in the folder, the request fails with error 404 NotFound or 405 Method Not Allowed. Integration Server issues 404 if the rst token inthe URI does not exist in the namespace, or 405 if one or more tokens in the URI identifyelements in the namespace but the URI does not correctly identify a folder and a serviceto execute.

    Example 1

    A resources folder contains the _get, _post, and _default services:

    If the client sends a... Integration Server responds by...

    GET request Executing the _get service

    POST request Executing the _post service

    DELETE request Executing the _default service

    Example 2

    A resources folder contains the _get, _put, and _delete services:

    If the client sends a... Integration Server responds by...

    GET request Executing the _get service

    PUT request Executing the _put service

    POST request Issuing error 405 Method Not Allowed

    Additional possible uses for the _default service are:

    Direct all REST requests through common code before branching o to individualGET, PUT, POST, or DELETE methods.

    Make PUT and POST processing the same by directing PUT and POST requests tothe same code.

    ConfigurationThere are a few things you can congure with respect to REST processing:

    Name of the REST directive

    If you want to allow clients to specify a name other than rest for the RESTdirective, you can do so with the wa.server.RESTDirective conguration parameter.

  • MEven Header

    REST Developers Guide Version 9.6 22

    For example, to allow clients to specify process for the REST directive, you wouldchange the property to the following:

    watt.server.RESTDirective=process

    With this seing, clients can specify rest or process for the REST directive. In thefollowing example, the two requests are equivalent:

    METHOD /process/discussion/topic/9876 HTTP/1.1

    METHOD /rest/discussion/topic/9876 HTTP/1.1

    For more information about the wa.server.RESTDirective property, refer towebMethods Integration Server Administrators Guide.

    Which ports will accept the rest directive

    By default, all Integration Server ports except the proxy port allow use of the restdirective. You can limit which ports will allow this directive by specifying them onthe wa.server.allowDirective conguration parameter. For more information aboutthis property, refer to the webMethods Integration Server Administrators Guide.

    Converting an Existing ApplicationIf you have an existing application that you want to transform into a REST application,one approach is to refactor your existing services into _get, _put, _post and _delete services.There is, however, another approach that might be faster and easier.

    The invoke directive, like the rest directive, supports the GET, PUT, POST, and DELETEmethods. For existing applications that use the invoke directive, you can updatea service to call the pub.flow:getTransportInfo service and then perform a branch on /transport/http/method to execute the appropriate portions of your existing code, as inthe following example:

    Note, however, that if you use the invoke directive instead of the rest directive, youcannot take advantage of these REST-specic features:

    $resourceID pipeline variable

    $path pipeline variable

    _default service

  • MOdd Header

    REST Developers Guide Version 9.6 23

    4 Documenting Your Rest Application Providing Information About Your Application .............................................................................. 24

  • MEven Header

    REST Developers Guide Version 9.6 24

    Providing Information About Your ApplicationIt is important to document your REST application so that your customers and partnerswill be able to build clients that interact with it correctly. Your documentation shouldcover how to:

    Send requests to your application

    Handle responses from your application

    The following sections describe the dierent areas your documentation should cover.

    General InformationInclude the following general information about your application:

    A list of resource types

    In the sample Discussion application described above, resource types would bediscussion and topic.

    The HTTP methods your application supports for each resource

    In the sample Discussion application, the discussion resource supports GET, but thetopics resource supports DELETE, GET, POST, and PUT.

    Information About Each RequestInclude the following information about each request:

    The format of the request URL

    For example, documentation for the Discussion application could provide a list ofpossible client requests:

    Return general information about the Discussion application:

    GET /rest/discussion HTTP/1.1

    Return a list of all topics contained in the database:

    GET /rest/discussion/topic HTTP/1.1

    Display entries made by participant Robertson in 2009 to topic 3419:

    GET /rest/discussion/topic/3419?year=2009&name=Robertson HTTP/1.1

    Which request header elds are required or optional and how your applicationresponds to them. For example, the Discussion application might specify thefollowing information to explain which header elds it accepts and how it respondsto them:

  • MOdd Header

    REST Developers Guide Version 9.6 25

    Authorization. The Discussion application accepts BASIC and DIGESTauthorization. All requests must include an Authorization header.

    Content-Type. Clients should include a Content-Type header with all requests.Acceptable Content-Type values for requests that contain a body are application/json, text/xml, text/html, and text/plain. For more information about Content-Types, see webMethods Integration Server Administrators Guide.

    Accept. Clients can optionally supply an Accept header to indicate the Content-Type they want the response to use. When you specify the Content-Type for theAccept header, Integration Server uses the content handler registered to thatContent-Type to respond to the request. For example, if the content handleris application/json, Integration Server responds to the request with JSONcontent. Acceptable values are application/json, text/xml, and text/html. If noAccept header is specied in the request, the response will use text/xml. Formore information about the Accept header, see webMethods Integration ServerAdministrators Guide.

    Whether a body is required and what structure the body should have.

    Documentation for the Discussion example might provide the following examples toillustrate body structure:

    Example 1: Creating a new topic

    Request:POST /discussion/topic HTTP/1.1 Host: IS_server:5555 Authorization: BASIC Content-Length: Content-Type: text/xml; charset=utf-8

    Response: If the request was valid, the Discussion application will respond with thefollowing:HTTP/1.1 201 Created Content-Length: 0 ETag: 32619 Location: http://host/discussion/topic/32619

    Example 2: Adding an entry to an existing topic

    Request:PUT /discussion/topic=36219 HTTP/1.1 Host: IS_server:5555 Authorization: BASIC Content-Length: 17 Content-Type: text/xml; charset=utf-8 comment=I+agree

    Response: If the request was valid, the Discussion application will respond with thefollowing:HTTP/1.1 200 OK Content-Length: 0 Location: http://host/discussion/topic/36219?comment=2

  • MEven Header

    REST Developers Guide Version 9.6 26

    Information About ResponsesYour documentation should include the following to describe the response thatcorresponds to each request:

    A list of HTTP Status-Codes and Reason-Phrases the application returns and thecircumstances under which it returns them. For a list of possible responses that youcan code your application to return, refer to hp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.

    A list of the response header elds you return and what they mean in the context ofyour application.

    A description of what will appear in the body of the response.

  • MIndex

    REST Developers Guide Version 9.6 27

    IndexAapplication services 20

    Cconfiguration 21converting an existing REST application 22

    Ddocumentation

    using effectively 5documenting your REST application 24

    Hheader fields of response to REST client 17HTTP request methods

    supported 10

    Iinvoke directive 22

    Mmessage body of response to REST client 17

    Pprocessing directives

    invoke 22rest 14, 22

    Rrequest messages

    format 14response to REST client

    header fields 17message body 17status line 16

    REST applicationdirectory structure 20setting up on Integration Server 20

    REST application services 20rest directive 14

    alternative name for 21REST processing

    input parameters 14passing input to application services 14request format from REST perspective 14

    request parsing 14supported HTTP request methods 10

    REST request messages 14REST server

    setting up on Integration Server 20, 20

    Sstatus line of response to REST client 16

    Symbols$httpMethod input variable 20$path input parameter 16$resourceID input parameter 15_default service 20_delete service 20_get service 20_post service 20_put service 20

    Table of ContentsAbout this GuideDocument ConventionsDocumentation InstallationOnline Information

    About Integration Server REST ProcessingOverview

    How REST Processing WorksAbout REST Request MessagesHow webMethods Integration Server Processes REST RequestsSending Responses to the REST ClientStatus LineHeader FieldsMessage Body

    Setting Up Your REST ApplicationSetting Up a REST Application on Integration ServerServicesConfiguration

    Converting an Existing Application

    Documenting Your Rest ApplicationProviding Information About Your ApplicationGeneral InformationInformation About Each RequestInformation About Responses

    Index