KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > security > propagation > SSHandler


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: SSHandler.java,v 1.16 2004/05/17 07:22:36 durieuxp Exp $
26  * --------------------------------------------------------------------------
27  */

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 JavaDoc;
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; // default value
48

49     /**
50      * Creates a new SSHandler instance.
51      */

52     public SSHandler(Context c, Object JavaDoc[] used_components) throws JonathanException {
53         int sid = ((Integer JavaDoc) 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 JavaDoc e) {
63             throw new JonathanException(e);
64         }
65     }
66     
67
68     /**
69      * Returns a request context.
70      * <p>
71      * If sender is not null, this method encodes the security context
72      * returned by the sending_request method of the sender, else it returns null.
73      *
74      * @param request_id the request identifier;
75      * @param response_expected unused;
76      * @param object_key unused.
77      * @return a service context.
78      */

79     public ServiceContext JavaDoc 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     /**
92      * Returns a reply context.
93      * <p>
94      * this method encodes the security context
95      * returned by its <code>sending_reply</code> method, else it returns null.
96      *
97      * @param request_id the corresponding request id.
98      * @return a service context.
99      */

100     public ServiceContext JavaDoc 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     /**
109      * This method is called by the services handler to let the operations
110      * related to the target service be performed on request arrival.
111      *
112      * @param context the service context of the request;
113      * @param request_id the request identifier;
114      * @param response_expected unused;
115      * @param object_key unused.
116      */

117     public void handleRequestContext(ServiceContext JavaDoc 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     /**
135      * This method is called by the services handler to let the operations
136      * related to the target service be performed on reply arrival.
137      *
138      * @param context the service context of the reply;
139      * @param request_id the corresponding request identifier.
140      */

141     public void handleReplyContext(ServiceContext JavaDoc 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     /**
155      * Encodes a Security context into an IOP service context.
156      *
157      * @param ctx the propagation context to be encoded.
158      * @return the resulting IOP service context.
159      */

160     private ServiceContext JavaDoc 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 JavaDoc e) {
171             System.err.println("SSHandler.encodeContext: exception");
172             e.printStackTrace();
173         }
174         return new ServiceContext JavaDoc(service_id, byteArray);
175     }
176
177     
178     /**
179      * Decodes a Security context from an IOP service context.
180      *
181      * @param ctx the IOP service context containing the encoded security context.
182      * @return the decoded security context.
183      */

184     private SecurityContext decodeContext(ServiceContext JavaDoc 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 JavaDoc 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