1313N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1313N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1313N/A *
1313N/A * This code is free software; you can redistribute it and/or modify it
1313N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1313N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1313N/A *
1313N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1313N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1313N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1313N/A * version 2 for more details (a copy is included in the LICENSE file that
1313N/A * accompanied this code).
1313N/A *
1313N/A * You should have received a copy of the GNU General Public License version
1313N/A * 2 along with this work; if not, write to the Free Software Foundation,
1313N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1313N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1313N/A */
1313N/A
1313N/Apackage sun.net.www.http;
1677N/A
1313N/Aimport java.io.*;
1313N/Aimport java.util.ArrayList;
1677N/Aimport java.util.regex.*;
1313N/Aimport sun.net.NetProperties;
1677N/Aimport sun.util.logging.PlatformLogger;
1313N/A
1313N/A/**
1313N/A * Main class of the HTTP traffic capture tool.
1313N/A * Captures are triggered by the sun.net.http.captureRules system property.
1313N/A * If set, it should point to a file containing the capture rules.
1313N/A * Format for the file is simple:
1313N/A * - 1 rule per line
1313N/A * - Lines starting with a # are considered comments and ignored
1313N/A * - a rule is a pair of a regular expression and file pattern, separated by a comma
1313N/A * - The regular expression is applied to URLs, if it matches, the traffic for
1313N/A * that URL will be captured in the associated file.
1313N/A * - if the file name contains a '%d', then that sequence will be replaced by a
1313N/A * unique random number for each URL. This allow for multi-threaded captures
1313N/A * of URLs matching the same pattern.
1313N/A * - Rules are checked in sequence, in the same order as in the file, until a
1313N/A * match is found or the end of the list is reached.
1313N/A *
1313N/A * Examples of rules:
1313N/A * www\.sun\.com , sun%d.log
1313N/A * yahoo\.com\/.*asf , yahoo.log
1313N/A *
1313N/A * @author jccollet
1313N/A */
1313N/Apublic class HttpCapture {
1313N/A private File file = null;
1313N/A private boolean incoming = true;
1313N/A private BufferedWriter out = null;
1313N/A private static boolean initialized = false;
1313N/A private static volatile ArrayList<Pattern> patterns = null;
1313N/A private static volatile ArrayList<String> capFiles = null;
1313N/A
1313N/A private static synchronized void init() {
1313N/A initialized = true;
1313N/A String rulesFile = java.security.AccessController.doPrivileged(
1313N/A new java.security.PrivilegedAction<String>() {
1313N/A public String run() {
1313N/A return NetProperties.get("sun.net.http.captureRules");
1313N/A }
1313N/A });
1313N/A if (rulesFile != null && !rulesFile.isEmpty()) {
1313N/A BufferedReader in;
1313N/A try {
1313N/A in = new BufferedReader(new FileReader(rulesFile));
1313N/A } catch (FileNotFoundException ex) {
1313N/A return;
1313N/A }
1313N/A try {
1313N/A String line = in.readLine();
1313N/A while (line != null) {
1313N/A line = line.trim();
1313N/A if (!line.startsWith("#")) {
1313N/A // skip line if it's a comment
1313N/A String[] s = line.split(",");
1313N/A if (s.length == 2) {
1313N/A if (patterns == null) {
1313N/A patterns = new ArrayList<Pattern>();
1313N/A capFiles = new ArrayList<String>();
1313N/A }
1313N/A patterns.add(Pattern.compile(s[0].trim()));
1313N/A capFiles.add(s[1].trim());
1313N/A }
1313N/A }
1313N/A line = in.readLine();
1313N/A }
1313N/A } catch (IOException ioe) {
1313N/A
1313N/A } finally {
1313N/A try {
1313N/A in.close();
1313N/A } catch (IOException ex) {
1313N/A }
1313N/A }
1313N/A }
1313N/A }
1313N/A
1313N/A private static synchronized boolean isInitialized() {
1313N/A return initialized;
1313N/A }
1313N/A
1313N/A private HttpCapture(File f, java.net.URL url) {
1313N/A file = f;
1313N/A try {
1313N/A out = new BufferedWriter(new FileWriter(file, true));
1313N/A out.write("URL: " + url + "\n");
1313N/A } catch (IOException ex) {
1677N/A PlatformLogger.getLogger(HttpCapture.class.getName()).severe(null, ex);
1313N/A }
1313N/A }
1313N/A
1313N/A public synchronized void sent(int c) throws IOException {
1313N/A if (incoming) {
1313N/A out.write("\n------>\n");
1313N/A incoming = false;
1313N/A out.flush();
1313N/A }
1313N/A out.write(c);
1313N/A }
1313N/A
1313N/A public synchronized void received(int c) throws IOException {
1313N/A if (!incoming) {
1313N/A out.write("\n<------\n");
1313N/A incoming = true;
1313N/A out.flush();
1313N/A }
1313N/A out.write(c);
1313N/A }
1313N/A
1313N/A public synchronized void flush() throws IOException {
1313N/A out.flush();
1313N/A }
1313N/A
1313N/A public static HttpCapture getCapture(java.net.URL url) {
1313N/A if (!isInitialized()) {
1313N/A init();
1313N/A }
1313N/A if (patterns == null || patterns.isEmpty()) {
1313N/A return null;
1313N/A }
1313N/A String s = url.toString();
1313N/A for (int i = 0; i < patterns.size(); i++) {
1313N/A Pattern p = patterns.get(i);
1313N/A if (p.matcher(s).find()) {
1313N/A String f = capFiles.get(i);
1313N/A File fi;
1313N/A if (f.indexOf("%d") >= 0) {
1313N/A java.util.Random rand = new java.util.Random();
1313N/A do {
1313N/A String f2 = f.replace("%d", Integer.toString(rand.nextInt()));
1313N/A fi = new File(f2);
1313N/A } while (fi.exists());
1313N/A } else {
1313N/A fi = new File(f);
1313N/A }
1313N/A return new HttpCapture(fi, url);
1313N/A }
1313N/A }
1313N/A return null;
1313N/A }
1313N/A}