You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by sm...@apache.org on 2020/03/15 19:13:21 UTC

[directory-fortress-core] branch master updated: FC-279 - Fix RestUtils error mapping for HTTP errors

This is an automated email from the ASF dual-hosted git repository.

smckinney pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-fortress-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 302395b  FC-279 - Fix RestUtils error mapping for HTTP errors
302395b is described below

commit 302395bfdb28ef43c4a87a89fbc2acc7b7a20285
Author: Shawn McKinney <sm...@symas.com>
AuthorDate: Sun Mar 15 14:13:15 2020 -0500

    FC-279 - Fix RestUtils error mapping for HTTP errors
---
 .../directory/fortress/core/GlobalErrIds.java      | 12 +++
 .../directory/fortress/core/rest/RestUtils.java    | 38 +++++++--
 .../directory/fortress/core/ReviewMgrConsole.java  | 90 +++++++++++-----------
 3 files changed, 92 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/apache/directory/fortress/core/GlobalErrIds.java b/src/main/java/org/apache/directory/fortress/core/GlobalErrIds.java
index f90ea7d..f0e2da2 100755
--- a/src/main/java/org/apache/directory/fortress/core/GlobalErrIds.java
+++ b/src/main/java/org/apache/directory/fortress/core/GlobalErrIds.java
@@ -1648,6 +1648,18 @@ public final class GlobalErrIds
      * The REST function could not get handle to HTTP Request.
      */
     public static final int REST_NULL_HTTP_REQ_ERR = 10110;
+
+    /**
+     * The REST function failed with an HTTP 500 Internal error.
+     */
+    public static final int REST_INTERNAL_ERR = 10111;
+
+    /**
+     * The REST function failed with an HTTP 400 Validation Exception.
+     */
+    public static final int REST_VALIDATION_ERR = 10112;
+
+
     /**
      * 10200's - RBAC Accelerator Error Ids
      */
diff --git a/src/main/java/org/apache/directory/fortress/core/rest/RestUtils.java b/src/main/java/org/apache/directory/fortress/core/rest/RestUtils.java
index 9906bdd..83c7507 100644
--- a/src/main/java/org/apache/directory/fortress/core/rest/RestUtils.java
+++ b/src/main/java/org/apache/directory/fortress/core/rest/RestUtils.java
@@ -80,6 +80,7 @@ public final class RestUtils
     private static final int HTTP_403_FORBIDDEN = 403;
     private static final int HTTP_404_NOT_FOUND = 404;
     private static final int HTTP_500_INTERNAL_SERVER_ERROR = 500;
+    private static final String VALID_RESPONSE = "FortResponse";
     private static CachedJaxbContext cachedJaxbContext = new CachedJaxbContext();
 
     // static member contains this
