need advice jaxm and web service

J

JPractitioner

hi guys..
I need to do something that will send and retrive data via webservice.
From some googling efforts, i found that jaxm might just fit the
purpose. However, this library is no longer distributed with jwsdp, and
i can not get it elsewhere other than on JPackage Project. The problem
with that option is that, their archieves are for linux likes. (and
yes.. i'm on windows). Can anyone tell me how i can still get this
library?

Other option for me is to write the xml codes straight away... but how
to straight away write the xml codes from a java file? I am expecting
clues on where to get started..



Thanks in advance. :D
 
J

JPractitioner

I think i can try write the xml on an OutputStream and send it via
HttpURLConnection...
hmm .. i'm thinking of doing an http post.. but if http post can be
done, why do i/ppl still need the web service? hmm....
 
J

JPractitioner

guys, if i did it by the way stated above... do i have to write the dtd
my self? I am acting as a client in this case.
 
I

iksrazal

JPractitioner escreveu:
I think i can try write the xml on an OutputStream and send it via
HttpURLConnection...

Just convert Document to string and send that as an HTTP name / value
pair.
hmm .. i'm thinking of doing an http post.. but if http post can be
done, why do i/ppl still need the web service? hmm....

For example, a nice set of tools that maps your XML into objects and
sends it over the wire in a standard, language independant way. Plus,
there are lots of security and infrastructure details that gets complex
quickly, that web services at least goes a long way to addressing.

In short, URLConnection or whatever is fine for something simple.
Beyond that, you start reinventing the wheel.

HTH,
Robert
http://www.braziloutsource.com/
 
J

JPractitioner

Hello Robert,
Thanks a lot for replying.

I am agree with you that i was about to reinvent the wheel (which
should not ever be done) as what i understand now, i am trying to send
xml formatted data (in String) via http post. I will not proceed with
this way. But what other ways/options i have to proceed?

Can u explain to me please, a little bit details on the processes
needed to be carried by using the approach you are suggesting? I did
not understood clearly, what u meant by
"Just convert Document to string and send that as an HTTP name / value
pair. "?


Thanks.
 
I

iksrazal

JPractitioner escreveu:
Hello Robert,
Thanks a lot for replying.

I am agree with you that i was about to reinvent the wheel (which
should not ever be done) as what i understand now, i am trying to send
xml formatted data (in String) via http post. I will not proceed with
this way. But what other ways/options i have to proceed?

Can u explain to me please, a little bit details on the processes
needed to be carried by using the approach you are suggesting? I did
not understood clearly, what u meant by
"Just convert Document to string and send that as an HTTP name / value
pair. "?


Thanks.

Sure - its easy enough. The example below sends an xml file as a String
to a servlet. It uses apache commons HttpClient - but you could also
use the JDK class URLConnectionReader. Also, the example uses basic
authentication in the servlet container - its easy enough to skip that
part if you don't need it. The servlet also returns a seperate string
to the caller:

package com.infoseg.mr.hclient;

import java.io.*;
import java.util.*;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;

import com.infoseg.mr.security.*;

public class PostXML
{
/** Configuração arquivo */
private static ResourceBundle rb;
private static String realm;
private static String domain;
private static String url;
private static String username;
private static String password;

/** Configuração arquivo name */
private final static String WZC_BUNDLE_NAME =
"com.infoseg.mr.hclient.post";

/**
Class Construtor
*/
private PostXML()
{
try
{
getConfig();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception
{
StringBuffer outfile = null;
try
{
PostXML post = new PostXML();
post.getConfig();
HttpClient client = new HttpClient();
client.getState().setCredentials(realm, domain, new
UsernamePasswordCredentials(username, password));

InputStream fis = PostXML.class.getResourceAsStream("at.xml");
//convert file to String
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
int ch;
StringBuffer buffer = new StringBuffer();
while ((ch = in.read()) > -1)
{
buffer.append((char)ch);
}
in.close();

PostMethod method = new PostMethod(url);
method.addParameter("xml", buffer.toString());
int returnCode = client.executeMethod(method);
String reposta = method.getResponseBodyAsString();
System.out.println("return code: " + returnCode);
System.out.println("Response body: " + reposta);

if (200 == returnCode)
{
// write response to file
String path = "/usr/local/logs/hclient/";
outfile = new StringBuffer();
outfile.append(path);
outfile.append("hclient");
outfile.append("-");
outfile.append(WSSTokenizer.getFormattedCurrentTimeAsString());
outfile.append(".xml");
OutputStream bos = new FileOutputStream(outfile.toString());
bos.write(reposta.getBytes());
bos.close();
}
else
{
System.out.println("\n\nCannot create XML response file, HTTP
server returned error code: " + returnCode);
}
}
catch (FileNotFoundException e)
{
System.err.println("\n\nCannot create XML file: " + outfile);
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
Load the configuration needed for a secure web service invokation.
<p>
@exception Exception thrown if the requested
config is not found or has problems.
*/
private void getConfig()
{
try
{
//ResourceBundle vai throw se no tem campos
rb = ResourceBundle.getBundle(WZC_BUNDLE_NAME);

realm = rb.getString("realm");
domain = rb.getString("domain");
url = rb.getString("url");
username = rb.getString("username");
password = rb.getString("password");

System.out.println("realm: " + realm);
System.out.println("domain: " + domain);
System.out.println("url: " + url);
System.out.println("username: " + username);
System.out.println("password: " + password);
}
catch (Exception e)
{
System.err.println ("\nConfiguration load failed\n");
e.printStackTrace();
}
}
}

Config file:

realm=atualiza
domain=localhost
url=http://localhost:8080/jaxb/servlet/com.infoseg.mr.atualiza.controller..XMLReciever
username=yyy
password=zzz

Servlet:

public class XMLReciever extends HttpServlet
{

public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
ResponseBuilder reposta = null;
try
{
// Read XML parameter from HTTP message
String xml = req.getParameter("xml");
Fwlog.debug(this, Fwlog.WI, "XML received: " + xml);

if ((null == xml) || (xml.length() < 1))
{
throw new java.lang.IllegalStateException("ERR: Invalid XML
received - cannot be null or blank");
}
// more code ...
return another_xml_as_string;
}
catch (Exception e)
{
// boom!
}

HTH,
Robert
http://www.braziloutsource.com/
 
J

JPractitioner

Hello Robert, thanks a lot!
I will definitely study the items you put for me here.


Thanks,
Makmur.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top