Skip to content

Commit 596d29e

Browse files
author
Igor Polevoy
committed
#272 Controller's inherited public methods are accessible via requests
1 parent bfdae69 commit 596d29e

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

activeweb/src/main/java/org/javalite/activeweb/ActionNotFoundException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public class ActionNotFoundException extends WebException{
2323
public ActionNotFoundException(Throwable cause) {
2424
super(cause);
2525
}
26+
27+
public ActionNotFoundException(String message) {
28+
super(message);
29+
}
2630
}

activeweb/src/main/java/org/javalite/activeweb/ControllerRunner.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
22
Copyright 2009-2014 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
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.
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.
1515
*/
1616
package org.javalite.activeweb;
1717

@@ -33,7 +33,7 @@
3333

3434
/**
3535
* One of the main classes of the framework, responsible for execution of controllers and filters.
36-
*
36+
*
3737
* @author Igor Polevoy
3838
*/
3939
class ControllerRunner {
@@ -193,6 +193,9 @@ private void processFlash() {
193193
}
194194
}
195195

196+
/**
197+
* Checks if the action method supports requested HTTP method
198+
*/
196199
private boolean checkActionMethod(AppController controller, String actionMethod) {
197200
HttpMethod method = HttpMethod.getMethod(Context.getHttpRequest());
198201
if (!controller.actionSupportsHttpMethod(actionMethod, method)) {
@@ -201,6 +204,8 @@ private boolean checkActionMethod(AppController controller, String actionMethod)
201204
res.setStatus(405);
202205
logger.warn("Requested action does not support HTTP method: " + method.name() + ", returning status code 405.");
203206
Context.setControllerResponse(res);
207+
208+
//TODO: candidate for caching below, list of allowed HTTP methods
204209
//see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
205210
Context.getHttpResponse().setHeader("Allow", join(controller.allowedActions(actionMethod), ", "));
206211
return false;
@@ -287,10 +292,14 @@ private void filterAfter(Route route, List<ControllerRegistry.FilterList> global
287292
private void executeAction(Object controller, String actionName) {
288293
try{
289294
Method m = controller.getClass().getMethod(actionName);
295+
Class c = m.getDeclaringClass();
296+
if(!AppController.class.isAssignableFrom(m.getDeclaringClass())){ // see https://github.com/javalite/activeweb/issues/272
297+
throw new ActionNotFoundException("Cannot execute action '" + actionName + "' on controller: " + controller);
298+
}
290299
m.invoke(controller);
291300
}catch(InvocationTargetException e){
292301
if(e.getCause() != null && e.getCause() instanceof WebException){
293-
throw (WebException)e.getCause();
302+
throw (WebException)e.getCause();
294303
}else if(e.getCause() != null && e.getCause() instanceof RuntimeException){
295304
throw (RuntimeException)e.getCause();
296305
}else if(e.getCause() != null){

activeweb/src/test/java/org/javalite/activeweb/RequestDispatcherSpec.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,14 @@ public void shouldIgnoreBadAjaxHeader() throws IOException, ServletException {
402402
dispatcher.doFilter(request, response, filterChain);
403403
a(response.getContentAsString()).shouldBeEqual("false");
404404
}
405+
406+
@Test
407+
public void should404_OnObjectMethods() throws IOException, ServletException {
408+
request.setServletPath("/ajax/wait");
409+
request.setMethod("GET");
410+
request.addHeader("X-Requested-With", "baaad header");
411+
dispatcher.doFilter(request, response, filterChain);
412+
System.out.println(response.getStatus());
413+
System.out.println(response.getContentAsString());
414+
}
405415
}

0 commit comments

Comments
 (0)