@Override public void getContactRequests(boolean fromWebsocket) throws ConnectionException { JsonArray array = Endpoints.AUTH_REQUESTS_URL .open(this) .as(JsonArray.class) .expect(200, "While loading authorization requests") .get(); for (JsonValue contactRequest : array) { JsonObject contactRequestObj = contactRequest.asObject(); try { ContactRequestImpl request = new ContactRequestImpl( contactRequestObj.get("event_time").asString(), getOrLoadContact(contactRequestObj.get("sender").asString()), contactRequestObj.get("greeting").asString(), this); if (!this.allContactRequests.contains(request)) { if (fromWebsocket) { ContactRequestEvent event = new ContactRequestEvent(request); getEventDispatcher().callEvent(event); } this.allContactRequests.add(request); } } catch (java.text.ParseException e) { getLogger().log(Level.WARNING, "Could not parse date for contact request", e); } } if (fromWebsocket) this.updateContactList(); }
static void assertJSONContainsClassNameJustOnce( JsonObject jsonObject1, JsonObject jsonObject2, String className) { boolean firstClassFound = jsonObject1.get("stackTrace").toString().contains("$" + className + "."); boolean secondClassFound = jsonObject2.get("stackTrace").toString().contains("$" + className + "."); assertTrue( format( "JSON for Management Center should contain stackTrace with class name '%s' exactly once", className), firstClassFound ^ secondClassFound); }
@Override public void fromJson(JsonObject json) { config = new WanReplicationConfig(); config.setName(json.get("name").asString()); List<WanPublisherConfig> publisherConfigs = config.getWanPublisherConfigs(); JsonArray publishers = json.get("publishers").asArray(); int size = publishers.size(); for (int i = 0; i < size; i++) { WanPublisherConfigDTO dto = new WanPublisherConfigDTO(new WanPublisherConfig()); dto.fromJson(publishers.get(0).asObject()); publisherConfigs.add(dto.getConfig()); } }
@Override protected void parseMember(JsonObject.Member member) { super.parseMember(member); try { JsonValue value = member.getValue(); String memberName = member.getName(); if ("roles".equals(memberName)) { this.writable = value .asArray() .values() .stream() .filter(JsonValue::isString) .map(JsonValue::asString) .anyMatch("write"::equalsIgnoreCase); } else if ("link".equals(memberName)) { link = new OneDriveSharingLink(value.asObject()); } else if ("grantedTo".equals(memberName)) { grantedTo = new OneDriveIdentitySet(value.asObject()); } else if ("inheritedFrom".equals(memberName)) { JsonObject valueObject = value.asObject(); String id = valueObject.get("id").asString(); OneDriveFolder inheritedFromFolder = new OneDriveFolder(getApi(), id); inheritedFrom = inheritedFromFolder.new Reference(valueObject); } else if ("shareId".equals(memberName)) { shareId = value.asString(); } } catch (ParseException e) { throw new OneDriveRuntimeException("Parse failed, maybe a bug in client.", e); } }
static void assertJSONContainsClassName(JsonObject jsonObject, String className) { String stackTrace = jsonObject.get("stackTrace").toString(); assertTrue( format( "JSON for Management Center should contain stackTrace with class name '%s'%n%s", className, stackTrace), stackTrace.contains("$" + className + ".")); }
@Override public OneDriveItem.Metadata next() throws OneDriveRuntimeException { JsonObject nextObject = jsonObjectIterator.next(); String id = nextObject.get("id").asString(); OneDriveItem.Metadata nextMetadata; if (nextObject.get("folder") != null && !nextObject.get("folder").isNull()) { OneDriveFolder folder = new OneDriveFolder(api, id); nextMetadata = folder.new Metadata(nextObject); } else if (nextObject.get("file") != null && !nextObject.get("file").isNull()) { OneDriveFile file = new OneDriveFile(api, id); nextMetadata = file.new Metadata(nextObject); } else { throw new OneDriveRuntimeException("The object type is currently not handled."); } return nextMetadata; }
@Override public void loadAllContacts() throws ConnectionException { JsonObject object = Endpoints.GET_ALL_CONTACTS .open(this, getUsername(), "default") .as(JsonObject.class) .expect(200, "While loading contacts") .get(); for (JsonValue value : object.get("contacts").asArray()) { JsonObject obj = value.asObject(); if (obj.get("suggested") == null || !obj.get("suggested").asBoolean()) { if (!allContacts.containsKey(obj.get("id").asString())) { this.allContacts.put(obj.get("id").asString(), new ContactImpl(this, obj)); } } } }
@Test public void testPausingWanState() throws Exception { ChangeWanStateRequest changeWanStateRequest = new ChangeWanStateRequest("schema", "publisher", false); JsonObject jsonObject = new JsonObject(); changeWanStateRequest.writeResponse(managementCenterService, jsonObject); JsonObject result = (JsonObject) jsonObject.get("result"); assertNotEquals(ChangeWanStateRequest.SUCCESS, changeWanStateRequest.readResponse(result)); }
@Override public void process( TernDoc doc, IJSONObjectHelper jsonObjectHelper, Object jsonObject, ITernLintCollector collector) { if (!(jsonObject instanceof JsonObject)) { // TODO: support for Rhino server return; } JsonArray messages = (JsonArray) ((JsonObject) jsonObject).get(MESSAGES_FIELD); // $NON-NLS-1$ if (messages != null) { TernLintQuery query = (TernLintQuery) doc.getQuery(); if (query.isGroupByFiles()) { JsonObject filesObject = null; String file = null; for (JsonValue files : messages) { filesObject = (JsonObject) files; file = jsonObjectHelper.getText(filesObject.get(FILE_FIELD)); // $NON-NLS-1$ try { collector.startLint(file); JsonArray messagesFile = (JsonArray) filesObject.get(MESSAGES_FIELD); // $NON-NLS-1$ if (messagesFile != null) { addMessages(jsonObjectHelper, messagesFile, query, collector); } } finally { collector.endLint(file); } } } else { String file = doc.getQuery().getFile(); try { collector.startLint(file); addMessages(jsonObjectHelper, messages, query, collector); } finally { collector.endLint(file); } } } }
private String getEtag(URI uri) throws IOException { Response resp = adminExecutor.execute(Request.Get(uri)); HttpResponse httpResp = resp.returnResponse(); assertNotNull(httpResp); HttpEntity entity = httpResp.getEntity(); assertNotNull(entity); StatusLine statusLine = httpResp.getStatusLine(); assertNotNull(statusLine); assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode()); assertNotNull("content type not null", entity.getContentType()); assertEquals( "check content type", Representation.HAL_JSON_MEDIA_TYPE, entity.getContentType().getValue()); String content = EntityUtils.toString(entity); assertNotNull("", content); JsonObject json = null; try { json = JsonObject.readFrom(content); } catch (Throwable t) { fail("parsing received json"); } assertNotNull("check not null json", json); assertNotNull("check not null _etag", json.get("_etag")); assertTrue("check _etag is object", json.get("_etag").isObject()); assertNotNull("check not null _etag.$oid", json.get("_etag").asObject().get("$oid")); assertNotNull( "check _etag.$oid is string", json.get("_etag").asObject().get("$oid").isString()); return json.get("_etag").asObject().get("$oid").asString(); }
protected void addMessages( IJSONObjectHelper helper, JsonArray messages, TernLintQuery query, ITernLintCollector collector) { String messageId = null; String message = null; String severity = null; String file = null; JsonObject messageObject = null; for (JsonValue value : messages) { messageObject = (JsonObject) value; messageId = helper.getText(messageObject.get(MESSAGE_ID_FIELD)); message = query.formatMessage(helper.getText(messageObject.get(MESSAGE_FIELD))); severity = helper.getText(messageObject.get(MESSAGE_FIELD)); severity = helper.getText(messageObject.get(SEVERITY_FIELD)); Long startCh = helper.getCh(messageObject, FROM_FIELD); Long endCh = helper.getCh(messageObject, TO_FIELD); Long line = helper.getCh(messageObject, LINE_NUMBER_FIELD); file = helper.getText(messageObject.get(FILE_FIELD)); collector.addMessage( messageId, message, startCh, endCh, line, severity, file, messageObject, helper); } }
@Override public void updateContactList() throws ConnectionException { JsonObject obj = Endpoints.GET_ALL_CONTACTS .open(this, getUsername(), "notification") .as(JsonObject.class) .expect(200, "While loading contacts") .get(); for (JsonValue value : obj.get("contacts").asArray()) { if (value.asObject().get("suggested") == null || !value.asObject().get("suggested").asBoolean()) { String id = value.asObject().get("id").asString(); ContactImpl impl = (ContactImpl) allContacts.get(id); impl.update(value.asObject()); } } }
@Test public void testShutdownCluster() throws Exception { ShutdownClusterRequest request = new ShutdownClusterRequest(); cluster.getClusterState(); JsonObject jsonObject = new JsonObject(); request.writeResponse(managementCenterService, jsonObject); JsonObject result = (JsonObject) jsonObject.get("result"); assertEquals("SUCCESS", request.readResponse(result)); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertFalse(lifecycleService.isRunning()); } }); }
/** Tests GetAllMetrics method. */ @Test public void testGetAllMetrics() { Counter onosCounter = new Counter(); onosCounter.inc(); Meter onosMeter = new Meter(); onosMeter.mark(); Timer onosTimer = new Timer(); onosTimer.update(1, TimeUnit.MILLISECONDS); ImmutableMap<String, Metric> metrics = new ImmutableMap.Builder<String, Metric>() .put("onosCounter", onosCounter) .put("onosMeter", onosMeter) .put("onosTimer", onosTimer) .build(); expect(mockMetricsService.getMetrics()).andReturn(metrics).anyTimes(); replay(mockMetricsService); WebTarget wt = target(); String response = wt.path("metrics").request().get(String.class); assertThat(response, containsString("{\"metrics\":[")); JsonObject result = Json.parse(response).asObject(); assertThat(result, notNullValue()); JsonArray jsonMetrics = result.get("metrics").asArray(); assertThat(jsonMetrics, notNullValue()); assertThat(jsonMetrics.size(), is(3)); assertTrue( matchesMetric(metrics.get("onosCounter")).matchesSafely(jsonMetrics.get(0).asObject())); assertTrue( matchesMetric(metrics.get("onosMeter")).matchesSafely(jsonMetrics.get(1).asObject())); assertTrue( matchesMetric(metrics.get("onosTimer")).matchesSafely(jsonMetrics.get(2).asObject())); }
/** Tests the result of the rest api GET when there is a config. */ @Test public void testConfigs() { setUpConfigData(); final WebTarget wt = target(); final String response = wt.path("network/configuration").request().get(String.class); final JsonObject result = Json.parse(response).asObject(); Assert.assertThat(result, notNullValue()); Assert.assertThat(result.names(), hasSize(2)); JsonValue devices = result.get("devices"); Assert.assertThat(devices, notNullValue()); JsonValue device1 = devices.asObject().get("device1"); Assert.assertThat(device1, notNullValue()); JsonValue basic = device1.asObject().get("basic"); Assert.assertThat(basic, notNullValue()); checkBasicAttributes(basic); }
@Override public void generateBuildScript(Configuration cfg, Context ctx) { try { final InputStream input = this.getClass().getClassLoader().getResourceAsStream("javascript/lib/package.json"); final List<String> packLines = IOUtils.readLines(input); String pack = ""; for (String line : packLines) { pack += line + "\n"; } input.close(); pack = pack.replace("<NAME>", cfg.getName()); final JsonObject json = JsonObject.readFrom(pack); final JsonValue deps = json.get("dependencies"); for (Thing t : cfg.allThings()) { for (String dep : t.annotation("js_dep")) { deps.asObject().add(dep.split(":")[0].trim(), dep.split(":")[1].trim()); } } for (Thing t : cfg.allThings()) { if (t.getStreams().size() > 0) { deps.asObject().add("rx", "^2.5.3"); deps.asObject().add("events", "^1.0.2"); break; } } final File f = new File(ctx.getOutputDirectory() + "/" + cfg.getName() + "/package.json"); f.setWritable(true); final PrintWriter w = new PrintWriter(new FileWriter(f)); w.println(json.toString()); w.close(); } catch (Exception e) { e.printStackTrace(); } }
@Override public boolean matchesSafely(JsonObject jsonObject) { JsonObject jsonMetric = jsonObject.get("metric").asObject(); JsonObject jsonCounter; JsonObject jsonMeter; JsonObject jsonTimer; Counter counter; Meter meter; Timer timer; // check counter metric if (jsonMetric.get("counter") != null) { jsonCounter = jsonMetric.get("counter").asObject(); counter = (Counter) metric; if (jsonCounter.get("counter").asLong() != counter.getCount()) { reason = "counter " + counter.getCount(); return false; } } // check meter metric if (jsonMetric.get("meter") != null) { jsonMeter = jsonMetric.get("meter").asObject(); meter = (Meter) metric; if (jsonMeter.get("counter").asLong() != meter.getCount()) { reason = "counter " + meter.getCount(); return false; } if (jsonMeter.get("1_min_rate").asDouble() != meter.getOneMinuteRate()) { reason = "1 minute rate " + meter.getOneMinuteRate(); return false; } if (jsonMeter.get("5_min_rate").asDouble() != meter.getOneMinuteRate()) { reason = "5 minute rate " + meter.getFiveMinuteRate(); return false; } if (jsonMeter.get("15_min_rate").asDouble() != meter.getFifteenMinuteRate()) { reason = "15 minute rate " + meter.getFifteenMinuteRate(); return false; } } if (jsonMetric.get("timer") != null) { jsonTimer = jsonMetric.get("timer").asObject(); timer = (Timer) metric; if (jsonTimer.get("counter").asLong() != timer.getCount()) { reason = "counter " + timer.getCount(); return false; } if (jsonTimer.get("1_min_rate").asDouble() != timer.getOneMinuteRate()) { reason = "1 minute rate " + timer.getOneMinuteRate(); return false; } if (jsonTimer.get("5_min_rate").asDouble() != timer.getOneMinuteRate()) { reason = "5 minute rate " + timer.getFiveMinuteRate(); return false; } if (jsonTimer.get("15_min_rate").asDouble() != timer.getFifteenMinuteRate()) { reason = "15 minute rate " + timer.getFifteenMinuteRate(); return false; } if (jsonTimer.get("mean").asDouble() != nanoToMs(timer.getSnapshot().getMean())) { reason = "mean " + timer.getSnapshot().getMean(); return false; } if (jsonTimer.get("min").asDouble() != nanoToMs(timer.getSnapshot().getMin())) { reason = "min " + timer.getSnapshot().getMin(); return false; } if (jsonTimer.get("max").asDouble() != nanoToMs(timer.getSnapshot().getMax())) { reason = "max " + timer.getSnapshot().getMax(); return false; } if (jsonTimer.get("stddev").asDouble() != nanoToMs(timer.getSnapshot().getStdDev())) { reason = "stddev " + timer.getSnapshot().getStdDev(); return false; } } return true; }
public static Integer getInteger(JsonObject obj, String name) { JsonValue value = obj.get(name); return value != null ? value.asInt() : null; }
private void _testGetAggregation(String uri) throws Exception { Response resp; URI aggrUri = buildURI( "/" + dbTmpName + "/" + collectionTmpName + "/" + RequestContext._AGGREGATIONS + "/" + uri); resp = adminExecutor.execute(Request.Get(aggrUri)); HttpResponse httpResp = resp.returnResponse(); assertNotNull(httpResp); HttpEntity entity = httpResp.getEntity(); assertNotNull(entity); StatusLine statusLine = httpResp.getStatusLine(); assertNotNull(statusLine); assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode()); assertNotNull("content type not null", entity.getContentType()); assertEquals( "check content type", Representation.HAL_JSON_MEDIA_TYPE, entity.getContentType().getValue()); String content = EntityUtils.toString(entity); assertNotNull("", content); JsonObject json = null; try { json = JsonObject.readFrom(content); } catch (Throwable t) { fail("parsing received json"); } assertNotNull("check not null json response", json); assertNotNull("check not null _embedded", json.get("_embedded")); assertTrue("check _embedded", json.get("_embedded").isObject()); assertNotNull("", json.get("_embedded").asObject().get("rh:result")); assertTrue( "check _embedded[\"rh:result\"]", json.get("_embedded").asObject().get("rh:result").isArray()); JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray(); assertTrue("check we have 2 results", results.size() == 2); results .values() .stream() .map( (v) -> { assertNotNull("check not null _id property", v.asObject().get("_id")); return v; }) .map( (v) -> { assertTrue( "check results _id property is string", v.asObject().get("_id").isString()); return v; }) .map( (v) -> { assertNotNull("check not null value property", v.asObject().get("value")); return v; }) .forEach( (v) -> { assertTrue( "check results value property is number", v.asObject().get("value").isNumber()); }); }
private static void processArchive( final Path path, final Set<Archive> output, final SettingsModelFactory settingsFactory) throws IOException, StarDBException { Archive originalArchive = new Archive(path); if (!originalArchive.extract()) { throw new IOException("Could not extract archive."); } Set<ArchiveFile> usedPaks = new HashSet<>(); for (ArchiveFile file : originalArchive.getFiles()) { if (FileHelper.getExtension(file.getPath()).equals("modinfo")) { JsonObject o = JsonObject.readFrom(new String(file.getData())); String preformattedPath = o.get("path").asString().replaceAll("\"", ""); if (preformattedPath.startsWith("./")) { preformattedPath = preformattedPath.replace("./", ""); } if (preformattedPath.startsWith(".")) { preformattedPath = preformattedPath.replace(".", ""); } if (preformattedPath.startsWith("/")) { preformattedPath = preformattedPath.replace("/", ""); } Path modinfoPath = file.getPath(); if (modinfoPath.getNameCount() > 2) { modinfoPath = modinfoPath.subpath(0, modinfoPath.getNameCount() - 1); } else if (modinfoPath.getNameCount() > 1) { modinfoPath = modinfoPath.getName(0); } else { modinfoPath = Paths.get(""); } Archive outputArchive = new Archive( settingsFactory .getInstance() .getPropertyPath("modsdir") .resolve(Paths.get(o.get("name").asString() + ".zip"))); if (file.getPath().getNameCount() == 1) { outputArchive.addFile(new ArchiveFile(file.getData(), file.getPath(), false)); log.debug( "'{}' -> '{}' relativized to '{}'", modinfoPath, file.getPath(), file.getPath()); } else { outputArchive.addFile( new ArchiveFile( file.getData(), modinfoPath.relativize(file.getPath()).normalize(), false)); log.debug( "'{}' -> '{}' relativized to '{}'", modinfoPath, file.getPath(), modinfoPath.relativize(file.getPath()).normalize()); } Path assetsPath = modinfoPath.resolve(Paths.get(preformattedPath)); log.debug("Assets path for modinfo '{}': {}", file.getPath(), assetsPath); if (originalArchive.getFile(assetsPath) != null && !assetsPath.toString().isEmpty() && !originalArchive.getFile(assetsPath).isFolder()) { if (FileHelper.identifyType(originalArchive.getFile(assetsPath).getData()) .equals("pak")) { log.debug("Assets for mod '{}' identified as .pak file: {}", modinfoPath, assetsPath); usedPaks.add(originalArchive.getFile(assetsPath)); ArchiveFile modinfo = outputArchive.getFile(".modinfo"); JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData())); o2.set("path", "assets"); modinfo.setData(o2.toString().getBytes()); // TODO Update StarDB to let me pass in a byte array instead of needing to open a file Path tempPath = Paths.get("tempPak" + System.nanoTime()); SeekableByteChannel tempPakFile = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE); tempPakFile.write(ByteBuffer.wrap(originalArchive.getFile(assetsPath).getData())); tempPakFile.close(); AssetDatabase database = AssetDatabase.open(tempPath); for (String assetFile : database.getFileList()) { outputArchive.addFile( new ArchiveFile( database.getAsset(assetFile), Paths.get("assets/" + assetFile), false)); log.trace("Asset extracted: {} | {}", assetFile, Paths.get("assets/" + assetFile)); } Files.deleteIfExists(tempPath); } } else { ArchiveFile modinfo = outputArchive.getFile(".modinfo"); JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData())); o2.set("path", "assets"); modinfo.setData(o2.toString().getBytes()); log.debug("Assets for mod '{}' is a standard assets folder: {}", modinfoPath, assetsPath); for (ArchiveFile f2 : originalArchive.getFiles()) { if ((assetsPath.toString().isEmpty() || f2.getPath().startsWith(assetsPath)) && !f2.isFolder() && !f2.getPath().toString().endsWith(".modinfo")) { if (modinfoPath.toString().isEmpty()) { if (f2.getPath().getNameCount() == 1) { if (!f2.getPath().startsWith(assetsPath)) { outputArchive.addFile( new ArchiveFile( f2.getData(), Paths.get("assets/").resolve(f2.getPath()).normalize(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), Paths.get("assets/").resolve(f2.getPath()).normalize()); } else { outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), f2.getPath()); } } else { if (!f2.getPath().startsWith(assetsPath)) { outputArchive.addFile( new ArchiveFile( f2.getData(), Paths.get("assets/").resolve(f2.getPath()).normalize(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), Paths.get("assets/").resolve(f2.getPath()).normalize()); } else { outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), f2.getPath()); } } } else { if (f2.getPath().getNameCount() == 1) { if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) { outputArchive.addFile( new ArchiveFile( f2.getData(), Paths.get("assets/").resolve(f2.getPath()), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), Paths.get("assets/").resolve(f2.getPath())); } else { outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), f2.getPath()); } } else { if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) { outputArchive.addFile( new ArchiveFile( f2.getData(), Paths.get("assets/") .resolve(modinfoPath.relativize(f2.getPath()).normalize()), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), Paths.get("assets/") .resolve(modinfoPath.relativize(f2.getPath()).normalize())); } else { outputArchive.addFile( new ArchiveFile( f2.getData(), modinfoPath.relativize(f2.getPath()).normalize(), false)); log.trace( "'{}' -> '{}' relativized to '{}'", modinfoPath, f2.getPath(), modinfoPath.relativize(f2.getPath()).normalize()); } } } } } } outputArchive.writeToFile( settingsFactory .getInstance() .getPropertyPath("modsdir") .resolve(Paths.get(o.get("name").asString() + ".zip")) .toFile()); // TODO output.add(outputArchive); } } for (ArchiveFile file : originalArchive.getFiles()) { if (!usedPaks.contains(file) && !file.isFolder() && FileHelper.identifyType(file.getData()).equals("pak")) { log.debug("Additional .pak identified: {}", file.getPath()); Path tempPath = Paths.get("tempPak" + System.nanoTime()); SeekableByteChannel tempPakFile = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE); tempPakFile.write(ByteBuffer.wrap(file.getData())); tempPakFile.close(); AssetDatabase database = AssetDatabase.open(tempPath); log.debug(database.getFileList()); if (database.getAsset("/pak.modinfo") != null) { log.debug("{} has a .modinfo file, parsing into mod.", file.getPath()); processPakFile(tempPath, output, settingsFactory); } else { log.debug("{} does not contain a .modinfo file, skipping.", file.getPath()); } Files.deleteIfExists(tempPath); } } }
public static Boolean getBoolean(JsonObject obj, String name) { JsonValue value = obj.get(name); return value != null ? value.asBoolean() : null; }
public static Set<Mod> load( final Path path, final int order, final SettingsModelFactory settingsFactory, final DatabaseModelFactory databaseFactory, final LocalizerModelFactory localizerFactory) { log.debug("Loading mod: {}", path); Set<Mod> mods = new HashSet<>(); // TODO Count all mods and send back progress info Set<Archive> archives = null; try { archives = processModFile(path, settingsFactory); } catch (IOException | StarDBException | ParseException e) { // TODO Error Dialogue log.error("", e); return new HashSet<Mod>(); } for (Archive archive : archives) { Mod mod = new Mod(localizerFactory, settingsFactory); mod.setOrder(order); mod.files = new HashSet<>(); mod.setArchiveName(archive.getFileName()); // Get the modinfo file and parse it JsonObject obj = JsonObject.readFrom(new String(archive.getFile(".modinfo").getData())); mod.setInternalName(obj.get("name").asString()); if (obj.get("version") != null) { mod.setGameVersion(obj.get("version").asString()); } else { mod.setGameVersion("Field Empty"); } Set<String> dependencies = new HashSet<>(); Set<String> ignoredFileNames = new HashSet<>(); if (obj.get("dependencies") != null) { JsonArray arr = obj.get("dependencies").asArray(); for (int i = 0; i < arr.size(); i++) { dependencies.add(arr.get(i).asString()); } } mod.setDependencies(dependencies); if (obj.get("metadata") != null) { JsonObject metadata = obj.get("metadata").asObject(); mod.setDisplayName(JSONHelper.getString(metadata, "displayname", mod.getInternalName())); mod.setAuthor(JSONHelper.getString(metadata, "author", NO_AUTHOR)); mod.setDescription(JSONHelper.getString(metadata, "description", NO_DESCRIPTION)); mod.setURL(JSONHelper.getString(metadata, "support_url", "")); mod.setModVersion(JSONHelper.getString(metadata, "version", NO_VERSION)); if (obj.get("ignoredfiles") != null) { JsonArray arr = obj.get("ignoredfiles").asArray(); for (int i = 0; i < arr.size(); i++) { ignoredFileNames.add(arr.get(i).asString()); } } } else { mod.setDisplayName(mod.getInternalName()); mod.setAuthor(NO_AUTHOR); mod.setDescription(NO_DESCRIPTION); mod.setURL(""); mod.setModVersion(NO_VERSION); } try { mod.setChecksum( FileHelper.getChecksum( new File( settingsFactory.getInstance().getPropertyString("modsdir") + File.separator + mod.archiveName) .toPath())); // TODO Better Path manipulation } catch (IOException e) { log.error("Setting Checksum", e); } for (ArchiveFile archiveFile : archive.getFiles()) { ModFile modFile = new ModFile(); modFile.setPath(archiveFile.getPath()); // Find and list all ignored files for (String ignored : ignoredFileNames) { if (archiveFile.getPath().endsWith(ignored) || archiveFile.getPath().endsWith(".txt")) { modFile.setIgnored(true); } } // Scan all json files and find those with mergeability if (!archiveFile.isFolder() && FileHelper.isJSON(archiveFile.getPath())) { modFile.setJson(true); String fileContents = new String(archiveFile.getData()); if (fileContents.contains("__merge")) { modFile.setAutoMerged(true); } } if (!archiveFile.isFolder()) { mod.files.add(modFile); } } try { databaseFactory.getInstance().updateMod(mod); } catch (SQLException e) { log.error("", e); MessageDialogue dialogue = new MessageDialogue( localizer.getMessage("mod.dbconnectionerror"), localizer.getMessage("mod.dbconnectionerror.title"), MessageType.ERROR, new LocalizerFactory()); dialogue.getResult(); return new HashSet<Mod>(); } mods.add(mod); } return mods; }
public static JsonArray getArray(JsonObject obj, String name) { JsonValue value = obj.get(name); return value != null ? value.asArray() : null; }
@Test public void testGetMapReduceWithVariable() throws Exception { String uri = "avg_ages"; String aggregationsMetadata = "{\"aggrs\": [" + "{" + "\"type\":\"mapReduce\"" + "," + "\"uri\": \"" + uri + "\"," + "\"map\": \"function() { var minage = JSON.parse($vars).minage; if (this.age > minage ) { emit(this.name, this.age); }; }\"," + "\"reduce\":\"function(key, values) { return Array.avg(values); }\"" + "," + "\"query\":{\"name\":{\"_$var\":\"name\"}}" + "}]}"; createTmpCollection(); createMetadataAndTestData(aggregationsMetadata); Response resp; URI aggrUri = buildURI( "/" + dbTmpName + "/" + collectionTmpName + "/" + RequestContext._AGGREGATIONS + "/" + uri, new NameValuePair[] { new BasicNameValuePair("avars", "{\"name\": \"a\", \"minage\": 20}") }); resp = adminExecutor.execute(Request.Get(aggrUri)); HttpResponse httpResp = resp.returnResponse(); assertNotNull(httpResp); HttpEntity entity = httpResp.getEntity(); assertNotNull(entity); StatusLine statusLine = httpResp.getStatusLine(); assertNotNull(statusLine); assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode()); assertNotNull("content type not null", entity.getContentType()); assertEquals( "check content type", Representation.HAL_JSON_MEDIA_TYPE, entity.getContentType().getValue()); String content = EntityUtils.toString(entity); assertNotNull("", content); JsonObject json = null; try { json = JsonObject.readFrom(content); } catch (Throwable t) { fail("parsing received json"); } assertNotNull("check not null json response", json); assertNotNull("check not null _embedded", json.get("_embedded")); assertTrue("check _embedded", json.get("_embedded").isObject()); assertNotNull("", json.get("_embedded").asObject().get("rh:result")); assertTrue( "check _embedded[\"rh:results\"]", json.get("_embedded").asObject().get("rh:result").isArray()); JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray(); assertTrue("check we have 2 results", results.size() == 1); results .values() .stream() .map( (v) -> { assertNotNull("check not null _id property", v.asObject().get("_id")); return v; }) .map( (v) -> { assertTrue( "check results _id property is string", v.asObject().get("_id").isString()); return v; }) .map( (v) -> { assertTrue( "check results _id property is a", v.asObject().get("_id").asString().equals("a")); return v; }) .map( (v) -> { assertNotNull("check not null value property", v.asObject().get("value")); return v; }) .forEach( (v) -> { assertTrue( "check results value property is number", v.asObject().get("value").isNumber()); }); }
// main program public static void main(String[] args) throws Exception { String parfile = ""; Connection conn = null; Statement stmt = null; ResultSet rs = null; ResultSetMetaData rsmd = null; int n = 0; long oldTime, curTime, sumTime, avgTime = 0; int rows = 0; try { parfile = args[0]; } catch (Exception exc) { System.out.println("Please use SpoolIt [configuration file]"); System.exit(1); } try { System.out.println("------------------------------------------------------------"); System.out.println("spoolIt Version " + _APP_VERSION); System.out.println("------------------------------------------------------------"); System.out.println("Report bugs to : [email protected]"); // record start time oldTime = System.currentTimeMillis(); // read parameter file try { /* * Example of configuration * { "DATABASE": "oracle", "INSTANCE": "your-database-instance-or-sid", "HOST": "your-database-ip-address", "PORT": "1521", "USER": "******", "PASSWORD": "******", "QUERY" : "SELECT * FROM DUAL WHERE ROWNUM <= 3", "OUTFILE": "/your/path/filename.dat", "DELIMITER": "|" } * */ BufferedReader in = new BufferedReader(new FileReader(parfile)); jsonObject = JsonObject.readFrom(in); jsonObject.add("CONNECTION_STRING", ""); } catch (IOException e) { } System.out.print("\n-------------------- Configuration -------------------------\n"); System.out.println("Database product : " + jsonObject.get("DATABASE").asString()); System.out.println("Database Instance : " + jsonObject.get("INSTANCE").asString()); System.out.println("Database Port : " + jsonObject.get("PORT").asString()); System.out.println("Database User ID : " + jsonObject.get("USER").asString()); System.out.println("SQL Query for export : " + jsonObject.get("QUERY").asString()); System.out.println("\n-------------------- Dump File -----------------------------"); System.out.println("Path : " + jsonObject.get("OUTFILE").asString()); System.out.println( "Type : Variable length separated by delimeter \"" + jsonObject.get("DELIMITER").asString() + "\""); System.out.println("Rows Fetch Size : " + FETCHSIZE); // load driver LoadDriver ld = new LoadDriver(); ld.loadNow(jsonObject.get("DATABASE").asString()); // create connection to database if (jsonObject.get("DATABASE").asString().equals("mysql")) { jsonObject.set( "CONNECTION_STRING", "jdbc:" + jsonObject.get("DATABASE").asString() + "://" + jsonObject.get("HOST").asString() + "/" + jsonObject.get("INSTANCE").asString() + "?user="******"USER").asString() + "&password="******"PASSWORD").asString()); // System.out.println("Connection string : " + // jsonObject.get("CONNECTION_STRING").asString() ); conn = DriverManager.getConnection(jsonObject.get("CONNECTION_STRING").asString()); } else if (jsonObject.get("DATABASE").asString().equals("oracle")) { /* * Connection string using service name jdbc:oracle:thin:@//oracle.hostserver2.mydomain.ca:1522/ABCD * * Connection string using tnsname jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522:ABCD * */ // jsonObject.set("CONNECTION_STRING", "jdbc:oracle:thin:@" + // jsonObject.get("HOST").asString() + // ":" + jsonObject.get("PORT").asString() + ":" + jsonObject.get("INSTANCE").asString() ); jsonObject.set( "CONNECTION_STRING", "jdbc:oracle:thin:@//" + jsonObject.get("HOST").asString() + ":" + jsonObject.get("PORT").asString() + "/" + jsonObject.get("INSTANCE").asString()); // System.out.println("Connection string : " + // jsonObject.get("CONNECTION_STRING").asString() ); conn = DriverManager.getConnection( jsonObject.get("CONNECTION_STRING").asString(), jsonObject.get("USER").asString(), jsonObject.get("PASSWORD").asString()); } else if (jsonObject.get("DATABASE").asString().equals("teradata")) { conn = DriverManager.getConnection( "jdbc:teradata://" + jsonObject.get("HOST").asString() + "/DATABASE=" + jsonObject.get("INSTANCE").asString() + ",TMODE=ANSI,CHARSET=UTF8,TYPE=FASTEXPORT", jsonObject.get("USER").asString(), jsonObject.get("PASSWORD").asString()); } else if (jsonObject.get("DATABASE").asString().equals("impala")) { // jdbc:hive2://myhost.example.com:21050/test_db;auth=noSasl conn = DriverManager.getConnection( "jdbc:hive2://" + jsonObject.get("HOST").asString() + ":" + jsonObject.get("PORT").asString() + "/" + jsonObject.get("INSTANCE").asString() + ";auth=noSasl"); } else if (jsonObject.get("DATABASE").asString().equals("hive")) { // jdbc:hive2://localhost:10000/default conn = DriverManager.getConnection( "jdbc:hive2://" + jsonObject.get("HOST").asString() + ":" + jsonObject.get("PORT").asString() + "/" + jsonObject.get("INSTANCE").asString()); } else if (jsonObject.get("DATABASE").asString().equals("db2")) { // String url = "jdbc:db2://myhost:5021/mydb:" + "user=dbadm;password=dbadm;" conn = DriverManager.getConnection( "jdbc:db2://" + jsonObject.get("HOST").asString() + ":" + jsonObject.get("PORT").asString() + "/" + jsonObject.get("INSTANCE").asString() + ":user="******"USER").asString() + ";password="******"PASSWORD").asString() + ";"); } // Implementation try { PreparedStatement ps = conn.prepareStatement(jsonObject.get("QUERY").asString()); // stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); // stmt.setFetchSize(FETCHSIZE); System.out.print("\nExecuting query..."); // change fetch size on result set // rs.setFetchSize(FETCHSIZE); // rs.setFetchDirection(ResultSet.FETCH_FORWARD); // rs = stmt.executeQuery(vQuery); rs = ps.executeQuery(); if (rs != null) { System.out.print("Done\n"); // change fetch size on result set // rs = stmt.getResultSet(); // rs.setFetchSize(FETCHSIZE); rsmd = rs.getMetaData(); n = rsmd.getColumnCount(); // fetch data and save to file BufferedWriter out = new BufferedWriter(new FileWriter(jsonObject.get("OUTFILE").asString())); String tmpStr = ""; System.out.print("Exporting data to " + jsonObject.get("OUTFILE").asString() + "\n"); while (rs.next()) { try { tmpStr = ""; for (int index = 1; index <= n; index++) { if (index != n) tmpStr = tmpStr + rs.getString(index) + jsonObject.get("DELIMITER").asString(); else tmpStr = tmpStr + rs.getString(index) + "\n"; } // tmpStr = rs.getString(1) + "\n"; out.write(tmpStr); rows++; } catch (IOException e) { } // sleep milisecond } // close dump file out.close(); System.out.println("Export succesfully"); } } finally { try { if (rs != null) { rs.close(); } rs = null; } catch (SQLException sqlEx) { } } try { // release connection conn.close(); } catch (SQLException sqlEx) { } // summary of time execution curTime = System.currentTimeMillis(); sumTime = (curTime - oldTime); System.out.println("\n-------------------- Performance ---------------------------"); System.out.println("Total rows exported : " + rows + " rows"); System.out.println("Overall dump speed : " + sumTime + " miliseconds"); } catch (SQLException ex) { System.out.println("SQLException : " + ex.getMessage()); } }