@@ -315,7 +316,7 @@ public final class RestUtils
             {
                 case HTTP_OK :
                     szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
-                    if( StringUtils.isNotEmpty( szResponse ) )
+                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                     {
                         LOG.debug( "post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                     }
@@ -335,12 +336,11 @@ public final class RestUtils
                     LOG.error( error );
                     throw new RestException( GlobalErrIds.REST_FORBIDDEN_ERR, error );
                 case HTTP_404_NOT_FOUND:
-                case HTTP_500_INTERNAL_SERVER_ERROR:
-                case HTTP_400_VALIDATION_EXCEPTION:
                     szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
-                    if( StringUtils.isNotEmpty( szResponse ) )
+                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
+                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                     {
-                        LOG.debug( "post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
+                        LOG.debug( "HTTP: 404: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                     }
                     else
                     {
@@ -349,6 +349,34 @@ public final class RestUtils
                         throw new RestException( GlobalErrIds.REST_NOT_FOUND_ERR, error );
                     }
                     break;
+                case HTTP_500_INTERNAL_SERVER_ERROR:
+                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
+                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
+                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
+                    {
+                        LOG.debug( "HTTP 500: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
+                    }
+                    else
+                    {
+                        error = generateErrorMessage( uri, function, "HTTP 500 Internal Error:" + response.getStatusLine().getStatusCode());
+                        LOG.error( error );
+                        throw new RestException( GlobalErrIds.REST_INTERNAL_ERR, error );
+                    }
+                    break;
+                case HTTP_400_VALIDATION_EXCEPTION:
+                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
+                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
+                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
+                    {
+                        LOG.debug( "HTTP 400: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
+                    }
+                    else
+                    {
+                        error = generateErrorMessage( uri, function, "HTTP 400 Validation Error:" + response.getStatusLine().getStatusCode());
+                        LOG.error( error );
+                        throw new RestException( GlobalErrIds.REST_VALIDATION_ERR, error );
+                    }
+                    break;
                 default :
                     error = generateErrorMessage( uri, function, "error received from host: " + response.getStatusLine().getStatusCode() );
                     LOG.error( error );
diff --git a/src/test/java/org/apache/directory/fortress/core/ReviewMgrConsole.java b/src/test/java/org/apache/directory/fortress/core/ReviewMgrConsole.java
index d936667..48da4b3 100755
--- a/src/test/java/org/apache/directory/fortress/core/ReviewMgrConsole.java
+++ b/src/test/java/org/apache/directory/fortress/core/ReviewMgrConsole.java
@@ -207,60 +207,64 @@ class ReviewMgrConsole
         String userVal;
 
         ReaderUtil.clearScreen();
-        try
-        {
-            System.out.println( "Enter User Search Value" );
+        try {
+            System.out.println("Enter User Search Value");
             userVal = ReaderUtil.readLn();
             User ue = new User();
-            ue.setUserId( userVal );
-            ArrayList list = ( ArrayList ) rm.findUsers( ue );
-            int size = list.size();
-
-            for ( int i = 0; i < size; i++ )
+            ue.setUserId(userVal);
+            ArrayList list = (ArrayList) rm.findUsers(ue);
+            if (list != null)
             {
-                ue = ( User ) list.get( i );
-                System.out.println( "USER[" + i + "]" );
-                System.out.println( "    userId      [" + ue.getUserId() + "]" );
-                System.out.println( "    internalId  [" + ue.getInternalId() + "]" );
-                System.out.println( "    description [" + ue.getDescription() + "]" );
-                System.out.println( "    common name [" + ue.getCn() + "]" );
-                System.out.println( "    surname     [" + ue.getSn() + "]" );
-                System.out.println( "    orgUnitId   [" + ue.getOu() + "]" );
-                System.out.println( "    pwpolicy    [" + ue.getPwPolicy() + "]" );
-                System.out.println( "    seqId       [" + ue.getSequenceId() + "]" );
-                printTemporal( ue, "USER" );
-                printPosixAccount( ue, "POSIX" );
-                printAddress( ue.getAddress(), "ADDRESS" );
-                printPhone( ue.getPhones(), "PHONES" );
-                printPhone( ue.getMobiles(), "MOBILES" );
-                if ( ue.getRoles() != null )
+                int size = list.size();
+                for (int i = 0; i < size; i++)
                 {
-                    for ( UserRole ur : ue.getRoles() )
+                    ue = (User) list.get(i);
+                    System.out.println("USER[" + i + "]");
+                    System.out.println("    userId      [" + ue.getUserId() + "]");
+                    System.out.println("    internalId  [" + ue.getInternalId() + "]");
+                    System.out.println("    description [" + ue.getDescription() + "]");
+                    System.out.println("    common name [" + ue.getCn() + "]");
+                    System.out.println("    surname     [" + ue.getSn() + "]");
+                    System.out.println("    orgUnitId   [" + ue.getOu() + "]");
+                    System.out.println("    pwpolicy    [" + ue.getPwPolicy() + "]");
+                    System.out.println("    seqId       [" + ue.getSequenceId() + "]");
+                    printTemporal(ue, "USER");
+                    printPosixAccount(ue, "POSIX");
+                    printAddress(ue.getAddress(), "ADDRESS");
+                    printPhone(ue.getPhones(), "PHONES");
+                    printPhone(ue.getMobiles(), "MOBILES");
+                    if (ue.getRoles() != null)
                     {
-                        printTemporal( ur, "RBACROLE" );
+                        for (UserRole ur : ue.getRoles())
+                        {
+                            printTemporal(ur, "RBACROLE");
+                        }
                     }
-                }
-                if ( ue.getAdminRoles() != null )
-                {
-                    for ( UserAdminRole ur : ue.getAdminRoles() )
+                    if (ue.getAdminRoles() != null)
                     {
-                        printAdminRole( ur );
-                        printTemporal( ur, "ADMINROLE" );
+                        for (UserAdminRole ur : ue.getAdminRoles()) {
+                            printAdminRole(ur);
+                            printTemporal(ur, "ADMINROLE");
+                        }
                     }
-                }
-                if ( ue.getProperties() != null && ue.getProperties().size() > 0 )
-                {
-                    int ctr = 0;
-                    for ( Enumeration e = ue.getProperties().propertyNames(); e.hasMoreElements(); )
+                    if (ue.getProperties() != null && ue.getProperties().size() > 0)
                     {
-                        String key = ( String ) e.nextElement();
-                        String val = ue.getProperty( key );
-                        System.out.println( "prop key[" + ctr + "]=" + key );
-                        System.out.println( "prop value[" + ctr++ + "]=" + val );
+                        int ctr = 0;
+                        for (Enumeration e = ue.getProperties().propertyNames(); e.hasMoreElements(); )
+                        {
+                            String key = (String) e.nextElement();
+                            String val = ue.getProperty(key);
+                            System.out.println("prop key[" + ctr + "]=" + key);
+                            System.out.println("prop value[" + ctr++ + "]=" + val);
+                        }
                     }
-                }
 
-                System.out.println();
+                    System.out.println();
+                }
+            }
+            else
+            {
+                System.out.println("User not found");
             }
             System.out.println( "ENTER to continue" );
         }