summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/application/jsmap/JSONWebServer.java
blob: 6f8c8be73804b4fe5ec2ee7e3352e6b12ee2efcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package de.lmu.ifi.dbs.elki.application.jsmap;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import de.lmu.ifi.dbs.elki.algorithm.outlier.spatial.neighborhood.NeighborSetPredicate;
import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.data.spatial.Polygon;
import de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject;
import de.lmu.ifi.dbs.elki.database.Database;
import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.ids.DBIDIter;
import de.lmu.ifi.dbs.elki.database.ids.DBIDRef;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.database.ids.DBIDs;
import de.lmu.ifi.dbs.elki.database.relation.Relation;
import de.lmu.ifi.dbs.elki.datasource.bundle.SingleObjectBundle;
import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector;
import de.lmu.ifi.dbs.elki.result.HierarchicalResult;
import de.lmu.ifi.dbs.elki.result.Result;
import de.lmu.ifi.dbs.elki.result.ResultHierarchy;
import de.lmu.ifi.dbs.elki.result.ResultUtil;
import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult;
import de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta;
import de.lmu.ifi.dbs.elki.utilities.datastructures.hierarchy.Hierarchy;
import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException;

/**
 * A simple web server to serve data base contents to a JavaScript client.
 * 
 * @author Erich Schubert
 * 
 * @apiviz.uses JSONBuffer
 */
public class JSONWebServer implements HttpHandler {
  /**
   * Our logger.
   */
  private static final Logging LOG = Logging.getLogger(JSONWebServer.class);

  /**
   * The base path we serve data from.
   */
  public static final String PATH_JSON = "/json/";

  /**
   * Server instance.
   */
  private HttpServer server;

  /**
   * The result tree we serve.
   */
  private HierarchicalResult result;

  /**
   * The database we use for obtaining object bundles.
   */
  private Database db;

  /**
   * Constructor.
   * 
   * @param port Port to listen on
   * @param result Result to serve
   */
  public JSONWebServer(int port, HierarchicalResult result) {
    super();
    this.result = result;
    assert (result != null) : "MapWebServer created with null result.";
    this.db = ResultUtil.findDatabase(result);

    try {
      InetSocketAddress addr = new InetSocketAddress(port);
      server = HttpServer.create(addr, 0);

      server.createContext(PATH_JSON, this);
      server.setExecutor(Executors.newCachedThreadPool());
      server.start();

      LOG.verbose("Webserver started on port " + port + ".");
    } catch (IOException e) {
      throw new AbortException("Could not start mini web server.", e);
    }
  }

  /**
   * Stop the web server.
   */
  public void stop() {
    server.stop(0);
  }

  /**
   * Parse a string into a DBID.
   * 
   * @param query Query string
   * @return DBID
   */
  private DBID stringToDBID(String query) {
    return DBIDUtil.importInteger(Integer.valueOf(query));
  }

  /**
   * Serialize an object bundle to JSON.
   * 
   * @param re Buffer to serialize to
   * @param id Object ID
   */
  protected void bundleToJSON(JSONBuffer re, DBIDRef id) {
    SingleObjectBundle bundle = db.getBundle(id);
    if (bundle != null) {
      for (int j = 0; j < bundle.metaLength(); j++) {
        final Object data = bundle.data(j);
        // TODO: refactor to JSONFormatters!
        // Format a NumberVector
        if (data instanceof NumberVector) {
          NumberVector<?> v = (NumberVector<?>) data;
          re.appendKeyArray(bundle.meta(j));
          for (int i = 0; i < v.getDimensionality(); i++) {
            re.append(v.doubleValue(i));
          }
          re.closeArray();
        }
        // Format a Polygon
        else if (data instanceof PolygonsObject) {
          re.appendKeyArray(bundle.meta(j));
          for (Polygon p : ((PolygonsObject) data).getPolygons()) {
            re.startArray();
            for (int i = 0; i < p.size(); i++) {
              Vector point = p.get(i);
              re.append(point.getArrayRef());
            }
            re.closeArray();
          }
          re.closeArray();
        }
        // Default serialization as string
        else {
          re.appendKeyValue(bundle.meta(j), data);
        }
        if (LOG.isDebuggingFiner()) {
          re.appendNewline();
        }
      }
    } else {
      re.appendKeyValue("error", "Object not found.");
    }
  }

