dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #18224
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7605: Web api abstract controller error handling
------------------------------------------------------------
revno: 7605
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-07-13 22:45:06 +0200
message:
Web api abstract controller error handling
modified:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java 2012-06-14 10:41:32 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java 2012-07-13 20:45:06 +0000
@@ -27,6 +27,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import org.hisp.dhis.api.utils.ContextUtils;
import org.hisp.dhis.api.utils.WebUtils;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
@@ -62,7 +63,7 @@
@Autowired
protected IdentifiableObjectManager manager;
-
+
//--------------------------------------------------------------------------
// GET
//--------------------------------------------------------------------------
@@ -88,11 +89,18 @@
}
@RequestMapping( value = "/{uid}", method = RequestMethod.GET )
- public String getObject( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
+ public String getObject( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters,
+ Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
{
WebOptions options = new WebOptions( parameters );
T entity = getEntity( uid );
+ if ( entity == null )
+ {
+ ContextUtils.notFoundResponse( response, "Object not found for uid: " + uid );
+ return null;
+ }
+
if ( options.hasLinks() )
{
WebUtils.generateLinks( entity );
@@ -104,13 +112,19 @@
return StringUtils.uncapitalize( getEntitySimpleName() );
}
- // FIXME proper error handling?
@RequestMapping( value = "/search/{query}", method = RequestMethod.GET )
- public String search( @PathVariable String query, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
+ public String search( @PathVariable String query, @RequestParam Map<String, String> parameters,
+ Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
{
WebOptions options = new WebOptions( parameters );
T entity = manager.search( getEntityClass(), query );
+ if ( entity == null )
+ {
+ ContextUtils.notFoundResponse( response, "Object not found for query: " + query );
+ return null;
+ }
+
if ( options.hasLinks() )
{
WebUtils.generateLinks( entity );
@@ -256,15 +270,18 @@
try
{
return (T) Class.forName( getEntityName() ).newInstance();
- } catch ( InstantiationException e )
- {
- throw new RuntimeException( e );
- } catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- } catch ( ClassNotFoundException e )
- {
- throw new RuntimeException( e );
+ }
+ catch ( InstantiationException ex )
+ {
+ throw new RuntimeException( ex );
+ }
+ catch ( IllegalAccessException ex )
+ {
+ throw new RuntimeException( ex );
+ }
+ catch ( ClassNotFoundException ex )
+ {
+ throw new RuntimeException( ex );
}
}
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java 2012-04-15 20:21:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java 2012-07-13 20:45:06 +0000
@@ -125,24 +125,36 @@
}
public static void conflictResponse( HttpServletResponse response, String message )
- throws IOException
{
response.setStatus( HttpServletResponse.SC_CONFLICT );
- response.setContentType( CONTENT_TYPE_TEXT );
-
- PrintWriter writer = response.getWriter();
- writer.println( message );
- writer.flush();
+ printResponse( response, message );
}
public static void okResponse( HttpServletResponse response, String message )
- throws IOException
{
response.setStatus( HttpServletResponse.SC_OK );
+ printResponse( response, message );
+ }
+
+ public static void notFoundResponse( HttpServletResponse response, String message )
+ {
+ response.setStatus( HttpServletResponse.SC_NOT_FOUND );
+ printResponse( response, message );
+ }
+
+ private static void printResponse( HttpServletResponse response, String message )
+ {
response.setContentType( CONTENT_TYPE_TEXT );
-
- PrintWriter writer = response.getWriter();
- writer.println( message );
- writer.flush();
+
+ try
+ {
+ PrintWriter writer = response.getWriter();
+ writer.println( message );
+ writer.flush();
+ }
+ catch ( IOException ex )
+ {
+ // Ignore
+ }
}
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java 2012-06-25 19:23:38 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/WebUtils.java 2012-07-13 20:45:06 +0000
@@ -190,7 +190,8 @@
try
{
port = Integer.parseInt( xForwardedPort );
- } catch ( NumberFormatException e )
+ }
+ catch ( NumberFormatException e )
{
port = request.getServerPort();
}