Creates and returns a REST command. @param session REST session @return code @throws QueryException query exception @throws IOException I/O exception
(final RESTSession session)
| 30 | * @throws IOException I/O exception |
| 31 | */ |
| 32 | public static RESTExec get(final RESTSession session) throws QueryException, IOException { |
| 33 | // create new database or update resource |
| 34 | final HTTPConnection conn = session.conn; |
| 35 | final String db = conn.db(); |
| 36 | if(db.isEmpty()) throw HTTPStatus.NO_DATABASE_SPECIFIED.get(); |
| 37 | |
| 38 | RESTCmd.assignOptions(session); |
| 39 | |
| 40 | final MainOptions options = conn.context.options; |
| 41 | final InputStream is = conn.request.getInputStream(); |
| 42 | final MediaType mt = conn.mediaType(); |
| 43 | |
| 44 | // choose correct importer |
| 45 | boolean xml = true; |
| 46 | if(mt.isJSON()) { |
| 47 | final JsonParserOptions opts = new JsonParserOptions(); |
| 48 | opts.assign(mt); |
| 49 | options.set(MainOptions.JSONPARSER, opts); |
| 50 | options.set(MainOptions.PARSER, MainParser.JSON); |
| 51 | } else if(mt.isCSV()) { |
| 52 | final CsvParserOptions opts = new CsvParserOptions(); |
| 53 | opts.assign(mt); |
| 54 | options.set(MainOptions.CSVPARSER, opts); |
| 55 | options.set(MainOptions.PARSER, MainParser.CSV); |
| 56 | } else if(mt.is(MediaType.TEXT_HTML)) { |
| 57 | final HtmlOptions opts = new HtmlOptions(); |
| 58 | opts.assign(mt); |
| 59 | options.set(MainOptions.HTMLPARSER, opts); |
| 60 | options.set(MainOptions.PARSER, MainParser.HTML); |
| 61 | } else if(!mt.is(MediaType.ALL_ALL) && !mt.isXml()) { |
| 62 | xml = false; |
| 63 | } |
| 64 | |
| 65 | // store data as XML or binary resource, depending on content type |
| 66 | final String path = conn.dbpath(); |
| 67 | if(path.isEmpty()) { |
| 68 | // do not OPEN database |
| 69 | session.clear(); |
| 70 | if(xml) { |
| 71 | session.add(new CreateDB(db), is); |
| 72 | } else { |
| 73 | session.add(new CreateDB(db)).add(new BinaryPut(db), is); |
| 74 | } |
| 75 | } else if(xml) { |
| 76 | session.add(new Put(path), is); |
| 77 | } else { |
| 78 | session.add(new Delete(path)).add(new BinaryPut(path), is); |
| 79 | } |
| 80 | return new RESTExec(session, true); |
| 81 | } |
| 82 | } |