001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)LAFServlet.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package demo;
030:
031: import java.io.BufferedReader;
032: import java.io.File;
033: import java.io.IOException;
034: import java.io.PrintWriter;
035: import java.text.SimpleDateFormat;
036: import java.util.Date;
037: import java.util.GregorianCalendar;
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServlet;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042:
043: /**
044: *
045: * @author chikkala
046: * @version
047: */
048: public class LAFServlet extends HttpServlet {
049: private static final String OUT_DIR = "payroll";
050: private static final String OUT_FILE_PREFIX = "LAFPayroll_";
051:
052: /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
053: * @param request servlet request
054: * @param response servlet response
055: */
056: protected void processRequest(HttpServletRequest request,
057: HttpServletResponse response) throws ServletException,
058: IOException {
059: PrintWriter out = null;
060: PrintWriter outFileWriter = null;
061: BufferedReader reader = null;
062:
063: try {
064: response.setContentType("text/xml;charset=UTF-8");
065: out = response.getWriter();
066:
067: reader = request.getReader();
068: // create new file to save the message in the request
069: String outFilePath = getNewOutputFilePath();
070: // System.out.println("LAFServlet:outFile:" + outFilePath );
071: outFileWriter = new PrintWriter(outFilePath);
072:
073: for (String line = null; (line = reader.readLine()) != null;) {
074: // write back the data as response
075: out.println(line);
076: // write to file
077: outFileWriter.println(line);
078: System.out.println("LAFServerlet:" + line);
079: }
080: } finally {
081:
082: if (out != null) {
083: out.close();
084: }
085: if (outFileWriter != null) {
086: outFileWriter.close();
087: }
088: try {
089: if (reader != null) {
090: reader.close();
091: }
092: } catch (Exception ex) {
093: // ignore
094: }
095: }
096: }
097:
098: // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
099: /** Handles the HTTP <code>GET</code> method.
100: * @param request servlet request
101: * @param response servlet response
102: */
103: protected void doGet(HttpServletRequest request,
104: HttpServletResponse response) throws ServletException,
105: IOException {
106: processRequest(request, response);
107: }
108:
109: /** Handles the HTTP <code>POST</code> method.
110: * @param request servlet request
111: * @param response servlet response
112: */
113: protected void doPost(HttpServletRequest request,
114: HttpServletResponse response) throws ServletException,
115: IOException {
116: processRequest(request, response);
117: }
118:
119: /** Returns a short description of the servlet.
120: */
121: public String getServletInfo() {
122: return "LAF Servlet";
123: }
124:
125: // </editor-fold>
126:
127: private String getDate() {
128: GregorianCalendar gregoriancalendar = new GregorianCalendar();
129: Date date = gregoriancalendar.getTime();
130: SimpleDateFormat simpledateformat = new SimpleDateFormat(
131: "yyMMddHHmmssSSS");
132: String s = simpledateformat.format(date);
133: return s;
134: }
135:
136: private String getNewOutputFilePath() {
137: String contextPath = getServletContext().getRealPath("/");
138: if (contextPath == null) {
139: contextPath = System.getProperty("java.io.tmpdir", "/tmp");
140: }
141: String outputDir = contextPath + File.separator + OUT_DIR;
142: String outputFilePath = outputDir + File.separator
143: + OUT_FILE_PREFIX + getDate() + ".xml";
144:
145: File outputDirFile = new File(outputDir);
146: outputDirFile.mkdirs();
147: return outputFilePath;
148: }
149:
150: }
|