Example usage for org.json.simple JSONObject containsKey

List of usage examples for org.json.simple JSONObject containsKey

Introduction

In this page you can find the example usage for org.json.simple JSONObject containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:org.alfresco.repo.web.scripts.calendar.AbstractCalendarWebScript.java

/**
 * Generates an activity entry for the entry
 *//*  w w w.  j  a va  2 s. c om*/
protected String addActivityEntry(String event, CalendarEntry entry, SiteInfo site, WebScriptRequest req,
        JSONObject json) {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    String dateOpt = "?date=" + fmt.format(entry.getStart());

    // What page is this for?
    String page = req.getParameter("page");
    if (page == null && json != null) {
        if (json.containsKey("page")) {
            page = (String) json.get("page");
        }
    }
    if (page == null) {
        // Default
        page = "calendar";
    }

    try {
        StringWriter activityJson = new StringWriter();
        JSONWriter activity = new JSONWriter(activityJson);
        activity.startObject();
        activity.writeValue("title", entry.getTitle());
        activity.writeValue("page", page + dateOpt);
        activity.endObject();

        activityService.postActivity("org.alfresco.calendar.event-" + event, site.getShortName(),
                CALENDAR_SERVICE_ACTIVITY_APP_NAME, activityJson.toString());
    } catch (Exception e) {
        // Warn, but carry on
        logger.warn("Error adding event " + event + " to activities feed", e);
    }

    // Return the date we used
    return dateOpt;
}

From source file:org.alfresco.repo.web.scripts.calendar.AbstractCalendarWebScript.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    if (templateVars == null) {
        String error = "No parameters supplied";
        if (useJSONErrors()) {
            return buildError(error);
        } else {/*from w w w  .j  a  v a  2 s  . c  o  m*/
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
        }
    }

    // Parse the JSON, if supplied
    JSONObject json = null;
    String contentType = req.getContentType();
    if (contentType != null && contentType.indexOf(';') != -1) {
        contentType = contentType.substring(0, contentType.indexOf(';'));
    }
    if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
        JSONParser parser = new JSONParser();
        try {
            json = (JSONObject) parser.parse(req.getContent().getContent());
        } catch (IOException io) {
            return buildError("Invalid JSON: " + io.getMessage());
        } catch (org.json.simple.parser.ParseException je) {
            return buildError("Invalid JSON: " + je.getMessage());
        }
    }

    // Get the site short name. Try quite hard to do so...
    String siteName = templateVars.get("siteid");
    if (siteName == null) {
        siteName = templateVars.get("site");
    }
    if (siteName == null) {
        siteName = req.getParameter("site");
    }
    if (siteName == null && json != null) {
        if (json.containsKey("siteid")) {
            siteName = (String) json.get("siteid");
        } else if (json.containsKey("site")) {
            siteName = (String) json.get("site");
        }
    }
    if (siteName == null) {
        String error = "No site given";
        if (useJSONErrors()) {
            return buildError("No site given");
        } else {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
        }
    }

    // Grab the requested site
    SiteInfo site = siteService.getSite(siteName);
    if (site == null) {
        String error = "Could not find site: " + siteName;
        if (useJSONErrors()) {
            return buildError(error);
        } else {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
        }
    }

    // Event name is optional
    String eventName = templateVars.get("eventname");

    // Have the real work done
    return executeImpl(site, eventName, req, json, status, cache);
}

From source file:org.alfresco.repo.web.scripts.comments.AbstractCommentsWebScript.java

/**
 * get the value from JSON for given key if exists
 * @param json/* ww  w .j  a va2  s. c om*/
 * @param key
 * @return
 */
protected String getOrNull(JSONObject json, String key) {
    if (json != null && json.containsKey(key)) {
        return (String) json.get(key);
    }
    return null;
}

From source file:org.alfresco.repo.web.scripts.comments.AbstractCommentsWebScript.java

/**
 * returns SiteInfo needed for post activity
 * @param req/*from   ww w  . jav  a2s  . c om*/
 * @return
 */
protected SiteInfo getSiteInfo(WebScriptRequest req, boolean searchForSiteInJSON) {
    String siteName = req.getParameter(JSON_KEY_SITE);

    if (siteName == null && searchForSiteInJSON) {
        JSONObject json = parseJSON(req);
        if (json != null) {
            if (json.containsKey(JSON_KEY_SITE)) {
                siteName = (String) json.get(JSON_KEY_SITE);
            } else if (json.containsKey(JSON_KEY_SITE_ID)) {
                siteName = (String) json.get(JSON_KEY_SITE_ID);
            }
        }
    }
    if (siteName != null) {
        SiteInfo site = siteService.getSite(siteName);
        return site;
    }

    return null;
}

From source file:org.alfresco.repo.web.scripts.discussion.AbstractDiscussionWebScript.java

protected String getOrNull(JSONObject json, String key) {
    if (json.containsKey(key)) {
        return (String) json.get(key);
    }//from   w w w  .j  a v  a  2 s  .c  o  m
    return null;
}

From source file:org.alfresco.repo.web.scripts.discussion.AbstractDiscussionWebScript.java

protected List<String> getTags(JSONObject json) {
    List<String> tags = null;
    if (json.containsKey("tags")) {
        // Is it "tags":"" or "tags":[...] ?
        if (json.get("tags") instanceof String) {
            // This is normally an empty string, skip
            String tagsS = (String) json.get("tags");
            if ("".equals(tagsS)) {
                // No tags were given
                return null;
            } else {
                // Log, and treat as empty
                logger.warn("Unexpected tag data: " + tagsS);
                return null;
            }//w w w.j  a  v a 2 s  .  co m
        } else {
            tags = new ArrayList<String>();
            JSONArray jsTags = (JSONArray) json.get("tags");
            for (int i = 0; i < jsTags.size(); i++) {
                tags.add((String) jsTags.get(i));
            }
        }
    }
    return tags;
}

