Просмотр исходного кода

fix jsonprettyprint flag for rest servies

Jason Rivard 5 лет назад
Родитель
Сommit
154383891f

+ 7 - 5
data-service/src/main/java/password/pwm/receiver/TelemetryRestReceiver.java

@@ -24,6 +24,7 @@ import password.pwm.bean.TelemetryPublishBean;
 import password.pwm.error.ErrorInformation;
 import password.pwm.error.PwmError;
 import password.pwm.error.PwmUnrecoverableException;
+import password.pwm.http.PwmHttpRequestWrapper;
 import password.pwm.i18n.Message;
 import password.pwm.util.ServletUtility;
 import password.pwm.util.java.JsonUtil;
@@ -49,23 +50,24 @@ public class TelemetryRestReceiver extends HttpServlet
     protected void doPost( final HttpServletRequest req, final HttpServletResponse resp )
             throws ServletException, IOException
     {
+        final boolean jsonPrettyPrint = PwmHttpRequestWrapper.isPrettyPrintJsonParameterTrue( req );
         try
         {
             resp.setHeader( "Content", "application/json" );
             final String input = ServletUtility.readRequestBodyAsString( req, 1024 * 1024 );
             final TelemetryPublishBean telemetryPublishBean = JsonUtil.deserialize( input, TelemetryPublishBean.class );
-            final Storage stoage = ContextManager.getContextManager( this.getServletContext() ).getApp().getStorage();
-            stoage.store( telemetryPublishBean );
-            resp.getWriter().print( RestResultBean.forSuccessMessage( null, null, null, Message.Success_Unknown ).toJson() );
+            final Storage storage = ContextManager.getContextManager( this.getServletContext() ).getApp().getStorage();
+            storage.store( telemetryPublishBean );
+            resp.getWriter().print( RestResultBean.forSuccessMessage( null, null, null, Message.Success_Unknown ).toJson( jsonPrettyPrint ) );
         }
         catch ( final PwmUnrecoverableException e )
         {
-            resp.getWriter().print( RestResultBean.fromError( e.getErrorInformation() ).toJson() );
+            resp.getWriter().print( RestResultBean.fromError( e.getErrorInformation() ).toJson( jsonPrettyPrint ) );
         }
         catch ( final Exception e )
         {
             final RestResultBean restResultBean = RestResultBean.fromError( new ErrorInformation( PwmError.ERROR_INTERNAL, e.getMessage() ) );
-            resp.getWriter().print( restResultBean.toJson() );
+            resp.getWriter().print( restResultBean.toJson( jsonPrettyPrint ) );
         }
     }
 }

+ 1 - 0
server/src/main/java/password/pwm/PwmConstants.java

@@ -155,6 +155,7 @@ public abstract class PwmConstants
     public static final String PARAM_SIGNED_FORM = "signedForm";
     public static final String PARAM_USERKEY = "userKey";
     public static final String PARAM_METHOD_CHOICE = "methodChoice";
+    public static final String PARAM_FORMAT_JSON_PRETTY = "jsonPrettyFormat";
 
 
     public static final String COOKIE_PERSISTENT_CONFIG_LOGIN = "CONFIG-AUTH";

+ 10 - 0
server/src/main/java/password/pwm/http/PwmHttpRequestWrapper.java

@@ -563,5 +563,15 @@ public class PwmHttpRequestWrapper
             throw e;
         }
     }
+
+    public static boolean isPrettyPrintJsonParameterTrue( final HttpServletRequest request )
+    {
+        return Boolean.parseBoolean( request.getParameter( PwmConstants.PARAM_FORMAT_JSON_PRETTY ) );
+    }
+
+    public boolean isPrettyPrintJsonParameterTrue()
+    {
+        return isPrettyPrintJsonParameterTrue( this.getHttpServletRequest() );
+    }
 }
 

+ 1 - 1
server/src/main/java/password/pwm/http/PwmResponse.java

@@ -218,7 +218,7 @@ public class PwmResponse extends PwmHttpResponseWrapper
     {
         preCommitActions();
         final HttpServletResponse resp = this.getHttpServletResponse();
-        final String outputString = restResultBean.toJson();
+        final String outputString = restResultBean.toJson( pwmRequest.isPrettyPrintJsonParameterTrue() );
         resp.setContentType( HttpContentType.json.getHeaderValueWithEncoding() );
         resp.getWriter().print( outputString );
         resp.getWriter().close();

+ 4 - 2
server/src/main/java/password/pwm/ws/server/RestResultBean.java

@@ -219,8 +219,10 @@ public class RestResultBean implements Serializable
     }
 
 
-    public String toJson( )
+    public String toJson( final boolean prettyPrintJson )
     {
-        return JsonUtil.serialize( this, JsonUtil.Flag.PrettyPrint ) + "\n";
+        return prettyPrintJson
+                ? JsonUtil.serialize( this, JsonUtil.Flag.PrettyPrint ) + "\n"
+                : JsonUtil.serialize( this );
     }
 }

+ 2 - 9
server/src/main/java/password/pwm/ws/server/RestServlet.java

@@ -363,17 +363,10 @@ public abstract class RestServlet extends HttpServlet
                 case json:
                 {
                     resp.setHeader( HttpHeader.ContentType.getHttpName(), HttpContentType.json.getHeaderValueWithEncoding() );
-                    final String formatParameter = request.getParameter( "format" );
+                    final boolean jsonPretty = Boolean.parseBoolean( request.getParameter( PwmConstants.PARAM_FORMAT_JSON_PRETTY ) );
                     try ( PrintWriter pw = resp.getWriter() )
                     {
-                        if ( "pretty".equalsIgnoreCase( formatParameter ) )
-                        {
-                            pw.write( JsonUtil.serialize( restResultBean, JsonUtil.Flag.PrettyPrint ) );
-                        }
-                        else
-                        {
-                            pw.write( restResultBean.toJson() );
-                        }
+                        pw.write( restResultBean.toJson( jsonPretty ) );
                     }
                 }
                 break;