1 22 package org.objectweb.security.context; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 34 public class Marshalling { 35 36 39 public static final int SEC_CTX_ID = 101; 40 41 44 private Marshalling() { 45 46 } 47 48 56 public static byte[] marshallSecurityContext(SecurityContext ctx) { 57 byte [] result = null; 58 try { 59 if (ctx != null) { 60 ArrayList cResult = new ArrayList (); 62 addBytes(cResult, ctx.getPrincipalName()); 64 String [] roles = ctx.getRoles(); 66 int nbRoles = 0; 67 if (roles != null) { 68 nbRoles = roles.length; 69 } 70 addBytes(cResult, new Integer (nbRoles).toString()); 71 for (int i = 0; i < nbRoles; i++) { 72 addBytes(cResult, roles[i]); 73 } 74 75 addBytes(cResult, new Integer (ctx.getRunAsRoleStack().size()).toString()); 77 Iterator iRunAs = ctx.getRunAsRoleStack().iterator(); 78 while (iRunAs.hasNext()) { 79 addBytes(cResult, (String ) iRunAs.next()); 80 } 81 82 addBytes(cResult, new Integer (ctx.getRunAsPrincipalStack().size()).toString()); 84 iRunAs = ctx.getRunAsPrincipalStack().iterator(); 85 while (iRunAs.hasNext()) { 86 addBytes(cResult, (String ) iRunAs.next()); 87 } 88 89 addBytes(cResult, new Integer (ctx.getRunAsPrincipalRolesStack().size()).toString()); 91 iRunAs = ctx.getRunAsPrincipalRolesStack().iterator(); 92 while (iRunAs.hasNext()) { 93 addBytes(cResult, (String []) iRunAs.next()); 94 } 95 96 result = new byte[cResult.size()]; 98 for (int i = 0; i < cResult.size(); i++) { 99 result[i] = ((Byte ) cResult.get(i)).byteValue(); 100 } 101 } 102 103 } catch (java.io.UnsupportedEncodingException uee) { 104 uee.printStackTrace(); 105 } 106 return result; 107 } 108 109 114 public static SecurityContext unmarshallSecurityContext(byte [] byteCtx) { 115 SecurityContext result = null; 116 if ((byteCtx != null) && (byteCtx.length > 0)) { 117 try { 118 Iterator istrs = getBytes2Strings(byteCtx).iterator(); 119 String principalName = (String ) istrs.next(); 120 int nbRoles = new Integer ((String ) istrs.next()).intValue(); 121 ArrayList roles = null; 122 if (nbRoles > 0) { 123 roles = new ArrayList (); 124 } 125 while (nbRoles > 0) { 126 roles.add(istrs.next()); 127 nbRoles--; 128 } 129 130 int nbRunas = new Integer ((String ) istrs.next()).intValue(); 132 ArrayList runas = null; 133 if (nbRunas > 0) { 134 runas = new ArrayList (); 135 } 136 while (nbRunas > 0) { 137 runas.add(istrs.next()); 138 nbRunas--; 139 } 140 141 int nbRunasPrincipal = new Integer ((String ) istrs.next()).intValue(); 143 ArrayList runasPrincipal = null; 144 if (nbRunasPrincipal > 0) { 145 runasPrincipal = new ArrayList (); 146 } 147 while (nbRunasPrincipal > 0) { 148 runasPrincipal.add(istrs.next()); 149 nbRunasPrincipal--; 150 } 151 152 int nbRunasRoles = new Integer ((String ) istrs.next()).intValue(); 154 ArrayList runasRoles = null; 155 if (nbRunasRoles > 0) { 156 int arrayLength = new Integer ((String ) istrs.next()).intValue(); 157 158 if (nbRunasRoles > 0) { 159 runasRoles = new ArrayList (); 160 } 161 while (nbRunasRoles > 0) { 162 String [] st = new String [arrayLength]; 163 for (int j = 0; j < arrayLength; j++) { 164 st[j] = (String ) istrs.next(); 165 } 166 runasRoles.add(st); 167 nbRunasRoles--; 168 } 169 } 170 result = new SecurityContext(principalName, roles, runas, runasPrincipal, runasRoles); 171 172 } catch (java.io.UnsupportedEncodingException uee) { 173 uee.printStackTrace(); 174 } 175 } 176 return result; 177 } 178 179 186 private static void addBytes(Collection c, String toAdd) throws java.io.UnsupportedEncodingException { 187 byte [] b = toAdd.getBytes("UTF8"); 188 c.add(new Byte ((byte) b.length)); 189 for (int i = 0; i < b.length; i++) { 190 c.add(new Byte (b[i])); 191 } 192 } 193 194 201 private static void addBytes(Collection c, String [] toAdd) throws java.io.UnsupportedEncodingException { 202 addBytes(c, new Integer (toAdd.length).toString()); 204 for (int n = 0; n < toAdd.length; n++) { 206 byte [] b = toAdd[n].getBytes("UTF8"); 207 c.add(new Byte ((byte) b.length)); 208 for (int i = 0; i < b.length; i++) { 209 c.add(new Byte (b[i])); 210 } 211 } 212 } 213 214 221 private static List getBytes2Strings(byte[] bytes) throws java.io.UnsupportedEncodingException { 222 ArrayList strs = new ArrayList (); 223 int index = 0; 224 while (bytes.length > index) { 225 int rSize = bytes[index]; 226 index++; 227 byte[] rName = new byte [rSize]; 228 for (int j = 0; j < rSize; j++) { 229 rName[j] = bytes[index]; 230 index++; 231 } 232 strs.add(new String (rName, "UTF8")); 233 } 234 return strs; 235 } 236 237 } 238 239 | Popular Tags |