From source file:org.alfresco.repo.web.scripts.discussion.AbstractDiscussionWebScript.java

/**
 * Generates an activity entry for the discussion item
 * //from w w  w. ja v  a  2  s .c  o  m
 * @param thing Either post or reply
 * @param event One of created, updated, deleted
 */
protected void addActivityEntry(String thing, String event, TopicInfo topic, PostInfo post, SiteInfo site,
        WebScriptRequest req, JSONObject json) {
    // We can only add activities against a site
    if (site == null) {
        logger.info("Unable to add activity entry for " + thing + " " + event + " as no site given");
        return;
    }

    // What page is this for?
    String page = req.getParameter("page");
    if (page == null && json != null) {
        if (json.containsKey("page")) {
            page = (String) json.get("page");
        }
    }
    if (page == null) {
        // Default
        page = "discussions-topicview";
    }

    // Get the title
    String title = topic.getTitle();
    if (post != null) {
        String postTitle = post.getTitle();
        if (postTitle != null && postTitle.length() > 0) {
            title = postTitle;
        }
    }

    try {
        JSONObject params = new JSONObject();
        params.put("topicId", topic.getSystemName());

        JSONObject activity = new JSONObject();
        activity.put("title", title);
        activity.put("page", page + "?topicId=" + topic.getSystemName());
        activity.put("params", params);

        activityService.postActivity("org.alfresco.discussions." + thing + "-" + event, site.getShortName(),
                DISCUSSIONS_SERVICE_ACTIVITY_APP_NAME, activity.toString());
    } catch (Exception e) {
        // Warn, but carry on
        logger.warn("Error adding discussions " + thing + " " + event + " to activities feed", e);
    }
}

From source file:org.alfresco.repo.web.scripts.links.AbstractLinksWebScript.java

protected List<String> getTags(JSONObject json) {
    List<String> tags = null;
    if (json.containsKey("tags")) {
        // Is it "tags":"" or "tags":[...] ?
        if (json.get("tags") instanceof String) {
            // This is normally an empty string, skip
            String tagsS = (String) json.get("tags");
            if ("".equals(tagsS)) {
                // No tags were given
                return null;
            } else {
                // Log, and treat as empty
                // (We don't support "tags":"a,b,c" in these webscripts)
                logger.warn("Unexpected tag data: " + tagsS);
                return null;
            }//from w  w  w.  j a v  a  2 s.c  om
        } else {
            tags = new ArrayList<String>();
            JSONArray jsTags = (JSONArray) json.get("tags");
            for (int i = 0; i < jsTags.size(); i++) {
                tags.add((String) jsTags.get(i));
            }
        }
    }
    return tags;
}

From source file:org.alfresco.repo.web.scripts.links.AbstractLinksWebScript.java

/**
 * Generates an activity entry for the link
 *//*from  ww w.  ja  v a  2s . co  m*/
protected void addActivityEntry(String event, LinkInfo link, SiteInfo site, WebScriptRequest req,
        JSONObject json) {
    // What page is this for?
    String page = req.getParameter("page");
    if (page == null && json != null) {
        if (json.containsKey("page")) {
            page = (String) json.get("page");
        }
    }
    if (page == null) {
        // Default
        page = "links";
    }

    try {
        StringWriter activityJson = new StringWriter();
        JSONWriter activity = new JSONWriter(activityJson);
        activity.startObject();
        activity.writeValue("title", link.getTitle());
        activity.writeValue("page", page + "?linkId=" + link.getSystemName());
        activity.endObject();

        activityService.postActivity("org.alfresco.links.link-" + event, site.getShortName(),
                LINKS_SERVICE_ACTIVITY_APP_NAME, activityJson.toString());
    } catch (Exception e) {
        // Warn, but carry on
        logger.warn("Error adding link " + event + " to activities feed", e);
    }
}

From source file:org.alfresco.repo.web.scripts.links.AbstractLinksWebScript.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    if (templateVars == null) {
        String error = "No parameters supplied";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }/*from w w  w .  ja  v a2s .  c  o m*/

    // Parse the JSON, if supplied
    JSONObject json = null;
    String contentType = req.getContentType();
    if (contentType != null && contentType.indexOf(';') != -1) {
        contentType = contentType.substring(0, contentType.indexOf(';'));
    }
    if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
        JSONParser parser = new JSONParser();
        try {
            json = (JSONObject) parser.parse(req.getContent().getContent());
        } catch (IOException io) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + io.getMessage());
        } catch (ParseException pe) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + pe.getMessage());
        }
    }

    // Get the site short name. Try quite hard to do so...
    String siteName = templateVars.get("site");
    if (siteName == null) {
        siteName = req.getParameter("site");
    }
    if (siteName == null && json != null) {
        if (json.containsKey("siteid")) {
            siteName = (String) json.get("siteid");
        } else if (json.containsKey("site")) {
            siteName = (String) json.get("site");
        }
    }
    if (siteName == null) {
        String error = "No site given";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }

    // Grab the requested site
    SiteInfo site = siteService.getSite(siteName);
    if (site == null) {
        String error = "Could not find site: " + siteName;
        throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
    }

    // Link name is optional
    String linkName = templateVars.get("path");

    //sanitise url
    if (json != null) {
        String url = getOrNull(json, "url");
        if (!isUrlCorrect(url)) {
            String error = "Url not allowed";
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
        }
    }

    // Have the real work done
    return executeImpl(site, linkName, req, json, status, cache);
}