KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > security > SpringSecuredBrokerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.security;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import javax.jbi.messaging.InOnly;
24 import javax.security.auth.Subject JavaDoc;
25 import javax.security.auth.callback.Callback JavaDoc;
26 import javax.security.auth.callback.CallbackHandler JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
30 import javax.security.auth.login.LoginContext JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
32
33 import org.apache.servicemix.client.DefaultServiceMixClient;
34 import org.apache.servicemix.client.ServiceMixClient;
35 import org.apache.servicemix.jbi.jaxp.StringSource;
36 import org.apache.servicemix.tck.Receiver;
37 import org.apache.servicemix.tck.SpringTestSupport;
38 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
39 import org.springframework.context.support.AbstractXmlApplicationContext;
40
41 public class SpringSecuredBrokerTest extends SpringTestSupport {
42
43     static {
44         String JavaDoc path = System.getProperty("java.security.auth.login.config");
45         if (path == null) {
46             URL JavaDoc resource = PropertiesLoginModuleTest.class.getResource("login.properties");
47             if (resource != null) {
48                 path = new File JavaDoc(resource.getFile()).getAbsolutePath();
49                 System.setProperty("java.security.auth.login.config", path);
50             }
51         }
52         System.err.println("Path to login config: " + path);
53     }
54
55     protected Receiver receiver1;
56     protected Receiver receiver2;
57     protected Receiver receiver3;
58     protected ServiceMixClient client;
59     
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         receiver1 = (Receiver) jbi.getBean("receiver1");
63         receiver2 = (Receiver) jbi.getBean("receiver2");
64         receiver3 = (Receiver) jbi.getBean("receiver3");
65         client = new DefaultServiceMixClient(jbi);
66     }
67     
68     protected void tearDown() throws Exception JavaDoc {
69         super.tearDown();
70     }
71
72     protected AbstractXmlApplicationContext createBeanFactory() {
73         return new ClassPathXmlApplicationContext("org/apache/servicemix/jbi/security/secure.xml");
74     }
75     
76     protected Subject JavaDoc login(final String JavaDoc username, final String JavaDoc password) throws Exception JavaDoc {
77         LoginContext JavaDoc context = new LoginContext JavaDoc("servicemix-domain", new CallbackHandler JavaDoc() {
78             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
79                 for (int i = 0; i < callbacks.length; i++) {
80                     if (callbacks[i] instanceof NameCallback JavaDoc) {
81                         ((NameCallback JavaDoc) callbacks[i]).setName(username);
82                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
83                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword(password.toCharArray());
84                     } else {
85                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
86                     }
87                 }
88             }
89         });
90         context.login();
91         return context.getSubject();
92     }
93     
94     protected void send(String JavaDoc username, String JavaDoc password, QName JavaDoc service) throws Exception JavaDoc {
95         Subject JavaDoc subject = login(username, password);
96         InOnly me = client.createInOnlyExchange();
97         me.setService(service);
98         me.getInMessage().setSecuritySubject(subject);
99         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
100         client.sendSync(me);
101     }
102     
103     public void testAuthorizationsOnReceiver1() throws Exception JavaDoc {
104         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/example/1", "receiver1");
105         // receiver1 should be available to: programmers, accounting, testers
106
send("first", "secret", service);
107         send("second", "password", service);
108         send("third", "another", service);
109     }
110     
111     public void testAuthorizationsOnReceiver2() throws Exception JavaDoc {
112         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/example/1", "receiver2");
113         // receiver2 should be available to: programmers, accounting
114
send("first", "secret", service);
115         send("second", "password", service);
116         try {
117             send("third", "another", service);
118             fail("receiver2 is not available to testers");
119         } catch (SecurityException JavaDoc e) {
120         }
121     }
122     
123     public void testAuthorizationsOnReceiver3() throws Exception JavaDoc {
124         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/example/2", "receiver1");
125         // receiver2 should be available to: programmers
126
send("first", "secret", service);
127         try {
128             send("second", "password", service);
129             fail("receiver2 is not available to accounting");
130         } catch (SecurityException JavaDoc e) {
131         }
132         try {
133             send("third", "another", service);
134             fail("receiver2 is not available to testers");
135         } catch (SecurityException JavaDoc e) {
136         }
137     }
138     
139 }
140
Popular Tags