You need to sign in to do that
Don't have an account?
How to persist login session?
I'm in the process of trying to convert the Enterprise java example to a servlet. The login works fine, but when I try to execute a query I get the followingn error:
Failed to execute query succesfully, error message was:
; nested exception is:
java.io.IOException: No serializer found for class com.sforce.soap.enterprise._SessionHeader in registry org.apache.axis.encoding.TypeMappingImpl@96ad7c
My guess is that I'm not persisting or passing the login session header to the query request. Can someone tell me how to do this in a servlet or JSP? I can't find examples of this anywhere. Here is my servlet:
package sforcetest3;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.xml.rpc.ServiceException;
import com.sforce.soap.enterprise.DescribeGlobalResult;
import com.sforce.soap.enterprise.DescribeSObjectResult;
import com.sforce.soap.enterprise.Field;
import com.sforce.soap.enterprise.FieldType;
import com.sforce.soap.enterprise.GetDeletedResult;
import com.sforce.soap.enterprise.GetUpdatedResult;
import com.sforce.soap.enterprise.GetUserInfoResult;
import com.sforce.soap.enterprise.ID;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.PicklistEntry;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.ResetPasswordResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.SearchRecord;
import com.sforce.soap.enterprise.SearchResult;
import com.sforce.soap.enterprise.SetPasswordResult;
import com.sforce.soap.enterprise.SforceServiceLocator;
import com.sforce.soap.enterprise.SoapBindingStub;
import com.sforce.soap.enterprise._QueryOptions;
import com.sforce.soap.enterprise._SessionHeader;
//import com.sforce.soap.enterprise.fault.LoginFault;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.soap.enterprise.sobject.Lead;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.soap.enterprise.sobject.Task;
import com.sforce.soap.enterprise.sobject.User;
import com.sforce.soap.enterprise.fault.*;
public class Servlet1
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
private SoapBindingStub binding;
private LoginResult loginResult = null;
private String un = "XXX";
private String pw = "XXX";
private boolean loggedIn = false;
private GetUserInfoResult userInfo = null;
private ID[] accounts = null;
private ID[] contacts = null;
private ID[] tasks = null;
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
if (request.getQueryString() == null) { // login if
out.println("attribute names: " + request.getQueryString());
out.println("Creating the binding to the web service...<P>");
/*
* There are 2 ways to get the binding, one by passing a url to
* the getSoap() method of the SforceServiceLocator,
* the other by not passing a url. In the second case the binding
* will use the url contained in the wsdl file when the proxy was
* generated.
*/
try {
binding = (SoapBindingStub)new SforceServiceLocator().getSoap();
}
catch (ServiceException ex) {
out.println(
"ERROR: createing binding to soap service, error was: \n"
+ ex.getMessage());
out.print("Hit return to continue...<P>");
}
// Time out after a minute
binding.setTimeout(60000);
//Attempt the login giving the user feedback
out.println("LOGGING IN NOW....<P>");
try {
loginResult = binding.login(un, pw);
}
catch (LoginFault lf) {
out.println(lf.getExceptionMessage());
//lf.printStackTrace();
}
catch (UnexpectedErrorFault uef) {
out.println(uef.getExceptionMessage());
uef.printStackTrace();
}
catch (RemoteException re) {
out.println(re.getMessage());
re.printStackTrace();
}
out.println("The session id is: " + loginResult.getSessionId() + "<P>");
out.println("The new server url is: " + loginResult.getServerUrl() +
"<P>");
//set the session header for subsequent call authentication
try {
binding = (SoapBindingStub)new SforceServiceLocator().getSoap(new URL(
loginResult.getServerUrl()));
}
catch (ServiceException jre) {
System.out.println(
"ERROR: creating binding to soap service, error was: \n" +
jre.getMessage());
}
catch (MalformedURLException e) {
e.printStackTrace();
}
//Create a new session header object and set the session id to that returned by the login
_SessionHeader sh = new _SessionHeader();
sh.setSessionId(loginResult.getSessionId());
binding.setHeader("SforceService", "SessionHeader", sh);
out.println("<a href=/sForceWeb/servlet1?doQuery=1>query</a>");
} // login if
//out.println(request.getParameter("doQuery"));
if (request.getParameter("doQuery") != null) {
out.println("doQuery");
QueryResult qr = null;
_QueryOptions qo = new _QueryOptions();
qo.setBatchSize(new Integer(3));
binding.setHeader("SoapService", "QueryOptions", qo);
try {
qr = binding.query(
"select id, Website, Name from Account where Name = 'Golden Straw'");
if (qr.getSize() != 0) {
Account account = ( (Account) qr.getRecords()[0]);
out.println("Retrieved " + new Integer(qr.getSize()).toString() +
" account(s) using Name = 'Golden Straw', ID = "
+ account.getId().getValue()
+ ", website = "
+ account.getWebsite());
}
qr = binding.query("select FirstName, LastName, Id from Contact");
boolean bContinue = true;
int loopCount = 0;
while (bContinue) {
out.println("Results set " + new Integer(loopCount++).toString() +
" - ");
//process the query results
for (int i = 0; i < qr.getRecords().length; i++) {
Contact con = (Contact) qr.getRecords()[i];
String fName = con.getFirstName();
String lName = con.getLastName();
if (fName == null) {
out.println("Contact " + (i + 1) + ": " + lName);
}
else {
out.println("Contact " + (i + 1) + ": " + fName + " " +
lName);
}
}
//handle the loop + 1 problem by checking to see if the most recent queryResult
if (qr.isDone()) {
bContinue = false;
}
else {
qr = binding.queryMore(qr.getQueryLocator());
}
}
out.println("\nQuery succesfully executed.");
//getUserInput("\nHit return to continue...");
}
catch (UnexpectedErrorFault uef) {
System.out.println(uef.getExceptionMessage());
}
catch (Exception ex) {
System.out.println(
"\nFailed to execute query succesfully, error message was: \n"
+ ex.getMessage());
}
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}