/** * Returns the marshaled context representing the state of all shared contexts. * * @return A ContextItems object representing the current state of all shared contexts. */ public ContextItems getMarshaledContext() { ContextItems marshaledContext = new ContextItems(); for (IManagedContext<?> managedContext : managedContexts) { marshaledContext.addItems(managedContext.getContextItems(false)); } return marshaledContext; }
/** * Remove all context items for the specified subject. * * @param subject Prefix whose items are to be removed. */ public void removeSubject(String subject) { String prefix = normalizePrefix(subject); for (String suffix : getSuffixes(prefix).keySet()) { setItem(prefix + suffix, null); } }
/** * Marshals the current context as a string. * * @param contextItems The context items to marshal. * @return The marshaled context. */ public String marshal(ContextItems contextItems) { SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmssz"); contextItems.setItem(PROPNAME_TIME, timestampFormat.format(new Date())); contextItems.setItem(PROPNAME_KEY, signer.getKeyName()); return contextItems.toString(); }
/** * Unmarshals the marshaled context. Performs digital signature verification, then returns the * unmarshaled context items. * * @param marshaledContext Marshaled context * @param authSignature If set, the digital signature is verified. * @return The unmarshaled context. * @throws Exception Unspecified exception. */ public ContextItems unmarshal(String marshaledContext, String authSignature) throws Exception { ContextItems contextItems = new ContextItems(); contextItems.addItems(marshaledContext); String whichKey = contextItems.getItem(PROPNAME_KEY); String timestamp = contextItems.getItem(PROPNAME_TIME); if (authSignature != null && !signer.verify(authSignature, marshaledContext, timestamp, whichKey)) { throw new MarshalException("Invalid digital signature"); } return contextItems; } }
/** * Returns a map consisting of suffixes of context items that match the specified prefix. * * @param prefix Item name less any suffix. * @param firstOnly If true, only the first match is returned. Otherwise, all matches are * returned. * @return Map of suffixes whose prefix matches the specified value. The value of each map entry * is the value of the original context item. */ private Map<String, String> getSuffixes(String prefix, Boolean firstOnly) { HashMap<String, String> matches = new HashMap<String, String>(); prefix = normalizePrefix(prefix); int i = prefix.length(); for (String itemName : index.keySet()) { if (itemName.startsWith(prefix)) { String suffix = lookupItemName(itemName, false).substring(i); matches.put(suffix, getItem(itemName)); if (firstOnly) { break; } } } return matches; }
/** * Performs a case-insensitive lookup of the item name + suffix in the index. * * @param itemName Item name * @param suffix Item suffix * @param autoAdd If true and item name not in index, add it. * @return Item name with suffix as stored internally */ private String lookupItemName(String itemName, String suffix, boolean autoAdd) { return lookupItemName(itemName + "." + suffix, autoAdd); }
/** * Initializes one or all managed contexts to their default state. * * @param item Managed context to initialize or, if null, initializes all managed contexts. * @return True if the operation was successful. */ public boolean init(IManagedContext item) { contextItems.clear(); boolean result = true; if (ccowIsActive()) { contextItems.addItems(ccowContextManager.getCCOWContext()); } if (item != null) { result = initItem(item); } else { for (IManagedContext managedContext : managedContexts) { result &= initItem(managedContext); } } return result; }
/** * Returns a map consisting of all suffixes of context items that match the specified prefix. * * @param prefix Item name less any suffix. * @return Map of all suffixes whose prefix matches the specified value. The value of each map * entry is the value of the original context item. */ public Map<String, String> getSuffixes(String prefix) { return getSuffixes(prefix, false); }
/** * Returns a date item associated with the specified item name. * * @param itemName Item name * @return Date value */ public Date getDate(String itemName) { try { return DateUtil.parseDate(getItem(itemName)); } catch (Exception e) { return null; } }
/** * @see org.carewebframework.api.context.IManagedContext#getContextItems(boolean) */ @Override public ContextItems getContextItems(boolean pending) { contextItems.clear(); DomainClass domainObject = getContextObject(pending); return domainObject == null ? contextItems : toCCOWContext(domainObject); }
/** * Adds context items to this set. * * @param contextItems Context items to add. */ public void addItems(ContextItems contextItems) { addItems(contextItems.items); }
/** * Returns true if the CCOW context contains context settings pertaining to the named subject. * * @param subject Name of the CCOW subject * @return True if settings for the specified subject were found. */ private boolean hasSubject(String subject) { boolean result = false; String s = subject + "."; int c = s.length(); for (String propName : contextItems.getItemNames()) { result = s.equalsIgnoreCase(propName.substring(0, c)); if (result) { break; } } return result; }
/** * Marshals the current context as a string. * * @param contextItems The context items to marshal. * @return The marshaled context. */ public String marshal(ContextItems contextItems) { SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmssz"); contextItems.setItem(PROPNAME_TIME, timestampFormat.format(new Date())); contextItems.setItem(PROPNAME_KEY, signer.getKeyName()); return contextItems.toString(); }
/** * Unmarshals the marshaled context. Performs digital signature verification, then returns the * unmarshaled context items. * * @param marshaledContext Marshaled context * @param authSignature If set, the digital signature is verified. * @return The unmarshaled context. * @throws Exception Unspecified exception. */ public ContextItems unmarshal(String marshaledContext, String authSignature) throws Exception { ContextItems contextItems = new ContextItems(); contextItems.addItems(marshaledContext); String whichKey = contextItems.getItem(PROPNAME_KEY); String timestamp = contextItems.getItem(PROPNAME_TIME); if (authSignature != null && !signer.verify(authSignature, marshaledContext, timestamp, whichKey)) { throw new MarshalException("Invalid digital signature"); } return contextItems; } }
/** * Returns a map consisting of suffixes of context items that match the specified prefix. * * @param prefix Item name less any suffix. * @param firstOnly If true, only the first match is returned. Otherwise, all matches are * returned. * @return Map of suffixes whose prefix matches the specified value. The value of each map entry * is the value of the original context item. */ private Map<String, String> getSuffixes(String prefix, Boolean firstOnly) { HashMap<String, String> matches = new HashMap<>(); prefix = normalizePrefix(prefix); int i = prefix.length(); for (String itemName : index.keySet()) { if (itemName.startsWith(prefix)) { String suffix = lookupItemName(itemName, false).substring(i); matches.put(suffix, getItem(itemName)); if (firstOnly) { break; } } } return matches; }
/** * Performs a case-insensitive lookup of the item name + suffix in the index. * * @param itemName Item name * @param suffix Item suffix * @param autoAdd If true and item name not in index, add it. * @return Item name with suffix as stored internally */ private String lookupItemName(String itemName, String suffix, boolean autoAdd) { return lookupItemName(itemName + "." + suffix, autoAdd); }
/** * Initializes one or all managed contexts to their default state. * * @param item Managed context to initialize or, if null, initializes all managed contexts. * @param callback Callback to report subscriber responses. */ public void init(IManagedContext<?> item, ISurveyCallback callback) { contextItems.clear(); if (ccowIsActive()) { contextItems.addItems(ccowContextManager.getCCOWContext()); } if (item != null) { initItem(item, callback); } else { SurveyResponse response = new SurveyResponse(); initItem(managedContexts.iterator(), response, callback); } }
/** * Returns a map consisting of all suffixes of context items that match the specified prefix. * * @param prefix Item name less any suffix. * @return Map of all suffixes whose prefix matches the specified value. The value of each map * entry is the value of the original context item. */ public Map<String, String> getSuffixes(String prefix) { return getSuffixes(prefix, false); }
/** * Returns a date item associated with the specified item name. * * @param itemName Item name * @return Date value */ public Date getDate(String itemName) { try { return DateUtil.parseDate(getItem(itemName)); } catch (Exception e) { return null; } }