Search in sources :

Example 96 with JsonReader

use of javax.json.JsonReader in project dataverse by IQSS.

the class Admin method convertUserFromBcryptToSha1.

/**
 * This method is used by an integration test in UsersIT.java to exercise
 * bug https://github.com/IQSS/dataverse/issues/3287 . Not for use by users!
 */
@Path("convertUserFromBcryptToSha1")
@POST
public Response convertUserFromBcryptToSha1(String json) {
    JsonReader jsonReader = Json.createReader(new StringReader(json));
    JsonObject object = jsonReader.readObject();
    jsonReader.close();
    BuiltinUser builtinUser = builtinUserService.find(new Long(object.getInt("builtinUserId")));
    // password is "sha-1Pass", 0 means SHA-1
    builtinUser.updateEncryptedPassword("4G7xxL9z11/JKN4jHPn4g9iIQck=", 0);
    BuiltinUser savedUser = builtinUserService.save(builtinUser);
    return ok("foo: " + savedUser);
}
Also used : BuiltinUser(edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 97 with JsonReader

use of javax.json.JsonReader in project dataverse by IQSS.

the class DataCaptureModuleServiceBeanIT method testUploadRequestAndScriptRequest.

@Test
public void testUploadRequestAndScriptRequest() throws InterruptedException, DataCaptureModuleException {
    // The DCM Vagrant box runs on port 8888: https://github.com/sbgrid/data-capture-module/blob/master/Vagrantfile
    String dcmVagrantUrl = "http://localhost:8888";
    // The DCM mock runs on port 5000: https://github.com/sbgrid/data-capture-module/blob/master/doc/mock.md
    String dcmMockUrl = "http://localhost:5000";
    String dcmBaseUrl = dcmMockUrl;
    DataCaptureModuleServiceBean dataCaptureModuleServiceBean = new DataCaptureModuleServiceBean();
    // Step 1: Upload request
    AuthenticatedUser authenticatedUser = makeAuthenticatedUser("Lauren", "Ipsum");
    Dataset dataset = new Dataset();
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    long timeInMillis = calendar.getTimeInMillis();
    String ident = Long.toString(timeInMillis);
    dataset.setIdentifier(ident);
    String jsonString = DataCaptureModuleUtil.generateJsonForUploadRequest(authenticatedUser, dataset).toString();
    logger.info("jsonString: " + jsonString);
    UploadRequestResponse uploadRequestResponse = dataCaptureModuleServiceBean.requestRsyncScriptCreation(jsonString, dcmBaseUrl + DataCaptureModuleServiceBean.uploadRequestPath);
    System.out.println("out: " + uploadRequestResponse.getResponse());
    assertEquals(200, uploadRequestResponse.getHttpStatusCode());
    String uploadRequestResponseString = uploadRequestResponse.getResponse();
    JsonReader jsonReader = Json.createReader(new StringReader((String) uploadRequestResponseString));
    JsonObject jsonObject = jsonReader.readObject();
    assertEquals("OK", jsonObject.getString("status"));
    // If you comment this out, expect to see a 404 when you try to download the script.
    sleep(DataCaptureModuleServiceBean.millisecondsToSleepBetweenUploadRequestAndScriptRequestCalls);
    // Step 2: Script request.
    ScriptRequestResponse scriptRequestResponseGood = dataCaptureModuleServiceBean.retreiveRequestedRsyncScript(dataset.getIdentifier(), dcmBaseUrl + DataCaptureModuleServiceBean.scriptRequestPath);
    System.out.println("script: " + scriptRequestResponseGood.getScript());
    assertNotNull(scriptRequestResponseGood.getScript());
    assertTrue(scriptRequestResponseGood.getScript().startsWith("#!"));
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) Calendar(java.util.Calendar) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) MocksFactory.makeAuthenticatedUser(edu.harvard.iq.dataverse.mocks.MocksFactory.makeAuthenticatedUser) Test(org.junit.Test)

Example 98 with JsonReader

use of javax.json.JsonReader in project dataverse by IQSS.

the class SchemaDotOrgExporterTest method testExportDataset.

/**
 * Test of exportDataset method, of class SchemaDotOrgExporter.
 */
