KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > security > CreateUser


1 package org.apache.slide.projector.processor.security;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.slide.projector.Context;
6 import org.apache.slide.projector.Information;
7 import org.apache.slide.projector.Processor;
8 import org.apache.slide.projector.Projector;
9 import org.apache.slide.projector.Result;
10 import org.apache.slide.projector.descriptor.ParameterDescriptor;
11 import org.apache.slide.projector.descriptor.ResultDescriptor;
12 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
13 import org.apache.slide.projector.descriptor.StateDescriptor;
14 import org.apache.slide.projector.descriptor.StringValueDescriptor;
15 import org.apache.slide.projector.i18n.DefaultMessage;
16 import org.apache.slide.projector.i18n.ErrorMessage;
17 import org.apache.slide.projector.i18n.ParameterMessage;
18 import org.apache.slide.projector.repository.UserExistsException;
19 import org.apache.slide.projector.value.NullValue;
20 import org.apache.slide.projector.value.URIValue;
21 import org.apache.slide.projector.value.Value;
22
23 /**
24  * @version $Revision: 1.3 $
25  */

26
27 public class CreateUser implements Processor {
28     private final static String JavaDoc USERNAME = "username";
29     private final static String JavaDoc PASSWORD = "password";
30     private final static String JavaDoc RETYPE_PASSWORD = "retypePassword";
31
32     private final static String JavaDoc USER = "user";
33     private final static String JavaDoc OK = "ok";
34     private final static String JavaDoc FAILED = "failed";
35
36     private final static ParameterDescriptor [] parameterDescriptor = new ParameterDescriptor[] {
37             new ParameterDescriptor(USERNAME, new ParameterMessage("create-user/username"), new StringValueDescriptor(1,24)),
38             new ParameterDescriptor(PASSWORD, new ParameterMessage("create-user/password"), new StringValueDescriptor(1,24)),
39             new ParameterDescriptor(RETYPE_PASSWORD, new ParameterMessage("create-user/retyped-password"), new StringValueDescriptor(1,24)),
40         };
41
42     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
43             new StateDescriptor[] {
44                     new StateDescriptor(OK, new DefaultMessage("create-user/state/ok")),
45                     new StateDescriptor(FAILED, new DefaultMessage("create-user/state/failed"))
46             }, new ResultEntryDescriptor[] {
47                     new ResultEntryDescriptor(USER, new DefaultMessage("createUser/result/user"), URIValue.CONTENT_TYPE, false)
48             });
49
50     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
51         String JavaDoc username = parameter.get(USERNAME).toString();
52         String JavaDoc retypedPassword = parameter.get(RETYPE_PASSWORD).toString();
53         String JavaDoc password = parameter.get(PASSWORD).toString();
54         String JavaDoc state = OK;
55         // Check spelling
56
Value user = NullValue.NULL;
57         if ( !password.equals(retypedPassword) ) {
58             context.addInformation(new Information(Information.ERROR, new ErrorMessage("register/passwordsNotIdentical"), new String JavaDoc[] { PASSWORD, RETYPE_PASSWORD }));
59             state = FAILED;
60         } else {
61             try {
62                 user = Projector.getRepository().createUser(username, password, context.getCredentials());
63             } catch ( UserExistsException exception ) {
64                 context.addInformation(new Information(Information.ERROR, exception.getErrorMessage(), new String JavaDoc[] { USERNAME }));
65                 state = FAILED;
66             }
67         }
68         if ( user == null ) {
69             context.addInformation(new Information(Information.ERROR, new ErrorMessage("register/failed"), new String JavaDoc[0]));
70             state = FAILED;
71         }
72         return new Result(state, USER, user);
73     }
74
75     public ParameterDescriptor[] getParameterDescriptors() {
76         return parameterDescriptor;
77     }
78
79     public ResultDescriptor getResultDescriptor() {
80         return resultDescriptor;
81     }
82 }
Popular Tags