Coverage Report - org.apache.shiro.samples.spring.ui.WebStartView
 
Classes in this File Line Coverage Branch Coverage Complexity
WebStartView
0%
0/56
0%
0/10
2.4
WebStartView$1
0%
0/3
N/A
2.4
 
 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  0
 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  0
         this.sampleManager = sampleManager;
 71  0
     }
 72  
 
 73  
     /*--------------------------------------------
 74  
     |               M E T H O D S               |
 75  
     ============================================*/
 76  
     public void afterPropertiesSet() throws Exception {
 77  0
         ClassPathResource resource = new ClassPathResource("logo.png");
 78  0
         ImageIcon icon = new ImageIcon(resource.getURL());
 79  0
         JLabel logo = new JLabel(icon);
 80  
 
 81  0
         valueField = new JTextField(20);
 82  0
         updateValueLabel();
 83  
 
 84  0
         saveButton = new JButton("Save Value");
 85  0
         saveButton.addActionListener(this);
 86  
 
 87  0
         refreshButton = new JButton("Refresh Value");
 88  0
         refreshButton.addActionListener(this);
 89  
 
 90  0
         JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
 91  0
         valuePanel.add(valueField);
 92  0
         valuePanel.add(saveButton);
 93  0
         valuePanel.add(refreshButton);
 94  
 
 95  0
         secureMethod1Button = new JButton("Method #1");
 96  0
         secureMethod1Button.addActionListener(this);
 97  
 
 98  0
         secureMethod2Button = new JButton("Method #2");
 99  0
         secureMethod2Button.addActionListener(this);
 100  
 
 101  0
         secureMethod3Button = new JButton("Method #3");
 102  0
         secureMethod3Button.addActionListener(this);
 103  
 
 104  0
         JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
 105  0
         methodPanel.add(secureMethod1Button);
 106  0
         methodPanel.add(secureMethod2Button);
 107  0
         methodPanel.add(secureMethod3Button);
 108  
 
 109  0
         frame = new JFrame("Apache Shiro Sample Application");
 110  0
         frame.setSize(500, 200);
 111  
 
 112  0
         Container panel = frame.getContentPane();
 113  0
         panel.setLayout(new BorderLayout());
 114  0
         panel.add(logo, BorderLayout.NORTH);
 115  0
         panel.add(valuePanel, BorderLayout.CENTER);
 116  0
         panel.add(methodPanel, BorderLayout.SOUTH);
 117  
 
 118  0
         frame.setVisible(true);
 119  0
         frame.addWindowListener(new WindowAdapter() {
 120  
             public void windowClosing(WindowEvent e) {
 121  0
                 System.exit(0);
 122  0
             }
 123  
         });
 124  0
     }
 125  
 
 126  
     private void updateValueLabel() {
 127  0
         valueField.setText(sampleManager.getValue());
 128  0
     }
 129  
 
 130  
     public void actionPerformed(ActionEvent e) {
 131  
         try {
 132  
 
 133  0
             if (e.getSource() == saveButton) {
 134  0
                 sampleManager.setValue(valueField.getText());
 135  
 
 136  0
             } else if (e.getSource() == refreshButton) {
 137  0
                 updateValueLabel();
 138  
 
 139  0
             } else if (e.getSource() == secureMethod1Button) {
 140  0
                 sampleManager.secureMethod1();
 141  0
                 JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
 142  
 
 143  0
             } else if (e.getSource() == secureMethod2Button) {
 144  0
                 sampleManager.secureMethod2();
 145  0
                 JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
 146  0
             } else if (e.getSource() == secureMethod3Button) {
 147  0
                 sampleManager.secureMethod3();
 148  0
                 JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
 149  
 
 150  
             } else {
 151  0
                 throw new RuntimeException("Unexpected action event from source: " + e.getSource());
 152  
             }
 153  
 
 154  0
         } catch (AuthorizationException ae) {
 155  0
             JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE);
 156  0
         }
 157  0
     }
 158  
 }