Skip to content

Commit 99c38de

Browse files
committed
Return HTTP 403 and 409 for IllegalArgumentException and IllegalStateException, respectively.
1 parent 7dad5b9 commit 99c38de

File tree

4 files changed

+23
-54
lines changed

4 files changed

+23
-54
lines changed

README.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,8 @@ For example, a service might use the request to get the name of the current user
250250

251251
The response object can also be used to produce a custom result. If a service method commits the response by writing to the output stream, the method's return value (if any) will be ignored by `WebService`. This allows a service to return content that cannot be easily represented as JSON, such as image data or other response formats such as XML.
252252

253-
### Authorization
254-
Service requests can be authorized by overriding the following method:
255-
256-
```java
257-
protected boolean isAuthorized(HttpServletRequest request, Method method) { ... }
258-
```
259-
260-
The first argument contains the current request, and the second the service method to be invoked. If `isAuthorized()` returns `true` (the default), method execution will proceed. Otherwise, the method will not be invoked, and an HTTP 403 response will be returned.
261-
262253
### Exceptions
263-
If any exception is thrown by a service method, an HTTP 500 response will be returned. If the response has not yet been committed, the exception message will be returned as plain text in the response body. This allows a service to provide the caller with insight into the cause of the failure. For example:
264-
265-
```java
266-
@RequestMethod("GET")
267-
@ResourcePath("error")
268-
public void generateError() throws Exception {
269-
throw new Exception("This is an error message.");
270-
}
271-
```
254+
If an exception is thrown by a service method and the response has not yet been committed, the exception message (if any) will be returned as plain text in the response body. If the exception is an instance of `IllegalArgumentException`, an HTTP 403 response will be returned. For `IllegalStateException`, HTTP 409 will be returned. For any other exception type, HTTP 500 will be returned.
272255

273256
### API Documentation
274257
API documentation can be viewed by appending "?api" to a service URL; for example:

httprpc-test/src/main/java/org/httprpc/test/TestService.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void testDeprecated() {
214214
@RequestMethod("GET")
215215
@ResourcePath("unauthorized")
216216
public void testUnauthorized() {
217-
// No-op
217+
throw new IllegalArgumentException();
218218
}
219219

220220
@RequestMethod("GET")
@@ -229,11 +229,4 @@ public int testTimeout(int value, int delay) throws InterruptedException {
229229

230230
return value;
231231
}
232-
233-
@Override
234-
protected boolean isAuthorized(HttpServletRequest request, Method method) {
235-
String pathInfo = request.getPathInfo();
236-
237-
return (pathInfo == null || !pathInfo.endsWith("unauthorized"));
238-
}
239232
}

httprpc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
}
2020

2121
group = 'org.httprpc'
22-
version = '6.3.5'
22+
version = '6.4'
2323

2424
repositories {
2525
mavenCentral()

httprpc/src/main/java/org/httprpc/WebService.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,6 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
302302
return;
303303
}
304304

305-
if (!isAuthorized(request, handler.method)) {
306-
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
307-
return;
308-
}
309-
310305
HashMap<String, String> keyMap = new HashMap<>();
311306

312307
for (int i = 0, n = keyList.size(); i < n; i++) {
@@ -333,13 +328,28 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
333328
Throwable cause = exception.getCause();
334329

335330
if (cause != null) {
336-
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
337-
response.setContentType(String.format("text/plain;charset=%s", UTF_8));
331+
int status;
332+
if (cause instanceof IllegalArgumentException) {
333+
status = HttpServletResponse.SC_FORBIDDEN;
334+
} else if (cause instanceof IllegalStateException) {
335+
status = HttpServletResponse.SC_CONFLICT;
336+
} else {
337+
status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
338+
}
339+
340+
response.setStatus(status);
338341

339-
PrintWriter writer = response.getWriter();
342+
String message = cause.getMessage();
340343

341-
writer.append(cause.getMessage());
342-
writer.flush();
344+
if (message != null) {
345+
response.setContentType(String.format("text/plain;charset=%s", UTF_8));
346+
347+
PrintWriter writer = response.getWriter();
348+
349+
writer.append(message);
350+
351+
writer.flush();
352+
}
343353

344354
return;
345355
} else {
@@ -570,23 +580,6 @@ protected String getKey(String name) {
570580
return keyMap.get().get(name);
571581
}
572582

573-
/**
574-
* Determines if the current request is authorized.
575-
*
576-
* @param request
577-
* The servlet request.
578-
*
579-
* @param method
580-
* The method to be invoked.
581-
*
582-
* @return
583-
* <tt>true</tt> if the method should be invoked; <tt>false</tt>,
584-
* otherwise.
585-
*/
586-
protected boolean isAuthorized(HttpServletRequest request, Method method) {
587-
return true;
588-
}
589-
590583
/**
591584
* Encodes the result of a service operation.
592585
*

0 commit comments

Comments
 (0)