TelemetryRestReceiver.java 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2017 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. package password.pwm.receiver;
  24. import org.apache.commons.io.IOUtils;
  25. import password.pwm.PwmConstants;
  26. import password.pwm.bean.TelemetryPublishBean;
  27. import password.pwm.error.ErrorInformation;
  28. import password.pwm.error.PwmError;
  29. import password.pwm.error.PwmUnrecoverableException;
  30. import password.pwm.i18n.Message;
  31. import password.pwm.util.java.JsonUtil;
  32. import password.pwm.ws.server.RestResultBean;
  33. import javax.servlet.ServletException;
  34. import javax.servlet.annotation.WebServlet;
  35. import javax.servlet.http.HttpServlet;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import java.io.IOException;
  39. import java.io.InputStreamReader;
  40. import java.io.Reader;
  41. import java.io.StringWriter;
  42. @WebServlet(
  43. name="TelemetryRestReceiver",
  44. urlPatterns={
  45. "/telemetry",
  46. }
  47. )
  48. public class TelemetryRestReceiver extends HttpServlet {
  49. @Override
  50. protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
  51. throws ServletException, IOException
  52. {
  53. try {
  54. resp.setHeader("Content","application/json");
  55. final String input = readRequestBodyAsString(req, 1024 * 1024);
  56. final TelemetryPublishBean telemetryPublishBean = JsonUtil.deserialize(input, TelemetryPublishBean.class);
  57. final Storage stoage = ContextManager.getContextManager(this.getServletContext()).getApp().getStorage();
  58. stoage.store(telemetryPublishBean);
  59. resp.getWriter().print(RestResultBean.forSuccessMessage(null, null, null, Message.Success_Unknown).toJson());
  60. } catch (PwmUnrecoverableException e) {
  61. resp.getWriter().print(RestResultBean.fromError(e.getErrorInformation()).toJson());
  62. } catch (Exception e) {
  63. final RestResultBean restResultBean = RestResultBean.fromError(new ErrorInformation(PwmError.ERROR_UNKNOWN, e.getMessage()));
  64. resp.getWriter().print(restResultBean.toJson());
  65. }
  66. }
  67. private static String readRequestBodyAsString(final HttpServletRequest req, final int maxChars)
  68. throws IOException, PwmUnrecoverableException
  69. {
  70. final StringWriter stringWriter = new StringWriter();
  71. final Reader readerStream = new InputStreamReader(
  72. req.getInputStream(),
  73. PwmConstants.DEFAULT_CHARSET
  74. );
  75. try {
  76. IOUtils.copy(readerStream, stringWriter);
  77. } catch (Exception e) {
  78. final String errorMsg = "error reading request body stream: " + e.getMessage();
  79. throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN,errorMsg));
  80. } finally {
  81. IOUtils.closeQuietly(readerStream);
  82. }
  83. final String stringValue = stringWriter.toString();
  84. if (stringValue.length() > maxChars) {
  85. throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN,"input request body is to big, size=" + stringValue.length() + ", max=" + maxChars));
  86. }
  87. return stringValue;
  88. }
  89. }