KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > extension > xmlrpc > handler > APIHandler


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.extension.xmlrpc.handler;
32
33 import org.apache.xmlrpc.XmlRpcException;
34 import org.blojsom.authorization.AuthorizationException;
35 import org.blojsom.authorization.AuthorizationProvider;
36 import org.blojsom.blog.Blog;
37 import org.blojsom.event.EventBroadcaster;
38 import org.blojsom.fetcher.Fetcher;
39
40 import javax.servlet.ServletConfig JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Properties JavaDoc;
45
46 /**
47  * API Handler
48  *
49  * @author David Czarnecki
50  * @since blojsom 3.0
51  * @version $Id: APIHandler.java,v 1.2 2006/04/05 00:44:02 czarneckid Exp $
52  */

53 public abstract class APIHandler {
54
55     protected static final int AUTHORIZATION_EXCEPTION = 1;
56     protected static final String JavaDoc AUTHORIZATION_EXCEPTION_MSG = "Invalid username and/or password";
57
58     protected static final int UNKNOWN_EXCEPTION = 1000;
59     protected static final String JavaDoc UNKNOWN_EXCEPTION_MSG = "An error occured processing your request";
60
61     protected static final int UNSUPPORTED_EXCEPTION = 1001;
62     protected static final String JavaDoc UNSUPPORTED_EXCEPTION_MSG = "Unsupported method";
63
64     protected static final int INVALID_POSTID = 2000;
65     protected static final String JavaDoc INVALID_POSTID_MSG = "The entry postid you submitted is invalid";
66
67     protected static final int NOBLOGS_EXCEPTION = 3000;
68     protected static final String JavaDoc NOBLOGS_EXCEPTION_MSG = "There are no categories defined";
69
70     protected static final int PERMISSION_EXCEPTION = 4000;
71     protected static final String JavaDoc PERMISSION_EXCEPTION_MSG = "User does not have permission to use this XML-RPC method";
72
73     protected AuthorizationProvider _authorizationProvider;
74     protected Fetcher _fetcher;
75     protected Blog _blog;
76     protected HttpServletRequest JavaDoc _httpServletRequest;
77     protected HttpServletResponse JavaDoc _httpServletResponse;
78     protected EventBroadcaster _eventBroadcaster;
79     protected Properties JavaDoc _properties;
80     protected ServletConfig JavaDoc _servletConfig;
81
82     /**
83      * Set the {@link AuthorizationProvider}
84      *
85      * @param authorizationProvider {@link AuthorizationProvider}
86      */

87     public void setAuthorizationProvider(AuthorizationProvider authorizationProvider) {
88         _authorizationProvider = authorizationProvider;
89     }
90
91     /**
92      * Set the {@link Fetcher}
93      *
94      * @param fetcher {@link Fetcher}
95      */

96     public void setFetcher(Fetcher fetcher) {
97         _fetcher = fetcher;
98     }
99
100     /**
101      * Set the {@link Blog}
102      *
103      * @param blog {@link Blog}
104      */

105     public void setBlog(Blog blog) {
106         _blog = blog;
107     }
108
109     /**
110      * Set the {@link HttpServletRequest}
111      *
112      * @param httpServletRequest {@link HttpServletRequest}
113      */

114     public void setHttpServletRequest(HttpServletRequest JavaDoc httpServletRequest) {
115         _httpServletRequest = httpServletRequest;
116     }
117
118     /**
119      * Set the {@link HttpServletResponse}
120      *
121      * @param httpServletResponse {@link HttpServletResponse}
122      */

123     public void setHttpServletResponse(HttpServletResponse JavaDoc httpServletResponse) {
124         _httpServletResponse = httpServletResponse;
125     }
126
127     /**
128      * Set the {@link EventBroadcaster}
129      *
130      * @param eventBroadcaster {@link EventBroadcaster}
131      */

132     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
133         _eventBroadcaster = eventBroadcaster;
134     }
135
136     /**
137      * Set the {@link ServletConfig}
138      *
139      * @param servletConfig {@link ServletConfig}
140      */

141     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
142         _servletConfig = servletConfig;
143     }
144
145     /**
146      * Set the properties for the handler
147      *
148      * @param properties Handler properties
149      */

150     public void setProperties(Properties JavaDoc properties) {
151         _properties = properties;
152     }
153
154     /**
155      * Retrieve the API handler name
156      *
157      * @return API handler name
158      */

159     public abstract String JavaDoc getName();
160
161     /**
162      * Check XML-RPC permissions for a given username
163      *
164      * @param username Username
165      * @param permission Permisison to check
166      * @throws org.apache.xmlrpc.XmlRpcException If the username does not have the required permission
167      */

168     protected void checkXMLRPCPermission(String JavaDoc username, String JavaDoc permission) throws XmlRpcException {
169         try {
170             _authorizationProvider.checkPermission(_blog, new HashMap JavaDoc(), username, permission);
171         } catch (AuthorizationException e) {
172             throw new XmlRpcException(PERMISSION_EXCEPTION, PERMISSION_EXCEPTION_MSG);
173         }
174     }
175 }
176
Popular Tags