     /*********************************************************************
      *                                                                   *
      *                  IBM Web Services Client for ILE                  *
      *                                                                   *
      *  FILE NAME:      restCS.c                                         *
      *                                                                   *
      *  DESCRIPTION:    Source to do REST request using transport APIs   *
      *                                                                   *
      *********************************************************************
      * LICENSE AND DISCLAIMER                                            *
      * ----------------------                                            *
      * This material contains IBM copyrighted sample programming source  *
      * code ( Sample Code ).                                             *
      * IBM grants you a nonexclusive license to compile, link, execute,  *
      * display, reproduce, distribute and prepare derivative works of    *
      * this Sample Code.  The Sample Code has not been thoroughly        *
      * tested under all conditions.  IBM, therefore, does not guarantee  *
      * or imply its reliability, serviceability, or function. IBM        *
      * provides no program services for the Sample Code.                 *
      *                                                                   *
      * All Sample Code contained herein is provided to you "AS IS"       *
      * without any warranties of any kind. THE IMPLIED WARRANTIES OF     *
      * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND             *
      * NON-INFRINGMENT ARE EXPRESSLY DISCLAIMED.                         *
      * SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED          *
      * WARRANTIES, SO THE ABOVE EXCLUSIONS MAY NOT APPLY TO YOU.  IN NO  *
      * EVENT WILL IBM BE LIABLE TO ANY PARTY FOR ANY DIRECT, INDIRECT,   *
      * SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY USE OF THE SAMPLE  *
      * CODE INCLUDING, WITHOUT LIMITATION, ANY LOST PROFITS, BUSINESS    *
      * INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON YOUR INFORMATION  *
      * HANDLING SYSTEM OR OTHERWISE, EVEN IF WE ARE EXPRESSLY ADVISED OF *
      * THE POSSIBILITY OF SUCH DAMAGES.                                  *
      *                                                                   *
      *  <START_COPYRIGHT>                                                *
      *                                                                   *
      *  Licensed Materials - Property of IBM                             *
      *                                                                   *
      *  5770-SS1                                                         *
      *                                                                   *
      *  (c) Copyright IBM Corp. 2016, 2016                               *
      *  All Rights Reserved                                              *
      *                                                                   *
      *  U.S. Government Users Restricted Rights - use,                   *
      *  duplication or disclosure restricted by GSA                      *
      *  ADP Schedule Contract with IBM Corp.                             *
      *                                                                   *
      *  Status: Version 1 Release 0                                      *
      *  <END_COPYRIGHT>                                                  *
      *                                                                   *
      *********************************************************************/


// CRTCMOD MODULE(AMRA/RESTCS) SRCSTMF('/restcs.c') DBGVIEW(*ALL)
// ENUM(*INT) INCDIR('/qibm/proddata/os/webservices/v1/client/include')  
// 
// CRTpgm  pgm(AMRA/RESTCS) module(amra/restcs) bndsrvpgm(qsysdir/qaxis10cc) ACTGRP(*NEW)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <axis/Axis.h>
#include <axis/ITransport.h>
#include <axis/AxisException.h>

#define MAX_BUFFER_SIZE    (60 * 1024)

AXISCHANDLE t = NULL;
char buffer[MAX_BUFFER_SIZE +1];


void checkError(char *theCall)
{     
     char *statusCode;
     int rc;

     int axisCode = axiscTransportGetLastErrorCode(t);
     printf("%s call failed: (%d) %s\n", theCall, axisCode, axiscTransportGetLastError(t));
}

int receiveData(void)
{
    int bytesRead = 0;
    int rc;
    char *header;

    memset(buffer, 0x00, MAX_BUFFER_SIZE + 1);
    while ((bytesRead < MAX_BUFFER_SIZE)
            && ((rc = axiscTransportReceive(t, buffer+bytesRead, MAX_BUFFER_SIZE-bytesRead, 0)) != -1))
    {
        if (rc == 0) break;
        bytesRead += rc;
    }

    if (rc == -1)
      checkError("TransportReceive");
    else if (bytesRead == 0)
        printf("No data to read\n");
    else if (bytesRead > 0)
    {
       printf("Bytes read: %d\n",  bytesRead);
       printf("Data: \n%s\n\n",  buffer);
    }

    // Get HTTP Status from response
    rc = axiscTransportGetProperty(t, AXISC_PROPERTY_HTTP_STATUS_CODE, &header);
    if (rc == -1)
       checkError("TransportGetProperty: HTTP Status code");
    else
        printf("HTTP Status code: %s\n", header);

    return rc;
}


main (int argc, char **argv)
{   
   int rc, i;
   char * uri;
   char urlBuf[2000];
   int timeout=4;
  
   // Uncomment if need to debug.
   // system("rmvlnk '/tmp/axistransport.log'");
   // axiscAxisStartTrace("/tmp/axistransport.log", NULL);

   // Set URI to web service
   if (argc > 1)
     uri = argv[1]; 
   else
     uri = "https://host:40443/web/students";

   // create transport handle
   t = axiscTransportCreate(uri, AXISC_PROTOCOL_HTTP11);
   if (t == NULL)
   {
     printf("TransportCreate() call failed\n");
     return -1;
   }

   // Set HTTP method
   rc = axiscTransportSetProperty(t, AXISC_PROPERTY_HTTP_METHOD, "GET");
   if (rc == -1)
       checkError("TransportSetProperty: HTTP_METHOD=GET");

   // Set basic auth
   rc = axiscTransportSetProperty(t, AXISC_PROPERTY_HTTP_BASICAUTH, "amra", "pwd");
   if (rc == -1)
       checkError("TransportSetProperty:  HTTP_BASICAUTH");

   // Set connect timeout
   rc = axiscTransportSetProperty(t, AXISC_PROPERTY_CONNECT_TIMEOUT, &timeout);

   // Set SSL information
   rc = axiscTransportSetProperty(t, AXISC_PROPERTY_HTTP_SSL,  
                    "/QIBM/USERDATA/ICSS/CERT/SERVER/DEFAULT.KDB", "", "", "NONE", "NONE", NULL);
   if (rc == -1)
      checkError("TransportSetProperty:  HTTP_SSL");

    // Flush to initiate request and then retrieve all student registration records
    rc = axiscTransportFlush(t);
    if (rc == -1)
        checkError("TransportFlush");
    else
        rc = receiveData();

   // Destroy transport handle
   axiscTransportDestroy(t);
   
   return 0;
}
