Skip to main content

Java_Admin_ACL_AddEntry.java

/*****************************************************************************************
*
* Java_Admin_ACL_AddEntry.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 com.eruces.teadmin.UserAndPasswordInfo;

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

// The principal the new ACL entry grants access to (any principal other than the owner).
private String m_strGranteeUser = "bob";

public void addACLEntry(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:");
System.out.println(" " + info.toString());

ACLInfo info2 = new ACLInfo();
info2.setACLID( info.getACLID() );
// The T-tag is REQUIRED on a freshly-constructed ACLInfo passed to updateACL -- it
// is how the server identifies the key whose ACL is being changed. Without it
// updateACL fails with the opaque "Add ACL Entry Failed". (Samples that pass back
// the ACLInfo returned by getACL already carry it.)
info2.setHiddenLink( strHL );

// Resolve the grantee by NAME. Principal ids are assigned as principals are
// created, so they must never be hardcoded; and the entry must name a principal
// other than the key's owner (the owner already holds an entry -- adding it again
// fails with "Cannot Add Owner's entry").
UserAndPasswordInfo grantee = admin.getUserByName(m_strGranteeUser);
if( grantee == null )
throw new Exception("grantee principal not found: " + m_strGranteeUser);

ACLEntry ent2add = new ACLEntry( info2 );
ent2add.setPrincipalID(grantee.getPrincipalID());
// Read + write. Do NOT set the owner bit (4): a key has exactly one owner and the
// server rejects a second owner entry with "Cannot Add Owner's entry".
ent2add.setFlag(3); // ACL right: 1=read, 2=write, 4=owner
ent2add.setMaxUsage(0);
ent2add.setStartTime(0);
ent2add.setEndTime(0);
ent2add.setSystemID(admin.getSystemID());
ent2add.setUserRight(1);

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

// Get updated info
info = admin.getACL(strHL);

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

System.out.println("");
System.out.println("ACL info got:");
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_AddEntry test = new Java_Admin_ACL_AddEntry();
test.connect("sampleserver", 8888, "alice", "alice1A");
test.addACLEntry(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();
}
}
}