Example usage for com.google.gson JsonObject JsonObject

List of usage examples for com.google.gson JsonObject JsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject JsonObject.

Prototype

JsonObject

Source Link

Usage

From source file:app.abhijit.iter.data.source.remote.IterApi.java

License:Open Source License

public static String fetchStudentDetails(String studentId) throws Exception {
    JsonObject request = new JsonObject();
    request.addProperty("sid", "studentdetails");
    request.addProperty("instituteid", INSTITUTE_ID);
    request.addProperty("studentid", studentId);
    return Http.post(API_ENDPOINT, "jdata=" + request.toString());
}

From source file:app.abhijit.iter.data.source.remote.IterApi.java

License:Open Source License

public static String fetchStudentSubjects(String studentId) throws Exception {
    JsonObject request = new JsonObject();
    request.addProperty("sid", "attendance");
    request.addProperty("instituteid", INSTITUTE_ID);
    request.addProperty("registrationid", REGISTRATION_ID);
    request.addProperty("studentid", studentId);
    return Http.post(API_ENDPOINT, "jdata=" + request.toString());
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void fetch() {
    String selectedRegistrationNumber = mLocalStore.getString(SELECTED_REGISTRATION_NUMBER_KEY, null);
    if (!StringUtils.equals(selectedRegistrationNumber, mSelectedRegistrationNumber)) {
        if (mFetchStudentAsyncTask != null) {
            mFetchStudentAsyncTask.cancel(true);
            mFetchStudentAsyncTask = null;
        }//from   www. ja v a 2s. c o  m
        if (mFetchStudentSubjectsAsyncTask != null) {
            mFetchStudentSubjectsAsyncTask.cancel(true);
            mFetchStudentSubjectsAsyncTask = null;
        }
        mSelectedRegistrationNumber = selectedRegistrationNumber;
    }

    if (mSelectedRegistrationNumber != null && mErrorMessage == null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        if (!mLocalStore.contains(mSelectedRegistrationNumber)) {
            JsonObject student = new JsonObject();
            student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
            mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
            mFetchStudentAsyncTask = new FetchStudentAsyncTask();
            mFetchStudentAsyncTask.execute();
        } else {
            JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                    .getAsJsonObject();
            if (!student.has(STUDENT_NAME_KEY)) {
                mFetchStudentAsyncTask = new FetchStudentAsyncTask();
                mFetchStudentAsyncTask.execute();
            } else {
                boolean isSubjectDataOld = (new Date().getTime()
                        - student.get(STUDENT_TIMESTAMP_KEY).getAsLong()) > CACHE_TIMEOUT;
                if (isSubjectDataOld || !student.has(STUDENT_SUBJECTS_KEY)) {
                    String studentId = student.get(STUDENT_ID_KEY).getAsString();
                    mFetchStudentSubjectsAsyncTask = new FetchStudentSubjectsAsyncTask();
                    mFetchStudentSubjectsAsyncTask.execute(studentId);
                }
            }
        }
    }

    if (mErrorMessage != null) {
        EventBus.getDefault().post(new Error(mErrorMessage));
        mErrorMessage = null;
    }

    ArrayList<Student> students = new ArrayList<>();

    for (Map.Entry<String, ?> entry : mLocalStore.getAll().entrySet()) {
        if (entry.getKey().equals(SELECTED_REGISTRATION_NUMBER_KEY))
            continue;
        JsonObject student = mJsonParser.parse(mLocalStore.getString(entry.getKey(), null)).getAsJsonObject();
        students.add(generateStudent(student));
    }

    EventBus.getDefault().post(ImmutableList.copyOf(students));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudent(String[] response) {
    mFetchStudentAsyncTask = null;//w  w  w.ja v  a 2 s .  co  m

    if (response[0] == null || response[1] == null) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    String studentId = response[0];

    if (studentId.equals("0")) {
        mErrorMessage = "Registration number " + mSelectedRegistrationNumber + " is not valid";
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    JsonObject student = new JsonObject();

    try {
        JsonObject studentDetails = mJsonParser.parse(response[1]).getAsJsonArray().get(0).getAsJsonObject();
        if (!studentDetails.has("name")) {
            throw new Exception();
        }
        student.addProperty(STUDENT_ID_KEY, studentId);
        student.addProperty(STUDENT_NAME_KEY, studentDetails.get("name").getAsString());
        student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
        student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    } catch (Exception e) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudentSubjects(String response) {
    mFetchStudentSubjectsAsyncTask = null;

    JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
            .getAsJsonObject();/*from  w w  w .  jav a2  s  .c  o m*/
    JsonObject subjects = new JsonObject();

    try {
        JsonArray studentSubjects = mJsonParser.parse(response).getAsJsonArray();
        for (int i = 0; i < studentSubjects.size(); i++) {
            if (!studentSubjects.get(i).getAsJsonObject().has("subjectcode")
                    || !studentSubjects.get(i).getAsJsonObject().has("subject")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalpresentclass")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalclasses")) {
                continue;
            }
            String code = studentSubjects.get(i).getAsJsonObject().get("subjectcode").getAsString();
            String name = studentSubjects.get(i).getAsJsonObject().get("subject").getAsString();
            int presentClasses = studentSubjects.get(i).getAsJsonObject().get("totalpresentclass").getAsInt();
            int totalClasses = studentSubjects.get(i).getAsJsonObject().get("totalclasses").getAsInt();
            if (totalClasses <= 0 || presentClasses < 0 || presentClasses > totalClasses)
                continue;
            long lastUpdated = new Date().getTime();
            int oldPresentClasses = presentClasses;
            int oldTotalClasses = totalClasses;
            if (student.has(STUDENT_SUBJECTS_KEY)
                    && student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().has(code)) {
                oldPresentClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
                oldTotalClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
                if (presentClasses == oldPresentClasses && totalClasses == oldTotalClasses) {
                    lastUpdated = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                            .getAsJsonObject().get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();
                }
            }
            JsonObject subject = new JsonObject();
            subject.addProperty(STUDENT_SUBJECT_CODE_KEY, code);
            subject.addProperty(STUDENT_SUBJECT_NAME_KEY, name);
            subject.addProperty(STUDENT_SUBJECT_PRESENT_CLASSES_KEY, presentClasses);
            subject.addProperty(STUDENT_SUBJECT_TOTAL_CLASSES_KEY, totalClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, oldPresentClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, oldTotalClasses);
            subject.addProperty(STUDENT_SUBJECT_LAST_UPDATED_KEY, lastUpdated);
            subjects.add(code, subject);
        }
    } catch (Exception e) {
        if (student.has(STUDENT_SUBJECTS_KEY)) {
            subjects = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject();
        } else {
            subjects = new JsonObject();
        }
    }

    student.add(STUDENT_SUBJECTS_KEY, subjects);
    student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:aproject.App.java

License:Apache License

public static JsonObject main(JsonObject args) {
    String name = "stranger";
    if (args.has("name"))
        name = args.getAsJsonPrimitive("name").getAsString();
    JsonObject response = new JsonObject();
    response.addProperty("greeting", "Hello " + name + "!");
    return response;
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

@Override
protected void defaults() {
    super.defaults();

    JsonObject subscribe = new JsonObject();
    subscribe.add("port", new JsonPrimitive(9000));
    subscribe.add("scheme", new JsonPrimitive("ws"));
    subscribe.add("path", new JsonPrimitive("/sparql"));
    parameters.add("subscribe", subscribe);

    JsonObject securesubscribe = new JsonObject();
    securesubscribe.add("port", new JsonPrimitive(9443));
    securesubscribe.add("scheme", new JsonPrimitive("wss"));
    securesubscribe.add("path", new JsonPrimitive("/secure/sparql"));
    parameters.add("securesubscribe", securesubscribe);

    JsonObject secureUpdate = new JsonObject();
    secureUpdate.add("port", new JsonPrimitive(8443));
    secureUpdate.add("scheme", new JsonPrimitive("https"));
    parameters.add("secureupdate", secureUpdate);

    JsonObject secureQuery = new JsonObject();
    secureQuery.add("port", new JsonPrimitive(8443));
    secureQuery.add("scheme", new JsonPrimitive("https"));
    parameters.add("securequery", secureQuery);

    JsonObject register = new JsonObject();
    register.add("register", new JsonPrimitive("/oauth/register"));
    register.add("requesttoken", new JsonPrimitive("/oauth/token"));
    register.add("port", new JsonPrimitive(8443));
    register.add("scheme", new JsonPrimitive("https"));
    parameters.add("authorizationserver", register);
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

/**
 * Sets the credentials.// w w w .j  a  va2 s  . c o  m
 *
 * @param id the username
 * @param secret the password
 * @throws IOException 
 */
public void setCredentials(String id, String secret) throws IOException {
    logger.debug("Set credentials Id: " + id + " Secret:" + secret);

    this.id = id;
    this.secret = secret;

    try {
        authorization = new BASE64Encoder().encode((id + ":" + secret).getBytes("UTF-8"));

        //TODO need a "\n", why?
        authorization = authorization.replace("\n", "");
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage());
    }

    //Save on file the encrypted version   
    if (parameters.get("security") == null) {
        JsonObject credentials = new JsonObject();
        credentials.add("client_id", new JsonPrimitive(SEPAEncryption.encrypt(id)));
        credentials.add("client_secret", new JsonPrimitive(SEPAEncryption.encrypt(secret)));
        parameters.add("security", credentials);
    } else {
        parameters.get("security").getAsJsonObject().add("client_id",
                new JsonPrimitive(SEPAEncryption.encrypt(id)));
        parameters.get("security").getAsJsonObject().add("client_secret",
                new JsonPrimitive(SEPAEncryption.encrypt(secret)));
    }

    storeProperties(propertiesFile);
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

/**
 * Sets the JWT./*from  w w w.java2 s  .  c  om*/
 *
 * @param jwt the JSON Web Token
 * @param expires the date when the token will expire
 * @param type the token type (e.g., bearer)
 * @throws IOException 
 */
public void setJWT(String jwt, Date expires, String type) throws IOException {

    this.jwt = jwt;
    this.expires = expires.getTime();
    this.tokenType = type;

    //Save on file the encrypted version
    if (parameters.get("security") == null) {
        JsonObject credentials = new JsonObject();
        credentials.add("jwt", new JsonPrimitive(SEPAEncryption.encrypt(jwt)));
        credentials.add("expires",
                new JsonPrimitive(SEPAEncryption.encrypt(String.format("%d", expires.getTime()))));
        credentials.add("type", new JsonPrimitive(SEPAEncryption.encrypt(type)));
        parameters.add("security", credentials);
    } else {
        parameters.get("security").getAsJsonObject().add("jwt", new JsonPrimitive(SEPAEncryption.encrypt(jwt)));
        parameters.get("security").getAsJsonObject().add("expires",
                new JsonPrimitive(SEPAEncryption.encrypt(String.format("%d", expires.getTime()))));
        parameters.get("security").getAsJsonObject().add("type",
                new JsonPrimitive(SEPAEncryption.encrypt(type)));
    }

    storeProperties(propertiesFile);
}

From source file:arces.unibo.SEPA.client.api.WebsocketClientEndpoint.java

License:Open Source License

private void sendSubscribeRequest() {
    if (wsClientSession == null) {
        logger.error("Session is null");
        return;//from w  w w  . j av a 2s .  com
    }

    try {
        JsonObject request = new JsonObject();
        request.add("subscribe", new JsonPrimitive(sparql));
        if (alias != null)
            request.add("alias", new JsonPrimitive(alias));
        else
            logger.debug("Alias is null");
        if (jwt != null)
            request.add("authorization", new JsonPrimitive(jwt));
        else
            logger.debug("Authorization is null");
        logger.debug(request.toString());

        wsClientSession.getBasicRemote().sendText(request.toString());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}