KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > xmlrpc > BaseAPIHandler


1 /*
2  * Created on Apr 11, 2003
3  */

4 package org.roller.presentation.xmlrpc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.xmlrpc.XmlRpcException;
9 import org.roller.model.UserManager;
10 import org.roller.pojos.UserData;
11 import org.roller.pojos.WebsiteData;
12 import org.roller.presentation.MainPageAction;
13 import org.roller.presentation.RollerContext;
14 import org.roller.presentation.RollerRequest;
15 import org.roller.presentation.pagecache.PageCacheFilter;
16 import org.roller.util.Utilities;
17
18 import java.io.Serializable JavaDoc;
19 import org.roller.config.RollerConfig;
20
21 /**
22  * Base API handler does user validation, provides exception types, etc.
23  * @author David M Johnson
24  */

25 public class BaseAPIHandler implements Serializable JavaDoc
26 {
27     static final long serialVersionUID = -698186274794937582L;
28     
29     private static Log mLogger =
30         LogFactory.getFactory().getInstance(BaseAPIHandler.class);
31
32     public static final int AUTHORIZATION_EXCEPTION = 0001;
33     public static final String JavaDoc AUTHORIZATION_EXCEPTION_MSG =
34             "Invalid Username and/or Password";
35             
36     public static final int UNKNOWN_EXCEPTION = 1000;
37     public static final String JavaDoc UNKNOWN_EXCEPTION_MSG =
38             "An error occured processing your request";
39     
40     public static final int BLOGGERAPI_DISABLED = 1000;
41     public static final String JavaDoc BLOGGERAPI_DISABLED_MSG =
42             "You have not enabled Blogger API support for your weblog";
43     
44     public static final int UNSUPPORTED_EXCEPTION = 1001;
45     public static final String JavaDoc UNSUPPORTED_EXCEPTION_MSG =
46             "Unsupported method - Roller does not support this method";
47             
48     public static final int INVALID_POSTID = 2000;
49     public static final String JavaDoc INVALID_POSTID_MSG =
50             "The entry postid you submitted is invalid";
51             
52     public static final int NOBLOGS_EXCEPTION = 3000;
53     public static final String JavaDoc NOBLOGS_EXCEPTION_MSG =
54             "There are no categories defined for your user";
55
56     public static final int UPLOAD_DENIED_EXCEPTION = 4000;
57     public static final String JavaDoc UPLOAD_DENIED_EXCEPTION_MSG =
58             "Upload denied";
59
60     //------------------------------------------------------------------------
61
public BaseAPIHandler()
62     {
63     }
64     
65     //------------------------------------------------------------------------
66
//public void prep( HttpServletRequest req )
67
//{
68
//mRoller = RollerContext.getRoller(req);
69
//mContextUrl = RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
70
//
71

72     //------------------------------------------------------------------------
73
protected WebsiteData validate(String JavaDoc username, String JavaDoc password) throws Exception JavaDoc
74     {
75         boolean authenticated = false;
76         boolean enabled = false;
77         WebsiteData website = null;
78         try
79         {
80             // Get Roller request object for current thread
81
RollerRequest rreq = RollerRequest.getRollerRequest();
82             
83             UserManager userMgr = rreq.getRoller().getUserManager();
84             website = userMgr.getWebsite(username);
85             
86             enabled = website.getEnableBloggerApi().booleanValue();
87             if (enabled)
88             {
89                 // are passwords encrypted?
90
RollerContext rollerContext =
91                     RollerContext.getRollerContext(rreq.getRequest());
92                 String JavaDoc encrypted =
93                         RollerConfig.getProperty("passwds.encryption.enabled");
94                 //System.out.print("password was [" + password + "] ");
95
if ("true".equalsIgnoreCase(encrypted))
96                 {
97                     password = Utilities.encodePassword(password,
98                       RollerConfig.getProperty("passwds.encryption.algorithm"));
99                 }
100                 //System.out.println("is now [" + password + "]");
101
authenticated= website.getUser().getPassword().equals(password);
102                 if (authenticated)
103                 {
104                     rreq.getRoller().setUser(website.getUser());
105                 }
106             }
107         }
108         catch (Exception JavaDoc e)
109         {
110             mLogger.error("ERROR internal error validating user", e);
111         }
112         
113         if ( !enabled )
114         {
115             throw new XmlRpcException(
116                 BLOGGERAPI_DISABLED, BLOGGERAPI_DISABLED_MSG);
117         }
118         
119         if ( !authenticated )
120         {
121             throw new XmlRpcException(
122                 AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG);
123         }
124         return website;
125     }
126
127     //------------------------------------------------------------------------
128
protected void flushPageCache(String JavaDoc user) throws Exception JavaDoc
129     {
130         // Get Roller request object for current thread
131
RollerRequest rreq = RollerRequest.getRollerRequest();
132         
133         UserManager umgr = rreq.getRoller().getUserManager();
134         UserData ud = umgr.getUser(user);
135         
136         PageCacheFilter.removeFromCache( rreq.getRequest(), ud );
137         MainPageAction.flushMainPageCache();
138     }
139 }
140
Popular Tags