Example usage for com.google.gson JsonParser JsonParser

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

Introduction

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

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:com.apifest.AccessTokenValidator.java

License:Apache License

protected static String extractTokenScope(String tokenContent) {
    String scope = null;/*from w w  w . j  av a2s  .  c  o  m*/
    JsonParser parser = new JsonParser();
    JsonObject jsonObject = parser.parse(tokenContent).getAsJsonObject();
    JsonElement scopeElement = jsonObject.get("scope");
    String rs = (scopeElement != null && !scopeElement.isJsonNull()) ? scopeElement.getAsString() : null;
    if (rs != null && !rs.toString().equals("null")) {
        scope = rs;
    }
    return scope;
}

From source file:com.apifest.api.BasicAction.java

License:Apache License

/**
 * Extracts userId from tokenValidationResponse.
 * @param response the response received after access token is validate
 * @return userId associated with a token
 *///from   w ww . ja  v a2s  .co m
public static String getUserId(HttpResponse tokenValidationResponse) {
    String userId = null;
    if (tokenValidationResponse != null) {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(tokenValidationResponse.getContent().toString(CharsetUtil.UTF_8))
                .getAsJsonObject();
        JsonElement userIdElement = json.get("userId");
        userId = (userIdElement != null && !userIdElement.isJsonNull()) ? userIdElement.getAsString() : null;
    }
    return userId;
}

From source file:com.apifest.example.AddSenderIdInBodyAction.java

License:Apache License

@Override
public HttpRequest execute(HttpRequest req, String internalURI, HttpResponse tokenValidationResponse)
        throws MappingException {
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(new String(req.getContent().toString(CharsetUtil.UTF_8))).getAsJsonObject();
    log.info("request body: " + json);
    json.addProperty("senderId", "1232");
    byte[] newContent = json.toString().getBytes(CharsetUtil.UTF_8);
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(newContent);
    req.setContent(buf);//from w  w w .j  a  va 2 s.  com
    HttpHeaders.setContentLength(req, newContent.length);
    return req;
}

From source file:com.apifest.example.RemoveBalanceFilter.java

License:Apache License

@Override
public HttpResponse execute(HttpResponse response) {
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(response.getContent().toString(CharsetUtil.UTF_8)).getAsJsonObject();
    log.info("response body: " + json.toString());
    json.remove("balance");
    log.info("modified response body: " + json.toString());
    byte[] newContent = json.toString().getBytes(CharsetUtil.UTF_8);
    response.setContent(ChannelBuffers.copiedBuffer(newContent));
    HttpHeaders.setContentLength(response, newContent.length);
    return response;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public boolean storeScope(Scope scope) {
    boolean stored = false;
    String id = scope.getScope();
    Gson gson = new Gson();
    String json = gson.toJson(scope);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    jsonObj.remove("scope");
    // use scope name as _id
    jsonObj.addProperty(ID_NAME, id);/*from   w ww .j a v  a  2s.  c  o m*/

    try {
        // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00
        Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class);

        // if scope already exits, updates it, otherwise creates the scope
        BasicDBObject query = new BasicDBObject(ID_NAME, id);
        BasicDBObject newObject = new BasicDBObject(result);
        DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
        coll.update(query, newObject, true, false);
        stored = true;
    } catch (JsonParseException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (JsonMappingException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (IOException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    }

    return stored;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

protected String constructDbId(Object object) {
    Gson gson = new Gson();
    String json = gson.toJson(object);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    if (jsonObj.has("id")) {
        String id = jsonObj.get("id").getAsString();
        jsonObj.remove("id");
        jsonObj.addProperty(ID_NAME, id);
    }/*from  w  w  w.ja v  a  2s.c  o  m*/
    return jsonObj.toString();
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public boolean storeScope(Scope scope) {
    boolean stored = false;
    String id = scope.getScope();
    Gson gson = new Gson();
    String json = gson.toJson(scope);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    jsonObj.remove("scope");
    // use scope name as _id
    jsonObj.addProperty(CLIENTS_ID, id);

    try {/*from  w ww .  j a  va2s .  c o m*/
        // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00
        Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class);

        // if scope already exits, updates it, otherwise creates the scope
        BasicDBObject query = new BasicDBObject(CLIENTS_ID, id);
        BasicDBObject newObject = new BasicDBObject(result);
        DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
        coll.update(query, newObject, true, false);
        stored = true;
    } catch (JsonParseException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (JsonMappingException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    } catch (IOException e) {
        log.error("cannot store scope {}", scope.getScope(), e);
    }

    return stored;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

protected String constructDbId(Object object) {
    Gson gson = new Gson();
    String json = gson.toJson(object);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    if (jsonObj.has("id")) {
        String id = jsonObj.get("id").getAsString();
        jsonObj.remove("id");
        jsonObj.addProperty(CLIENTS_ID, id);
    }/* ww  w . j ava 2  s.co  m*/
    return jsonObj.toString();
}

From source file:com.apifest.oauth20.RevokeTokenRequest.java

License:Apache License

public RevokeTokenRequest(HttpRequest request) {
    String content = request.getContent().toString(CharsetUtil.UTF_8);
    JsonParser parser = new JsonParser();
    try {//  w ww. j a va 2s.c o  m
        JsonObject jsonObj = parser.parse(content).getAsJsonObject();
        this.accessToken = (jsonObj.get(ACCESS_TOKEN) != null) ? jsonObj.get(ACCESS_TOKEN).getAsString() : null;
        this.clientId = (jsonObj.get(CLIENT_ID) != null) ? jsonObj.get(CLIENT_ID).getAsString() : null;
    } catch (JsonSyntaxException e) {
        // do nothing
    }
}

From source file:com.apifest.oauth20.RevokeUserTokensRequest.java

License:Apache License

public RevokeUserTokensRequest(HttpRequest request) {
    String content = request.getContent().toString(CharsetUtil.UTF_8);
    JsonParser parser = new JsonParser();
    try {//from  w  w  w .j a  v  a  2  s  .  c o  m
        JsonObject jsonObj = parser.parse(content).getAsJsonObject();
        this.userId = (jsonObj.get(USER_ID) != null) ? jsonObj.get(USER_ID).getAsString() : null;
    } catch (JsonSyntaxException e) {
        // do nothing
    }
}