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/