36 |
import java.net.*; |
import java.net.*; |
37 |
import java.util.Iterator; |
import java.util.Iterator; |
38 |
import java.util.NoSuchElementException; |
import java.util.NoSuchElementException; |
39 |
|
import java.security.*; |
40 |
|
|
41 |
/** An HTTP server serving blocks from a Storm pool. When started from command |
/** An HTTP server serving blocks from a Storm pool. When started from command |
42 |
* line, it servers the directory given as first argument. |
* line, it servers the directory given as first argument. |
43 |
*/ |
*/ |
44 |
public class HTTPProxy implements Runnable { |
public class HTTPProxy implements Runnable { |
45 |
public static boolean dbg = false; |
public static boolean dbg = true; |
46 |
private static void p(String s) { System.out.println("HTTPProxy: "+s); } |
private static void p(String s) { System.out.println("HTTPProxy: "+s); } |
47 |
|
|
48 |
protected IndexedPool pool; |
protected IndexedPool pool; |
53 |
protected String URNPAC; |
protected String URNPAC; |
54 |
protected String BACKLINKS; |
protected String BACKLINKS; |
55 |
protected HTTPServer server; |
protected HTTPServer server; |
56 |
|
|
57 |
|
protected boolean acceptPut = false; |
58 |
|
protected KeyPair keyPair; |
59 |
|
|
60 |
public HTTPProxy(IndexedPool pool, int port) throws IOException { |
public HTTPProxy(IndexedPool pool, int port) throws IOException { |
61 |
this.pool = pool; |
this.pool = pool; |
69 |
this.server = new HTTPServer(new Factory(), port); |
this.server = new HTTPServer(new Factory(), port); |
70 |
} |
} |
71 |
|
|
72 |
|
/** Construct a gateway that allows PUT to pointer URNs. |
73 |
|
* Needs to be passed the key pair to sign the URNs. |
74 |
|
*/ |
75 |
|
public HTTPProxy(IndexedPool pool, int port, |
76 |
|
KeyPair keyPair) throws IOException { |
77 |
|
this(pool, port); |
78 |
|
this.acceptPut = true; |
79 |
|
this.keyPair = keyPair; |
80 |
|
} |
81 |
|
|
82 |
public void run() { |
public void run() { |
83 |
if(dbg) p("Starting Storm URN server @ "+ROOTURL); |
if(dbg) p("Starting Storm URN server @ "+ROOTURL); |
84 |
server.run(); |
server.run(); |
345 |
w.close(); |
w.close(); |
346 |
return resp; |
return resp; |
347 |
} |
} |
348 |
|
|
349 |
|
protected HTTPResponse doPut(HTTPRequest req, HTTPResponse.Factory resf) |
350 |
|
throws IOException { |
351 |
|
|
352 |
|
String uri = req.getRequestURI(); |
353 |
|
System.out.println("PUT: "+uri); |
354 |
|
|
355 |
|
if(!acceptPut) |
356 |
|
return doUnknown(req, resf); |
357 |
|
|
358 |
|
if(dbg) p("PUT accepted"); |
359 |
|
PointerId id = new PointerId(uri); |
360 |
|
PointerIndex idx = |
361 |
|
(PointerIndex)pool.getIndex(PointerIndex.uri); |
362 |
|
BlockId current; |
363 |
|
|
364 |
|
try { |
365 |
|
current = idx.get(id); |
366 |
|
} catch(Exception _) { |
367 |
|
_.printStackTrace(); |
368 |
|
throw new Error("Exception while getting key XXX"); |
369 |
|
} |
370 |
|
|
371 |
|
if(dbg) p("Got old pointer value"); |
372 |
|
|
373 |
|
// because we don't get a content-type |
374 |
|
// assume it's the same as before |
375 |
|
BlockOutputStream bos = |
376 |
|
pool.getBlockOutputStream(current.getContentType()); |
377 |
|
|
378 |
|
CopyUtil.copy(req.getInputStream(), bos); |
379 |
|
|
380 |
|
if(dbg) p("Created new block: "+bos.getBlockId()); |
381 |
|
|
382 |
|
try { |
383 |
|
idx.set(id, bos.getBlockId(), keyPair); |
384 |
|
} catch(Exception _) { |
385 |
|
_.printStackTrace(); |
386 |
|
throw new Error("Exception while getting key XXX"); |
387 |
|
} |
388 |
|
|
389 |
|
if(dbg) p("Set pointer, making response."); |
390 |
|
|
391 |
|
HTTPResponse resp = resf.makeResponse(200, "Ok"); |
392 |
|
resp.setField("Content-Type", "text/html"); |
393 |
|
Writer w = new OutputStreamWriter(resp.getOutputStream(), |
394 |
|
"US-ASCII"); |
395 |
|
w.write("<html><head><title>Ok</title></head><body>"); |
396 |
|
w.write("PUT was successful, new block is: "+bos.getBlockId()); |
397 |
|
w.write("</body></html>"); |
398 |
|
w.close(); |
399 |
|
|
400 |
|
if(dbg) p("Return response"); |
401 |
|
return resp; |
402 |
|
} |
403 |
} |
} |
404 |
|
|
405 |
protected BlockId getBlockId(String uri) { |
protected BlockId getBlockId(String uri) { |
433 |
java.util.Collections.singleton(PointerIndex.type); |
java.util.Collections.singleton(PointerIndex.type); |
434 |
pool = new DirPool(new File(args[0]), indexTypes); |
pool = new DirPool(new File(args[0]), indexTypes); |
435 |
|
|
436 |
new HTTPProxy(pool, 5555).run(); |
if(args.length == 1) |
437 |
|
new HTTPProxy(pool, 5555).run(); |
438 |
|
else { |
439 |
|
KeyPair keys = SetPointer.readKeyPair(new File(args[1])); |
440 |
|
new HTTPProxy(pool, 5555, keys).run(); |
441 |
|
} |
442 |
} |
} |
443 |
} |
} |