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 public String username = null;
56 /** The password. */
57 public String password = null;
58
59 /**
60 * Do not allow public instantiation.
61 */
62 private Authorization() {
63 }
64
65 /**
66 * Private constructor.
67 *
68 * @param username
69 * @param password
70 */
71 private Authorization(String username, String password) {
72 this.username = username;
73 this.password = password;
74 }
75
76 /**
77 * Interrogate the user for thier 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 String username = null;
85 String password = null;
86
87 output.print("Enter your username: ");
88 username = input.readLine();
89 output.print("Enter your password: ");
90 password = input.readLine();
91 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 String username = null;
102 String password = null;
103 boolean nextValue = false;
104 for (int i = 0; i < args.length; i++) {
105
106 if (nextValue) {
107 username = args[i];
108 break;
109 }
110 if (args[i].equalsIgnoreCase("-u"))
111 nextValue = true;
112 if (args[i].equalsIgnoreCase("-user"))
113 nextValue = true;
114 if (args[i].equalsIgnoreCase("-username"))
115 nextValue = true;
116 if (args[i].equalsIgnoreCase("--username"))
117 nextValue = true;
118 }
119
120 nextValue = false;
121 for (int i = 0; i < args.length; i++) {
122 if (nextValue) {
123 password = args[i];
124 break;
125 }
126 if (args[i].equalsIgnoreCase("-p"))
127 nextValue = true;
128 if (args[i].equalsIgnoreCase("-pass"))
129 nextValue = true;
130 if (args[i].equalsIgnoreCase("-password"))
131 nextValue = true;
132 if (args[i].equalsIgnoreCase("--password"))
133 nextValue = true;
134 }
135
136 if (username != null && password != null) {
137 return new Authorization(username, password);
138 }else
139 return null;
140 }
141
142 }