コード例 #1
0
ファイル: ImportUrl.java プロジェクト: Jfeng3/h2o
 @Override
 protected Response serve() {
   InputStream s = null;
   String urlStr = _url.value();
   try {
     // if( urlStr.startsWith("file://") ) {
     // urlStr = urlStr.substring("file://".length());
     if (urlStr.startsWith("file:///")) {
       urlStr = urlStr.substring("file:///".length());
       File f = new File(urlStr);
       // urlStr = "file://"+f.getCanonicalPath();
       urlStr = "file:///" + f.getCanonicalPath();
     }
     URL url = new URL(urlStr);
     Key k = _key.value();
     if (k == null) k = Key.make(urlStr);
     s = url.openStream();
     if (s == null) return Response.error("Unable to open stream to URL " + url.toString());
     ValueArray.readPut(k, s);
     JsonObject json = new JsonObject();
     json.addProperty(KEY, k.toString());
     json.addProperty(URL, urlStr);
     Response r = Response.done(json);
     r.setBuilder(KEY, new KeyElementBuilder());
     return r;
   } catch (IllegalArgumentException e) {
     return Response.error("Not a valid key: " + urlStr);
   } catch (IOException e) {
     return Response.error(e);
   } finally {
     Closeables.closeQuietly(s);
   }
 }