001: /*
002: * Created on 04/09/2006 21:23:22
003: */
004: package net.jforum.api.rest;
005:
006: import java.util.List;
007:
008: import freemarker.template.SimpleHash;
009: import freemarker.template.Template;
010:
011: import net.jforum.Command;
012: import net.jforum.JForumExecutionContext;
013: import net.jforum.context.RequestContext;
014: import net.jforum.context.ResponseContext;
015: import net.jforum.dao.DataAccessDriver;
016: import net.jforum.dao.UserDAO;
017: import net.jforum.entities.User;
018: import net.jforum.exceptions.APIException;
019: import net.jforum.util.I18n;
020: import net.jforum.util.preferences.ConfigKeys;
021: import net.jforum.util.preferences.SystemGlobals;
022: import net.jforum.util.preferences.TemplateKeys;
023:
024: /**
025: * @author Rafael Steil
026: * @version $Id: UserREST.java,v 1.3 2006/10/10 00:49:04 rafaelsteil Exp $
027: */
028: public class UserREST extends Command {
029: /**
030: * List all users
031: */
032: public void list() {
033: try {
034: this .authenticate();
035:
036: UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
037: List users = dao.selectAll();
038:
039: this .setTemplateName(TemplateKeys.API_USER_LIST);
040: this .context.put("users", users);
041: } catch (Exception e) {
042: this .setTemplateName(TemplateKeys.API_ERROR);
043: this .context.put("exception", e);
044: }
045: }
046:
047: /**
048: * Creates a new user.
049: * Required parameters ara "username", "email" and "password".
050: */
051: public void insert() {
052: try {
053: this .authenticate();
054:
055: String username = this .requiredRequestParameter("username");
056: String email = this .requiredRequestParameter("email");
057: String password = this .requiredRequestParameter("password");
058:
059: if (username.length() > SystemGlobals
060: .getIntValue(ConfigKeys.USERNAME_MAX_LENGTH)) {
061: throw new APIException(I18n
062: .getMessage("User.usernameTooBig"));
063: }
064:
065: if (username.indexOf('<') > -1
066: || username.indexOf('>') > -1) {
067: throw new APIException(I18n
068: .getMessage("User.usernameInvalidChars"));
069: }
070:
071: UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
072:
073: if (dao.isUsernameRegistered(username)) {
074: throw new APIException(I18n
075: .getMessage("UsernameExists"));
076: }
077:
078: if (dao.findByEmail(email) != null) {
079: throw new APIException(I18n.getMessage(
080: "User.emailExists", new Object[] { email }));
081: }
082:
083: // Ok, time to insert the user
084: User user = new User();
085: user.setUsername(username);
086: user.setEmail(email);
087: user.setPassword(password);
088:
089: int userId = dao.addNew(user);
090:
091: this .setTemplateName(TemplateKeys.API_USER_INSERT);
092: this .context.put("userId", new Integer(userId));
093: } catch (Exception e) {
094: this .setTemplateName(TemplateKeys.API_ERROR);
095: this .context.put("exception", e);
096: }
097: }
098:
099: /**
100: * Retrieves a parameter from the request and ensures it exists
101: * @param paramName the parameter name to retrieve its value
102: * @return the parameter value
103: * @throws APIException if the parameter is not found or its value is empty
104: */
105: private String requiredRequestParameter(String paramName) {
106: String value = this .request.getParameter(paramName);
107:
108: if (value == null || value.trim().length() == 0) {
109: throw new APIException("The parameter '" + paramName
110: + "' was not found");
111: }
112:
113: return value;
114: }
115:
116: /**
117: * Tries to authenticate the user accessing the API
118: * @throws APIException if the authentication fails
119: */
120: private void authenticate() {
121: String apiKey = this .requiredRequestParameter("api_key");
122:
123: RESTAuthentication auth = new RESTAuthentication();
124:
125: if (!auth.validateApiKey(apiKey)) {
126: throw new APIException(
127: "The provided API authentication information is not valid");
128: }
129: }
130:
131: public Template process(RequestContext request,
132: ResponseContext response, SimpleHash context) {
133: JForumExecutionContext.setContentType("text/xml");
134: return super.process(request, response, context);
135: }
136: }
|