  /**
   * Serialize an arbitrary result into JSON.
   * 
   * @param re Buffer to serialize to
   * @param name Result requested
   */
  // TODO: refactor
  protected void resultToJSON(JSONBuffer re, String name) {
    ResultHierarchy hier = result.getHierarchy();
    // Find requested result
    String[] parts = name.split("/");
    Result cur = result;
    int partpos = 0;
    {
      for (; partpos < parts.length; partpos++) {
        // FIXME: handle name collisions. E.g. type_123?
        boolean found = false;
        for (Hierarchy.Iter<Result> iter = hier.iterChildren(cur); iter.valid(); iter.advance()) {
          // logger.debug("Testing result: " + child.getShortName() + " <-> " +
          // parts[partpos]);
          Result child = iter.get();
          if (child.getLongName().equals(parts[partpos]) || child.getShortName().equals(parts[partpos])) {
            cur = child;
            found = true;
            break;
          }
        }
        if (!found) {
          break;
        }
      }
      if (cur == null) {
        re.appendKeyValue("error", "result not found.");
        return;
      }
    }
    // logger.debug(FormatUtil.format(parts, ",") + " " + partpos + " " + cur);

    // Result structure discovery via "children" parameter.
    if (parts.length == partpos + 1) {
      if ("children".equals(parts[partpos])) {
        re.appendKeyArray("children");
        for (Hierarchy.Iter<Result> iter = hier.iterChildren(cur); iter.valid(); iter.advance()) {
          Result child = iter.get();
          re.startHash();
          re.appendKeyValue("name", child.getShortName());
          re.appendKeyValue("type", child.getClass().getSimpleName());
          re.closeHash();
        }
        re.closeArray();
        return;
      }
    }

    // Database object access
    if (cur instanceof Database) {
      if (parts.length == partpos + 1) {
        DBID id = stringToDBID(parts[partpos]);
        if (id != null) {
          bundleToJSON(re, id);
          return;
        } else {
          re.appendKeyValue("error", "Object not found");
          return;
        }
      }
    }

    // Relation object access
    if (cur instanceof Relation) {
      if (parts.length == partpos + 1) {
        Relation<?> rel = (Relation<?>) cur;
        DBID id = stringToDBID(parts[partpos]);
        if (id != null) {
          Object data = rel.get(id);
          re.appendKeyValue("data", data);
        } else {
          re.appendKeyValue("error", "Object not found");
          return;
        }
      }
    }

    // Neighbor access
    if (cur instanceof NeighborSetPredicate) {
      if (parts.length == partpos + 1) {
        NeighborSetPredicate pred = (NeighborSetPredicate) cur;
        DBID id = stringToDBID(parts[partpos]);
        if (id != null) {
          DBIDs neighbors = pred.getNeighborDBIDs(id);
          re.appendKeyValue("DBID", id);
          re.appendKeyArray("neighbors");
          for (DBIDIter iter = neighbors.iter(); iter.valid(); iter.advance()) {
            re.appendString(iter.toString());
          }
          re.closeArray();
          return;
        } else {
          re.appendKeyValue("error", "Object not found");
          return;
        }
      }
    }

    // Outlier Score access
    if (cur instanceof OutlierResult) {
      OutlierResult or = (OutlierResult) cur;
      if (parts.length >= partpos + 1) {
        if ("table".equals(parts[partpos])) {
          // Handle paging
          int offset = 0;
          int pagesize = 100;

          if (parts.length >= partpos + 2) {
            offset = Integer.valueOf(parts[partpos + 1]);
          }
          if (parts.length >= partpos + 3) {
            pagesize = Integer.valueOf(parts[partpos + 2]);
          }
          re.appendKeyHash("paging");
          re.appendKeyValue("offset", offset);
          re.appendKeyValue("pagesize", pagesize);
          re.closeHash();
          if (LOG.isDebuggingFiner()) {
            re.appendNewline();
          }

          // Serialize meta
          OutlierScoreMeta meta = or.getOutlierMeta();
          outlierMetaToJSON(re, meta);

          re.appendKeyArray("scores");
          Relation<Double> scores = or.getScores();
          DBIDIter iter = or.getOrdering().iter(scores.getDBIDs()).iter();
          for (int i = 0; i < offset && iter.valid(); i++) {
            iter.advance();
          }
          for (int i = 0; i < pagesize && iter.valid(); i++, iter.advance()) {
            re.startHash();
            bundleToJSON(re, iter);
            final Double val = scores.get(iter);
            if (val != null) {
              re.appendKeyValue("score", val);
            }
            re.closeHash();
          }
          re.closeArray();
          return;
        }
      }
    }
    re.appendKeyValue("error", "unknown query");
  }

