00001
00022 package org.objectweb.security.context;
00023
00024 import java.util.ArrayList;
00025 import java.util.Collection;
00026 import java.util.Iterator;
00027
00033 public class Marshalling {
00034
00038 public static final int SEC_CTX_ID = 101;
00039
00043 private Marshalling() {
00044
00045 }
00046
00055 public static byte[] marshallSecurityContext(SecurityContext ctx) {
00056 byte [] result = null;
00057 try {
00058 if (ctx != null) {
00059
00060 ArrayList cResult = new ArrayList();
00061
00062 addBytes(cResult, ctx.getPrincipalName());
00063
00064 String [] roles = ctx.getRoles();
00065 int nbRoles = 0;
00066 if (roles != null) {
00067 nbRoles = roles.length;
00068 }
00069 addBytes(cResult, new Integer(nbRoles).toString());
00070 for (int i = 0; i < nbRoles; i++) {
00071 addBytes(cResult, roles[i]);
00072 }
00073
00074
00075 addBytes(cResult, new Integer(ctx.getRunAsRoleStack().size()).toString());
00076 Iterator iRunAs = ctx.getRunAsRoleStack().iterator();
00077 while (iRunAs.hasNext()) {
00078 addBytes(cResult, (String) iRunAs.next());
00079 }
00080
00081
00082 addBytes(cResult, new Integer(ctx.getRunAsPrincipalStack().size()).toString());
00083 iRunAs = ctx.getRunAsPrincipalStack().iterator();
00084 while (iRunAs.hasNext()) {
00085 addBytes(cResult, (String) iRunAs.next());
00086 }
00087
00088
00089 addBytes(cResult, new Integer(ctx.getRunAsPrincipalRolesStack().size()).toString());
00090 iRunAs = ctx.getRunAsPrincipalRolesStack().iterator();
00091 while (iRunAs.hasNext()) {
00092 addBytes(cResult, (String[]) iRunAs.next());
00093 }
00094
00095
00096 result = new byte[cResult.size()];
00097 for (int i = 0; i < cResult.size(); i++) {
00098 result[i] = ((Byte) cResult.get(i)).byteValue();
00099 }
00100 }
00101
00102 } catch (java.io.UnsupportedEncodingException uee) {
00103 uee.printStackTrace();
00104 }
00105 return result;
00106 }
00107
00113 public static SecurityContext unmarshallSecurityContext(byte [] byteCtx) {
00114 SecurityContext result = null;
00115 if ((byteCtx != null) && (byteCtx.length > 0)) {
00116 try {
00117 Iterator istrs = getBytes2Strings(byteCtx).iterator();
00118 String principalName = (String) istrs.next();
00119 int nbRoles = new Integer((String) istrs.next()).intValue();
00120 ArrayList roles = null;
00121 if (nbRoles > 0) {
00122 roles = new ArrayList();
00123 }
00124 while (nbRoles > 0) {
00125 roles.add(istrs.next());
00126 nbRoles--;
00127 }
00128
00129
00130 int nbRunas = new Integer((String) istrs.next()).intValue();
00131 ArrayList runas = null;
00132 if (nbRunas > 0) {
00133 runas = new ArrayList();
00134 }
00135 while (nbRunas > 0) {
00136 runas.add(istrs.next());
00137 nbRunas--;
00138 }
00139
00140
00141 int nbRunasPrincipal = new Integer((String) istrs.next()).intValue();
00142 ArrayList runasPrincipal = null;
00143 if (nbRunasPrincipal > 0) {
00144 runasPrincipal = new ArrayList();
00145 }
00146 while (nbRunasPrincipal > 0) {
00147 runasPrincipal.add(istrs.next());
00148 nbRunasPrincipal--;
00149 }
00150
00151
00152 int nbRunasRoles = new Integer((String) istrs.next()).intValue();
00153 ArrayList runasRoles = null;
00154 if (nbRunasRoles > 0) {
00155 int arrayLength = new Integer((String) istrs.next()).intValue();
00156
00157 if (nbRunasRoles > 0) {
00158 runasRoles = new ArrayList();
00159 }
00160 while (nbRunasRoles > 0) {
00161 String[] st = new String[arrayLength];
00162 for (int j = 0; j < arrayLength; j++) {
00163 st[j] = (String) istrs.next();
00164 }
00165 runasRoles.add(st);
00166 nbRunasRoles--;
00167 }
00168 }
00169 result = new SecurityContext(principalName, roles, runas, runasPrincipal, runasRoles);
00170
00171 } catch (java.io.UnsupportedEncodingException uee) {
00172 uee.printStackTrace();
00173 }
00174 }
00175 return result;
00176 }
00177
00185 private static void addBytes(Collection c, String toAdd) throws java.io.UnsupportedEncodingException {
00186 byte [] b = toAdd.getBytes("UTF8");
00187 c.add(new Byte((byte) b.length));
00188 for (int i = 0; i < b.length; i++) {
00189 c.add(new Byte(b[i]));
00190 }
00191 }
00192
00200 private static void addBytes(Collection c, String[] toAdd) throws java.io.UnsupportedEncodingException {
00201
00202 addBytes(c, new Integer(toAdd.length).toString());
00203
00204 for (int n = 0; n < toAdd.length; n++) {
00205 byte [] b = toAdd[n].getBytes("UTF8");
00206 c.add(new Byte((byte) b.length));
00207 for (int i = 0; i < b.length; i++) {
00208 c.add(new Byte(b[i]));
00209 }
00210 }
00211 }
00212
00220 private static ArrayList getBytes2Strings(byte[] bytes) throws java.io.UnsupportedEncodingException {
00221 ArrayList strs = new ArrayList();
00222 int index = 0;
00223 while (bytes.length > index) {
00224 int rSize = bytes[index];
00225 index++;
00226 byte[] rName = new byte [rSize];
00227 for (int j = 0; j < rSize; j++) {
00228 rName[j] = bytes[index];
00229 index++;
00230 }
00231 strs.add(new String(rName, "UTF8"));
00232 }
00233 return strs;
00234 }
00235
00236 }
00237