I am developing a Python application and I want to use Jasper Reports as a report generator. My purpose is only to launch a report that is in a repository of Jasper Reports . The JasperServer server is working and I have designed reports using iReport . However, when I try to launch using resquest the report always gives me 404 error (it does not find the resource).
I have created a Python class to create the requests , just like this:
# -*- coding: utf-8 -*-
import requests
import webbrowser
from datetime import datetime
import hashlib
import os
# 23-10-2016 Módulo de Jasper Reports.
class jasperreports(object):
'''Clase de configuración de informes para Jasper Reports'''
def __init__(self, maquina, puerto, recurso):
self.__maquina = maquina
self.__puerto = puerto
self.__recurso = recurso
self.__session = requests.Session()
self.__token = self.__generador_token()
def __generador_token(self):
'''Generador de token'''
return hashlib.sha1(os.urandom(128)).hexdigest()
def login(self, usuario, passwd):
'''Clase login para acceso a JasperServer'''
url = "http://%s:%s/jasperserver/rest_v2/login" % (self.__maquina, \
self.__puerto)
data = "j_username=%s&j_password=%s" % (usuario, passwd)
header = {"content-type" : "application/x-www-form-urlencoded"}
ret = self.__session.post(url, data, headers = header)
self.__cookie = ret.cookies['JSESSIONID']
print ret
def report(self, path_report, report, format_ = None, params = None):
url = 'http://%s:%s/%s/rest_v2/reports/%s/%s' % (self.__maquina, \
str(self.__puerto), \
self.__recurso, \
path_report, \
report)
headers = {"Authorization": "Basic " + self.__token,
"Accept": "application/json",
"Content-Type": "application/xml"}
auth = ('jasperadmin', 'jasperadmin')
cookies = dict(cookies=self.__cookie)
ret = self.__session.get(url, headers = headers, auth = \
('jasperadmin', 'jasperadmin'), cookies = cookies)
try:
webbrowser.open(ret)
except:
nomfich = 'temp_error_%s.html' % str(datetime.now())
f = open('temp/%s' % nomfich,'w')
f.write(ret.text)
f.close()
webbrowser.open('temp/%s' % nomfich)
def info(self):
url = 'http://%s:%s/%s/rest_v2/serverInfo' % (self.__maquina, \
str(self.__puerto), \
self.__recurso)
# Se realiza petición de información al servidor JasperServer...
ret = requests.get(url,headers={"accept":"application/json"}).json()
# Se recupera información del servidor si todo ha ido bien...
data = { 'dateFormatPattern' : ret['dateFormatPattern'],
'datetimeFormatPattern' : ret['datetimeFormatPattern'],
'version' : ret['version'],
'edition' : ret['edition'],
'build' : ret['edition']
}
# Se devuelven datos.
return data
Well, I urge the class:
jr = jasperreports("localhost", 8080, "jasperserver")
jr.login('jasperadmin', 'jasperadmin')
jr.report('wshifts/Corporativo', 'usuario', 'jprint')
I understand, reading the documentation of JasperReports, that it is not necessary to use SOAP, that with rest_v2 it is enough. Well, when I try to access the report I get 404 error . I think I'm leaving something, but I do not know what it is. It generates the cookie perfectly and it seems that it is not a token problem.