need advice jaxm and web service

Discussion in 'Java' started by JPractitioner, Mar 13, 2006.

  1. 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, Mar 13, 2006
    #1
    1. Advertising

  2. 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, Mar 13, 2006
    #2
    1. Advertising

  3. 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.
     
    JPractitioner, Mar 13, 2006
    #3
  4. JPractitioner

    Guest

    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/
     
    , Mar 13, 2006
    #4
  5. 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.
     
    JPractitioner, Mar 14, 2006
    #5
  6. JPractitioner

    Guest

    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/
     
    , Mar 14, 2006
    #6
  7. JPractitioner

    Guest

    escreveu:

    > JPractitioner 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/
     
    , Mar 14, 2006
    #7
  8. Hello Robert, thanks a lot!
    I will definitely study the items you put for me here.


    Thanks,
    Makmur.
     
    JPractitioner, Mar 15, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joerg
    Replies:
    0
    Views:
    421
    joerg
    Oct 13, 2003
  2. Chris Berg

    SOAP / XML / JAXM? question

    Chris Berg, Nov 10, 2003, in forum: Java
    Replies:
    1
    Views:
    352
    Marcus Crafter
    Nov 10, 2003
  3. NahtMy ReelName

    JAXM's demise in JWSDP 1.2 and 1.3

    NahtMy ReelName, Nov 27, 2003, in forum: Java
    Replies:
    1
    Views:
    576
    NahtMy ReelName
    Nov 29, 2003
  4. Danny M
    Replies:
    0
    Views:
    417
    Danny M
    Feb 3, 2004
  5. Replies:
    0
    Views:
    505
Loading...

Share This Page