* @throws IOException If an error occurs while interacting with the client.
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
LDAPConnectionOptions connOptions = new LDAPConnectionOptions();
LDAPConnection connection = null;
BatchRequest batchRequest = null;
// Keep the Servlet input stream buffered in case the SOAP unmarshalling
// fails, the SAX parsing will be able to retrieve the requestID even if
// the XML is malmformed by resetting the input stream.
BufferedInputStream is = new BufferedInputStream(req.getInputStream(),
65536);
if ( is.markSupported() ) {
is.mark(65536);
}
// Create response in the beginning as it might be used if the parsing
// failes.
BatchResponse batchResponse = objFactory.createBatchResponse();
List<JAXBElement<?>> batchResponses = batchResponse.getBatchResponses();
Document doc = db.newDocument();
SOAPBody soapBody = null;
MimeHeaders mimeHeaders = new MimeHeaders();
Enumeration en = req.getHeaderNames();
String bindDN = null;
String bindPassword = null;
boolean authorizationInHeader = false;
while (en.hasMoreElements()) {
String headerName = (String) en.nextElement();
String headerVal = req.getHeader(headerName);
if (headerName.equalsIgnoreCase("authorization")) {
if (headerVal.startsWith("Basic ")) {
authorizationInHeader = true;
String authorization = headerVal.substring(6).trim();
try {
String unencoded = new String(Base64.decode(authorization));
int colon = unencoded.indexOf(':');
if (colon > 0) {
bindDN = unencoded.substring(0, colon).trim();
bindPassword = unencoded.substring(colon + 1);
}
} catch (ParseException ex) {
// DN:password parsing error
batchResponses.add(
createErrorResponse(
new LDAPException(LDAPResultCode.INVALID_CREDENTIALS,
Message.raw(ex.getMessage()))));
break;
}
}
}
StringTokenizer tk = new StringTokenizer(headerVal, ",");
while (tk.hasMoreTokens()) {
mimeHeaders.addHeader(headerName, tk.nextToken().trim());
}
}
if ( ! authorizationInHeader ) {
// if no authorization, set default user
bindDN = "";
bindPassword = "";
} else {
// otherwise if DN or password is null, send back an error
if ( (bindDN == null || bindPassword == null)
&& batchResponses.size()==0) {
batchResponses.add(
createErrorResponse(
new LDAPException(LDAPResultCode.INVALID_CREDENTIALS,
Message.raw("Unable to retrieve credentials."))));
}
}
// if an error already occured, the list is not empty
if ( batchResponses.size() == 0 ) {
try {
SOAPMessage message = messageFactory.createMessage(mimeHeaders, is);
soapBody = message.getSOAPBody();
} catch (SOAPException ex) {
// SOAP was unable to parse XML successfully
batchResponses.add(
createXMLParsingErrorResponse(is,
batchResponse,
String.valueOf(ex.getCause())));
}
}
if ( soapBody != null ) {
Iterator it = soapBody.getChildElements();
while (it.hasNext()) {
Object obj = it.next();
if (!(obj instanceof SOAPElement)) {
continue;
}
SOAPElement se = (SOAPElement) obj;
JAXBElement<BatchRequest> batchRequestElement = null;
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
batchRequestElement = unmarshaller.unmarshal(se, BatchRequest.class);
} catch (JAXBException e) {
// schema validation failed
batchResponses.add(createXMLParsingErrorResponse(is,
batchResponse,
String.valueOf(e)));
}
if ( batchRequestElement != null ) {
batchRequest = batchRequestElement.getValue();
// set requestID in response
batchResponse.setRequestID(batchRequest.getRequestID());
boolean connected = false;
if ( connection == null ) {
connection = new LDAPConnection(hostName, port, connOptions);
try {
connection.connectToHost(bindDN, bindPassword);
connected = true;
} catch (LDAPConnectionException e) {
// if connection failed, return appropriate error response
batchResponses.add(createErrorResponse(e));
}
}
if ( connected ) {
List<DsmlMessage> list = batchRequest.getBatchRequests();
for (DsmlMessage request : list) {
JAXBElement<?> result = performLDAPRequest(connection, request);
if ( result != null ) {
batchResponses.add(result);
}
// evaluate response to check if an error occured
Object o = result.getValue();
if ( o instanceof ErrorResponse ) {
if ( ON_ERROR_EXIT.equals(batchRequest.getOnError()) ) {
break;
}
} else if ( o instanceof LDAPResult ) {
int code = ((LDAPResult)o).getResultCode().getCode();
if ( code != LDAPResultCode.SUCCESS
&& code != LDAPResultCode.REFERRAL
&& code != LDAPResultCode.COMPARE_TRUE
&& code != LDAPResultCode.COMPARE_FALSE ) {
if ( ON_ERROR_EXIT.equals(batchRequest.getOnError()) ) {
break;
}
}
}
}
}
// close connection to LDAP server
if ( connection != null ) {
connection.close(nextMessageID);
}
}
}
}
try {