Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Authorization |
|
| 4.75;4.75 |
1 | /* | |
2 | * $Source$ | |
3 | * $Revision$ | |
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 | 18 | public String username = null; |
56 | /** The password. */ | |
57 | 18 | 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 | 18 | private Authorization(String username, String password) { |
72 | 18 | this.username = username; |
73 | 18 | this.password = password; |
74 | 18 | } |
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 | 8 | String username = null; |
85 | 8 | String password = null; |
86 | ||
87 | 8 | output.print("Enter your username: "); |
88 | 8 | username = input.readLine(); |
89 | 8 | output.print("Enter your password: "); |
90 | 8 | password = input.readLine(); |
91 | 8 | 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 | 15 | String username = null; |
102 | 15 | String password = null; |
103 | 15 | boolean nextValue = false; |
104 | 68 | for (int i = 0; i < args.length; i++) { |
105 | ||
106 | 64 | if (nextValue) { |
107 | 11 | username = args[i]; |
108 | 11 | break; |
109 | } | |
110 | 53 | if (args[i].equalsIgnoreCase("-u")) |
111 | 8 | nextValue = true; |
112 | 53 | if (args[i].equalsIgnoreCase("-user")) |
113 | 1 | nextValue = true; |
114 | 53 | if (args[i].equalsIgnoreCase("-username")) |
115 | 1 | nextValue = true; |
116 | 53 | if (args[i].equalsIgnoreCase("--username")) |
117 | 1 | nextValue = true; |
118 | } | |
119 | ||
120 | 15 | nextValue = false; |
121 | 88 | for (int i = 0; i < args.length; i++) { |
122 | 84 | if (nextValue) { |
123 | 11 | password = args[i]; |
124 | 11 | break; |
125 | } | |
126 | 73 | if (args[i].equalsIgnoreCase("-p")) |
127 | 8 | nextValue = true; |
128 | 73 | if (args[i].equalsIgnoreCase("-pass")) |
129 | 1 | nextValue = true; |
130 | 73 | if (args[i].equalsIgnoreCase("-password")) |
131 | 1 | nextValue = true; |
132 | 73 | if (args[i].equalsIgnoreCase("--password")) |
133 | 1 | nextValue = true; |
134 | } | |
135 | ||
136 | 15 | if (username != null && password != null) { |
137 | 10 | return new Authorization(username, password); |
138 | } else | |
139 | 5 | return null; |
140 | } | |
141 | ||
142 | } |