00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.objectweb.security.propagation;
00030
00031 import org.objectweb.jeremie.services.handler.api.Service;
00032 import org.objectweb.jonathan.apis.kernel.Context;
00033 import org.objectweb.jonathan.apis.kernel.JonathanException;
00034 import org.objectweb.jonathan.presentation.api.Marshaller;
00035 import org.objectweb.jonathan.presentation.api.MarshallerFactory;
00036 import org.objectweb.jonathan.presentation.api.UnMarshaller;
00037 import org.objectweb.jonathan.resources.api.Chunk;
00038 import org.objectweb.jonathan.helpers.MessageHelpers;
00039 import org.objectweb.security.context.SecurityContext;
00040 import org.omg.IOP.ServiceContext;
00041
00042 public class SSHandler implements Service {
00043
00044 private SecuritySender sender = null;
00045 private SecurityReceiver receiver = null;
00046 private MarshallerFactory mf;
00047 private int service_id = 200;
00048
00052 public SSHandler(Context c, Object[] used_components) throws JonathanException {
00053 int sid = ((Integer) used_components[0]).intValue();
00054 if (sid != Integer.MAX_VALUE) {
00055 service_id = sid;
00056 }
00057
00058 try {
00059 sender = (SecuritySender) used_components[1];
00060 receiver = (SecurityReceiver) used_components[2];
00061 mf = (MarshallerFactory) used_components[3];
00062 } catch (Exception e) {
00063 throw new JonathanException(e);
00064 }
00065 }
00066
00067
00079 public ServiceContext getRequestContext(int request_id,
00080 boolean response_expected,
00081 byte[] object_key,
00082 Context kContext) {
00083 if (sender == null) {
00084 return null;
00085 }
00086 SecurityContext ctx = sender.sending_request(request_id);
00087 return encodeContext(ctx);
00088 }
00089
00090
00100 public ServiceContext getReplyContext(int request_id, Context kContext) {
00101 if (receiver == null) {
00102 return null;
00103 }
00104 SecurityContext ctx = receiver.sending_reply(request_id);
00105 return encodeContext(ctx);
00106 }
00107
00117 public void handleRequestContext(ServiceContext context,
00118 int request_id,
00119 boolean response_expected,
00120 byte[] object_key,
00121 Context kContext) {
00122 if ((receiver == null) ||
00123 (context == null) ||
00124 (context.context_data == null) ||
00125 (context.context_data.length == 0)) {
00126 return;
00127 }
00128 SecurityContext ctx = decodeContext(context);
00129 if (ctx != null) {
00130 receiver.received_request(request_id, ctx);
00131 }
00132 }
00133
00141 public void handleReplyContext(ServiceContext context, int request_id, Context kContext) {
00142 if ((receiver == null) ||
00143 (context == null) ||
00144 (context.context_data == null) ||
00145 (context.context_data.length == 0)) {
00146 return;
00147 }
00148 SecurityContext ctx = decodeContext(context);
00149 if (ctx != null) {
00150 sender.received_reply(request_id, ctx);
00151 }
00152 }
00153
00160 private ServiceContext encodeContext(SecurityContext ctx) {
00161 if (ctx == null) {
00162 return null;
00163 }
00164 Marshaller marshaller = mf.newMarshaller();
00165 byte[] byteArray = null;
00166 try {
00167 marshaller.writeReference(ctx);
00168 byteArray = MessageHelpers.copy(marshaller);
00169 marshaller.close();
00170 } catch (Exception e) {
00171 System.err.println("SSHandler.encodeContext: exception");
00172 e.printStackTrace();
00173 }
00174 return new ServiceContext(service_id, byteArray);
00175 }
00176
00177
00184 private SecurityContext decodeContext(ServiceContext sc) {
00185 if ((sc == null) ||
00186 (sc.context_data == null) ||
00187 (sc.context_data.length == 0)) {
00188 return null;
00189 }
00190 SecurityContext ctx = null;
00191 byte[] byteArray = sc.context_data;
00192 UnMarshaller unmarshaller =
00193 mf.newUnMarshaller(new Chunk(byteArray, 0, byteArray.length), 0);
00194 try {
00195 ctx = (SecurityContext) unmarshaller.readReference();
00196 unmarshaller.close();
00197 } catch (Exception e) {
00198 System.err.println("SSHandler.decodeContext: exception");
00199 System.err.println(e.toString() + "\n");
00200 }
00201 return ctx;
00202 }
00203
00204 }