Ejemplo n.º 1
0
  @Override
  public IEventInstance readFrom(
      Class<IEventInstance> type,
      Type generic,
      Annotation[] annotation,
      MediaType media,
      MultivaluedMap<String, String> map,
      InputStream eventStream)
      throws IOException, WebApplicationException {

    logger.info("started plain text message body reader");

    // the InputStream event contains event in plain text format
    // we should parse the event text, create IEventInstance and return it -
    // the resource's put/post function will be invoked

    // String eventString = getEventString(eventStream);
    String eventString = "Name=StockBuy;id=111;amount=100;price=5000";
    logger.info("extracted event string: " + eventString);

    // the eventString consists of name=value pairs separated by delimiter
    int nameAttrIndex = eventString.indexOf(EventHeader.NAME_ATTRIBUTE);
    String nameSubstring = eventString.substring(nameAttrIndex);

    int delimiterIndex = nameSubstring.indexOf(DELIMITER);
    int tagDataSeparatorIndex = nameSubstring.indexOf(TAG_DATA_SEPARATOR);
    String nameValue = nameSubstring.substring(tagDataSeparatorIndex + 1, delimiterIndex);
    IEventType eventType =
        WebMetadataFacade.getInstance().getEventMetadataFacade().getEventType(nameValue);

    // search for all pairs of tag-data by using delimiter
    Map<String, Object> attrValues = new HashMap<String, Object>();

    String[] tagValuePairs = eventString.split(DELIMITER);
    for (String tagValue : tagValuePairs) {
      // separate the tag from the value using the tagDataSeparator
      String[] separatedPair = tagValue.split(TAG_DATA_SEPARATOR);
      String attrName = separatedPair[0]; // the tag is always the first in the pair

      // some attributes might not have value specified at all
      if (separatedPair.length < 2) {
        attrValues.put(attrName, null);
        continue;
      }
      String attrStringValue = separatedPair[1];
      if (attrStringValue.equals("null")) {
        // the attribute has a value of null
        attrValues.put(attrName, null);
        continue;
      }

      TypeAttribute eventTypeAttribute = eventType.getTypeAttributeSet().getAttribute(attrName);
      AttributeTypesEnum attrType = eventTypeAttribute.getTypeEnum();
      if (attrType.equals(AttributeTypesEnum.STRING) || eventTypeAttribute.getDimension() > 0) {
        attrStringValue = "'" + attrStringValue + "'";
      }

      Object attrValueObject;
      try {
        attrValueObject =
            TypeAttribute.parseConstantValue(
                attrStringValue,
                attrName,
                eventType,
                null,
                WebFacadesManager.getInstance().getEepFacade());
        attrValues.put(attrName, attrValueObject);
      } catch (Exception e) {
        String msg =
            "Could not parse the event string" + eventString + ", reason: " + e.getMessage();
        logger.severe(msg);

        throw new ResponseException(msg);
      }
    }

    IEventInstance event = new EventInstance(eventType, attrValues);
    event.setDetectionTime(System.currentTimeMillis());

    logger.info("finished message body reader");
    return event;
  }
Ejemplo n.º 2
0
  @Override
  public EventInstance readFrom(
      Class<EventInstance> type,
      Type generic,
      Annotation[] annotation,
      MediaType media,
      MultivaluedMap<String, String> map,
      InputStream eventStream)
      throws IOException, WebApplicationException {

    logger.info("started event message body reader");
    EventInstance event = null;
    String attrName = null;
    String attrStringValue = null;

    // the InputStream event contains event in XML NGSI format
    // we should parse the object, create IEventInstance and return it -
    // the resource's put/post function will be invoked

    try {

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(eventStream);
      doc.getDocumentElement().normalize();

      Node entityIdNode = doc.getElementsByTagName("entityId").item(0);
      String entityType = entityIdNode.getAttributes().getNamedItem("type").getNodeValue();
      String entityId = getNodeValue(((Node) doc.getElementsByTagName("id").item(0)));
      // System.out.println("Entity type: " + entityType + " Entity id:"	+ entityId);
      String eventName = entityType + EVENT_NAME_SUFFIX;

      IEventType eventType =
          WebMetadataFacade.getInstance().getEventMetadataFacade().getEventType(eventName);
      logger.info("Event: " + eventName);
      Map<String, Object> attrValues = new HashMap<String, Object>();

      DateFormat dateFormatter = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
      addAttribute(ENTITY_ID_ATTRIBUTE, entityId, eventType, attrValues, dateFormatter);
      addAttribute(ENTITY_TYPE_ATTRIBUTE, entityType, eventType, attrValues, dateFormatter);

      // get all pairs of attribute name and value
      NodeList attrNodes = doc.getElementsByTagName("contextAttribute");

      for (int i = 0; i < attrNodes.getLength(); i++) {
        Element attr = (Element) attrNodes.item(i);
        attrName = getNodeValue(attr.getElementsByTagName("name").item(0));
        attrStringValue = getNodeValue(attr.getElementsByTagName("contextValue").item(0));
        // System.out.println("Attribute[" + i + "] name: " + attrName +
        //					" value:" + attrStringValue);
        if (attrStringValue != null) {
          addAttribute(attrName, attrStringValue, eventType, attrValues, dateFormatter);
        }
      }

      event = new EventInstance(eventType, attrValues);
      event.setDetectionTime(System.currentTimeMillis());

    } catch (Exception e) {
      String msg =
          "Could not parse XML NGSI event "
              + e
              + ", reason: "
              + e.getMessage()
              + "\n last attribute name: "
              + attrName
              + " last value: "
              + attrStringValue;
      logger.severe(msg);
      new ResponseException(msg);
    }

    logger.info("finished event message body reader");

    return event;
  }