use of com.infiniteautomation.mango.rest.latest.model.ArrayWithTotal in project ma-modules-public by infiniteautomation.
the class GenericCSVMessageConverter method writeInternal.
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
try {
Class<?> serializationView = null;
FilterProvider filters = null;
Object value = object;
JavaType javaType = null;
if (object instanceof MappingJacksonValue) {
MappingJacksonValue container = (MappingJacksonValue) object;
value = container.getValue();
serializationView = container.getSerializationView();
filters = container.getFilters();
}
if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
javaType = getJavaType(type, null);
}
ObjectWriter objectWriter;
if (serializationView != null) {
objectWriter = this.getObjectMapper().writerWithView(serializationView);
} else if (filters != null) {
objectWriter = this.getObjectMapper().writer(filters);
} else {
objectWriter = this.getObjectMapper().writer();
}
if (javaType != null && javaType.isContainerType()) {
objectWriter = objectWriter.forType(javaType);
}
// special handling for our streamed arrays with total, unwrap the array and discard the total
if (value instanceof ArrayWithTotal) {
value = ((ArrayWithTotal<?>) value).getItems();
}
JsonNode root;
try (TokenBuffer generator = new TokenBuffer(this.getObjectMapper(), false)) {
if (this.getObjectMapper().isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
generator.forceUseOfBigDecimal(true);
}
objectWriter.writeValue(generator, value);
generator.flush();
JsonParser p = generator.asParser();
root = this.getObjectMapper().readTree(p);
p.close();
}
MediaType contentType = outputMessage.getHeaders().getContentType();
Charset charset = this.charsetForContentType(contentType);
try (Writer out = new OutputStreamWriter(outputMessage.getBody(), charset)) {
if (charset.name().startsWith("UTF-")) {
// write the UTF BOM (byte order mark)
out.write('\ufeff');
}
if (!root.isArray()) {
ArrayNode array = this.nodeFactory.arrayNode(1);
array.add(root);
root = array;
}
CSVWriter csvWriter = new CSVWriter(out);
writeCSV(csvWriter, (ArrayNode) root);
out.flush();
}
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex);
}
}
use of com.infiniteautomation.mango.rest.latest.model.ArrayWithTotal in project ma-modules-public by infiniteautomation.
the class EventsWebSocketHandler method handleTextMessage.
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
try {
JsonNode tree = this.jacksonMapper.readTree(message.getPayload());
if (!WebSocketMessageType.REQUEST.messageTypeMatches(tree) || tree.get("requestType") == null) {
return;
}
EventsWebsocketRequest request = this.jacksonMapper.treeToValue(tree, EventsWebsocketRequest.class);
request.ensureValid();
if (request instanceof EventsSubscriptionRequest) {
EventsSubscriptionRequest subscription = (EventsSubscriptionRequest) request;
Set<AlarmLevels> levels = subscription.getLevels();
if (levels == null) {
levels = Collections.emptySet();
}
Set<EventActionEnum> actions = subscription.getActions();
if (actions == null) {
actions = EnumSet.noneOf(EventActionEnum.class);
}
boolean emptySubscriptions = levels.isEmpty() || actions.isEmpty();
synchronized (this.lock) {
// Configure listener
Boolean subscribed = (Boolean) session.getAttributes().get(SUBSCRIPTION_ATTRIBUTE);
if (subscribed == null || subscribed == Boolean.FALSE) {
if (!emptySubscriptions) {
changeLevels(levels);
changeActions(actions);
initialize();
session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.TRUE);
}
} else {
if (emptySubscriptions) {
terminate();
session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.FALSE);
} else {
changeActions(actions);
changeLevels(levels);
}
}
}
EventsSubscriptionResponse response = new EventsSubscriptionResponse();
if (subscription.isSendActiveSummary()) {
List<UserEventLevelSummary> summaries = service.getActiveSummary();
List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
}).collect(Collectors.toList());
response.setActiveSummary(models);
}
if (subscription.isSendUnacknowledgedSummary()) {
List<UserEventLevelSummary> summaries = service.getUnacknowledgedSummary();
List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
}).collect(Collectors.toList());
response.setUnacknowledgedSummary(models);
}
this.sendRawMessage(session, new WebSocketResponse<>(request.getSequenceNumber(), response));
} else if (request instanceof EventsDataPointSummaryRequest) {
EventsDataPointSummaryRequest query = (EventsDataPointSummaryRequest) request;
WebSocketResponse<List<DataPointEventSummaryModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
Collection<DataPointEventLevelSummary> summaries = service.getDataPointEventSummaries(query.getDataPointXids());
List<DataPointEventSummaryModel> models = summaries.stream().map(s -> new DataPointEventSummaryModel(s.getXid(), s.getCounts())).collect(Collectors.toList());
response.setPayload(models);
this.sendRawMessage(session, response);
} else if (request instanceof AllActiveEventsRequest) {
WebSocketResponse<List<EventInstanceModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
List<EventInstance> active = service.getAllActiveUserEvents();
List<EventInstanceModel> models = new ArrayList<>(active.size());
for (EventInstance vo : active) {
models.add(modelMapper.map(vo, EventInstanceModel.class, user));
}
response.setPayload(models);
this.sendRawMessage(session, response);
} else if (request instanceof ActiveEventsQuery) {
List<EventInstance> active = service.getAllActiveUserEvents();
List<EventInstanceModel> models = new ArrayList<>(active.size());
for (EventInstance vo : active) {
models.add(modelMapper.map(vo, EventInstanceModel.class, user));
}
String query = ((ActiveEventsQuery) request).getQuery();
ASTNode rql = RQLUtils.parseRQLtoAST(query);
WebSocketResponse<ArrayWithTotal<Stream<EventInstanceModel>>> response = new WebSocketResponse<>(request.getSequenceNumber());
response.setPayload(new FilteredStreamWithTotal<>(models, rql, user.getTranslations()));
this.sendRawMessage(session, response);
}
} catch (NotFoundException e) {
try {
this.sendErrorMessage(session, MangoWebSocketErrorType.NOT_FOUND, e.getTranslatableMessage());
} catch (Exception e1) {
log.error(e.getMessage(), e);
}
} catch (PermissionException e) {
try {
this.sendErrorMessage(session, MangoWebSocketErrorType.PERMISSION_DENIED, e.getTranslatableMessage());
} catch (Exception e1) {
log.error(e.getMessage(), e);
}
} catch (Exception e) {
try {
this.sendErrorMessage(session, MangoWebSocketErrorType.SERVER_ERROR, new TranslatableMessage("rest.error.serverError", e.getMessage()));
} catch (Exception e1) {
log.error(e.getMessage(), e);
}
}
}
Aggregations