1 28 29 package org.objectweb.security.propagation; 30 31 import org.objectweb.jeremie.services.handler.api.Service; 32 import org.objectweb.jonathan.apis.kernel.Context; 33 import org.objectweb.jonathan.apis.kernel.JonathanException; 34 import org.objectweb.jonathan.presentation.api.Marshaller; 35 import org.objectweb.jonathan.presentation.api.MarshallerFactory; 36 import org.objectweb.jonathan.presentation.api.UnMarshaller; 37 import org.objectweb.jonathan.resources.api.Chunk; 38 import org.objectweb.jonathan.helpers.MessageHelpers; 39 import org.objectweb.security.context.SecurityContext; 40 import org.omg.IOP.ServiceContext ; 41 42 public class SSHandler implements Service { 43 44 private SecuritySender sender = null; 45 private SecurityReceiver receiver = null; 46 private MarshallerFactory mf; 47 private int service_id = 200; 49 52 public SSHandler(Context c, Object [] used_components) throws JonathanException { 53 int sid = ((Integer ) used_components[0]).intValue(); 54 if (sid != Integer.MAX_VALUE) { 55 service_id = sid; 56 } 57 58 try { 59 sender = (SecuritySender) used_components[1]; 60 receiver = (SecurityReceiver) used_components[2]; 61 mf = (MarshallerFactory) used_components[3]; 62 } catch (Exception e) { 63 throw new JonathanException(e); 64 } 65 } 66 67 68 79 public ServiceContext getRequestContext(int request_id, 80 boolean response_expected, 81 byte[] object_key, 82 Context kContext) { 83 if (sender == null) { 84 return null; 85 } 86 SecurityContext ctx = sender.sending_request(request_id); 87 return encodeContext(ctx); 88 } 89 90 91 100 public ServiceContext getReplyContext(int request_id, Context kContext) { 101 if (receiver == null) { 102 return null; 103 } 104 SecurityContext ctx = receiver.sending_reply(request_id); 105 return encodeContext(ctx); 106 } 107 108 117 public void handleRequestContext(ServiceContext context, 118 int request_id, 119 boolean response_expected, 120 byte[] object_key, 121 Context kContext) { 122 if ((receiver == null) || 123 (context == null) || 124 (context.context_data == null) || 125 (context.context_data.length == 0)) { 126 return; 127 } 128 SecurityContext ctx = decodeContext(context); 129 if (ctx != null) { 130 receiver.received_request(request_id, ctx); 131 } 132 } 133 134 141 public void handleReplyContext(ServiceContext context, int request_id, Context kContext) { 142 if ((receiver == null) || 143 (context == null) || 144 (context.context_data == null) || 145 (context.context_data.length == 0)) { 146 return; 147 } 148 SecurityContext ctx = decodeContext(context); 149 if (ctx != null) { 150 sender.received_reply(request_id, ctx); 151 } 152 } 153 154 160 private ServiceContext encodeContext(SecurityContext ctx) { 161 if (ctx == null) { 162 return null; 163 } 164 Marshaller marshaller = mf.newMarshaller(); 165 byte[] byteArray = null; 166 try { 167 marshaller.writeReference(ctx); 168 byteArray = MessageHelpers.copy(marshaller); 169 marshaller.close(); 170 } catch (Exception e) { 171 System.err.println("SSHandler.encodeContext: exception"); 172 e.printStackTrace(); 173 } 174 return new ServiceContext (service_id, byteArray); 175 } 176 177 178 184 private SecurityContext decodeContext(ServiceContext sc) { 185 if ((sc == null) || 186 (sc.context_data == null) || 187 (sc.context_data.length == 0)) { 188 return null; 189 } 190 SecurityContext ctx = null; 191 byte[] byteArray = sc.context_data; 192 UnMarshaller unmarshaller = 193 mf.newUnMarshaller(new Chunk(byteArray, 0, byteArray.length), 0); 194 try { 195 ctx = (SecurityContext) unmarshaller.readReference(); 196 unmarshaller.close(); 197 } catch (Exception e) { 198 System.err.println("SSHandler.decodeContext: exception"); 199 System.err.println(e.toString() + "\n"); 200 } 201 return ctx; 202 } 203 204 } 205 | Popular Tags |