| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Authorization |
|
| 4.75;4.75 |
| 1 | /* | |
| 2 | * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.login.Authorization.html,v $ | |
| 3 | * $Revision: 1.1 $ | |
| 4 | * | |
| 5 | * Copyright (C) 2006 Tim Pizey | |
| 6 | * | |
| 7 | * Part of Melati (http://melati.org), a framework for the rapid | |
| 8 | * development of clean, maintainable web applications. | |
| 9 | * | |
| 10 | * Melati is free software; Permission is granted to copy, distribute | |
| 11 | * and/or modify this software under the terms either: | |
| 12 | * | |
| 13 | * a) the GNU General Public License as published by the Free Software | |
| 14 | * Foundation; either version 2 of the License, or (at your option) | |
| 15 | * any later version, | |
| 16 | * | |
| 17 | * or | |
| 18 | * | |
| 19 | * b) any version of the Melati Software License, as published | |
| 20 | * at http://melati.org | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License and | |
| 23 | * the Melati Software License along with this program; | |
| 24 | * if not, write to the Free Software Foundation, Inc., | |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
| 26 | * GNU General Public License and visit http://melati.org to obtain the | |
| 27 | * Melati Software License. | |
| 28 | * | |
| 29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
| 30 | * if you would like to work out a different arrangement than the options | |
| 31 | * outlined here. It is our intention to allow Melati to be used by as | |
| 32 | * wide an audience as possible. | |
| 33 | * | |
| 34 | * This program is distributed in the hope that it will be useful, | |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 37 | * GNU General Public License for more details. | |
| 38 | * | |
| 39 | * Contact details for copyright holder: | |
| 40 | * | |
| 41 | * Tim Pizey <timp At paneris.org> | |
| 42 | * http://paneris.org/~timp | |
| 43 | */ | |
| 44 | package org.melati.login; | |
| 45 | ||
| 46 | import java.io.BufferedReader; | |
| 47 | import java.io.IOException; | |
| 48 | import java.io.PrintStream; | |
| 49 | ||
| 50 | /** | |
| 51 | * A store for a username and password. | |
| 52 | */ | |
| 53 | final class Authorization { | |
| 54 | /** The username. */ | |
| 55 | 36 | public String username = null; |
| 56 | /** The password. */ | |
| 57 | 36 | public String password = null; |
| 58 | ||
| 59 | /** | |
| 60 | * Do not allow public instantiation. | |
| 61 | */ | |
| 62 | 0 | private Authorization() { |
| 63 | 0 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Private constructor. | |
| 67 | * | |
| 68 | * @param username | |
| 69 | * @param password | |
| 70 | */ | |
| 71 | 36 | private Authorization(String username, String password) { |
| 72 | 36 | this.username = username; |
| 73 | 36 | this.password = password; |
| 74 | 36 | } |
| 75 | ||
| 76 | /** | |
| 77 | * Interrogate the user for their details. | |
| 78 | * | |
| 79 | * @param input an open reader | |
| 80 | * @param output normally System.out | |
| 81 | * @return a new Authorisation object or null | |
| 82 | */ | |
| 83 | static Authorization from(BufferedReader input, PrintStream output) throws IOException { | |
| 84 | 16 | String username = null; |
| 85 | 16 | String password = null; |
| 86 | ||
| 87 | 16 | output.print("Enter your username: "); |
| 88 | 16 | username = input.readLine(); |
| 89 | 16 | output.print("Enter your password: "); |
| 90 | 16 | password = input.readLine(); |
| 91 | 16 | return new Authorization(username, password); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Create an Authorisation from an array, typically the stored | |
| 96 | * arguments. | |
| 97 | * @param args Command line arguments, stored in Melati | |
| 98 | * @return a new Authorization object or null | |
| 99 | */ | |
| 100 | static Authorization from(String[] args) { | |
| 101 | 30 | String username = null; |
| 102 | 30 | String password = null; |
| 103 | 30 | boolean nextValue = false; |
| 104 | 136 | for (int i = 0; i < args.length; i++) { |
| 105 | ||
| 106 | 128 | if (nextValue) { |
| 107 | 22 | username = args[i]; |
| 108 | 22 | break; |
| 109 | } | |
| 110 | 106 | if (args[i].equalsIgnoreCase("-u")) |
| 111 | 16 | nextValue = true; |
| 112 | 106 | if (args[i].equalsIgnoreCase("-user")) |
| 113 | 2 | nextValue = true; |
| 114 | 106 | if (args[i].equalsIgnoreCase("-username")) |
| 115 | 2 | nextValue = true; |
| 116 | 106 | if (args[i].equalsIgnoreCase("--username")) |
| 117 | 2 | nextValue = true; |
| 118 | } | |
| 119 | ||
| 120 | 30 | nextValue = false; |
| 121 | 176 | for (int i = 0; i < args.length; i++) { |
| 122 | 168 | if (nextValue) { |
| 123 | 22 | password = args[i]; |
| 124 | 22 | break; |
| 125 | } | |
| 126 | 146 | if (args[i].equalsIgnoreCase("-p")) |
| 127 | 16 | nextValue = true; |
| 128 | 146 | if (args[i].equalsIgnoreCase("-pass")) |
| 129 | 2 | nextValue = true; |
| 130 | 146 | if (args[i].equalsIgnoreCase("-password")) |
| 131 | 2 | nextValue = true; |
| 132 | 146 | if (args[i].equalsIgnoreCase("--password")) |
| 133 | 2 | nextValue = true; |
| 134 | } | |
| 135 | ||
| 136 | 30 | if (username != null && password != null) { |
| 137 | 20 | return new Authorization(username, password); |
| 138 | } else | |
| 139 | 10 | return null; |
| 140 | } | |
| 141 | ||
| 142 | } |