Skip to main content

Java_Admin_ACL_RemoveEntry.java

/*****************************************************************************************
*
* Java_Admin_ACL_RemoveEntry.java
*
******************************************************************************************/

import com.eruces.teagent.TEAgentConnection;
import com.eruces.teagent.TEAgentException;
import com.eruces.teagent.TEAgentConnectionFactory;
import com.eruces.teadmin.Administration;
import com.eruces.teadmin.AdminException;
import com.eruces.teadmin.ACLInfo;
import com.eruces.teadmin.ACLEntry;

import java.util.TreeSet;
import java.util.Iterator;

public class Java_Admin_ACL_RemoveEntry
{
private String m_strServerName;
private int m_nPort;
private String m_strUsername;
private String m_strPasswd;
private TEAgentConnection m_conn;

public void removeACLEntry(String strHL) throws TEAgentException, AdminException, Exception
{
Administration admin = new Administration();
admin.attachConnection(m_conn);

// User must be owner of the hiddenlink.
ACLInfo info = admin.getACL(strHL);

if( info == null )
throw new Exception("ACL info not found !");

System.out.println("");
System.out.println("ACL info got:");
if (info.isExpired())
System.out.println(" ACL info is expired");
else
System.out.println(" ACL info is not expired");
System.out.println(" " + info.toString());

//--------------------------------------------------------------
info.clearACLEntry();
ACLEntry entry = new ACLEntry(info);
entry.setPrincipalID(1001);
entry.setSystemID(admin.getSystemID());
entry.setStartTime(0);
entry.setEndTime(0);
entry.setUserRight(ACLEntry.ACL_RIGHT_READ); //read only priveledge
entry.setMaxUsage(-1);

admin.updateACL(Administration.ADD_ACLENTRY, info); // add ACL entry to ACL info.
System.out.println("ACL entry was added to ACL info");

// Get ACL after adding ACL entry
info = admin.getACL(strHL);

if( info == null )
throw new Exception("ACL info not found !");

System.out.println("");
System.out.println("Updated ACL info:");
System.out.println(" " + info.toString());

// Find the ACL entry to remove
ACLEntry ent2remove = null;
TreeSet allACLEntries = info.getAllEntries();
Iterator iter = allACLEntries.iterator();
while (iter.hasNext())
{
ACLEntry ent = (ACLEntry)iter.next();
if (ent.getPrincipalID() == 1001)
{
ent2remove = ent;
break;
}
}

if (ent2remove == null)
throw new Exception("ACL entry not found !");

//--------------------------------------------------------------
info.clearACLEntry();
info.addACLEntry(ent2remove);

admin.updateACL(Administration.REMOVE_ACLENTRY, info); // remove ACL entry from ACL info.
System.out.println("ACL entry was removed from ACL info");

// Get updated info
info = admin.getACL(strHL);
if( info == null )
throw new Exception("ACL entry not found !");

System.out.println("");
System.out.println("ACL info got:");
if (info.isExpired())
System.out.println(" ACL info is expired");
else
System.out.println(" ACL info is not expired");
System.out.println(" " + info.toString());

admin.detachConnection();
}


public void connect(String strServerName, int nPort, String strUsername, String strPasswd) throws TEAgentException
{
m_strServerName = strServerName;
m_nPort = nPort;
m_strUsername = strUsername;
m_strPasswd = strPasswd;

m_conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.SECURE_USER_PASSWORD);
// Connect to server.
m_conn.open(m_strServerName, m_nPort, m_strUsername, m_strPasswd);
System.out.println("Connected to " + m_strServerName + " at " + m_nPort + " as " + m_strUsername);
}

public void disconnect() throws TEAgentException
{
m_conn.close(); // disconnect from the server.
System.out.println("Disconnected from " + m_strServerName);
}

public static void main(String[] args)
{
try {
// Generate HiddenLink
TEAgentConnection conn = Java_HelperFunctions.getConnect("sampleserver", 8888, "alice", "alice1A");
String strHL = Java_HelperFunctions.generateTtag(conn);
conn.close();
System.out.println("strHL = " + strHL);

Java_Admin_ACL_RemoveEntry test = new Java_Admin_ACL_RemoveEntry();
test.connect("sampleserver", 8888, "alice", "alice1A");
test.removeACLEntry(strHL);
test.disconnect();
}
catch (TEAgentException te) {
System.out.println("TEAgentException encountered: ");
te.printStackTrace();
}
catch (AdminException te) {
System.out.println("AdminException encountered: ");
te.printStackTrace();
}
catch (Exception ex) {
System.out.println("Exception encountered: ");
ex.printStackTrace();
}
}
}