TelemetryRestReceiver.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2021 The PWM Project
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package password.pwm.receiver;
  21. import jakarta.servlet.ServletException;
  22. import jakarta.servlet.annotation.WebServlet;
  23. import jakarta.servlet.http.HttpServlet;
  24. import jakarta.servlet.http.HttpServletRequest;
  25. import jakarta.servlet.http.HttpServletResponse;
  26. import password.pwm.PwmConstants;
  27. import password.pwm.bean.TelemetryPublishBean;
  28. import password.pwm.error.ErrorInformation;
  29. import password.pwm.error.PwmError;
  30. import password.pwm.error.PwmUnrecoverableException;
  31. import password.pwm.i18n.Message;
  32. import password.pwm.util.java.JavaHelper;
  33. import password.pwm.util.json.JsonFactory;
  34. import password.pwm.ws.server.RestResultBean;
  35. import java.io.IOException;
  36. @WebServlet(
  37. urlPatterns = {
  38. "/telemetry",
  39. }
  40. )
  41. public class TelemetryRestReceiver extends HttpServlet
  42. {
  43. private static final Logger LOGGER = Logger.createLogger( TelemetryViewerServlet.class );
  44. @Override
  45. protected void doPost( final HttpServletRequest req, final HttpServletResponse resp )
  46. throws ServletException, IOException
  47. {
  48. try
  49. {
  50. final ContextManager contextManager = ContextManager.getContextManager( req.getServletContext() );
  51. final PwmReceiverApp app = contextManager.getApp();
  52. app.getStatisticCounterBundle().increment( PwmReceiverApp.CounterStatsKey.TelemetryPublishRequests );
  53. app.getStatisticEpsBundle().markEvent( PwmReceiverApp.EpsStatKey.TelemetryPublishRequests );
  54. final String input = readRequestBodyAsString( req, 1024 * 1024 );
  55. final TelemetryPublishBean telemetryPublishBean = JsonFactory.get().deserialize( input, TelemetryPublishBean.class );
  56. final Storage storage = app.getStorage();
  57. storage.store( telemetryPublishBean );
  58. final RestResultBean restResultBean = RestResultBean.forSuccessMessage( null, null, null, Message.Success_Unknown );
  59. ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
  60. LOGGER.debug( () -> "http telemetry rest data received from " + telemetryPublishBean.getSiteDescription() );
  61. }
  62. catch ( final PwmUnrecoverableException e )
  63. {
  64. final RestResultBean restResultBean = RestResultBean.fromError( e.getErrorInformation() );
  65. ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
  66. }
  67. catch ( final Exception e )
  68. {
  69. final RestResultBean restResultBean = RestResultBean.fromError( new ErrorInformation( PwmError.ERROR_INTERNAL, e.getMessage() ) );
  70. ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
  71. }
  72. }
  73. public static String readRequestBodyAsString( final HttpServletRequest req, final int maxChars )
  74. throws IOException, PwmUnrecoverableException
  75. {
  76. final String value = JavaHelper.copyToString( req.getInputStream(), PwmConstants.DEFAULT_CHARSET, maxChars + 1 )
  77. .orElse( "" );
  78. if ( value.length() > maxChars )
  79. {
  80. final String msg = "input request body is to big, size=" + value.length() + ", max=" + maxChars;
  81. throw new PwmUnrecoverableException( new ErrorInformation( PwmError.ERROR_INTERNAL, msg ) );
  82. }
  83. return value;
  84. }
  85. }