org.apache.jena.atlas.json.JSON Java Examples
The following examples show how to use
org.apache.jena.atlas.json.JSON.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S_DRPC.java From rdf-delta with Apache License 2.0 | 6 votes |
private static JsonObject getFieldAsObject(JsonObject arg, String field) { try { if ( ! arg.hasKey(field) ) { LOG.warn("Bad request: Missing Field: "+field+" Arg: "+JSON.toStringFlat(arg)) ; throw new DeltaBadRequestException("Missing field: "+field) ; } JsonValue jv = arg.get(field) ; if ( ! jv.isObject() ) { } return jv.getAsObject(); } catch (JsonException ex) { LOG.warn("Bad request: Field: "+field+" Arg: "+JSON.toStringFlat(arg)) ; throw new DeltaBadRequestException("Bad field '"+field+"' : "+arg.get(field)) ; } }
Example #2
Source File: S_JSON.java From rdf-delta with Apache License 2.0 | 6 votes |
public static void json(HttpServletRequest req, HttpServletResponse resp, JsonValue responseContent) { try { resp.setHeader(HttpNames.hCacheControl, "no-cache"); resp.setHeader(HttpNames.hContentType, WebContent.contentTypeJSON); resp.setStatus(HttpSC.OK_200); try(ServletOutputStream out = resp.getOutputStream(); IndentedWriter b = new IndentedWriter(out); ) { b.setFlatMode(true); JSON.write(b, responseContent); b.ensureStartOfLine(); b.flush(); out.write('\n'); } } catch (IOException ex) { LOG.warn("json: IOException", ex); try { resp.sendError(HttpSC.INTERNAL_SERVER_ERROR_500, "Internal server error"); } catch (IOException ex2) {} } }
Example #3
Source File: FileArea.java From rdf-delta with Apache License 2.0 | 6 votes |
/** * Look for {@link DataSource DataSources} in a disk area given by {@code location}. * <p> * Scan the given area for directories (must have a config file), check they are enabled, * and deal with {@code log_type}. */ public static List<DataSourceDescription> scanForLogs(Path location) { // PatchStore's that rely on the scan of local directories Pair<List<Path>, List<Path>> pair = scanDirectory(location); List<Path> dataSourcePaths = pair.getLeft(); List<Path> disabledDataSources = pair.getRight(); //dataSourcePaths.forEach(p->LOG.info("Data source paths: "+p)); disabledDataSources.forEach(p->LOG.info(" Data source: "+p+" : Disabled")); List<DataSourceDescription> descriptions = ListUtils.toList (dataSourcePaths.stream() .map(p->{ // Extract name from disk name. String dsName = p.getFileName().toString(); // read config file. JsonObject sourceObj = JSON.read(p.resolve(FileNames.DS_CONFIG).toString()); DataSourceDescription dsd = DataSourceDescription.fromJson(sourceObj); if ( ! Objects.equals(dsName, dsd.getName()) ) throw new DeltaConfigException("Names do not match: directory="+dsName+", dsd="+dsd); return dsd; }) .filter(Objects::nonNull) ); return descriptions; }
Example #4
Source File: DeltaBackupServer.java From rdf-delta with Apache License 2.0 | 5 votes |
private void writeConf(BackupConfig cfg) { JsonObject obj = JSONX.buildObject(b->{ b .pair(jPort, cfg.port) .key(jLogs).startArray(); cfg.logs.forEach(a-> b.startObject().pair(jName, a.name).pair(jDir, a.dir).pair(jFile, a.file).finishObject() ); b.finishArray(); }); JSON.write(System.out, obj); }
Example #5
Source File: QueryTestCaseType.java From shacl with Apache License 2.0 | 5 votes |
@Override public void run(Model results) throws Exception { Resource testCase = getResource(); String queryString = JenaUtil.getStringProperty(testCase, SH.select); Model model = testCase.getModel(); JsonObject expectedJSON = JSON.parse(JenaUtil.getStringProperty(testCase, DASH.expectedResult)); for(TestCaseContextFactory contextFactory : contextFactories) { TestCaseContext context = contextFactory.createContext(); context.setUpTestContext(); try { String actual = createResultSetJSON(queryString, model); JsonObject actualJSON = JSON.parse(actual); if(!actualJSON.equals(expectedJSON)) { System.err.println("---- Test failure: "+testCase); System.err.println(queryString); System.err.println("---- Expected ----"); System.err.println(expectedJSON); System.err.println("---- Actual ----"); System.err.println(actual); System.err.println("----"); createFailure(results, "Mismatching result set. Actual: " + actual, context); return; } } finally { context.tearDownTestContext(); } } createResult(results, DASH.SuccessTestCaseResult); }
Example #6
Source File: DeltaLinkHTTP.java From rdf-delta with Apache License 2.0 | 5 votes |
private JsonValue rpcToValue(String opName, JsonObject arg) { JsonObject argx = ( arg == null ) ? emptyObject : arg; // [NET] Network point return retry(()->DRPC.rpc(remoteServer + DeltaConst.EP_RPC, opName, argx), ()->true, ()->format("Retry : %s",opName), ()->format("Failed : %s %s",opName,JSON.toStringFlat(argx)) ); }
Example #7
Source File: DeltaLinkHTTP.java From rdf-delta with Apache License 2.0 | 5 votes |
@Override public Version append(Id dsRef, RDFPatch patch) { checkLink(); long t1 = System.currentTimeMillis(); String str = retry(()->{ RDFChangesHTTP remote = createRDFChanges(dsRef); // [NET] Network point // If not re-applyable, we need a copy. patch.apply(remote); return remote.getResponse(); }, ()->patch.repeatable(), ()->"Retry append patch.", ()->"Failed to append patch : "+dsRef); long t2 = System.currentTimeMillis(); long elapsed_ms = (t2-t1); if ( str != null ) { try { JsonObject obj = JSON.parse(str); Version version = Version.fromJson(obj, DeltaConst.F_VERSION); event(listener->listener.append(dsRef, version, patch)); return version; } catch (Exception ex) { FmtLog.warn(this.getClass(), "[%s] Error in response body : %s", dsRef, ex.getMessage()); } } else { FmtLog.warn(this.getClass(), "[%s] No response body", dsRef); } // No response body or syntax error. event(listener->listener.append(dsRef, Version.UNSET, patch)); return Version.UNSET; }
Example #8
Source File: S_DRPC.java From rdf-delta with Apache License 2.0 | 5 votes |
private static String getFieldAsString(JsonObject arg, String field, boolean required) { try { if ( ! arg.hasKey(field) ) { if ( required ) { LOG.warn("Bad request: Missing Field: "+field+" Arg: "+JSON.toStringFlat(arg)) ; throw new DeltaBadRequestException("Missing field: "+field) ; } return null; } return arg.get(field).getAsString().value() ; } catch (JsonException ex) { LOG.warn("Bad request: Field not a string: "+field+" Arg: "+JSON.toStringFlat(arg)) ; throw new DeltaBadRequestException("Bad field '"+field+"' : "+arg.get(field)) ; } }
Example #9
Source File: EntityJSONRDFReader.java From JedAIToolkit with Apache License 2.0 | 5 votes |
private void readModel(String inpFIle) throws IOException, NotFoundException { //read each ntriples JsonObject jsonObject = JSON.read(inpFIle); String key = jsonObject.keys().toArray()[0].toString(); JsonArray jarr = jsonObject.get(key).getAsArray(); // JSON file example: /*{"triples": [ {Subject: "OperaHouse", Predicate: "located_in", Object: "Sydney"}, {Subject: "Sydney", Predicate: "located_in", Object: "Australia"}, ...... */ jarr.forEach((jsonValue) -> { final CharSequence predicate = jsonValue.getAsObject().get("Predicate").toString(); final String pred = predicate.toString(); if (!(attributesToExclude.contains(pred))) { final CharSequence subject = jsonValue.getAsObject().get("Subject").toString(); String sub = subject.toString(); if (!prefix.equals("")) { sub = sub.replace(prefix, ""); } final CharSequence object = jsonValue.getAsObject().get("Object").toString(); final String obj = object.toString(); //if already exists a profile for the subject, simply add po as <Att>-<Value> EntityProfile entityProfile = urlToEntity.get(sub); if (entityProfile == null) { entityProfile = new EntityProfile(sub); entityProfiles.add(entityProfile); urlToEntity.put(sub, entityProfile); } if (!obj.isEmpty()) { entityProfile.addAttribute(pred, obj); } } }); }
Example #10
Source File: DeltaBackupServer.java From rdf-delta with Apache License 2.0 | 5 votes |
private void parseConf(BackupConfig cfg, String cfgFile) { try { JsonObject obj = JSON.read(cfgFile); cfg.port = obj.get(jPort).getAsNumber().value().intValue(); JsonArray a = obj.get(jLogs).getAsArray(); a.forEach(elt-> { BackupArea area = parseLogObject(cfg, elt); cfg.logs.add(area); }); } catch (Exception ex) { throw new CmdException("Failed to process configuration file: "+ex.getMessage()); } }
Example #11
Source File: PatchLogIndexZk.java From rdf-delta with Apache License 2.0 | 5 votes |
private void jsonSetState(JsonObject obj) { try { FmtLog.debug(LOG, "jsonToState %s",JSON.toStringFlat(obj)); LogEntry entry = JsonLogEntry.jsonToLogEntry(obj); long ver = entry.getVersion().value(); if ( ver == version ) return ; Id newCurrent = entry.getPatchId(); Id newPrevious = entry.getPrevious(); newState(ver, newCurrent, newPrevious); } catch (RuntimeException ex) { FmtLog.error(this.getClass(), "Failed to load the patch log index state", ex); } }
Example #12
Source File: FileArea.java From rdf-delta with Apache License 2.0 | 5 votes |
/** * Set up a disk file area for the data source * @param root * @param patchStore * @param dsd */ /*package*/ public static Path setupDataSourceByFile(Path root, PatchStore patchStore, DataSourceDescription dsd) { // Disk file setup Path sourcePath = root.resolve(dsd.getName()); if ( patchStore.logExists(dsd.getId()) ) throw new DeltaBadRequestException("DataSource area already exists and is active at: "+sourcePath); // Checking. // The area can exist, but it must not be formatted for a DataSource // if ( sourceArea.exists() ) // throw new DeltaException("Area already exists"); if ( FileArea.isMinimalDataSource(sourcePath) ) throw new DeltaBadRequestException("DataSource area already exists at: "+sourcePath); if ( ! FileArea.isEnabled(sourcePath) ) throw new DeltaBadRequestException("DataSource area disabled: "+sourcePath); try { Files.createDirectories(sourcePath); } catch (IOException e) { throw new DeltaBadRequestException("Failed to create DataSource area: "+sourcePath); } // Create source.cfg. JsonObject obj = dsd.asJson(); obj.put(F_LOG_TYPE, patchStore.getProvider().getShortName()); try (OutputStream out = Files.newOutputStream(sourcePath.resolve(FileNames.DS_CONFIG))) { JSON.write(out, obj); } catch (IOException ex) { throw IOX.exception(ex); } return sourcePath; }
Example #13
Source File: PatchStoreAnyLocal.java From rdf-delta with Apache License 2.0 | 5 votes |
private PatchStore choose(DataSourceDescription dsd, Path patchLogDir) { //Look in source.cfg. if ( ! Files.exists(patchLogDir) ) return patchStoreDefaultNew; Path sourceCfgPath = patchLogDir.resolve(FileNames.DS_CONFIG); if ( ! Files.exists(sourceCfgPath) ) { // Directory, no source.cfg. return patchStoreDefaultNew; } try { //c.f. LocalServerConfig. JsonObject obj = JSON.read(sourceCfgPath.toString()); DataSourceDescription dsdCfg = DataSourceDescription.fromJson(obj); String logTypeName = obj.get(F_LOG_TYPE).getAsString().value(); if ( logTypeName != null ) { Provider provider = DPS.providerByName(logTypeName); if ( provider == null ) throw new DeltaConfigException("Unknown log type: "+logTypeName); switch(provider) { case FILE : return patchStoreFile; case MEM : return patchStoreMem; case ROCKS : return patchStoreRocks; case LOCAL : throw new DeltaException(dsdCfg.getName()+":"+FileNames.DS_CONFIG+" : log_type = local"); default: throw new DeltaException(dsdCfg.getName()+":"+FileNames.DS_CONFIG+" : log_type not for a local patch store"); } } Path dbPath = patchLogDir.resolve(RocksConst.databaseFilename).toAbsolutePath(); boolean rocks = Files.exists(dbPath); if ( rocks ) return patchStoreRocks; return patchStoreDefaultNew; } catch (Exception ex) { throw new DeltaException("Exception while reading log configuration: "+dsd.getName(), ex); } }
Example #14
Source File: TestDeltaServerConfig.java From rdf-delta with Apache License 2.0 | 5 votes |
private void roundTrip(DeltaServerConfig c) { JsonObject obj = c.asJSON(); DeltaServerConfig c2 = DeltaServerConfig.create(obj); if ( ! c.equals(c2) ) { System.out.println("c : "+c.zkMode); System.out.println("c2 : "+c2.zkMode); JSON.write(obj); JSON.write(c2.asJSON()); } assertEquals(c, c2); }
Example #15
Source File: DeltaServerConfig.java From rdf-delta with Apache License 2.0 | 5 votes |
public static void writeJSON(DeltaServerConfig config, String file) { IOX.run(()->{ JsonObject obj = config.asJSON(); try (OutputStream out = Files.newOutputStream(Paths.get(file))) { JSON.write(out, obj); }}); }
Example #16
Source File: PatchStoreZk.java From rdf-delta with Apache License 2.0 | 4 votes |
private static byte[] jsonBytes(JsonValue json) { ByteArrayOutputStream out = new ByteArrayOutputStream(8*1024); JSON.write(out, json); return out.toByteArray(); }
Example #17
Source File: Version.java From rdf-delta with Apache License 2.0 | 4 votes |
public static Version fromJson(JsonObject obj, String field) { long ver = JSONX.getLong(obj, field, -99) ; if ( ver < -1 ) throw new DeltaException("Bad version number: '"+JSON.toStringFlat(obj.get(field))+"'"); return create(ver); }
Example #18
Source File: LocalServerConfig.java From rdf-delta with Apache License 2.0 | 4 votes |
/** Parse a configuration file. */ public Builder parse(String configFile) { // Relationship to DeltaServerConfig in command line. Path path = Paths.get(configFile); if ( ! Files.exists(path) ) throw new DeltaConfigException("File not found: "+configFile); // -- version JsonObject obj = JSON.read(configFile); int version = JSONX.getInt(obj, F_VERSION, -99); if ( version == -99 ) { LOG.warn("No version number for the configuration file : assuming 'current'"); version = DeltaConst.SYSTEM_VERSION; } if ( version != SYSTEM_VERSION ) throw new DeltaConfigException("Version number for LocalServer must be "+DeltaConst.SYSTEM_VERSION+"."); this.configFile = configFile; // -- log provider // Default. logProvider = Provider.LOCAL; String logTypeName = JSONX.getStrOrNull(obj, F_LOG_TYPE); if ( logTypeName != null ) { Provider provider = DPS.providerByName(logTypeName); if ( provider == null ) throw new DeltaConfigException("Unknown log type: "+logTypeName); logProvider = provider ; } // -- store (file, rocks, any local) if ( isLocalProvider(logProvider) ) { String store = JSONX.getStrOrNull(obj, F_STORE); Path storeLocation = null; if ( store == null ) // Default to directory where the config file is. storeLocation = path.getParent(); else storeLocation = path.getParent().resolve(store); if ( storeLocation != null ) setProperty(DeltaConst.pDeltaStore, storeLocation.toString()); } // TODO -- General properties. return this; }
Example #19
Source File: S_DRPC.java From rdf-delta with Apache License 2.0 | 4 votes |
@Override protected void executeAction(DeltaAction action) throws IOException { JsonValue rslt = null ; JsonObject arg = action.rpcArg; // Some operations are logged at DEBUG because they are high-volume polling. // They shoudl all be "read" operations. boolean infoLogThisRPC = true; String recordOp = null; try { switch(action.opName) { case OP_PING: infoLogThisRPC = false; rslt = ping(action); break ; case OP_LIST_DS: infoLogThisRPC = false; rslt = listDataSources(action); break ; case OP_DESCR_DS: infoLogThisRPC = false; rslt = describeDataSource(action); break ; case OP_DESCR_LOG: infoLogThisRPC = false; rslt = describePatchLog(action); break ; case OP_LIST_LOG_INFO: // This operation is used to poll for changes so there can be a lot // of such requests. Don't log. // If "same as last time" infoLogThisRPC = ! OP_LIST_LOG_INFO.equals(lastOpName); infoLogThisRPC = false; rslt = listPatchLogInfo(action); break ; case OP_LIST_DSD: infoLogThisRPC = false; rslt = listDataSourcesDescriptions(action); break ; // Operations that change the server. case OP_CREATE_DS: rslt = createDataSource(action); break; case OP_COPY_DS: rslt = copyDataSource(action); break; case OP_RENAME_DS: rslt = renameDataSource(action); break; case OP_REMOVE_DS: rslt = removeDataSource(action); break; default: throw new InternalErrorException("Unknown operation: "+action.opName); } // recordOp = action.opName; } catch (Exception ex) { recordOp = null; throw ex ; } finally { if ( infoLogThisRPC ) lastOpName = recordOp; } if ( infoLogThisRPC ) FmtLog.info(LOG, "[%d] %s %s => %s", action.id, action.opName, JSON.toStringFlat(arg), JSON.toStringFlat(rslt)) ; else FmtLog.debug(LOG, "[%d] %s %s => %s", action.id, action.opName, JSON.toStringFlat(arg), JSON.toStringFlat(rslt)) ; sendJsonResponse(action.response, rslt); }
Example #20
Source File: DeltaServerConfig.java From rdf-delta with Apache License 2.0 | 4 votes |
public static void writeJSON(DeltaServerConfig config, OutputStream out) { JsonObject obj = config.asJSON(); JSON.write(out, obj); }
Example #21
Source File: DataState.java From rdf-delta with Apache License 2.0 | 4 votes |
private static String stateToString(Id datasource, String name, String uri, LocalStorageType storage, Version version, Id patchId) { JsonObject json = stateToJson(datasource, name, uri, storage, version, patchId); return JSON.toString(json); }
Example #22
Source File: DataState.java From rdf-delta with Apache License 2.0 | 4 votes |
/** Set version and datasource id from a string which is JOSN */ private static void setStateFromString(DataState state, String string) { JsonObject obj = JSON.parse(string); setFromJsonObject(state, obj); }
Example #23
Source File: DataState.java From rdf-delta with Apache License 2.0 | 4 votes |
/** JsonObject -> DataState */ private static void setFromJsonObject(DataState dataState, JsonObject sourceObj) { Version version = Version.fromJson(sourceObj, F_VERSION, Version.UNSET); if ( ! Version.isValid(version) ) { LOG.warn("No version: "+JSON.toStringFlat(sourceObj)); } dataState.version = version; String patchStr = JSONX.getStrOrNull(sourceObj, F_ID); if ( patchStr == null || patchStr.isEmpty() ) { dataState.patchId = null; } else { dataState.patchId = Id.fromString(patchStr); } String dsStr = JSONX.getStrOrNull(sourceObj, F_DATASOURCE); if ( dsStr != null ) dataState.datasource = Id.fromString(dsStr); else { LOG.error("No datasource: "+JSON.toStringFlat(sourceObj)); throw new DeltaException("No datasource: "+JSON.toStringFlat(sourceObj)); } String name = JSONX.getStrOrNull(sourceObj, F_NAME); if ( name != null ) dataState.name = name; else { LOG.error("No datasource name: "+JSON.toStringFlat(sourceObj)); throw new DeltaException("No datasource name: "+JSON.toStringFlat(sourceObj)); } String uri = JSONX.getStrOrNull(sourceObj, F_URI); if ( uri != null ) dataState.uri = uri; String typeName = JSONX.getStrOrNull(sourceObj, F_STORAGE); LocalStorageType storage = LocalStorageType.fromString(typeName); // if ( storage == null ) // throw new DeltaException("No storage type: "+JSON.toStringFlat(sourceObj)); dataState.storage = storage; }
Example #24
Source File: DeltaLinkHTTP.java From rdf-delta with Apache License 2.0 | 4 votes |
private JsonObject rpc(String opName, JsonObject arg) { JsonValue r = rpcToValue(opName, arg); if ( ! r.isObject() ) throw new DeltaException("Bad result to '"+opName+"': "+JSON.toStringFlat(r)); return r.getAsObject(); }
Example #25
Source File: DeltaServerConfig.java From rdf-delta with Apache License 2.0 | 4 votes |
public static DeltaServerConfig read(InputStream input) { JsonObject obj = JSON.parse(input); return create(obj); }
Example #26
Source File: DeltaLinkHTTP.java From rdf-delta with Apache License 2.0 | 4 votes |
private JsonObject rpcOnce(String opName, JsonObject arg) { JsonValue r = rpcOnceToValue(opName, arg); if ( ! r.isObject() ) throw new DeltaException("Bad result to '"+opName+"': "+JSON.toStringFlat(r)); return r.getAsObject(); }
Example #27
Source File: DeltaServerConfig.java From rdf-delta with Apache License 2.0 | 4 votes |
public static DeltaServerConfig read(String file) { JsonObject obj = JSON.read(file); return create(obj); }