File Source: Handler.java
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.weblogger.webservices.adminprotocol;
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import javax.servlet.http.HttpServletRequest;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.weblogger.config.WebloggerConfig;
26 import org.apache.roller.weblogger.business.Weblogger;
27 import org.apache.roller.weblogger.business.WebloggerFactory;
28 import org.apache.roller.weblogger.webservices.adminprotocol.sdk.EntrySet;
29 import java.util.regex.Pattern;
30 import java.util.regex.Matcher;
31 import org.apache.roller.weblogger.WebloggerException;
32 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
33 import org.apache.roller.weblogger.business.UserManager;
34 import org.apache.roller.weblogger.pojos.User;
35 import org.apache.roller.weblogger.pojos.Weblog;
36 import org.apache.roller.weblogger.webservices.adminprotocol.sdk.UnexpectedRootElementException;
37 import org.jdom.Document;
38 import org.jdom.JDOMException;
39 import org.jdom.input.SAXBuilder;
40
41 /**
42 * This class is the abstract notion of an RAP request handler.
43 * It processes HTTP requests for each of the four HTTP verbs:
44 * GET, POST, PUT, DELETE, for a given weblog resource.
45 */
46 abstract class Handler {
47 protected static final String ENDPOINT = "/rap";
48
49 static class URI {
/*
P/P * Method: org.apache.roller.weblogger.webservices.adminprotocol.Handler$URI__static_init
*
* Postconditions:
* init'ed(PATHINFO_PATTERN)
*/
50 private static Pattern PATHINFO_PATTERN = Pattern.compile("^/(users|weblogs|members)(?:/(.*))?$");
51
52 private String type;
53 private String entryId;
54
/*
P/P * Method: void org.apache.roller.weblogger.webservices.adminprotocol.Handler$URI(HttpServletRequest)
*
* Preconditions:
* request != null
* (soft) PATHINFO_PATTERN != null
*
* Presumptions:
* java.util.regex.Matcher:matches(...)@63 == 1
* java.util.regex.Pattern:matcher(...)@62 != null
*
* Postconditions:
* init'ed(this.entryId)
* init'ed(this.type)
*
* Test Vectors:
* java.lang.String:length(...)@58: {1..232-1}, {0}
* javax.servlet.http.HttpServletRequest:getPathInfo(...)@56: Addr_Set{null}, Inverse{null}
*/
55 public URI(HttpServletRequest request) throws BadRequestException {
56 String pi = request.getPathInfo();
57
58 if (pi == null || pi.length() == 0) {
59 type = null;
60 entryId = null;
61 } else {
62 Matcher m = PATHINFO_PATTERN.matcher(pi);
63 if (!m.matches()) {
64 throw new BadRequestException("ERROR: Invalid path info: " + pi);
65 }
66
67 type = m.group(1);
68 entryId = m.group(2);
69 }
70 }
71
72 public String getType() {
/*
P/P * Method: String getType()
*
* Preconditions:
* init'ed(this.type)
*
* Postconditions:
* return_value == this.type
* init'ed(return_value)
*/
73 return type;
74 }
75
76 public String getEntryId() {
/*
P/P * Method: String getEntryId()
*
* Preconditions:
* init'ed(this.entryId)
*
* Postconditions:
* return_value == this.entryId
* init'ed(return_value)
*/
77 return entryId;
78 }
79
80 public boolean isIntrospection() {
/*
P/P * Method: bool isIntrospection()
*
* Preconditions:
* init'ed(this.entryId)
* (soft) init'ed(this.type)
*
* Postconditions:
* init'ed(return_value)
*/
81 return getEntryId() == null && type == null;
82 }
83
84 public boolean isCollection() {
/*
P/P * Method: bool isCollection()
*
* Preconditions:
* init'ed(this.entryId)
* (soft) init'ed(this.type)
*
* Postconditions:
* init'ed(return_value)
*/
85 return getEntryId() == null && type != null;
86 }
87
88 public boolean isEntry() {
/*
P/P * Method: bool isEntry()
*
* Preconditions:
* init'ed(this.entryId)
* (soft) init'ed(this.type)
*
* Postconditions:
* init'ed(return_value)
*/
89 return getEntryId() != null && type != null;
90 }
91 }
92
/*
P/P * Method: org.apache.roller.weblogger.webservices.adminprotocol.Handler__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getFactory(...)@93 != null
*
* Postconditions:
* init'ed(logger)
*/
93 protected static final Log logger = LogFactory.getFactory().getInstance(Handler.class);
94
95 private HttpServletRequest request;
96 private Weblogger roller;
97 private String userName;
98 private URI uri;
99 private String urlPrefix;
100
101 /** Get a Handler object implementation based on the given request. */
102 public static Handler getHandler(HttpServletRequest req) throws HandlerException {
/*
P/P * Method: Handler getHandler(HttpServletRequest)
*
* Preconditions:
* req != null
* (soft) org/apache/roller/weblogger/webservices/adminprotocol/Handler$URI.PATHINFO_PATTERN != null
*
* Presumptions:
* java.lang.String:equals(...)@119 == 1
* org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@103 == 1
*
* Postconditions:
* init'ed(java.lang.String:substring(...)._tainted)
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value in Addr_Set{&new RollerMemberHandler(getHandler#5),&new RollerUserHandler(getHandler#4),&new RollerWeblogHandler(getHandler#3),&new IntrospectionHandler(getHandler#2)}
* new Handler$URI(Handler#1) num objects == 1
* possibly_updated(new Handler$URI(Handler#1).entryId)
* possibly_updated(new Handler$URI(Handler#1).type)
* new IntrospectionHandler(getHandler#2) num objects <= 1
* new IntrospectionHandler(getHandler#2).request == req
* new IntrospectionHandler(getHandler#2).request != null
* init'ed(new IntrospectionHandler(getHandler#2).roller)
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@115: {0}, {1}
* java.lang.String:equals(...)@117: {0}, {1}
*/
103 boolean enabled = WebloggerConfig.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 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
/*
P/P * Method: void org.apache.roller.weblogger.webservices.adminprotocol.Handler(HttpServletRequest)
*
* Preconditions:
* request != null
* (soft) org/apache/roller/weblogger/webservices/adminprotocol/Handler$URI.PATHINFO_PATTERN != null
*
* Presumptions:
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@40 != null
*
* Postconditions:
* init'ed(java.lang.String:substring(...)._tainted)
* java.lang.StringBuilder:toString(...)._tainted == 0
* this.request == request
* this.request != null
* init'ed(this.roller)
* this.uri == &new Handler$URI(Handler#1)
* this.urlPrefix == &java.lang.StringBuilder:toString(...)
* this.userName == One-of{null, &java.lang.String:substring(...)}
* this.userName in Addr_Set{null,&java.lang.String:substring(...)}
* new Handler$URI(Handler#1) num objects == 1
* ...
*/
131 public Handler(HttpServletRequest request) throws HandlerException {
132 this.request = request;
133 this.uri = new URI(request);
134 this.roller = WebloggerFactory.getWeblogger();
135 //TODO: is this the right thing to do? hardcode roller-services?
136 this.urlPrefix = WebloggerRuntimeConfig.getAbsoluteContextURL() + "/roller-services" + ENDPOINT;
137
138 // TODO: decide what to do about authentication, is WSSE going to fly?
139 //Authenticator auth = new WSSEAuthenticator(request);
140 Authenticator auth = new BasicAuthenticator(request);
141 auth.authenticate();
142 setUserName(auth.getUserName());
143 }
144
145 /**
146 * Get the authenticated user name.
147 * If this method returns null, then authentication has failed.
148 */
149 public String getUserName() {
/*
P/P * Method: String getUserName()
*
* Preconditions:
* init'ed(this.userName)
*
* Postconditions:
* return_value == this.userName
* init'ed(return_value)
*/
150 return userName;
151 }
152
153 private void setUserName(String userName) {
/*
P/P * Method: void setUserName(String)
*
* Postconditions:
* this.userName == userName
* init'ed(this.userName)
*/
154 this.userName = userName;
155 }
156
157 /** Process an HTTP GET request. */
158 public abstract EntrySet processGet() throws HandlerException;
159 /** Process an HTTP POST request. */
160 public abstract EntrySet processPost(Reader r) throws HandlerException;
161 /** Process an HTTP PUT request. */
162 public abstract EntrySet processPut(Reader r) throws HandlerException;
163 /** Process an HTTP DELETE request. */
164 public abstract EntrySet processDelete() throws HandlerException;
165
166 protected URI getUri() {
/*
P/P * Method: Handler$URI getUri()
*
* Preconditions:
* init'ed(this.uri)
*
* Postconditions:
* return_value == this.uri
* init'ed(return_value)
*/
167 return uri;
168 }
169
170 protected HttpServletRequest getRequest() {
/*
P/P * Method: HttpServletRequest getRequest()
*
* Preconditions:
* init'ed(this.request)
*
* Postconditions:
* return_value == this.request
* init'ed(return_value)
*/
171 return request;
172 }
173
174 protected Weblogger getRoller() {
/*
P/P * Method: Weblogger getRoller()
*
* Preconditions:
* init'ed(this.roller)
*
* Postconditions:
* return_value == this.roller
* init'ed(return_value)
*/
175 return roller;
176 }
177
178 protected String getUrlPrefix() {
/*
P/P * Method: String getUrlPrefix()
*
* Preconditions:
* init'ed(this.urlPrefix)
*
* Postconditions:
* return_value == this.urlPrefix
* init'ed(return_value)
*/
179 return urlPrefix;
180 }
181
182 protected abstract EntrySet getEntrySet(Document d) throws UnexpectedRootElementException;
183
184 protected EntrySet getEntrySet(Reader r) throws HandlerException {
185 try {
/*
P/P * Method: EntrySet getEntrySet(Reader)
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* init'ed(return_value)
* new MemberEntrySet(getEntrySet#1*) num objects <= 1
* init'ed(new MemberEntrySet(getEntrySet#1*).entries)
* init'ed(new MemberEntrySet(getEntrySet#1*).href)
* new UserEntrySet(getEntrySet#1*) num objects <= 1
* init'ed(new UserEntrySet(getEntrySet#1*).entries)
* init'ed(new UserEntrySet(getEntrySet#1*).href)
* new WeblogEntrySet(getEntrySet#1*) num objects <= 1
* init'ed(new WeblogEntrySet(getEntrySet#1*).entries)
* ...
*/
186 SAXBuilder builder = new SAXBuilder();
187 Document d = builder.build(r);
188 EntrySet c = getEntrySet(d);
189
190 return c;
191 } catch (UnexpectedRootElementException ure) {
192 throw new BadRequestException("ERROR: Failed to parse content", ure);
193 } catch (JDOMException jde) {
194 throw new BadRequestException("ERROR: Failed to parse content", jde);
195 } catch (IOException ioe) {
196 throw new InternalException("ERROR: Failed to parse content", ioe);
197 }
198 }
199
200 protected Weblog getWebsiteData(String handle) throws NotFoundException, InternalException {
201 try {
/*
P/P * Method: Weblog getWebsiteData(String)
*
* Preconditions:
* this.roller != null
*
* Presumptions:
* init'ed(java.lang.Boolean.FALSE)
* init'ed(java.lang.Boolean.TRUE)
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@202 != null
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@204 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle(...)@202: Inverse{null}, Addr_Set{null}
*/
202 Weblog wd = getRoller().getUserManager().getWebsiteByHandle(handle, Boolean.TRUE);
203 if (wd == null) {
204 wd = getRoller().getUserManager().getWebsiteByHandle(handle, Boolean.FALSE);
205 }
206 if (wd == null) {
+ 207 throw new NotFoundException("ERROR: Unknown weblog handle: " + handle);
208 }
209
210 return wd;
211 } catch (WebloggerException re) {
212 throw new InternalException("ERROR: Could not get weblog: " + handle, re);
213 }
214 }
215
216 protected User getUserData(String name) throws NotFoundException, InternalException {
217 try {
/*
P/P * Method: User getUserData(String)
*
* Preconditions:
* this.roller != null
*
* Presumptions:
* init'ed(java.lang.Boolean.FALSE)
* init'ed(java.lang.Boolean.TRUE)
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@218 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* org.apache.roller.weblogger.business.UserManager:getUserByUserName(...)@219: Inverse{null}, Addr_Set{null}
*/
218 UserManager mgr = getRoller().getUserManager();
219 User ud = mgr.getUserByUserName(name, Boolean.TRUE);
220 if (ud == null) {
221 ud = mgr.getUserByUserName(name, Boolean.FALSE);
222 }
223 if (ud == null) {
+ 224 throw new NotFoundException("ERROR: Unknown user: " + name);
225 }
226
227 return ud;
228 } catch (WebloggerException re) {
229 throw new InternalException("ERROR: Could not get user: " + name, re);
230 }
231 }
232
233 }
234
SofCheck Inspector Build Version : 2.18479
| Handler.java |
2009-Jan-02 14:24:58 |
| Handler.class |
2009-Sep-04 03:12:45 |
| Handler$URI.class |
2009-Sep-04 03:12:45 |