View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.spring.ui;
20  
21  import java.awt.*;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.WindowAdapter;
25  import java.awt.event.WindowEvent;
26  import javax.swing.*;
27  
28  import org.springframework.beans.factory.InitializingBean;
29  import org.springframework.core.io.ClassPathResource;
30  
31  import org.apache.shiro.authz.AuthorizationException;
32  import org.apache.shiro.samples.spring.SampleManager;
33  
34  
35  /**
36   * Simple web start application that helps to demo single sign-on and
37   * remoting authorization using Shiro.  The injected <tt>SampleManager</tt>
38   * is hosted by the Spring sample web application and remotely invoked
39   * when the buttons in this view are clicked.
40   *
41   * @since 0.1
42   */
43  public class WebStartView implements ActionListener, InitializingBean {
44  
45      /*--------------------------------------------
46      |             C O N S T A N T S             |
47      ============================================*/
48  
49      /*--------------------------------------------
50      |    I N S T A N C E   V A R I A B L E S    |
51      ============================================*/
52      private SampleManager sampleManager;
53      private JTextField valueField;
54      private JButton saveButton;
55      private JButton refreshButton;
56      private JButton secureMethod1Button;
57      private JButton secureMethod2Button;
58      private JButton secureMethod3Button;
59      private JFrame frame;
60  
61      /*--------------------------------------------
62      |         C O N S T R U C T O R S           |
63      ============================================*/
64  
65      /*--------------------------------------------
66      |  A C C E S S O R S / M O D I F I E R S    |
67      ============================================*/
68  
69      public void setSampleManager(SampleManager sampleManager) {
70          this.sampleManager = sampleManager;
71      }
72  
73      /*--------------------------------------------
74      |               M E T H O D S               |
75      ============================================*/
76      public void afterPropertiesSet() throws Exception {
77          ClassPathResource resource = new ClassPathResource("logo.png");
78          ImageIcon icon = new ImageIcon(resource.getURL());
79          JLabel logo = new JLabel(icon);
80  
81          valueField = new JTextField(20);
82          updateValueLabel();
83  
84          saveButton = new JButton("Save Value");
85          saveButton.addActionListener(this);
86  
87          refreshButton = new JButton("Refresh Value");
88          refreshButton.addActionListener(this);
89  
90          JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
91          valuePanel.add(valueField);
92          valuePanel.add(saveButton);
93          valuePanel.add(refreshButton);
94  
95          secureMethod1Button = new JButton("Method #1");
96          secureMethod1Button.addActionListener(this);
97  
98          secureMethod2Button = new JButton("Method #2");
99          secureMethod2Button.addActionListener(this);
100 
101         secureMethod3Button = new JButton("Method #3");
102         secureMethod3Button.addActionListener(this);
103 
104         JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
105         methodPanel.add(secureMethod1Button);
106         methodPanel.add(secureMethod2Button);
107         methodPanel.add(secureMethod3Button);
108 
109         frame = new JFrame("Apache Shiro Sample Application");
110         frame.setSize(500, 200);
111 
112         Container panel = frame.getContentPane();
113         panel.setLayout(new BorderLayout());
114         panel.add(logo, BorderLayout.NORTH);
115         panel.add(valuePanel, BorderLayout.CENTER);
116         panel.add(methodPanel, BorderLayout.SOUTH);
117 
118         frame.setVisible(true);
119         frame.addWindowListener(new WindowAdapter() {
120             public void windowClosing(WindowEvent e) {
121                 System.exit(0);
122             }
123         });
124     }
125 
126     private void updateValueLabel() {
127         valueField.setText(sampleManager.getValue());
128     }
129 
130     public void actionPerformed(ActionEvent e) {
131         try {
132 
133             if (e.getSource() == saveButton) {
134                 sampleManager.setValue(valueField.getText());
135 
136             } else if (e.getSource() == refreshButton) {
137                 updateValueLabel();
138 
139             } else if (e.getSource() == secureMethod1Button) {
140                 sampleManager.secureMethod1();
141                 JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
142 
143             } else if (e.getSource() == secureMethod2Button) {
144                 sampleManager.secureMethod2();
145                 JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
146             } else if (e.getSource() == secureMethod3Button) {
147                 sampleManager.secureMethod3();
148                 JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
149 
150             } else {
151                 throw new RuntimeException("Unexpected action event from source: " + e.getSource());
152             }
153 
154         } catch (AuthorizationException ae) {
155             JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE);
156         }
157     }
158 }