Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

need advice jaxm and web service

Thread view: 
JPractitioner - 13 Mar 2006 08:32 GMT
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
JPractitioner - 13 Mar 2006 10:23 GMT
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....
JPractitioner - 13 Mar 2006 10:29 GMT
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.
iksrazal@gmail.com - 13 Mar 2006 13:37 GMT
> 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/
JPractitioner - 14 Mar 2006 10:58 GMT
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.
iksrazal@gmail.com - 14 Mar 2006 14:23 GMT
> Hello Robert,
> Thanks a lot for replying.
[quoted text clipped - 11 lines]
>
> 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/
iksrazal@gmail.com - 14 Mar 2006 14:27 GMT
iksra...@gmail.com escreveu:

// this is wrong
return another_xml_as_string;

It should be :

out.println(another_xml_as_string);

There was a lot of code in that servlet that didn't contribute to the
example.

HTH,
Robert
http://www.braziloutsource.com/
JPractitioner - 15 Mar 2006 12:29 GMT
Hello Robert, thanks a lot!
I will definitely study the items you put for me here.

Thanks,
Makmur.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.