KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > security > context > Marshalling


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  */

22 package org.objectweb.security.context;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Class allowing to marshall/unmarshall Security Context class
31  * @author Guillaume Riviere (initial developer)
32  * @author Florent Benoit
33  */

34 public class Marshalling {
35
36     /**
37      * security context id
38      */

39     public static final int SEC_CTX_ID = 101;
40
41     /**
42      * Utility class
43      */

44     private Marshalling() {
45
46     }
47
48     /**
49      * Custom UTF8 marshalling SecurityContext
50      * The resulting bute array is composed of the following elements:
51      * principal-name, roles-number, role1, ...., runas-number, runas1, ....
52      * then principal stack then roles of principal stack
53      * @return byte [] the marshalled context
54      * @param ctx SecurityContext
55      */

56     public static byte[] marshallSecurityContext(SecurityContext ctx) {
57         byte [] result = null;
58         try {
59             if (ctx != null) {
60                 // build a new byte from ctx
61
ArrayList JavaDoc cResult = new ArrayList JavaDoc();
62                 // add the pname
63
addBytes(cResult, ctx.getPrincipalName());
64                 // add the roles
65
String JavaDoc [] roles = ctx.getRoles();
66                 int nbRoles = 0;
67                 if (roles != null) {
68                     nbRoles = roles.length;
69                 }
70                 addBytes(cResult, new Integer JavaDoc(nbRoles).toString());
71                 for (int i = 0; i < nbRoles; i++) {
72                     addBytes(cResult, roles[i]);
73                 }
74
75                 // add runas
76
addBytes(cResult, new Integer JavaDoc(ctx.getRunAsRoleStack().size()).toString());
77                 Iterator JavaDoc iRunAs = ctx.getRunAsRoleStack().iterator();
78                 while (iRunAs.hasNext()) {
79                     addBytes(cResult, (String JavaDoc) iRunAs.next());
80                 }
81
82                 // add runas principal stack
83
addBytes(cResult, new Integer JavaDoc(ctx.getRunAsPrincipalStack().size()).toString());
84                 iRunAs = ctx.getRunAsPrincipalStack().iterator();
85                 while (iRunAs.hasNext()) {
86                     addBytes(cResult, (String JavaDoc) iRunAs.next());
87                 }
88
89                 // add runas roles of principal mapping
90
addBytes(cResult, new Integer JavaDoc(ctx.getRunAsPrincipalRolesStack().size()).toString());
91                 iRunAs = ctx.getRunAsPrincipalRolesStack().iterator();
92                 while (iRunAs.hasNext()) {
93                     addBytes(cResult, (String JavaDoc[]) iRunAs.next());
94                 }
95
96                 // result casting
97
result = new byte[cResult.size()];
98                 for (int i = 0; i < cResult.size(); i++) {
99                     result[i] = ((Byte JavaDoc) cResult.get(i)).byteValue();
100                 }
101             }
102
103         } catch (java.io.UnsupportedEncodingException JavaDoc uee) {
104             uee.printStackTrace();
105         }
106         return result;
107     }
108
109     /**
110      * Custom UTF8 marshalling SecurityContext
111      * @param byteCtx the marshalled context
112      * @return SecurityContext
113      */

114     public static SecurityContext unmarshallSecurityContext(byte [] byteCtx) {
115         SecurityContext result = null;
116         if ((byteCtx != null) && (byteCtx.length > 0)) {
117             try {
118                 Iterator JavaDoc istrs = getBytes2Strings(byteCtx).iterator();
119                 String JavaDoc principalName = (String JavaDoc) istrs.next();
120                 int nbRoles = new Integer JavaDoc((String JavaDoc) istrs.next()).intValue();
121                 ArrayList JavaDoc roles = null;
122                 if (nbRoles > 0) {
123                     roles = new ArrayList JavaDoc();
124                 }
125                 while (nbRoles > 0) {
126                     roles.add(istrs.next());
127                     nbRoles--;
128                 }
129
130                 // unmarshall runAs role
131
int nbRunas = new Integer JavaDoc((String JavaDoc) istrs.next()).intValue();
132                 ArrayList JavaDoc runas = null;
133                 if (nbRunas > 0) {
134                     runas = new ArrayList JavaDoc();
135                 }
136                 while (nbRunas > 0) {
137                     runas.add(istrs.next());
138                     nbRunas--;
139                 }
140
141                 // unmarshall runAs principal
142
int nbRunasPrincipal = new Integer JavaDoc((String JavaDoc) istrs.next()).intValue();
143                 ArrayList JavaDoc runasPrincipal = null;
144                 if (nbRunasPrincipal > 0) {
145                     runasPrincipal = new ArrayList JavaDoc();
146                 }
147                 while (nbRunasPrincipal > 0) {
148                     runasPrincipal.add(istrs.next());
149                     nbRunasPrincipal--;
150                 }
151
152                 // unmarshall runAs principal to roles mapping
153
int nbRunasRoles = new Integer JavaDoc((String JavaDoc) istrs.next()).intValue();
154                 ArrayList JavaDoc runasRoles = null;
155                 if (nbRunasRoles > 0) {
156                     int arrayLength = new Integer JavaDoc((String JavaDoc) istrs.next()).intValue();
157
158                     if (nbRunasRoles > 0) {
159                         runasRoles = new ArrayList JavaDoc();
160                     }
161                     while (nbRunasRoles > 0) {
162                         String JavaDoc[] st = new String JavaDoc[arrayLength];
163                         for (int j = 0; j < arrayLength; j++) {
164                             st[j] = (String JavaDoc) 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 JavaDoc uee) {
173                 uee.printStackTrace();
174             }
175         }
176         return result;
177     }
178
179     /**
180      * Add to the collection of a byte[], the given string.
181      * (its lengths, following by its value)
182      * @param c collection of byte[]
183      * @param toAdd string to transform to a byte[] and to add to the collection
184      * @throws java.io.UnsupportedEncodingException if the UTF-8 encoding fails
185      */

186     private static void addBytes(Collection JavaDoc c, String JavaDoc toAdd) throws java.io.UnsupportedEncodingException JavaDoc {
187         byte [] b = toAdd.getBytes("UTF8");
188         c.add(new Byte JavaDoc((byte) b.length));
189         for (int i = 0; i < b.length; i++) {
190             c.add(new Byte JavaDoc(b[i]));
191         }
192     }
193
194     /**
195      * Add to the collection of a byte[], the given string.
196      * (its lengths, following by its value)
197      * @param c collection of byte[]
198      * @param toAdd string to transform to a byte[] and to add to the collection
199      * @throws java.io.UnsupportedEncodingException if the UTF-8 encoding fails
200      */

201     private static void addBytes(Collection JavaDoc c, String JavaDoc[] toAdd) throws java.io.UnsupportedEncodingException JavaDoc {
202         // add length of the array
203
addBytes(c, new Integer JavaDoc(toAdd.length).toString());
204         // add each string of the array
205
for (int n = 0; n < toAdd.length; n++) {
206             byte [] b = toAdd[n].getBytes("UTF8");
207             c.add(new Byte JavaDoc((byte) b.length));
208             for (int i = 0; i < b.length; i++) {
209                 c.add(new Byte JavaDoc(b[i]));
210             }
211         }
212     }
213
214     /**
215      * Transform a bytes array to a Strin array.
216      * (The bytes array contains for each string, its length, following by its value).
217      * @param bytes byte array to transform
218      * @return the array list of string
219      * @throws java.io.UnsupportedEncodingException if the UTF-8 encoding fails
220      */

221     private static List JavaDoc getBytes2Strings(byte[] bytes) throws java.io.UnsupportedEncodingException JavaDoc {
222         ArrayList JavaDoc strs = new ArrayList JavaDoc();
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 JavaDoc(rName, "UTF8"));
233         }
234         return strs;
235     }
236
237 }
238
239
Popular Tags