/ri-examples/quickstart/JbiDemo/LAFPayrollWebService/src/java/demo/LAFServlet.java

https://bitbucket.org/ldassonville/openesb-core · Java · 162 lines · 94 code · 15 blank · 53 comment · 10 complexity · b5ea564d04a5febfcf94e4e82c1f6e2a MD5 · raw file

  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)LAFServlet.java
  24. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  25. *
  26. * END_HEADER - DO NOT EDIT
  27. */
  28. package demo;
  29. import java.io.BufferedReader;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.io.PrintWriter;
  33. import java.text.SimpleDateFormat;
  34. import java.util.Date;
  35. import java.util.GregorianCalendar;
  36. import javax.servlet.ServletException;
  37. import javax.servlet.http.HttpServlet;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. /**
  41. *
  42. * @author chikkala
  43. * @version
  44. */
  45. public class LAFServlet extends HttpServlet
  46. {
  47. private static final String OUT_DIR = "payroll";
  48. private static final String OUT_FILE_PREFIX = "LAFPayroll_";
  49. /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  50. * @param request servlet request
  51. * @param response servlet response
  52. */
  53. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  54. throws ServletException, IOException
  55. {
  56. PrintWriter out = null;
  57. PrintWriter outFileWriter = null;
  58. BufferedReader reader = null;
  59. try
  60. {
  61. response.setContentType("text/xml;charset=UTF-8");
  62. out = response.getWriter();
  63. reader = request.getReader();
  64. // create new file to save the message in the request
  65. String outFilePath = getNewOutputFilePath();
  66. // System.out.println("LAFServlet:outFile:" + outFilePath );
  67. outFileWriter = new PrintWriter(outFilePath);
  68. for ( String line = null; (line = reader.readLine()) != null ; )
  69. {
  70. // write back the data as response
  71. out.println(line);
  72. // write to file
  73. outFileWriter.println(line);
  74. System.out.println("LAFServerlet:" + line);
  75. }
  76. }
  77. finally
  78. {
  79. if ( out != null )
  80. {
  81. out.close();
  82. }
  83. if ( outFileWriter != null )
  84. {
  85. outFileWriter.close();
  86. }
  87. try
  88. {
  89. if ( reader != null )
  90. {
  91. reader.close();
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. // ignore
  97. }
  98. }
  99. }
  100. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  101. /** Handles the HTTP <code>GET</code> method.
  102. * @param request servlet request
  103. * @param response servlet response
  104. */
  105. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  106. throws ServletException, IOException
  107. {
  108. processRequest(request, response);
  109. }
  110. /** Handles the HTTP <code>POST</code> method.
  111. * @param request servlet request
  112. * @param response servlet response
  113. */
  114. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  115. throws ServletException, IOException
  116. {
  117. processRequest(request, response);
  118. }
  119. /** Returns a short description of the servlet.
  120. */
  121. public String getServletInfo()
  122. {
  123. return "LAF Servlet";
  124. }
  125. // </editor-fold>
  126. private String getDate()
  127. {
  128. GregorianCalendar gregoriancalendar = new GregorianCalendar();
  129. Date date = gregoriancalendar.getTime();
  130. SimpleDateFormat simpledateformat = new SimpleDateFormat("yyMMddHHmmssSSS");
  131. String s = simpledateformat.format(date);
  132. return s;
  133. }
  134. private String getNewOutputFilePath()
  135. {
  136. String contextPath = getServletContext().getRealPath("/");
  137. if ( contextPath == null )
  138. {
  139. contextPath = System.getProperty("java.io.tmpdir", "/tmp");
  140. }
  141. String outputDir = contextPath + File.separator + OUT_DIR;
  142. String outputFilePath = outputDir + File.separator + OUT_FILE_PREFIX + getDate() + ".xml";
  143. File outputDirFile = new File(outputDir);
  144. outputDirFile.mkdirs();
  145. return outputFilePath;
  146. }
  147. }