Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

26 June 2007

Being a Java Developer or Oracle Developer?

Two weeks ago, i have assigned to call a middleware application via HTTP. The middleware app, accepts XML commands as a webservice manner. I got help from a java developer to write down java codes to establish the work. Finally a java application is created. I put java into Oracle:
create or replace and compile java source named java_notify as
public class MW
{
public static String ExecuteCommand( String _inputXML, String _MWUrl )
{
String ReturnMessage = "";

try
{
String Line;
java.net.URL MWUrl = new java.net.URL(_MWUrl);
java.net.URLConnection MWConnection = MWUrl.openConnection();
java.io.PrintWriter RequestWriter = null ;
java.io.BufferedReader RequestReader = null ;


MWConnection.setRequestProperty("Content-Type", "text/xml");
MWConnection.setDoOutput(true);

RequestWriter = new java.io.PrintWriter( MWConnection.getOutputStream() );

RequestWriter.println( _inputXML );
RequestWriter.close();

RequestReader = new java.io.BufferedReader( new java.io.InputStreamReader( MWConnection.getInputStream() ) );
Line = RequestReader.readLine() ;
while ( Line != null )
{
ReturnMessage += Line;
Line = RequestReader.readLine();
}

RequestReader.close();
}
catch ( Exception e)
{
ReturnMessage = "ERROR";
ReturnMessage += e.toString();
e.printStackTrace();
}

return ReturnMessage;
}

}
/


I wrap the java source with a function:
CREATE OR REPLACE FUNCTION fnc_Not
(
pis_InputXML IN VARCHAR2,
pis_MWUrl IN VARCHAR2
) RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'MW.ExecuteCommand( java.lang.String, java.lang.String ) return java.lang.String';
/


Then call this function, from packages. The process logic was loading a java source into Oracle. I start to write pure oracle codes to accomplish this task. I write some Oracle codes:
--create request : set meta data of request object
--POST method and HTTP 1.0 protocol
vt_HttpRequest := utl_http.begin_request(vs_MWUrl,'POST','HTTP/1.0');

--XML text and capable for turkish chars
utl_http.set_header(vt_HttpRequest,'Content-Type', 'text/xml;charset=UTF-8');

--set content length
utl_http.set_header(vt_HttpRequest,'Content-Length', length(pis_Request));

--assign input XML to request object
utl_http.write_text(vt_HttpRequest, pis_Request);

--execute command
vt_HttpResponse := utl_http.get_response(vt_HttpRequest);

--get response object from request.
utl_http.read_text(vt_HttpResponse, vs_Response);

--close response
utl_http.end_response(vt_HttpResponse);


These two approach does the same work. So, being a java programmer for oracle programmers is not important. For instance, i am not a java developer; but i can do all my work with Oracle.

18 June 2007

Character Encoding Problem While Executing Web Services With Oracle


A few months ago, I have writen a post about web services in Oracle. It works funny until one of my colleagues opened me a defect :( The defect was about, utl_http package of oracle, was not able to handle Turkish characters. I walk around the code again I realized that, I have forgetten set Content-Type property in http header of web service. I made a small modification and it runs correctly again. The only thing i did is that setting charset property of http request.
I put down the simple code below:
DECLARE
vs_Request VARCHAR2(32767);
vs_Response VARCHAR2(32767);
http_request utl_http.req;
http_response utl_http.resp;
response_xml xmltype;
vs_Url VARCHAR2(64);
BEGIN
vs_Request := 'some well defined xml data.';
vs_Url := 'http://10.14.194.224/WS';



http_request := utl_http.begin_request(vs_Url, 'POST', 'HTTP/1.0');
utl_http.set_header(http_request, 'Content-Type', 'text/xml;charset=UTF-8');
utl_http.set_header(http_request, 'Content-Length', length(vs_Request));

utl_http.write_text(http_request, vs_Request);

http_response := utl_http.get_response(http_request);

utl_http.read_text(http_response, vs_Response);
utl_http.end_response(http_response);

dbms_output.put_line('Response of request is :');
dbms_output.put_line(substr(vs_Response, 1, 255));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error occured while requesting...');
dbms_output.put_line('Error is ');
dbms_output.put_line(SQLERRM);

END;



But be careful on red line of code. If you put a space such as 'text/xml; charset=UTF-8' you can get error again.