본문 바로가기

Prog.Lang. or Query/Jython

jython example


 - text to html

line_break="\n"

data=file("e://untitled.txt","r").read()
out=data.replace(line_break,"<br/>")

file("c://out.txt","w").write(out)


 - Skype chat to speech

# ----------------------------------------------------------------------------------------------------
#  Python / Skype4Py example that prints out chat messages
#
#  Tested with  Skype4Py version 0.9.28.5 and Skype verson 3.8.0.96

import sys
import os
import time
import Skype4Py
import random

def ndsSay(ndsWords):
    ndsIn = str(ndsWords)
    zcmd='espeak "'+ndsIn+'"'
    print zcmd
    f=os.popen(zcmd)
    f.close()
    ndsWords=""
    ndsTalk=""
    zcmd=''
       
# ----------------------------------------------------------------------------------------------------
# Fired on attachment status change. Here used to re-attach this script to Skype in case attachment is lost. Just in
#case.
def OnAttach(status):
    print 'API attachment status: ' + skype.Convert.AttachmentStatusToText(status)
    if status == Skype4Py.apiAttachAvailable:
        skype.Attach()

    if status == Skype4Py.apiAttachSuccess:
       print('***************************************')


# ----------------------------------------------------------------------------------------------------
# Fired on chat message status change.
# Statuses can be: 'UNKNOWN' 'SENDING' 'SENT' 'RECEIVED' 'READ'       

def OnMessageStatus(Message, Status):
    if Status == 'RECEIVED':
        print(Message.FromDisplayName + ': ' + Message.Body)
        ndsSay(Message.FromDisplayName)
        ndsSay(Message.Body)

    if Status == 'READ':
        ndsMonkey = "todo"
        print(Message.FromDisplayName + ': ' + Message.Body)
        ndsSay(Message.FromDisplayName)
        ndsSay(Message.Body)

    if Status == 'SENT':
        print('Myself ' + Message.Body)


# ----------------------------------------------------------------------------------------------------
# Creating instance of Skype object, assigning handler functions and attaching to Skype.
skype = Skype4Py.Skype()
skype.OnAttachmentStatus = OnAttach
skype.OnMessageStatus = OnMessageStatus

print('***************************************')
print 'Connecting to Skype..'
skype.Attach()

# ----------------------------------------------------------------------------------------------------
# Looping until user types 'exit'
Cmd = ''
while not Cmd == 'exit':
    Cmd = raw_input('')


 - How To Invoke A Web Service From Jython In ODI

Yes, it is possible to call simple Web Services from Jython.

For accessing a Web Service, the following source code template is based on the SOAP protocol:
from java.net import *
from java.io import *

#Set Data
xmldata = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<[the Information to be exchanged with the Web Service]>
</soap:Body>
</soap:Envelope>'''

#Create socket
host = "[the Host]"
port = [the Port number]
addr = InetAddress.getByName(host)
sock = Socket(addr, port)
encd = "UTF-8"
srvc = "[the Service name]"

#Send header
path = "[path to the Web Service]"
wr = BufferedWriter(OutputStreamWriter(sock.getOutputStream(),encd))
wr.write('''POST %s HTTP/1.1\r\n''' % path)
wr.write('''Host: %s\r\n''' % (host))
wr.write('''Content-Type: text/xml; charset=%s\r\n''' % (encd))
wr.write('''Content-Length: %s\r\n''' % (len(xmldata)))
wr.write('''SOAPAction: "%s/%s"\r\n''' % (path,srvc))
wr.write('''\r\n''')

#Send data
wr.write(xmldata)
wr.flush()

#Read Response
rd = BufferedReader(InputStreamReader(sock.getInputStream()))
line=rd.readLine()
output=''
readdata=0

while line<>'':
line=rd.readLine()
if readdata==1:
output+=line+"\n"
if line.count("Content-Length")>0:
readdata=1

output=output.replace('&lt;','<').replace('&gt;','>').strip()

print output


Complex Web Services (BPEL...) may not all be accessed in this way and require a case by case study.


The Jython template above is not Apache Axis2 based, as it does not use WS stack but directly the SOAP protocol.
Apache Axis2 is the core engine of the Web Services stack and aims to be the platform of the next generation of Web Services and Service Oriented Architectures (SOA). It provides the basis for the entire WS stack, supporting both SOAP and REST.
SOAP protocol (Simple Object Access Protocol) related links:
http://en.wikipedia.org/wiki/Simple_Object_Access_Protocol#Structure_of_a_SOAP_message
http://www.w3schools.com/soap/soap_intro.asp
http://www.intertwingly.net/stories/2002/03/16/aGentleIntroductionToSoap.html