SSHandler.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): ____________________________________.
00022  * Contributor(s): ______________________________________.
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: SSHandler.java,v 1.16 2004/05/17 07:22:36 durieuxp Exp $
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; // default value
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 }

Generated on Tue Feb 15 15:06:04 2005 for JOnAS by  doxygen 1.3.9.1