KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > xmlrpc > BaseAPIHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 /*
19  * Created on Apr 11, 2003
20  */

21 package org.apache.roller.webservices.xmlrpc;
22
23 import java.io.Serializable JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.xmlrpc.XmlRpcException;
28 import org.apache.roller.config.RollerConfig;
29 import org.apache.roller.model.RollerFactory;
30 import org.apache.roller.model.UserManager;
31 import org.apache.roller.pojos.UserData;
32 import org.apache.roller.pojos.WebsiteData;
33 import org.apache.roller.ui.core.RollerContext;
34 import org.apache.roller.util.cache.CacheManager;
35 import org.apache.roller.util.Utilities;
36
37 /**
38  * Base API handler does user validation, provides exception types, etc.
39  * @author David M Johnson
40  */

41 public class BaseAPIHandler implements Serializable JavaDoc {
42     static final long serialVersionUID = -698186274794937582L;
43     
44     private static Log mLogger =
45             LogFactory.getFactory().getInstance(BaseAPIHandler.class);
46     
47     public static final int AUTHORIZATION_EXCEPTION = 0001;
48     public static final String JavaDoc AUTHORIZATION_EXCEPTION_MSG =
49             "Invalid Username and/or Password";
50     
51     public static final int UNKNOWN_EXCEPTION = 1000;
52     public static final String JavaDoc UNKNOWN_EXCEPTION_MSG =
53             "An error occured processing your request";
54     
55     public static final int UNSUPPORTED_EXCEPTION = 1001;
56     public static final String JavaDoc UNSUPPORTED_EXCEPTION_MSG =
57             "Unsupported method - Roller does not support this method";
58     
59     public static final int USER_DISABLED = 1002;
60     public static final String JavaDoc USER_DISABLED_MSG =
61             "User is disabled";
62     
63     public static final int WEBLOG_NOT_FOUND = 1003;
64     public static final String JavaDoc WEBLOG_NOT_FOUND_MSG =
65             "Weblog is not found or is disabled";
66     
67     public static final int WEBLOG_DISABLED = 1004;
68     public static final String JavaDoc WEBLOG_DISABLED_MSG =
69             "Weblog is not found or is disabled";
70     
71     public static final int BLOGGERAPI_DISABLED = 1005;
72     public static final String JavaDoc BLOGGERAPI_DISABLED_MSG =
73             "Weblog does not exist or XML-RPC disabled in web";
74     
75     public static final int BLOGGERAPI_INCOMPLETE_POST = 1006;
76     public static final String JavaDoc BLOGGERAPI_INCOMPLETE_POST_MSG =
77             "Incomplete weblog entry";
78     
79     public static final int INVALID_POSTID = 2000;
80     public static final String JavaDoc INVALID_POSTID_MSG =
81             "The entry postid you submitted is invalid";
82     
83     //public static final int NOBLOGS_EXCEPTION = 3000;
84
//public static final String NOBLOGS_EXCEPTION_MSG =
85
//"There are no categories defined for your user";
86

87     public static final int UPLOAD_DENIED_EXCEPTION = 4000;
88     public static final String JavaDoc UPLOAD_DENIED_EXCEPTION_MSG =
89             "Upload denied";
90     
91     //------------------------------------------------------------------------
92
public BaseAPIHandler() {
93     }
94     
95     //------------------------------------------------------------------------
96
//public void prep( HttpServletRequest req )
97
//{
98
//mRoller = RollerContext.getRoller(req);
99
//mContextUrl = RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
100
//
101

102     //------------------------------------------------------------------------
103
/**
104      * Returns website, but only if user authenticates and is authorized to edit.
105      * @param blogid Blogid sent in request (used as website's hanldle)
106      * @param username Username sent in request
107      * @param password Password sent in requeset
108      */

109     protected WebsiteData validate(String JavaDoc blogid, String JavaDoc username, String JavaDoc password)
110     throws Exception JavaDoc {
111         boolean authenticated = false;
112         boolean userEnabled = false;
113         boolean weblogEnabled = false;
114         boolean apiEnabled = false;
115         boolean weblogFound = false;
116         WebsiteData website = null;
117         UserData user = null;
118         try {
119             UserManager userMgr = RollerFactory.getRoller().getUserManager();
120             user = userMgr.getUserByUserName(username);
121             userEnabled = user.getEnabled().booleanValue();
122             
123             website = userMgr.getWebsiteByHandle(blogid);
124             if (website != null) {
125                 weblogFound = true;
126                 weblogEnabled = website.getEnabled().booleanValue();
127                 apiEnabled = website.getEnableBloggerApi().booleanValue();
128             }
129             
130             if (user != null) {
131                 // are passwords encrypted?
132
RollerContext rollerContext =
133                         RollerContext.getRollerContext();
134                 String JavaDoc encrypted =
135                         RollerConfig.getProperty("passwds.encryption.enabled");
136                 //System.out.print("password was [" + password + "] ");
137
if ("true".equalsIgnoreCase(encrypted)) {
138                     password = Utilities.encodePassword(password,
139                             RollerConfig.getProperty("passwds.encryption.algorithm"));
140                 }
141                 authenticated = password.equals(user.getPassword());
142             }
143         } catch (Exception JavaDoc e) {
144             mLogger.error("ERROR internal error validating user", e);
145         }
146         
147         if ( !authenticated ) {
148             throw new XmlRpcException(
149                     AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG);
150         }
151         if ( !userEnabled ) {
152             throw new XmlRpcException(
153                     USER_DISABLED, USER_DISABLED_MSG);
154         }
155         if ( !weblogEnabled ) {
156             throw new XmlRpcException(
157                     WEBLOG_DISABLED, WEBLOG_DISABLED_MSG);
158         }
159         if ( !weblogFound ) {
160             throw new XmlRpcException(
161                     WEBLOG_NOT_FOUND, WEBLOG_NOT_FOUND_MSG);
162         }
163         if ( !apiEnabled ) {
164             throw new XmlRpcException(
165                     BLOGGERAPI_DISABLED, BLOGGERAPI_DISABLED_MSG);
166         }
167         return website;
168     }
169     
170     //------------------------------------------------------------------------
171
/**
172      * Returns true if username/password are valid and user is not disabled.
173      * @param username Username sent in request
174      * @param password Password sent in requeset
175      */

176     protected boolean validateUser(String JavaDoc username, String JavaDoc password)
177     throws Exception JavaDoc {
178         boolean authenticated = false;
179         boolean enabled = false;
180         UserData user = null;
181         try {
182             
183             UserManager userMgr = RollerFactory.getRoller().getUserManager();
184             user = userMgr.getUserByUserName(username);
185             
186             enabled = user.getEnabled().booleanValue();
187             if (enabled) {
188                 // are passwords encrypted?
189
RollerContext rollerContext =
190                         RollerContext.getRollerContext();
191                 String JavaDoc encrypted =
192                         RollerConfig.getProperty("passwds.encryption.enabled");
193                 //System.out.print("password was [" + password + "] ");
194
if ("true".equalsIgnoreCase(encrypted)) {
195                     password = Utilities.encodePassword(password,
196                             RollerConfig.getProperty("passwds.encryption.algorithm"));
197                 }
198                 //System.out.println("is now [" + password + "]");
199
authenticated = user.getPassword().equals(password);
200                 if (authenticated) {
201                     //RollerFactory.getRoller().setUser(user);
202
}
203             }
204         } catch (Exception JavaDoc e) {
205             mLogger.error("ERROR internal error validating user", e);
206         }
207         
208         if ( !enabled ) {
209             throw new XmlRpcException(
210                     BLOGGERAPI_DISABLED, BLOGGERAPI_DISABLED_MSG);
211         }
212         
213         if ( !authenticated ) {
214             throw new XmlRpcException(
215                     AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG);
216         }
217         return authenticated;
218     }
219     
220     //------------------------------------------------------------------------
221
protected void flushPageCache(WebsiteData website) throws Exception JavaDoc {
222         CacheManager.invalidate(website);
223     }
224 }
225
Popular Tags