  /**
   * Serialize outlier metadata as JSON.
   * 
   * @param re Output buffer
   * @param meta Metadata
   */
  private void outlierMetaToJSON(JSONBuffer re, OutlierScoreMeta meta) {
    re.appendKeyHash("meta");
    re.appendKeyValue("min", meta.getActualMinimum());
    re.appendKeyValue("max", meta.getActualMaximum());
    re.appendKeyValue("tmin", meta.getTheoreticalMinimum());
    re.appendKeyValue("tmax", meta.getTheoreticalMaximum());
    re.appendKeyValue("base", meta.getTheoreticalBaseline());
    re.appendKeyValue("type", meta.getClass().getSimpleName());
    re.closeHash();
    if (LOG.isDebuggingFiner()) {
      re.appendNewline();
    }
  }

  @Override
  public void handle(HttpExchange exchange) throws IOException {
    String requestMethod = exchange.getRequestMethod();
    if (!requestMethod.equalsIgnoreCase("GET")) {
      return;
    }
    String path = exchange.getRequestURI().getPath();
    // logger.debug("Request for " + path);
    if (path.startsWith(PATH_JSON)) {
      path = path.substring(PATH_JSON.length());
    } else {
      LOG.warning("Unexpected path in request handler: " + path);
      throw new AbortException("Unexpected path: " + path);
    }

    // Get JSON-with-padding callback name.
    String callback = null;
    {
      String query = exchange.getRequestURI().getQuery();
      if (query != null) {
        String[] frags = query.split("&");
        for (String frag : frags) {
          if (frag.startsWith("jsonp=")) {
            callback = URLDecoder.decode(frag.substring("jsonp=".length()), "UTF-8");
          }
          if (frag.startsWith("callback=")) {
            callback = URLDecoder.decode(frag.substring("callback=".length()), "UTF-8");
          }
        }
      }
      // if(logger.isDebuggingFinest() && callback != null) {
      // logger.debugFinest("Callback parameter: " + callback);
      // }
    }

    // Prepare JSON response.
    StringBuilder response = new StringBuilder();
    {
      if (callback != null) {
        response.append(callback);
        response.append("(");
      }

      // JSON serializer
      JSONBuffer jsonbuf = new JSONBuffer(response);
      try {
        jsonbuf.startHash();
        resultToJSON(jsonbuf, path);
        jsonbuf.closeHash();
      } catch (Throwable e) {
        LOG.exception("Exception occurred in embedded web server:", e);
        throw (new IOException(e));
      }
      // wrap up
      if (callback != null) {
        response.append(")");
      }
    }

    byte[] rbuf = response.toString().getBytes("UTF-8");
    // Send
    Headers responseHeaders = exchange.getResponseHeaders();
    responseHeaders.set("Content-Type", "text/javascript");
    exchange.sendResponseHeaders(200, rbuf.length);
    OutputStream responseBody = exchange.getResponseBody();
    responseBody.write(rbuf);
    responseBody.close();
  }
}