示例#1
0
  private void addAttribute(
      String attrName, String attrStringValue, IEventType eventType, Map<String, Object> attrMap)
      throws AdapterException {

    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, dateFormatter, eep);
      attrMap.put(attrName, attrValueObject);
    } catch (Exception e) {
      throw new AdapterException(
          "Could not convert XML input attribute " + attrName + " to event attribute");
    }
  }
  private void addAttribute(
      String attrName,
      String attrStringValue,
      IEventType eventType,
      Map<String, Object> attrMap,
      DateFormat dateFormatter)
      throws AdapterException {

    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,
              dateFormatter,
              WebFacadesManager.getInstance().getEepFacade());
      attrMap.put(attrName, attrValueObject);
    } catch (Exception e) {
      String msg =
          "Could not convert XML input attribute "
              + attrName
              + " to event attribute "
              + e
              + ", reason: "
              + e.getMessage();
      logger.severe(msg);
      // throw new ResponseException(msg);
    }
  }
  @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;
  }