Commit 8439bf46 authored by Sunil Kumar's avatar Sunil Kumar

Added support for Object based LDAP Backup and Restore

parent a45cf848
/* **************************************************************************
* $OpenLDAP$
*
* Copyright (C) 1999, 2000, 2001 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
package com.novell.ldap.extensions;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BackupRestoreConstants {
/**
* A constant for eDirectory LDAP Based Backup/Restore OIDs.
*/
public static final String NLDAP_LDAP_BACKUP_REQUEST
= "2.16.840.1.113719.1.27.100.96";
public static final String NLDAP_LDAP_BACKUP_RESPONSE
= "2.16.840.1.113719.1.27.100.97";
public static final String NLDAP_LDAP_RESTORE_REQUEST
= "2.16.840.1.113719.1.27.100.98";
public static final String NLDAP_LDAP_RESTORE_RESPONSE
= "2.16.840.1.113719.1.27.100.99";
/**
*
*/
public BackupRestoreConstants() {
super();
// TODO Auto-generated constructor stub
}
}
/* **************************************************************************
* $OpenLDAP$
*
* Copyright (C) 1999, 2000, 2001 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
package com.novell.ldap.extensions;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedOperation;
import com.novell.ldap.LDAPExtendedResponse;
import com.novell.ldap.LDAPLocalException;
import com.novell.ldap.asn1.ASN1OctetString;
import com.novell.ldap.asn1.LBEREncoder;
import com.novell.ldap.resources.ExceptionMessages;
import com.novell.ldap.asn1.ASN1Integer;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class LDAPBackupRequest extends LDAPExtendedOperation {
static {
/*
* Register the extendedresponse class which is returned by the server
* in response to a LDAPBackupRequest
*/
try {
LDAPExtendedResponse.register(
BackupRestoreConstants.NLDAP_LDAP_BACKUP_RESPONSE,
Class.forName("com.novell.ldap.extensions.LDAPBackupResponse"));
} catch (ClassNotFoundException e) {
System.err.println("Could not register Extended Response -"
+ " Class not found");
} catch (Exception e) {
e.printStackTrace();
}
}
public LDAPBackupRequest(String objectDN, String stateInfo)
throws LDAPException, LDAPLocalException {
super(BackupRestoreConstants.NLDAP_LDAP_BACKUP_REQUEST, null);
int mts, revision; //mts = modifaction time stamp
String mtsStr, revisionStr;
try {
if (objectDN == null)
throw new IllegalArgumentException(
ExceptionMessages.PARAM_ERROR);
if (stateInfo == null) {
mts = 0;
revision = 0; //initialize these
} else {
//Parse this. stateInfo is should contain mts+revision
stateInfo = stateInfo.trim();
int index = stateInfo.indexOf('+');
mtsStr = stateInfo.substring(0, index);
revisionStr = stateInfo.substring(index + 1);
try {
mts = Integer.parseInt(mtsStr);
} catch (NumberFormatException e) {
throw new LDAPLocalException(
"Invalid Modification Timestamp send in the request",
LDAPException.ENCODING_ERROR);
}
try {
revision = Integer.parseInt(revisionStr);
} catch (NumberFormatException e) {
throw new LDAPLocalException(
"Invalid Revision send in the request",
LDAPException.ENCODING_ERROR);
}
}
ByteArrayOutputStream encodedData = new ByteArrayOutputStream();
LBEREncoder encoder = new LBEREncoder();
ASN1OctetString asn1_serverDN = new ASN1OctetString(objectDN);
ASN1Integer asn1_mts = new ASN1Integer(mts);
ASN1Integer asn1_revision = new ASN1Integer(revision);
asn1_serverDN.encode(encoder, encodedData);
asn1_mts.encode(encoder, encodedData);
asn1_revision.encode(encoder, encodedData);
setValue(encodedData.toByteArray());
} catch (IOException ioe) {
throw new LDAPException(ExceptionMessages.ENCODING_ERROR,
LDAPException.ENCODING_ERROR, (String) null);
}
}
}
\ No newline at end of file
/* **************************************************************************
* $OpenLDAP$
*
* Copyright (C) 1999, 2000, 2001 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
package com.novell.ldap.extensions;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedResponse;
import com.novell.ldap.asn1.ASN1Integer;
import com.novell.ldap.asn1.ASN1OctetString;
import com.novell.ldap.asn1.ASN1Sequence;
import com.novell.ldap.asn1.ASN1Set;
import com.novell.ldap.asn1.LBERDecoder;
import com.novell.ldap.rfc2251.RfcLDAPMessage;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class LDAPBackupResponse extends LDAPExtendedResponse {
private int bufferLength;
private String stateInfo;
private String parsedString;
//parsedString = no_of_chunks;sizeOf(chunk1);sizeOf(chunk2)..sizeOf(chunkn);returnedBuffer
public LDAPBackupResponse(RfcLDAPMessage rfcMessage) throws IOException {
super(rfcMessage);
int modificationTime = 0;
int revision = 0;
String returnedBuffer = null;
int chunksSize = 0;
int[] chunks = null; //This will hold size of each chunks returned from server
if (getID() == null
|| !(getID()
.equals(BackupRestoreConstants.NLDAP_LDAP_BACKUP_RESPONSE)))
throw new IOException("LDAP Extended Operation not supported");
if (getResultCode() == LDAPException.SUCCESS) {
// parse the contents of the reply
byte[] returnedValue = this.getValue();
if (returnedValue == null)
throw new IOException(
"LDAP Operations error. No returned value.");
// Create a decoder object
LBERDecoder decoder = new LBERDecoder();
if (decoder == null)
throw new IOException("Decoding error");
// Parse the parameters in the order
ByteArrayInputStream currentPtr = new ByteArrayInputStream(
returnedValue);
// Parse bufferLength
ASN1Integer asn1_bufferLength = (ASN1Integer) decoder
.decode(currentPtr);
if (asn1_bufferLength == null)
throw new IOException("Decoding error");
bufferLength = asn1_bufferLength.intValue();
System.out.println("sudhir buffer length =" + bufferLength);
// Parse modificationTime
ASN1Integer asn1_modificationTime = (ASN1Integer) decoder
.decode(currentPtr);
if (asn1_modificationTime == null)
throw new IOException("Decoding error");
modificationTime = asn1_modificationTime.intValue();
// Parse revision
ASN1Integer asn1_revision = (ASN1Integer) decoder
.decode(currentPtr);
if (asn1_revision == null)
throw new IOException("Decoding error");
revision = asn1_revision.intValue();
//format stateInfo
this.stateInfo = modificationTime + "+" + revision;
// Parse returnedBuffer
ASN1OctetString asn1_returnedBuffer = (ASN1OctetString) decoder
.decode(currentPtr);
if (asn1_returnedBuffer == null)
throw new IOException("Decoding error");
returnedBuffer = asn1_returnedBuffer.stringValue();
System.out.println("Ravi buffer =" + returnedBuffer);
System.out.println("sudhir buffer size =" + returnedBuffer.length());
//Chunks are encoded as shown below
//SEQUENCE{no_of_seq, SET{SEQUENCE1{size1}, SEQUENCE2{size2}....SEQUENCEn{sizen}} }
ASN1Sequence asn1_chunksSeq = (ASN1Sequence) decoder
.decode(currentPtr);
if (asn1_chunksSeq == null)
throw new IOException("Decoding error");
chunksSize = ((ASN1Integer)asn1_chunksSeq.get(0)).intValue();
chunks = new int[chunksSize];
ASN1Set asn1_chunksSet = (ASN1Set)asn1_chunksSeq.get(1);
for (int index = 0; index < chunksSize; index++) {
ASN1Sequence asn1_eachSeq = (ASN1Sequence)asn1_chunksSet.get(index);
chunks[index] = ((ASN1Integer)asn1_eachSeq.get(0)).intValue();
}
StringBuffer tempBuffer = new StringBuffer();
tempBuffer.append(chunksSize);
tempBuffer.append(";");
for (int i = 0; i < chunksSize; i++) {
tempBuffer.append(chunks[i]);
tempBuffer.append(";");
}
tempBuffer.append(returnedBuffer);
this.parsedString = tempBuffer.toString();
} else {
this.bufferLength = 0;
this.stateInfo = null;
this.parsedString = null;
}
}
public int getBufferLength() {
return bufferLength;
}
public String getParsedString() {
return parsedString;
}
public String getStatusInfo(){
return stateInfo;
}
}
\ No newline at end of file
/* **************************************************************************
* $OpenLDAP$
*
* Copyright (C) 1999, 2000, 2001 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
package com.novell.ldap.extensions;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedOperation;
import com.novell.ldap.LDAPLocalException;
import com.novell.ldap.asn1.ASN1Integer;
import com.novell.ldap.asn1.ASN1OctetString;
import com.novell.ldap.asn1.ASN1Sequence;
import com.novell.ldap.asn1.ASN1Set;
import com.novell.ldap.asn1.LBEREncoder;
import com.novell.ldap.resources.ExceptionMessages;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class LDAPRestoreRequest extends LDAPExtendedOperation {
public LDAPRestoreRequest(String objectDN, int bufferLength, String buffer)
throws LDAPException, LDAPLocalException {
super(BackupRestoreConstants.NLDAP_LDAP_RESTORE_REQUEST, null);
try {
if (objectDN == null || bufferLength == 0 || buffer == null)
throw new IllegalArgumentException(
ExceptionMessages.PARAM_ERROR);
ByteArrayOutputStream encodedData = new ByteArrayOutputStream();
LBEREncoder encoder = new LBEREncoder();
//rervers parse the buffer
int index;
int chunkSize;
int chunks[] = null;
index = buffer.indexOf(';');
try {
chunkSize = Integer.parseInt(buffer.substring(0, index));
} catch (NumberFormatException e) {
throw new LDAPLocalException(
"Invalid data buffer send in the request",
LDAPException.ENCODING_ERROR);
}
if (chunkSize == 0)
throw new IllegalArgumentException(
ExceptionMessages.PARAM_ERROR);
buffer = buffer.substring(index + 1);
int chunkIndex;
chunks = new int[chunkSize];
for (int i = 0; i < chunkSize; i++) {
chunkIndex = buffer.indexOf(';');
chunks[i] = Integer.parseInt(buffer.substring(0, chunkIndex));
buffer = buffer.substring(chunkIndex + 1);
}
ASN1OctetString asn1_serverDN = new ASN1OctetString(objectDN);
//Form the sequence to be passed to Server
ASN1Sequence asn1_chunksSeq = new ASN1Sequence();
asn1_chunksSeq.add(new ASN1Integer(chunkSize));
ASN1Set asn1_chunksSet = new ASN1Set();
for (int i = 0; i < chunkSize; i++) {
ASN1Integer tmpChunk = new ASN1Integer(chunks[i]);
ASN1Sequence tmpSeq = new ASN1Sequence();
tmpSeq.add(tmpChunk);
asn1_chunksSet.add(tmpSeq);
//asn1_chunksSet.add((new ASN1Sequence()).add(new
// ASN1Integer(chunks[i])));
}
asn1_chunksSeq.add(asn1_chunksSet);
ASN1Integer asn1_bufferLength = new ASN1Integer(bufferLength);
ASN1OctetString asn1_buffer = new ASN1OctetString(buffer);
asn1_chunksSeq.encode(encoder, encodedData);
asn1_serverDN.encode(encoder, encodedData);
asn1_bufferLength.encode(encoder, encodedData);
asn1_buffer.encode(encoder, encodedData);
setValue(encodedData.toByteArray());
} catch (IOException ioe) {
throw new LDAPException(ExceptionMessages.ENCODING_ERROR,
LDAPException.ENCODING_ERROR, (String) null);
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment