0

We are students. In our project,we want to send xml block,basically saml assertion,from one server to another server via http post method. Can anyone help us out in sending the XML object from one servlet to another servlet where each servlet resides on two different computers in java.

/* here we are trying to send xml object(root) from one servlet to another servlet which resides on different pc... but dispatcher method isnt working in this case.*/

public class sp1serv extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws      ServletException,java.io.IOException
{
Connection c=null;
Statement s= null; 
ResultSet rs = null;
String d=null;
int flag=0;
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
Response response=null;
XMLObject root=null;
HttpSession session1=req.getSession();
System.out.println(session1.getAttribute("sAccessLevel"));
System.out.println(session1.getAttribute("sUserId"));
String eid=session1.getAttribute("sUserId").toString();
String[] str1 = {"response","attr",session1.getAttribute("sAccessLevel").toString(),      session1.getAttribute("sUserId").toString() };
String filename= eid.concat(".xml");
try {
response=SAMLProtocol.passResponse(str1);
root=SAMLSignature.passSignature(response,filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
req.setAttribute("SP1",root);
String abc="http://169.254.229.232:8080/sp_response_handler";
RequestDispatcher rd=getServletContext().getRequestDispatcher(abc);
rd.forward(req, resp);
break;
}
}
}
}}

/* this servlet is used for retrieving xml object(root) and parsing it..on another server.*/

public class sp1_response_handler extends HttpServlet {
private static final long serialVersionUID = 1L;
public sp1_response_handler() {
super();
// TODO Auto-generated constructor stub
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {
// TODO Auto-generated method stub
Response resp=null;
//XMLObject resp=null;
resp=(Response) request.getAttribute("SP1");
int result=0;
//SAMLSignature verification=null;
try {
result=SAMLSignature.verify(resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result==1){
List attributeStatements = resp.getAssertions().get(0).getAttributeStatements();
for (int i = 0; i < attributeStatements.size(); i++)
{
List attributes = ((AttributeStatement) attributeStatements.get(i)).getAttributes();
for (int x = 0; x < attributes.size(); x++)
{
String strAttributeName = ((XMLObject) attributes.get(x)).getDOM().getAttribute("Name");
List<XMLObject> attributeValues = ((Attribute) attributes.get(x)).getAttributeValues();
for (int y = 0; y < attributeValues.size(); y++)
{
String strAttributeValue = attributeValues.get(y).getDOM().getTextContent();
System.out.println(strAttributeName + ": " + strAttributeValue);
}
}
}
response.sendRedirect("SP1.jsp");
}
else
{
System.out.println("NOT a Valid Signature");
}

}}
Yesha Dave
  • 1
  • 1
  • 2
  • 2
    usually people on stackoverflow can help you in figuring out what's wrong with your code, but we don't do your homework :) Try something, show your code, and you'll get help :) – MarcoS Mar 16 '12 at 14:51
  • possible duplicate of [Calling servlet from servlet](http://stackoverflow.com/questions/7774749/calling-servlet-from-servlet) – Ted Hopp Mar 16 '12 at 14:52

2 Answers2

0

If you are using spring, you can use RestTemplate. From the docs:

String uri = "http://example.com/hotels/1/bookings";
PostMethod post = new PostMethod(uri);
// create booking request content
String request = post.setRequestEntity(new StringRequestEntity(request));
httpClient.executeMethod(post);

if (HttpStatus.SC_CREATED == post.getStatusCode()) {
  Header location = post.getRequestHeader("Location");
  if (location != null) {
    System.out.println("Created new booking at :" + location.getValue());
  }
}
beerbajay
  • 19,652
  • 6
  • 58
  • 75
0

Something like that should work (with the parameters being a Map<String,String>):

            StringBuffer data = new StringBuffer();
            if (parameters != null && parameters.size() > 0) {
                for (Entry<String, String> e : parameters.entrySet()) {
                    if (data.length() > 0) {
                        data.append('&');
                    }
                    data.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8"));
                }
            }
            String parametersAsString = data.toString();
            // Send data
            URL local_url = new URL(url);
            URLConnection conn = local_url.openConnection();
            conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(parametersAsString);
            wr.flush();
            break;
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117