jdk/src/share/classes/com/sun/tools/hat/internal/server/HttpReader.java
author ohair
Wed, 30 Apr 2008 17:34:41 -0700
changeset 468 642c8c0be52e
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6695553: Cleanup GPLv2+SPL legal notices in hat sources Summary: Just correcting the legal notices on the HAT sources. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    30
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package com.sun.tools.hat.internal.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Reads a single HTTP query from a socket, and starts up a QueryHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * to server it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.BufferedWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.io.PrintWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.hat.internal.model.Snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.hat.internal.oql.OQLEngine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class HttpReader implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private PrintWriter out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Snapshot snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private OQLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public HttpReader (Socket s, Snapshot snapshot, OQLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.socket = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.snapshot = snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            in = new BufferedInputStream(socket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            out = new PrintWriter(new BufferedWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                            new OutputStreamWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                socket.getOutputStream())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            out.println("HTTP/1.0 200 OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            out.println("Cache-Control: no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            out.println("Pragma: no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (in.read() != 'G' || in.read() != 'E'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    || in.read() != 'T' || in.read() != ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                outputError("Protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            StringBuffer queryBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            while ((data = in.read()) != -1 && data != ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                char ch = (char) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                queryBuf.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String query = queryBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            query = java.net.URLDecoder.decode(query, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            QueryHandler handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (snapshot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                outputError("The heap snapshot is still being read.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            } else if (query.equals("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                handler = new AllClassesQuery(true, engine != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                handler.setUrlStart("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else if (query.startsWith("/oql/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (engine != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    handler = new OQLQuery(engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    handler.setUrlStart("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    handler.setQuery(query.substring(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else if (query.startsWith("/oqlhelp/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (engine != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    handler = new OQLHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    handler.setUrlStart("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } else if (query.equals("/allClassesWithPlatform/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                handler = new AllClassesQuery(false, engine != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } else if (query.equals("/showRoots/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                handler = new AllRootsQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } else if (query.equals("/showInstanceCounts/includePlatform/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                handler = new InstancesCountQuery(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                handler.setUrlStart("../../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if (query.equals("/showInstanceCounts/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                handler = new InstancesCountQuery(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } else if (query.startsWith("/instances/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                handler = new InstancesQuery(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                handler.setQuery(query.substring(11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }  else if (query.startsWith("/newInstances/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                handler = new InstancesQuery(false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                handler.setQuery(query.substring(14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }  else if (query.startsWith("/allInstances/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                handler = new InstancesQuery(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                handler.setQuery(query.substring(14));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }  else if (query.startsWith("/allNewInstances/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                handler = new InstancesQuery(true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                handler.setQuery(query.substring(17));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else if (query.startsWith("/object/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                handler = new ObjectQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                handler.setQuery(query.substring(8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else if (query.startsWith("/class/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                handler = new ClassQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                handler.setQuery(query.substring(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else if (query.startsWith("/roots/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                handler = new RootsQuery(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                handler.setQuery(query.substring(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else if (query.startsWith("/allRoots/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                handler = new RootsQuery(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                handler.setQuery(query.substring(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else if (query.startsWith("/reachableFrom/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                handler = new ReachableQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                handler.setQuery(query.substring(15));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else if (query.startsWith("/rootStack/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                handler = new RootStackQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                handler.setQuery(query.substring(11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (query.startsWith("/histo/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                handler = new HistogramQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                handler.setQuery(query.substring(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else if (query.startsWith("/refsByType/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                handler = new RefsByTypeQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                handler.setQuery(query.substring(12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else if (query.startsWith("/finalizerSummary/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                handler = new FinalizerSummaryQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else if (query.startsWith("/finalizerObjects/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                handler = new FinalizerObjectsQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                handler.setUrlStart("../");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                handler.setQuery("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                handler.setOutput(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                handler.setSnapshot(snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                handler.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                outputError("Query '" + query + "' not implemented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (out != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } catch (IOException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (IOException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void outputError(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        out.println("<html><body bgcolor=\"#ffffff\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        out.println("</body></html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}