- Main Page
- Data Structures
- Files

- File List
- Globals

# samples/jsput.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2007 JasperSoft http://www.jaspersoft.com
    00003  *
    00004  * This program is free software; you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation; either version 2 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed WITHOUT ANY WARRANTY; and without the
    00010  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00011  * See the GNU General Public License for more details.
    00012  *
    00013  * You should have received a copy of the GNU General Public License
    00014  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
    00015  * or write to:
    00016  *
    00017  * Free Software Foundation, Inc.,
    00018  * 59 Temple Place - Suite 330,
    00019  * Boston, MA  USA  02111-1307
    00020  */
    00021
    00034 #include <jasperserver.h>
    00035
    00036 const char server[] = "http://127.0.0.1:8080/jasperserver/services/repository";
    00037
    00046 void put_resource(char *name, char *parentUri, char *infile)
    00047 {
    00048       jasperserver_request_t *request = NULL;
    00049       jasperserver_server_t *jasperServer = NULL;
    00050       jasperserver_resource_descriptor_t *res = NULL;
    00051       jasperserver_operation_result_t *operationResult = NULL;
    00052       jasperserver_resource_property_t *propHasData = NULL;
    00053       jasperserver_resource_property_t *propParentUri = NULL;
    00054
    00055       // Create a new structure of type jasperserver_server_t to save the
    00056       // server url and credentials.
    00057       jasperServer = jasperserver_server_new();
    00058       jasperserver_string_cset(jasperServer->url, server);
    00059       jasperserver_string_cset(jasperServer->username, "tomcat");
    00060       jasperserver_string_cset(jasperServer->password, "tomcat");
    00061
    00062       // Create a new structure of type jasperserver_request_t to store
    00063       // the request information like the resource to list
    00064       request = jasperserver_request_new();
    00065
    00066       //Creating and populating a new resource_descriptor...
    00067       // The descriptor will be an image.
    00068       res = jasperserver_resource_descriptor_new();
    00069       request->resource = res;
    00070
    00071       jasperserver_string_cset(res->name, name);
    00072       jasperserver_string_cset(res->label, name);
    00073
    00074       // If you are uploading a new resource, be sure you set the isNew flag
    00075       // otherwise set it to 0 (that's when you just update a resource)
    00076       res->isNew = 1;
    00077       jasperserver_string_cset(res->uriString, parentUri);
    00078       jasperserver_string_cset(res->wsType, JS_TYPE_IMAGE );
    00079       jasperserver_string_cappend(res->uriString, "/");
    00080       jasperserver_string_cappend(res->uriString, name);
    00081
    00082       // Set the property JS_PROP_FILERESOURCE_HAS_DATA if there is come content
    00083       propHasData = jasperserver_resource_property_new();
    00084       jasperserver_string_cset(propHasData->name, JS_PROP_FILERESOURCE_HAS_DATA);
    00085       jasperserver_string_cset(propHasData->value, (infile) ? "true" : "false");
    00086
    00087       // In order to correctly save the resource in the right place, you need to set the JS_PROP_PARENT_FOLDER
    00088       propParentUri = jasperserver_resource_property_new();
    00089       jasperserver_string_cset(propParentUri->name, JS_PROP_PARENT_FOLDER);
    00090       jasperserver_string_cset(propParentUri->value, parentUri);
    00091
    00092       jasperserver_list_append((jasperserver_list_t **)&res->properties, (jasperserver_list_t *)propHasData);
    00093       jasperserver_list_append((jasperserver_list_t **)&res->properties, (jasperserver_list_t *)propParentUri);
    00094
    00095       // Call the web service
    00096       operationResult = jasperserver_ws_put(jasperServer, request, infile);
    00097
    00098       if (operationResult->returnCode == 0)
    00099       {
    00100          printf("Server version: %s, Return code: '%d', Return message '%s'\n", JS_CSTR( operationResult->version),  operationResult->returnCode, JS_CSTR( operationResult->returnMessage ));
    00101          printf("Uploaded resource: %s\n\n", JS_CSTR( request->resource->uriString));
    00102          fflush( stdout );
    00103
    00104          if (operationResult->resources)
    00105          {
    00106             jasperserver_print_resource_descriptor( operationResult->resources, 0 );
    00107          }
    00108       }
    00109       else
    00110       {
    00111           //printf("Error executing the service: %d (%s)", operationResult->returnCode, JS_UTFSTR(operationResult->returnMessage) );
    00112           printf("Error executing the service: %d (%s)", operationResult->returnCode, JS_UTFSTR(operationResult->returnMessage) );
    00113       }
    00114
    00115       jasperserver_server_free( jasperServer );
    00116       jasperserver_request_free( request );
    00117       jasperserver_operation_result_free( operationResult );
    00118
    00119 }
    00120
    00121
    00122 int main(int argc, char **argv)
    00123 {
    00124    char *parentUri = NULL;
    00125    char *resourceName = NULL;
    00126    char *inputfile = NULL;
    00127    printf("JasperServer list sample (C) 2007 JasperSoft Corp.!\n");
    00128
    00129    if (argc > 2)
    00130    {
    00131      resourceName = argv[1];
    00132      parentUri = argv[2];
    00133
    00134      if (argc > 3)
    00135      {
    00136         inputfile = argv[3];
    00137      }
    00138    }
    00139    else
    00140    {
    00141      printf("Usage: jsput <parent uri> <resource name> [input file name]\n");
    00142      printf("I.e.  jsput newImage /images  [myimage.gif]\n");
    00143      exit(0);
    00144    }
    00145    put_resource(resourceName, parentUri, inputfile);
    00146
    00147    return 0;
    00148 }