Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Developing Junos Space Applications > Creating REST Services

Calling a REST Service from REST APIs

You can call a REST API from your application's REST API using the JSServiceClient class. This class is responsible for constructing the HTTP request, and carries the authorization and authentication credentials of the user who invokes the REST API.

In many cases, an HTTP client calls a REST service of an application which then calls another REST service. Other times, the first REST service may not want to call another REST service directly, but calls an EJB method instead, and then the EJB method calls the second REST service.

In either case, the initial REST service can be called by the client using the BasicAuth authentication header in each request, or the client could be logged into an HTTP session. In this scenario, a session context is established on behalf of the entire call stack.

JSServiceClient Authentication Scenarios

Scenario Authentication Mechanism Forwarding credentials to REST-Svc2
Client --> REST-Svc1 --> REST-Svc2

No Session
Calling client passes the credentials in the HTTP request header's Authorization field. REST-Svc1 calls JSServiceClient(ApiCtx) which retrieves the HTTP request credentials from ApiContext and calls REST-Svc2.
Client --> REST-Svc1 --> EJB --> REST-Svc2

No Session
Calling client passes the credentials in the HTTP request header's Authorization field. ApiContext must passed from REST-Svc1 to EJB. EJB calls JSServiceClient(ApiCtx) which retrieves the HTTP request credentials from ApiCtx and calls REST-Svc2.
Client --> REST-Svc1 --> REST-Svc2

HTTP Session
Before calling REST-Svc1, the calling client logs into Junos Space, and establishes an HTTP session. No credentials are passed in when calling REST-Svc1. REST-Svc1 calls JSServiceClient(ApiCtx) which associates the call with the live HTTP session.
Client --> REST-Svc1 --> EJB --> REST-Svc2

Http Session
Before calling REST-Svc1, the calling client logs into Junos Space, and establishes an HTTP session. No credentials are passed in when calling REST-Svc1. ApiContext must be passed from REST-Svc1 to EJB. EJB calls JSServiceClient(ApiCtx) which associates the call with the live HTTP session.
EJB --> REST-Svc from an AppEnabledCallback initialization or initDB().

Use user $$$rest which doesn't require any password, but only works when calling the rest service from a native app running within the Space Fabric.
JSServiceClient can be used to call a REST service.
EJB --> REST-Svc from an EJB scheduled by a job manager

Use JSServiceClient and pass in the ApiContext, or use the $$$rest as the user.
JSServiceClient can be used to call a REST service.

Usage Example

Calling REST API from a REST API

public net.juniper.jmp.helloworld.v1.Countries getCountries( 
    UriContext uriCtxt) { 
  // Note: UriContext is required for any service which uses JSServiceClient to forward http 
  // requests to other Junos Space URL's 
  // UriContext provides getBaseUrl() API to get the Base URL
  String url = uriCtxt.getBaseUrl()+"/api/space/user-management/users " ; 
  // Create an instance of JSServiceClient 
  JSServiceClient client = new JSServiceClient(uriCtxt); 
  // Create an HttpMethod instance. 
  HttpMethod method = new GetMethod(url); 
  // Provide custom retry handler if necessary 
  method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER , 
      new DefaultHttpMethodRetryHandler(3, false )); 
  try { 
    // Execute the method using the JSServiceClient. 
    int statusCode = client.httpClient().executeMethod(method); 
    if (statusCode != HttpStatus.SC_OK ) { 
      logger.error( "Method failed: " + method.getStatusLine()); 
    } 
    // Read the response body. 
    byte [] responseBody = method.getResponseBody(); 
    // Deal with the response. 
    // Use caution: ensure correct character encoding and is not binary data 
    logger.debug( new String(responseBody)); 
  } catch (HttpException e) { 
    logger.error( "Fatal protocol violation: " + e.getMessage()); 
  } catch (IOException e) { 
    logger.error( "Fatal transport error: " + e.getMessage()); 
  } finally { 
    // Release the connection. 
    method.releaseConnection(); 
  } 
} 

Note: For more information on UriContext, refer to the UriContext section.