use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.
the class WorkspaceService method getWorkspaces.
@SuppressWarnings("unchecked")
@Listener("/service/workspaces")
public void getWorkspaces(final ServerSession remote, Message message) {
ServerMessage.Mutable reply = new ServerMessageImpl();
Map<String, Object> data = message.getDataAsMap();
Subject subject = (Subject) bayeux.getContext().getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
String username = SubjectUtils.getName(subject);
// No workspaces persisted for a guest user (whose username="")
if (StringUtils.isNotBlank(username)) {
if (data == null || data.isEmpty() || data.get("workspaces") == null) {
List<Map<String, Object>> workspacesList = new ArrayList<Map<String, Object>>();
try {
workspacesList = persistentStore.get(PersistentStore.WORKSPACE_TYPE, "user = '" + username + "'");
if (workspacesList.size() == 1) {
// Convert workspace's JSON representation back to nested maps of Map<String, Object>
Map<String, Object> workspaces = (Map<String, Object>) workspacesList.get(0);
JSONContext.Client jsonContext = new Jackson1JSONContextClient();
String json = (String) workspaces.get("workspaces_json_txt");
LOGGER.debug("workspaces extracted JSON text:\n {}", json);
Map<String, Object> workspacesMap;
try {
workspacesMap = jsonContext.getParser().parse(new StringReader(json), Map.class);
reply.putAll(workspacesMap);
} catch (ParseException e) {
LOGGER.info("ParseException while trying to convert persisted workspaces's for user {} from JSON", username, e);
}
}
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to retrieve persisted workspaces for user {}", username, e);
}
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, "/service/workspaces", reply);
} else {
LOGGER.debug("Persisting workspaces for username = {}", username);
// Use JSON serializer so that only "data" component is serialized, not entire Message
JSONContext.Server jsonContext = new Jackson1JSONContextServer();
String json = jsonContext.getGenerator().generate(data);
LOGGER.debug("workspaces JSON text:\n {}", json);
PersistentItem item = new PersistentItem();
item.addIdProperty(username);
item.addProperty("user", username);
item.addProperty("workspaces_json", json);
try {
persistentStore.add(PersistentStore.WORKSPACE_TYPE, item);
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to persist workspaces for user {}", username, e);
}
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, "/service/workspaces", reply);
}
}
}
use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.
the class PersistentStoreImpl method get.
@Override
public // Returned Map will have suffixes in the key names - client is responsible for handling them
List<Map<String, Object>> get(String type, String cql) throws PersistenceException {
if (StringUtils.isBlank(type)) {
throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
}
List<Map<String, Object>> results = new ArrayList<>();
// Set Solr Core name to type and create/connect to Solr Core
SolrClient solrClient = getSolrClient(type);
if (solrClient == null) {
throw new PersistenceException("Unable to create Solr client.");
}
SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
try {
SolrQuery solrQuery;
// If not cql specified, then return all items
if (StringUtils.isBlank(cql)) {
solrQuery = new SolrQuery("*:*");
} else {
Filter filter = CQL.toFilter(cql);
solrQuery = (SolrQuery) filter.accept(visitor, null);
}
QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
long numResults = solrResponse.getResults().getNumFound();
LOGGER.debug("numResults = {}", numResults);
SolrDocumentList docs = solrResponse.getResults();
for (SolrDocument doc : docs) {
PersistentItem result = new PersistentItem();
Collection<String> fieldNames = doc.getFieldNames();
for (String name : fieldNames) {
LOGGER.debug("field name = {} has value = {}", name, doc.getFieldValue(name));
if (name.endsWith(PersistentItem.TEXT_SUFFIX) && doc.getFieldValues(name).size() > 1) {
result.addProperty(name, doc.getFieldValues(name).stream().filter(s -> s instanceof String).map(s -> (String) s).collect(Collectors.toSet()));
} else if (name.endsWith(PersistentItem.XML_SUFFIX)) {
result.addXmlProperty(name, (String) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.TEXT_SUFFIX)) {
result.addProperty(name, (String) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.LONG_SUFFIX)) {
result.addProperty(name, (Long) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.INT_SUFFIX)) {
result.addProperty(name, (Integer) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.DATE_SUFFIX)) {
result.addProperty(name, (Date) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.BINARY_SUFFIX)) {
result.addProperty(name, (byte[]) doc.getFirstValue(name));
} else {
LOGGER.debug("Not adding field {} because it has invalid suffix", name);
}
}
results.add(result);
}
} catch (CQLException e) {
throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
} catch (SolrServerException | IOException e) {
throw new PersistenceException("SolrServerException while getting Solr data with cql statement " + cql, e);
}
return results;
}
use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.
the class TestCatalog method persistToWorkspace.
private void persistToWorkspace(int size) throws Exception {
getServiceManager().waitForRequiredApps("search-ui-app");
// Generate very large data block
Map<String, String> map = Maps.newHashMap();
for (int i = 0; i < size; i++) {
map.put("Key-" + i, "Val-" + i);
}
String jsonString = new JSONObject(map).toJSONString();
final PersistentStore pstore = getServiceManager().getService(PersistentStore.class);
PersistentItem item = new PersistentItem();
item.addIdProperty("itest");
item.addProperty("user", "itest");
item.addProperty("workspaces_json", jsonString);
try {
int wait = 0;
while (true) {
try {
pstore.get(PersistentStore.WORKSPACE_TYPE);
break;
} catch (Exception e) {
LOGGER.info("Waiting for persistence store to come online.");
Thread.sleep(1000);
if (wait++ > 60) {
break;
}
}
}
assertThat(pstore.get(PersistentStore.WORKSPACE_TYPE), is(empty()));
pstore.add(PersistentStore.WORKSPACE_TYPE, item);
expect("Solr core to be spun up and item to be persisted").within(5, TimeUnit.MINUTES).until(() -> pstore.get(PersistentStore.WORKSPACE_TYPE).size(), equalTo(1));
List<Map<String, Object>> storedWs = pstore.get(PersistentStore.WORKSPACE_TYPE, "id = 'itest'");
assertThat(storedWs, hasSize(1));
assertThat(storedWs.get(0).get("user_txt"), is("itest"));
} finally {
pstore.delete(PersistentStore.WORKSPACE_TYPE, "id = 'itest'");
expect("Workspace to be empty").within(5, TimeUnit.MINUTES).until(() -> pstore.get(PersistentStore.WORKSPACE_TYPE).size(), equalTo(0));
}
}
use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.
the class SubscriptionPersistor method metadataToPersistentItem.
/**
* Given subscription metadata, transform it into a persistent item for storage.
*/
private PersistentItem metadataToPersistentItem(SubscriptionMetadata metadata) {
notNull(metadata, "subscription metadata cannot be null");
PersistentItem persistentItem = new PersistentItem();
persistentItem.addProperty(VERSION_LABEL, VERSION);
persistentItem.addProperty(SUBSCRIPTION_ID_SOLR, metadata.getId());
persistentItem.addProperty(SUBSCRIPTION_TYPE, metadata.getTypeName());
persistentItem.addProperty(SUBSCRIPTION_FILTER, metadata.getFilter());
persistentItem.addProperty(SUBSCRIPTION_CALLBACK, metadata.getCallbackAddress());
return persistentItem;
}
use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.
the class SubscriptionPersistor method insert.
/**
* Add the subscription metadata to the persistent store using its id for the document key.
* <p>
* Insertion operations with the same key overwrite the previous value, which means a duplicate
* being added is a no-op, minus the network overhead.
*
* @param metadata the subscription metadata to add to the persistent store.
* @throws SubscriptionStoreException if a problem occurs during insert.
*/
public void insert(SubscriptionMetadata metadata) {
LOGGER.debug("Adding [{}] to persistence store. ", metadata.getId());
PersistentItem persistentSubscription = metadataToPersistentItem(metadata);
try {
persistentStore.add(PersistentStore.EVENT_SUBSCRIPTIONS_TYPE, persistentSubscription);
} catch (PersistenceException e) {
throw new SubscriptionStoreException("Exception while persisting subscription: ", e);
}
}
Aggregations