- Main Page
- Data Structures
- Files

- File List
- Globals

# jasperserver/services.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
    00022 #include <stdio.h>
    00023 #include "services.h"
    00024 #include "marshaller.h"
    00025 #include "unmarshaller.h"
    00026
    00027 #include "soap/soapH.h"
    00028 #include "soap/repositorySoapBinding.nsmap"
    00029
    00030 void jasperserver_store_soap_error(jasperserver_operation_result_t *response, struct soap *soap);
    00031
    00032 void jasperserver_init_soap(jasperserver_server_t *server, struct soap *soap)
    00033 {
    00034      soap_init(soap);
    00035      soap->userid = (char *)JS_CSTR(server->username);
    00036      soap->passwd = (char *)JS_CSTR(server->password);
    00037      soap->encodingStyle = "http://xml.apache.org/xml-soap/literalxml";
    00038 }
    00039
    00040 void jasperserver_end_soap(struct soap *soap)
    00041 {
    00042      soap_destroy(soap);
    00043      soap_end(soap);
    00044      soap_done(soap);
    00045 }
    00046
    00047
    00048 jasperserver_string_t *jasperserver_get_file_parent(char* path)
    00049 {
    00050      char* str = NULL;
    00051      str = path;
    00052      jasperserver_string_t *newPath = NULL;
    00053      newPath = jasperserver_string_new();
    00054
    00055
    00056      // Set the correct path separator..
    00057      while (strchr(str,'\\') )
    00058      {
    00059            str = strchr(str,'\\');
    00060            *str = '/';
    00061      }
    00062
    00063      str = path;
    00064      while (strchr(str,'/'))
    00065      {
    00066            str = strchr(str,'/');
    00067            str++;
    00068      }
    00069
    00070      if (str != path)
    00071      {
    00072         int len = 0;
    00073         str--;
    00074         len = str - path;
    00075
    00076         xmlBufferAdd(newPath->buffer , path, len);
    00077      }
    00078
    00079      return newPath;
    00080 }
    00081
    00082 jasperserver_operation_result_t * jasperserver_ws_list(jasperserver_server_t *server, jasperserver_request_t *request)
    00083 {
    00084   struct soap soap;
    00085   int rc = 0;
    00086   jasperserver_string_t *request_xml = NULL;
    00087   jasperserver_string_t *response_xml = NULL;
    00088   jasperserver_operation_result_t *operationResult = NULL;
    00089   char* response_buffer = NULL;
    00090
    00091   request_xml = jasperserver_string_new();
    00092   rc = jasperserver_request_marshal(request, request_xml);
    00093   if (rc < 0) {
    00094         printf("jasperserver_ws_list: Error at jasperserver_request_marshal\n");
    00095         return NULL;
    00096   }
    00097
    00098   jasperserver_init_soap(server, &soap);
    00099
    00100   soap_call_ns1__list(&soap, JS_CSTR(server->url), "", (char *)JS_CSTR(request_xml), &response_buffer);
    00101
    00102   jasperserver_string_free(request_xml);
    00103
    00104   if (soap.error)
    00105   {
    00106     operationResult = jasperserver_operation_result_new();
    00107     operationResult->returnCode = -1;
    00108     jasperserver_store_soap_error(operationResult, &soap);
    00109   }
    00110   else
    00111   {
    00112     //printf("Result %s\n", response_xml);
    00113     //fflush(stdout);
    00114     response_xml = jasperserver_string_new();
    00115     jasperserver_string_cappend(response_xml, BAD_CAST response_buffer );
    00116     operationResult = jasperserver_response_unmarshal(response_xml);
    00117   }
    00118
    00119   jasperserver_end_soap( &soap );
    00120
    00121   return operationResult;
    00122 }
    00123
    00124 jasperserver_operation_result_t * jasperserver_ws_delete(jasperserver_server_t *server, jasperserver_request_t *request)
    00125 {
    00126   struct soap soap;
    00127   int rc = 0;
    00128   jasperserver_string_t *request_xml = NULL;
    00129   jasperserver_string_t *response_xml = NULL;
    00130   jasperserver_operation_result_t *operationResult = NULL;
    00131   char* response_buffer = NULL;
    00132
    00133   request_xml = jasperserver_string_new();
    00134   rc = jasperserver_request_marshal(request, request_xml);
    00135   if (rc < 0) {
    00136         printf("jasperserver_ws_delete: Error at jasperserver_request_marshal\n");
    00137         return NULL;
    00138   }
    00139
    00140   jasperserver_init_soap(server, &soap);
    00141
    00142   soap_call_ns1__delete(&soap, JS_CSTR(server->url), "", (char *)JS_CSTR(request_xml), &response_buffer);
    00143
    00144   if (soap.error)
    00145   {
    00146     operationResult = jasperserver_operation_result_new();
    00147     operationResult->returnCode = -1;
    00148     jasperserver_store_soap_error(operationResult, &soap);
    00149   }
    00150   else
    00151   {
    00152     //printf("Result %s\n", response_xml);
    00153     //fflush(stdout);
    00154     response_xml = jasperserver_string_new();
    00155     jasperserver_string_cappend(response_xml, BAD_CAST response_buffer );
    00156     operationResult = jasperserver_response_unmarshal(response_xml);
    00157   }
    00158
    00159   jasperserver_string_free(request_xml);
    00160
    00161   jasperserver_end_soap( &soap );
    00162
    00163   return operationResult;
    00164 }
    00165
    00166 jasperserver_operation_result_t * jasperserver_ws_get(jasperserver_server_t *server, jasperserver_request_t *request, char *filename)
    00167 {
    00168   struct soap soap;
    00169   int rc = 0;
    00170   struct soap_multipart *attachment;
    00171   jasperserver_string_t *request_xml = NULL;
    00172   jasperserver_string_t *response_xml = NULL;
    00173   jasperserver_operation_result_t *operationResult = NULL;
    00174   char* response_buffer = NULL;
    00175
    00176   request_xml = jasperserver_string_new();
    00177   rc = jasperserver_request_marshal(request, request_xml);
    00178   if (rc < 0) {
    00179         printf("jasperserver_ws_get: Error at jasperserver_request_marshal\n");
    00180         return NULL;
    00181   }
    00182
    00183   jasperserver_init_soap(server, &soap);
    00184
    00185   soap_call_ns1__get(&soap, JS_CSTR(server->url), "", (char *)JS_CSTR(request_xml), &response_buffer);
    00186
    00187   if (soap.error)
    00188   {
    00189     operationResult = jasperserver_operation_result_new();
    00190     operationResult->returnCode = -1;
    00191     jasperserver_store_soap_error(operationResult, &soap);
    00192   }
    00193   else
    00194   {
    00195     //printf("Result %s\n", response_xml);
    00196     //fflush(stdout);
    00197     response_xml = jasperserver_string_new();
    00198     jasperserver_string_cappend(response_xml, BAD_CAST response_buffer );
    00199     operationResult = jasperserver_response_unmarshal(response_xml);
    00200
    00201     if (filename)
    00202     {
    00203         for (attachment = soap.mime.list; attachment; attachment = attachment->next)
    00204         {
    00205           if ((*attachment).id && !strncmp((*attachment).id,"<attachment>", strlen("<attachment>")))
    00206           {
    00207                 FILE *fp=fopen( filename,"wb");
    00208                 if (fp)
    00209             {
    00210                 fwrite((*attachment).ptr, (*attachment).size,1, fp);
    00211                     fclose(fp);
    00212             }
    00213             else
    00214             {
    00215                 fprintf(stderr, "Unable to open file: %s\n", filename);
    00216                 fflush(stderr);
    00217             }
    00218             break;
    00219           }
    00220         }
    00221     }
    00222   }
    00223
    00224   jasperserver_end_soap( &soap );
    00225   jasperserver_string_free(request_xml);
    00226
    00227   return operationResult;
    00228 }
    00229
    00230 jasperserver_operation_result_t * jasperserver_ws_runReport(jasperserver_server_t *server, jasperserver_request_t *request, char *filename)
    00231 {
    00232   struct soap soap;
    00233   int rc = 0;
    00234   struct soap_multipart *attachment;
    00235   jasperserver_string_t *request_xml = NULL;
    00236   jasperserver_string_t *response_xml = NULL;
    00237   jasperserver_operation_result_t *operationResult = NULL;
    00238   char* response_buffer = NULL;
    00239
    00240   request_xml = jasperserver_string_new();
    00241   rc = jasperserver_request_marshal(request, request_xml);
    00242   if (rc < 0) {
    00243         printf("jasperserver_ws_runReport: Error at jasperserver_request_marshal\n");
    00244         return NULL;
    00245   }
    00246
    00247   jasperserver_init_soap(server, &soap);
    00248
    00249   soap_call_ns1__runReport(&soap, JS_CSTR(server->url), "", (char *)JS_CSTR(request_xml), &response_buffer);
    00250
    00251   if (soap.error)
    00252   {
    00253     operationResult = jasperserver_operation_result_new();
    00254     operationResult->returnCode = -1;
    00255     jasperserver_store_soap_error(operationResult, &soap);
    00256   }
    00257   else
    00258   {
    00259     int attachNum = 0;
    00260     //printf("Result %s\n", response_xml);
    00261     //fflush(stdout);
    00262     response_xml = jasperserver_string_new();
    00263     jasperserver_string_cappend(response_xml, BAD_CAST response_buffer );
    00264     operationResult = jasperserver_response_unmarshal(response_xml);
    00265
    00266     if (filename)
    00267     {
    00268         jasperserver_string_t *dir = NULL;
    00269         dir = jasperserver_get_file_parent(filename);
    00270         if (JS_NOTNULL(dir))
    00271         {
    00272            jasperserver_string_cappend(dir,"/images");
    00273         }
    00274         else
    00275         {
    00276             jasperserver_string_cappend(dir,"images");
    00277         }
    00278
    00279         for (attachment = soap.mime.list; attachment; attachment = attachment->next)
    00280         {
    00281           attachNum++;
    00282           if ((*attachment).id && !strncmp((*attachment).id,"<report>", strlen("<report>")))
    00283           {
    00284                 FILE *fp=fopen( filename,"wb");
    00285                 if (fp)
    00286             {
    00287                 fwrite((*attachment).ptr, (*attachment).size,1, fp);
    00288                     fclose(fp);
    00289             }
    00290             else
    00291             {
    00292                 fprintf(stderr, "Unable to open file: %s\n", filename);
    00293                 fflush(stderr);
    00294             }
    00295             break;
    00296           }
    00297           else if ((*attachment).id && !strncmp((*attachment).id,"<img_", strlen("<img_")))
    00298           {
    00299                int img_name_len = 0;
    00300                char *image_name_str = NULL;
    00301
    00302                mkdir(JS_CSTR(dir),0755);
    00303
    00304                img_name_len = strlen((*attachment).id) + strlen(JS_CSTR(dir)) + 2;
    00305                image_name_str = (char *)malloc(img_name_len);
    00306                sprintf(image_name_str,"%s/",JS_CSTR(dir));
    00307                strncat(image_name_str, (char *)((attachment->id)+1), strlen((*attachment).id)-2);
    00308
    00309                FILE *fp=fopen( image_name_str,"wb");
    00310                    if (fp)
    00311                {
    00312                     fwrite((*attachment).ptr, (*attachment).size,1, fp);
    00313                     fclose(fp);
    00314                }
    00315                else
    00316                {
    00317                     fprintf(stderr, "Unable to open file: %s\n", image_name_str);
    00318                     fflush(stderr);
    00319                }
    00320
    00321                free(image_name_str);
    00322
    00323           }
    00324         }
    00325
    00326         jasperserver_string_free(dir);
    00327     }
    00328   }
    00329
    00330   jasperserver_end_soap( &soap );
    00331   jasperserver_string_free(request_xml);
    00332
    00333   return operationResult;
    00334 }
    00335
    00336 jasperserver_operation_result_t * jasperserver_ws_put(jasperserver_server_t *server, jasperserver_request_t *request, char *filename)
    00337 {
    00338   struct soap soap;
    00339   int rc = 0;
    00340   struct soap_multipart *attachment;
    00341   jasperserver_string_t *request_xml = NULL;
    00342   jasperserver_string_t *response_xml = NULL;
    00343   jasperserver_operation_result_t *operationResult = NULL;
    00344   char* response_buffer = NULL;
    00345
    00346   request_xml = jasperserver_string_new();
    00347   rc = jasperserver_request_marshal(request, request_xml);
    00348   if (rc < 0) {
    00349         printf("jasperserver_ws_runReport: Error at jasperserver_request_marshal\n");
    00350         return NULL;
    00351   }
    00352
    00353   jasperserver_init_soap(server, &soap);
    00354   if (filename != NULL)
    00355   {
    00356       soap_set_dime(&soap);
    00357       rc = jasperserver_attach_file(&soap, filename);
    00358       if (rc != 0)
    00359       {
    00360          operationResult = jasperserver_operation_result_new();
    00361          operationResult->returnCode = -1;
    00362          jasperserver_string_format(operationResult->returnMessage, "Unable to attach file %s: error [%d]",filename, rc);
    00363       }
    00364   }
    00365
    00366   soap_call_ns1__put(&soap, JS_CSTR(server->url), "", (char *)JS_CSTR(request_xml), &response_buffer);
    00367
    00368   if (soap.error)
    00369   {
    00370     operationResult = jasperserver_operation_result_new();
    00371     operationResult->returnCode = -1;
    00372     jasperserver_store_soap_error(operationResult, &soap);
    00373   }
    00374   else
    00375   {
    00376     //printf("Result %s\n", response_xml);
    00377     //fflush(stdout);
    00378     response_xml = jasperserver_string_new();
    00379     jasperserver_string_cappend(response_xml, BAD_CAST response_buffer );
    00380     operationResult = jasperserver_response_unmarshal(response_xml);
    00381   }
    00382
    00383   jasperserver_end_soap( &soap );
    00384   jasperserver_string_free(request_xml);
    00385
    00386   return operationResult;
    00387 }
    00388
    00389
    00390 /*
    00391 *
    00392 *
    00393 */
    00394 void jasperserver_store_soap_error(jasperserver_operation_result_t *response, struct soap *soap)
    00395 {
    00396    int written = 0;
    00397    if (soap_check_state(soap))
    00398     jasperserver_string_cset(response->returnMessage, "Error: soap struct not initialized\n");
    00399   else if (soap->error)
    00400   { const char *c, *v = NULL, *s, **d;
    00401     d = soap_faultcode(soap);
    00402     if (!*d)
    00403       soap_set_fault(soap);
    00404     c = *d;
    00405     if (soap->version == 2)
    00406       v = *soap_faultsubcode(soap);
    00407     s = *soap_faultstring(soap);
    00408     d = soap_faultdetail(soap);
    00409
    00410     jasperserver_string_format(response->returnMessage, "%s%d fault: %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ", soap->version ? (int)soap->version : soap->error, c, v ? v : "no subcode", s ? s : "[no reason]", d && *d ? *d : "[no detail]");
    00411
    00412   }
    00413 }