KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > Handler


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 package org.apache.roller.webservices.adminapi;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.config.RollerConfig;
26 import org.apache.roller.model.Roller;
27 import org.apache.roller.model.RollerFactory;
28 import org.apache.roller.ui.core.RollerContext;
29 import org.apache.roller.webservices.adminapi.sdk.EntrySet;
30
31 import java.util.regex.Pattern JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import org.apache.roller.config.RollerRuntimeConfig;
34 import org.apache.roller.webservices.adminapi.sdk.MissingElementException;
35 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
36 import org.jdom.Document;
37 import org.jdom.JDOMException;
38 import org.jdom.input.SAXBuilder;
39
40 /**
41  * This class is the abstract notion of an AAPP request handler.
42  * It processes HTTP requests for each of the four HTTP verbs:
43  * GET, POST, PUT, DELETE, for a given weblog resource.
44  */

45 abstract class Handler {
46     protected static final String JavaDoc ENDPOINT = "/aapp";
47     
48     static class URI {
49         private static Pattern JavaDoc PATHINFO_PATTERN = Pattern.compile("^/(users|weblogs|members)(?:/(.*))?$");
50         
51         private String JavaDoc type;
52         private String JavaDoc entryId;
53         
54         public URI(HttpServletRequest JavaDoc request) throws BadRequestException {
55             String JavaDoc pi = request.getPathInfo();
56             
57             if (pi == null || pi.length() == 0) {
58                 type = null;
59                 entryId = null;
60             } else {
61                 Matcher JavaDoc m = PATHINFO_PATTERN.matcher(pi);
62                 if (!m.matches()) {
63                     throw new BadRequestException("ERROR: Invalid path info: " + pi);
64                 }
65                 
66                 type = m.group(1);
67                 entryId = m.group(2);
68             }
69         }
70         
71         public String JavaDoc getType() {
72             return type;
73         }
74         
75         public String JavaDoc getEntryId() {
76             return entryId;
77         }
78         
79         public boolean isIntrospection() {
80             return getEntryId() == null && type == null;
81         }
82         
83         public boolean isCollection() {
84             return getEntryId() == null && type != null;
85         }
86         
87         public boolean isEntry() {
88             return getEntryId() != null && type != null;
89         }
90     }
91     
92     protected static final Log logger = LogFactory.getFactory().getInstance(Handler.class);
93     
94     private HttpServletRequest JavaDoc request;
95     private Roller roller;
96     private RollerContext rollerContext;
97     private String JavaDoc userName;
98     private URI uri;
99     private String JavaDoc urlPrefix;
100     
101     /** Get a Handler object implementation based on the given request. */
102     public static Handler getHandler(HttpServletRequest JavaDoc req) throws HandlerException {
103         boolean enabled = RollerConfig.getBooleanProperty("webservices.adminprotocol.enabled");
104         if (!enabled) {
105             throw new NotAllowedException("ERROR: Admin protocol not enabled");
106         }
107         
108         URI uri = new URI(req);
109         Handler handler;
110         
111         if (uri.isIntrospection()) {
112             handler = new IntrospectionHandler(req);
113         } else if (uri.isCollection() || uri.isEntry()) {
114             String JavaDoc type = uri.getType();
115             if (type.equals(EntrySet.Types.WEBLOGS)) {
116                 handler = new RollerWeblogHandler(req);
117             } else if (type.equals(EntrySet.Types.USERS)) {
118                 handler = new RollerUserHandler(req);
119             } else if (type.equals(EntrySet.Types.MEMBERS)) {
120                 handler = new RollerMemberHandler(req);
121             } else {
122                 throw new BadRequestException("ERROR: Unknown type: " + uri.getType());
123             }
124         } else {
125             throw new BadRequestException("ERROR: Unknown URI type");
126         }
127         
128         return handler;
129     }
130     
131     public Handler(HttpServletRequest JavaDoc request) throws HandlerException {
132         this.request = request;
133         this.uri = new URI(request);
134         this.rollerContext = RollerContext.getRollerContext();
135         this.roller = RollerFactory.getRoller();
136         //TODO: is this the right thing to do? hardcode roller-services?
137
this.urlPrefix = RollerRuntimeConfig.getAbsoluteContextURL() + "/roller-services" + ENDPOINT;
138         
139         // TODO: decide what to do about authentication, is WSSE going to fly?
140
//Authenticator auth = new WSSEAuthenticator(request);
141
Authenticator auth = new BasicAuthenticator(request);
142         auth.authenticate();
143         setUserName(auth.getUserName());
144     }
145     
146     /**
147      * Get the authenticated user name.
148      * If this method returns null, then authentication has failed.
149      */

150     public String JavaDoc getUserName() {
151         return userName;
152     }
153     
154     private void setUserName(String JavaDoc userName) {
155         this.userName = userName;
156     }
157     
158     /** Process an HTTP GET request. */
159     public abstract EntrySet processGet() throws HandlerException;
160     /** Process an HTTP POST request. */
161     public abstract EntrySet processPost(Reader JavaDoc r) throws HandlerException;
162     /** Process an HTTP PUT request. */
163     public abstract EntrySet processPut(Reader JavaDoc r) throws HandlerException;
164     /** Process an HTTP DELETE request. */
165     public abstract EntrySet processDelete() throws HandlerException;
166     
167     protected URI getUri() {
168         return uri;
169     }
170     
171     protected HttpServletRequest JavaDoc getRequest() {
172         return request;
173     }
174     
175     protected RollerContext getRollerContext() {
176         return rollerContext;
177     }
178     
179     protected Roller getRoller() {
180         return roller;
181     }
182     
183     protected String JavaDoc getUrlPrefix() {
184         return urlPrefix;
185     }
186     
187     protected abstract EntrySet getEntrySet(Document d) throws MissingElementException, UnexpectedRootElementException;
188     
189     protected EntrySet getEntrySet(Reader JavaDoc r) throws HandlerException {
190         try {
191             SAXBuilder builder = new SAXBuilder();
192             Document d = builder.build(r);
193             EntrySet c = getEntrySet(d);
194             
195             return c;
196         } catch (UnexpectedRootElementException ure) {
197             throw new BadRequestException("ERROR: Failed to parse content", ure);
198         } catch (MissingElementException mee) {
199             throw new BadRequestException("ERROR: Failed to parse content", mee);
200         } catch (JDOMException jde) {
201             throw new BadRequestException("ERROR: Failed to parse content", jde);
202         } catch (IOException JavaDoc ioe) {
203             throw new InternalException("ERROR: Failed to parse content", ioe);
204         }
205     }
206 }
207
208
Popular Tags