use of org.apache.directory.api.dsmlv2.DsmlDecorator in project directory-ldap-api by apache.
the class BatchResponseTest method testResponseWith1ModifyResponse.
/**
* Test parsing of a Response with the 1 ModifyResponse
*/
@Test
public void testResponseWith1ModifyResponse() {
Dsmlv2ResponseParser parser = null;
try {
parser = new Dsmlv2ResponseParser(getCodec());
parser.setInput(BatchResponseTest.class.getResource("response_with_1_ModifyResponse.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
BatchResponseDsml batchResponse = parser.getBatchResponse();
assertEquals(1, batchResponse.getResponses().size());
DsmlDecorator<? extends Response> response = batchResponse.getCurrentResponse();
if (response instanceof ModifyResponse) {
assertTrue(true);
} else {
fail();
}
}
use of org.apache.directory.api.dsmlv2.DsmlDecorator in project directory-ldap-api by apache.
the class Dsmlv2Engine method processRequest.
/**
* Processes a single request
*
* @param request the request to process
* @param respWriter The writer used to store the DSML response
* @exception Exception If we had an error while processing the request
*/
protected void processRequest(DsmlDecorator<? extends Request> request, BufferedWriter respWriter) throws Exception {
ResultCodeEnum resultCode = null;
switch(request.getDecorated().getType()) {
case ABANDON_REQUEST:
connection.abandon((AbandonRequest) request);
return;
case ADD_REQUEST:
AddResponse response = connection.add((AddRequest) request);
resultCode = response.getLdapResult().getResultCode();
AddResponseDsml addResponseDsml = new AddResponseDsml(connection.getCodecService(), response);
writeResponse(respWriter, addResponseDsml);
break;
case BIND_REQUEST:
BindResponse bindResponse = connection.bind((BindRequest) request);
resultCode = bindResponse.getLdapResult().getResultCode();
BindResponseDsml authResponseDsml = new BindResponseDsml(connection.getCodecService(), bindResponse);
writeResponse(respWriter, authResponseDsml);
break;
case COMPARE_REQUEST:
CompareResponse compareResponse = connection.compare((CompareRequest) request);
resultCode = compareResponse.getLdapResult().getResultCode();
CompareResponseDsml compareResponseDsml = new CompareResponseDsml(connection.getCodecService(), compareResponse);
writeResponse(respWriter, compareResponseDsml);
break;
case DEL_REQUEST:
DeleteResponse delResponse = connection.delete((DeleteRequest) request);
resultCode = delResponse.getLdapResult().getResultCode();
DelResponseDsml delResponseDsml = new DelResponseDsml(connection.getCodecService(), delResponse);
writeResponse(respWriter, delResponseDsml);
break;
case EXTENDED_REQUEST:
ExtendedResponse extendedResponse = connection.extended((ExtendedRequest) request);
resultCode = extendedResponse.getLdapResult().getResultCode();
ExtendedResponseDsml extendedResponseDsml = new ExtendedResponseDsml(connection.getCodecService(), extendedResponse);
writeResponse(respWriter, extendedResponseDsml);
break;
case MODIFY_REQUEST:
ModifyResponse modifyResponse = connection.modify((ModifyRequest) request);
resultCode = modifyResponse.getLdapResult().getResultCode();
ModifyResponseDsml modifyResponseDsml = new ModifyResponseDsml(connection.getCodecService(), modifyResponse);
writeResponse(respWriter, modifyResponseDsml);
break;
case MODIFYDN_REQUEST:
ModifyDnResponse modifyDnResponse = connection.modifyDn((ModifyDnRequest) request);
resultCode = modifyDnResponse.getLdapResult().getResultCode();
ModDNResponseDsml modDNResponseDsml = new ModDNResponseDsml(connection.getCodecService(), modifyDnResponse);
writeResponse(respWriter, modDNResponseDsml);
break;
case SEARCH_REQUEST:
SearchCursor searchResponses = connection.search((SearchRequest) request);
SearchResponseDsml searchResponseDsml = new SearchResponseDsml(connection.getCodecService());
if (respWriter != null) {
StringBuilder sb = new StringBuilder();
sb.append("<searchResponse");
if (request.getDecorated().getMessageId() > 0) {
sb.append(" requestID=\"");
sb.append(request.getDecorated().getMessageId());
sb.append('"');
}
sb.append('>');
respWriter.write(sb.toString());
}
while (searchResponses.next()) {
Response searchResponse = searchResponses.get();
if (searchResponse.getType() == MessageTypeEnum.SEARCH_RESULT_ENTRY) {
SearchResultEntry searchResultEntry = (SearchResultEntry) searchResponse;
SearchResultEntryDsml searchResultEntryDsml = new SearchResultEntryDsml(connection.getCodecService(), searchResultEntry);
searchResponseDsml = new SearchResponseDsml(connection.getCodecService(), searchResultEntryDsml);
if (respWriter != null) {
writeResponse(respWriter, searchResultEntryDsml);
} else {
searchResponseDsml.addResponse(searchResultEntryDsml);
}
} else if (searchResponse.getType() == MessageTypeEnum.SEARCH_RESULT_REFERENCE) {
SearchResultReference searchResultReference = (SearchResultReference) searchResponse;
SearchResultReferenceDsml searchResultReferenceDsml = new SearchResultReferenceDsml(connection.getCodecService(), searchResultReference);
searchResponseDsml = new SearchResponseDsml(connection.getCodecService(), searchResultReferenceDsml);
if (respWriter != null) {
writeResponse(respWriter, searchResultReferenceDsml);
} else {
searchResponseDsml.addResponse(searchResultReferenceDsml);
}
}
}
SearchResultDone srDone = searchResponses.getSearchResultDone();
if (srDone != null) {
resultCode = srDone.getLdapResult().getResultCode();
SearchResultDoneDsml srdDsml = new SearchResultDoneDsml(connection.getCodecService(), srDone);
if (respWriter != null) {
writeResponse(respWriter, srdDsml);
respWriter.write("</searchResponse>");
} else {
searchResponseDsml.addResponse(srdDsml);
batchResponse.addResponse(searchResponseDsml);
}
}
break;
case UNBIND_REQUEST:
connection.unBind();
break;
default:
throw new IllegalStateException("Unexpected request tpye " + request.getDecorated().getType());
}
if ((!continueOnError) && (resultCode != null) && (resultCode != ResultCodeEnum.SUCCESS) && (resultCode != ResultCodeEnum.COMPARE_TRUE) && (resultCode != ResultCodeEnum.COMPARE_FALSE) && (resultCode != ResultCodeEnum.REFERRAL)) {
// Turning on Exit flag
exit = true;
}
}
use of org.apache.directory.api.dsmlv2.DsmlDecorator in project directory-ldap-api by apache.
the class Dsmlv2Engine method processDSML.
/**
* Processes the DSML batch request and writes the response of each operation will be
* written to the given response stream if it is not null
*
* @param outStream the stream to which the responses will be written, can be null
* @throws IOException If we had an issue while reading or writing the data
*/
protected void processDSML(OutputStream outStream) throws IOException {
BufferedWriter respWriter = null;
if (outStream != null) {
respWriter = new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8));
if (generateSoapResp) {
respWriter.write("<Envelope ");
Namespace soapNs = new Namespace(null, "http://www.w3.org/2001/12/soap-envelope");
soapNs.write(respWriter);
respWriter.write("><Body>");
}
}
// Binding to LDAP Server
try {
bind(1);
} catch (Exception e) {
LOG.warn("Failed to bind", e);
// Unable to connect to server
// We create a new ErrorResponse and return the XML response.
ErrorResponse errorResponse = new ErrorResponse(0, ErrorResponseType.COULD_NOT_CONNECT, e.getLocalizedMessage());
batchResponse.addResponse(errorResponse);
if (respWriter != null) {
respWriter.write(batchResponse.toDsml());
if (generateSoapResp) {
respWriter.write(BODY_ENVELOPE);
}
respWriter.flush();
}
return;
}
// - Getting and registering options from BatchRequest
try {
processBatchRequest();
} catch (XmlPullParserException e) {
// We create a new ErrorResponse and return the XML response.
ErrorResponse errorResponse = new ErrorResponse(0, ErrorResponseType.MALFORMED_REQUEST, I18n.err(I18n.ERR_03001, e.getLocalizedMessage(), e.getLineNumber(), e.getColumnNumber()));
batchResponse.addResponse(errorResponse);
if (respWriter != null) {
respWriter.write(batchResponse.toDsml());
if (generateSoapResp) {
respWriter.write(BODY_ENVELOPE);
}
respWriter.flush();
}
return;
}
if (respWriter != null) {
StringBuilder sb = new StringBuilder();
sb.append("<batchResponse ");
sb.append(ParserUtils.DSML_NAMESPACE.asXML());
// a space to separate the namespace declarations
sb.append(" ");
sb.append(ParserUtils.XSD_NAMESPACE.asXML());
// a space to separate the namespace declarations
sb.append(" ");
sb.append(ParserUtils.XSI_NAMESPACE.asXML());
sb.append(" requestID=\"");
sb.append(batchRequest.getRequestID());
sb.append("\">");
respWriter.write(sb.toString());
}
// Processing each request:
// - Getting a new request
// - Checking if the request is well formed
// - Sending the request to the server
// - Getting and converting reponse(s) as XML
// - Looping until last request
DsmlDecorator<? extends Request> request = null;
try {
request = parser.getNextRequest();
} catch (XmlPullParserException e) {
LOG.warn("Failed while getting next request", e);
int reqId = 0;
// We create a new ErrorResponse and return the XML response.
ErrorResponse errorResponse = new ErrorResponse(reqId, ErrorResponseType.MALFORMED_REQUEST, I18n.err(I18n.ERR_03001, e.getLocalizedMessage(), e.getLineNumber(), e.getColumnNumber()));
batchResponse.addResponse(errorResponse);
if (respWriter != null) {
respWriter.write(batchResponse.toDsml());
if (generateSoapResp) {
respWriter.write(BODY_ENVELOPE);
}
respWriter.flush();
}
return;
}
// (Request == null when there's no more request to process)
while (request != null) {
// Checking the request has a requestID attribute if Processing = Parallel and ResponseOrder = Unordered
if ((batchRequest.getProcessing().equals(Processing.PARALLEL)) && (batchRequest.getResponseOrder().equals(ResponseOrder.UNORDERED)) && (request.getDecorated().getMessageId() <= 0)) {
// Then we have to send an errorResponse
ErrorResponse errorResponse = new ErrorResponse(0, ErrorResponseType.MALFORMED_REQUEST, I18n.err(I18n.ERR_03002));
if (respWriter != null) {
writeResponse(respWriter, errorResponse);
} else {
batchResponse.addResponse(errorResponse);
}
break;
}
try {
processRequest(request, respWriter);
} catch (Exception e) {
LOG.warn("Failed to process request", e);
// We create a new ErrorResponse and return the XML response.
ErrorResponse errorResponse = new ErrorResponse(request.getDecorated().getMessageId(), ErrorResponseType.GATEWAY_INTERNAL_ERROR, I18n.err(I18n.ERR_03003, e.getMessage()));
if (respWriter != null) {
writeResponse(respWriter, errorResponse);
} else {
batchResponse.addResponse(errorResponse);
}
break;
}
// Checking if we need to exit processing (if an error has occurred if onError == Exit)
if (exit) {
break;
}
// Getting next request
try {
request = parser.getNextRequest();
} catch (XmlPullParserException e) {
// We create a new ErrorResponse and return the XML response.
ErrorResponse errorResponse = new ErrorResponse(0, ErrorResponseType.MALFORMED_REQUEST, I18n.err(I18n.ERR_03001, e.getLocalizedMessage(), e.getLineNumber(), e.getColumnNumber()));
if (respWriter != null) {
writeResponse(respWriter, errorResponse);
} else {
batchResponse.addResponse(errorResponse);
}
break;
}
}
if (respWriter != null) {
respWriter.write("</batchResponse>");
if (generateSoapResp) {
respWriter.write(BODY_ENVELOPE);
}
respWriter.flush();
}
}
use of org.apache.directory.api.dsmlv2.DsmlDecorator in project directory-ldap-api by apache.
the class BatchRequestTest method testResponseWith1AuthRequestAnd1AddRequest.
/**
* Test parsing of a Request with 1 AuthRequest and 1 AddRequest
*/
@Test
public void testResponseWith1AuthRequestAnd1AddRequest() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(BatchRequestTest.class.getResource("request_with_1_AuthRequest_1_AddRequest.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
BatchRequestDsml batchRequest = parser.getBatchRequest();
List<DsmlDecorator<? extends Request>> requests = batchRequest.getRequests();
assertEquals(2, requests.size());
if (requests.get(0) instanceof BindRequest) {
assertTrue(true);
} else {
fail();
}
if (requests.get(1) instanceof AddRequest) {
assertTrue(true);
} else {
fail();
}
}
use of org.apache.directory.api.dsmlv2.DsmlDecorator in project directory-ldap-api by apache.
the class BatchResponseTest method testResponseWith1AddResponse.
/**
* Test parsing of a Response with the 1 AddResponse
*/
@Test
public void testResponseWith1AddResponse() {
Dsmlv2ResponseParser parser = null;
try {
parser = new Dsmlv2ResponseParser(getCodec());
parser.setInput(BatchResponseTest.class.getResource("response_with_1_AddResponse.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
BatchResponseDsml batchResponse = parser.getBatchResponse();
assertEquals(1, batchResponse.getResponses().size());
DsmlDecorator<? extends Response> response = batchResponse.getCurrentResponse();
if (response instanceof AddResponse) {
assertTrue(true);
} else {
fail();
}
}
Aggregations