@Test
public void testExportDataset() throws Exception {
    System.out.println("exportDataset");
    File datasetVersionJson = new File("src/test/resources/json/dataset-finch1.json");
    String datasetVersionAsJson = new String(Files.readAllBytes(Paths.get(datasetVersionJson.getAbsolutePath())));
    JsonReader jsonReader1 = Json.createReader(new StringReader(datasetVersionAsJson));
    JsonObject json1 = jsonReader1.readObject();
    JsonParser jsonParser = new JsonParser(datasetFieldTypeSvc, null, null);
    DatasetVersion version = jsonParser.parseDatasetVersion(json1.getJsonObject("datasetVersion"));
    version.setVersionState(DatasetVersion.VersionState.RELEASED);
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    Date publicationDate = dateFmt.parse("19551105");
    version.setReleaseTime(publicationDate);
    version.setVersionNumber(1l);
    // TODO: It might be nice to test TermsOfUseAndAccess some day
    version.setTermsOfUseAndAccess(null);
    Dataset dataset = new Dataset();
    dataset.setProtocol("doi");
    dataset.setAuthority("myAuthority");
    dataset.setIdentifier("myIdentifier");
    version.setDataset(dataset);
    Dataverse dataverse = new Dataverse();
    dataverse.setName("LibraScholar");
    dataset.setOwner(dataverse);
    System.setProperty(SITE_URL, "https://librascholar.org");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    schemaDotOrgExporter.exportDataset(version, json1, byteArrayOutputStream);
    String jsonLd = byteArrayOutputStream.toString();
    System.out.println("schema.org JSON-LD: " + JsonUtil.prettyPrint(jsonLd));
    JsonReader jsonReader2 = Json.createReader(new StringReader(jsonLd));
    JsonObject json2 = jsonReader2.readObject();
    assertEquals("http://schema.org", json2.getString("@context"));
    assertEquals("Dataset", json2.getString("@type"));
    assertEquals("https://doi.org/myAuthority/myIdentifier", json2.getString("identifier"));
    assertEquals("Darwin's Finches", json2.getString("name"));
    assertEquals("Finch, Fiona", json2.getJsonArray("author").getJsonObject(0).getString("name"));
    assertEquals("Birds Inc.", json2.getJsonArray("author").getJsonObject(0).getString("affiliation"));
    assertEquals("1955-11-05", json2.getString("dateModified"));
    assertEquals("1", json2.getString("version"));
    assertEquals("Darwin's finches (also known as the Galápagos finches) are a group of about fifteen species of passerine birds.", json2.getString("description"));
    assertEquals("Medicine, Health and Life Sciences", json2.getJsonArray("keywords").getString(0));
    assertEquals("https://schema.org/version/3.3", json2.getString("schemaVersion"));
    assertEquals("DataCatalog", json2.getJsonObject("includedInDataCatalog").getString("@type"));
    assertEquals("LibraScholar", json2.getJsonObject("includedInDataCatalog").getString("name"));
    assertEquals("https://librascholar.org", json2.getJsonObject("includedInDataCatalog").getString("url"));
    assertEquals("Organization", json2.getJsonObject("provider").getString("@type"));
    assertEquals("Dataverse", json2.getJsonObject("provider").getString("name"));
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Dataverse(edu.harvard.iq.dataverse.Dataverse) Date(java.util.Date) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) Test(org.junit.Test)

Example 99 with JsonReader

use of javax.json.JsonReader in project dataverse by IQSS.

the class AbstractApiBeanTest method testMessagesNoJsonObject.

@Test
public void testMessagesNoJsonObject() {
    String message = "myMessage";
    Response response = sut.ok(message);
    JsonReader jsonReader = Json.createReader(new StringReader((String) response.getEntity().toString()));
    JsonObject jsonObject = jsonReader.readObject();
    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jwf = Json.createWriterFactory(config);
    StringWriter sw = new StringWriter();
    try (JsonWriter jsonWriter = jwf.createWriter(sw)) {
        jsonWriter.writeObject(jsonObject);
    }
    logger.info(sw.toString());
    assertEquals(message, jsonObject.getJsonObject("data").getString("message"));
}
Also used : MockResponse(edu.harvard.iq.dataverse.util.MockResponse) Response(javax.ws.rs.core.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonWriter(javax.json.JsonWriter) JsonWriterFactory(javax.json.JsonWriterFactory) Test(org.junit.Test)

Example 100 with JsonReader

use of javax.json.JsonReader in project solr-document-store by DBCDK.

the class AsyncJobWebSocketServer method message.

@OnMessage
public void message(String message, Session session) {
    log.info("Recieved message: {}", message);
    try (JsonReader reader = Json.createReader(new StringReader(message))) {
        JsonObject jsonMessage = reader.readObject();
        String uuid = jsonMessage.getString("uuid");
        switch(jsonMessage.getString("type")) {
            case "Requesting subscription":
                sessionHandler.subscribe(session, uuid);
                break;
            case "Requesting unsubscription":
                sessionHandler.unsubscribe(session, uuid);
                break;
        }
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) OnMessage(javax.websocket.OnMessage)

Aggregations

JsonReader (javax.json.JsonReader)130 JsonObject (javax.json.JsonObject)110 StringReader (java.io.StringReader)78 Test (org.junit.Test)47 JsonArray (javax.json.JsonArray)44 JsonString (javax.json.JsonString)42 HashMap (java.util.HashMap)21 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)13 File (java.io.File)10 LinkedHashMap (java.util.LinkedHashMap)10 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)9 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)8 InputStream (java.io.InputStream)7 Gson (com.google.gson.Gson)6 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)6 Response (com.ning.http.client.Response)6 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)5