Julian Noye

Greenhorn
+ Follow
since Nov 17, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Julian Noye

Originally posted by Amol Nayak:
Can i know how are you invoking this servlet?



Hi Amol

I'm invoking it from the browser for testing and then via javascript in a html page.

Regards
Julian
15 years ago
Hi Guys

I am trying to create a servlet that uses SAAJ to communicate with a SOAP webservice.

The webservice uses user/pw in the HTTP Authorization header to validate the request.

My problem is I have not been able to set the user/pw in the HTTP header and I get a 401 return code.

I have based my servlet on this example http://faq.javaranch.com/java/WebServicesHowTo#saaj-client

Here is my code,

package examples.java.servlets;

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

import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Node;

public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{

String username = request.getParameter("julian");
String password = request.getParameter("pw");
Integer timeout = new Integer(5*60*1000);
String operation = "getLanguages";
String urn = "LanguagesRequest";
String destination = "http://julian.vic.imrworldwide.com:8081/axis2/services/Reference";

try
{
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

// Set HTTP headers.
// This does not work!!!
String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);

// Create and populate the body
SOAPBody body = envelope.getBody();

// Create the main element and namespace

SOAPElement bodyElement = body.addChildElement(
envelope.createName(operation, "ns1", "http://sc.online.nielsen.com/api/ws"));

// Add parameters
bodyElement.addChildElement("LanguagesRequest");
envelope = soapPart.getEnvelope();
SOAPHeader sh = envelope.getHeader();
sh = null;


// Save the message
message.saveChanges();
SOAPMessage msg = message;

// Send the message and get the reply
SOAPMessage reply = connection.call(msg, destination);

// Retrieve the result - no error checking is done: BAD!
soapPart = reply.getSOAPPart();

body = envelope.getBody();
Iterator iter = body.getChildElements();
Node resultOuter = ((Node) iter.next()).getFirstChild();
Node result = resultOuter.getFirstChild();
//System.out.println("add("+arg1+","+arg2+") = "+result.getNodeValue());

// Close the connection
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World SC API</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World SC API 999.0</h1>");
pw.println("</body></html>");
}
}
15 years ago
Hi Guys

I am trying to create a servlet that uses SAAJ to communicate with a SOAP webservice.

The webservice uses user/pw in the HTTP Authorization header to validate the request.

My problem is I have not been able to set the user/pw in the HTTP header and I get a 401 return code.

I have based my servlet on this example http://faq.javaranch.com/java/WebServicesHowTo#saaj-client

Here is my code,

package examples.java.servlets;

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

import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Node;

public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{

String username = request.getParameter("julian");
String password = request.getParameter("pw");
Integer timeout = new Integer(5*60*1000);
String operation = "getLanguages";
String urn = "LanguagesRequest";
String destination = "http://julian.vic.imrworldwide.com:8081/axis2/services/Reference";

try
{
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

// Set HTTP headers.
// This does not work!!!
String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);

// Create and populate the body
SOAPBody body = envelope.getBody();

// Create the main element and namespace

SOAPElement bodyElement = body.addChildElement(
envelope.createName(operation, "ns1", "http://sc.online.nielsen.com/api/ws"));

// Add parameters
bodyElement.addChildElement("LanguagesRequest");
envelope = soapPart.getEnvelope();
SOAPHeader sh = envelope.getHeader();
sh = null;


// Save the message
message.saveChanges();
SOAPMessage msg = message;

// Send the message and get the reply
SOAPMessage reply = connection.call(msg, destination);

// Retrieve the result - no error checking is done: BAD!
soapPart = reply.getSOAPPart();

body = envelope.getBody();
Iterator iter = body.getChildElements();
Node resultOuter = ((Node) iter.next()).getFirstChild();
Node result = resultOuter.getFirstChild();
//System.out.println("add("+arg1+","+arg2+") = "+result.getNodeValue());

// Close the connection
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World SC API</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World SC API 999.0</h1>");
pw.println("</body></html>");
}
}
15 years ago