001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.monitor.client;
043:
044: import java.io.FileNotFoundException;
045: import java.io.InputStreamReader;
046: import java.io.IOException;
047: import java.io.PrintWriter;
048: import java.io.StringWriter;
049: import java.util.Enumeration;
050: import java.util.StringTokenizer;
051:
052: import javax.servlet.ServletConfig;
053: import javax.servlet.ServletException;
054: import javax.servlet.http.HttpServlet;
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057: import javax.swing.SwingUtilities;
058:
059: import org.openide.filesystems.FileAlreadyLockedException;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062:
063: import org.netbeans.modules.web.monitor.data.Constants;
064:
065: /*
066: * Put a transaction
067: */
068:
069: public class PutTransaction extends HttpServlet {
070:
071: private static FileObject currDir = null;
072: private static boolean debug = false;
073:
074: private ServletConfig servletConfig = null;
075:
076: public void doPost(HttpServletRequest req, HttpServletResponse res)
077: throws ServletException, IOException {
078:
079: if (debug)
080: log("doPost"); //NOI18N
081: if (currDir == null) {
082: try {
083: currDir = Controller.getCurrDir();
084: } catch (FileNotFoundException ex) {
085: // PENDING report this error properly
086: if (debug)
087: log("Couldn't write the transaction data"); //NOI18N
088: return;
089: }
090: }
091:
092: // As soon as you get the parameters, you've gotten an input
093: // string for this. Don't do that.
094:
095: String id = req.getQueryString();
096: if (id == null || id.length() == 0) {
097: if (debug)
098: log("Bad request, exiting..."); //NOI18N
099: return;
100: }
101:
102: id = id.substring(0, id.indexOf(Constants.Punctuation.itemSep));
103:
104: if (debug)
105: log(" Trying to add the transaction"); //NOI18N
106: FileObject fo = null;
107:
108: try {
109: if (debug)
110: log(" Before creating the file"); //NOI18N
111: fo = currDir.createData(id, "xml"); //NOI18N
112: if (debug)
113: log(" After creating the file"); //NOI18N
114: } catch (IOException ioex) {
115: if (debug)
116: log(" Could not create the file, exiting...");
117: return;
118: }
119: FileLock lock = null;
120: try {
121: lock = fo.lock();
122: if (debug)
123: log(" Got the lock"); //NOI18N
124: } catch (FileAlreadyLockedException falex) {
125: if (debug)
126: log(" Couldn't get a file lock, exiting..."); //NOI18N
127: return;
128: }
129:
130: boolean success = false;
131: try {
132: PrintWriter fout = new PrintWriter(fo.getOutputStream(lock));
133: try {
134: InputStreamReader isr = new InputStreamReader(req
135: .getInputStream());
136: try {
137: char[] charBuf = new char[4096];
138: int numChars;
139:
140: while ((numChars = isr.read(charBuf, 0, 4096)) != -1) {
141: fout.write(charBuf, 0, numChars);
142: }
143: } finally {
144: isr.close();
145: }
146: } finally {
147: fout.close();
148: }
149: success = true;
150: if (debug)
151: log("...success"); //NOI18N
152: } catch (IOException ioex) {
153: if (debug) {
154: log("Failed to read/write the record:");
155: log(ioex);
156: }
157: } finally {
158: lock.releaseLock();
159:
160: try {
161: res.setContentType("text/plain"); //NOI18N
162: PrintWriter out = res.getWriter();
163: try {
164: out.println(Constants.Comm.ACK);
165: } finally {
166: out.close();
167: }
168: } catch (Exception ex) {
169: // It doesn't actually matter if this goes wrong
170: }
171: }
172: final boolean success2 = success;
173: final String id2 = id;
174: // window system code must be run in AWT thread
175: SwingUtilities.invokeLater(new Runnable() {
176: public void run() {
177: if (success2) {
178: MonitorAction.addTransaction(id2);
179: }
180: }
181: });
182: }
183:
184: // PENDING - deal better with this
185: public void doGet(HttpServletRequest req, HttpServletResponse res)
186: throws ServletException, IOException {
187:
188: if (debug)
189: log("doGet"); //NOI18N
190:
191: PrintWriter out = res.getWriter();
192: try {
193: //out.println(id);
194: out.println("Shouldn't use GET for this!"); //NOI18N
195: } catch (Exception e) {
196: if (debug)
197: log(e.getMessage());
198: }
199: try {
200: out.close();
201: } catch (Exception ex) {
202: }
203: }
204:
205: /**
206: * Init method for this filter
207: *
208: */
209: public void init(ServletConfig servletConfig) {
210:
211: this .servletConfig = servletConfig;
212: if (debug)
213: log("init"); //NOI18N
214: }
215:
216: public void log(String msg) {
217: System.out.println("PutTransaction::" + msg); //NOI18N
218:
219: }
220:
221: public void log(Throwable t) {
222: log(getStackTrace(t));
223: }
224:
225: public static String getStackTrace(Throwable t) {
226:
227: String stackTrace = null;
228:
229: try {
230: StringWriter sw = new StringWriter();
231: PrintWriter pw = new PrintWriter(sw);
232: t.printStackTrace(pw);
233: pw.close();
234: sw.close();
235: stackTrace = sw.getBuffer().toString();
236: } catch (Exception ex) {
237: }
238: return stackTrace;
239: }
240:
241: } //PutTransaction.java
|