Example #1
0
 private Property createTmlProperty(Prop p) {
   Property result;
   if (Property.SELECTION_PROPERTY.equals(p.type())) {
     result =
         NavajoFactory.getInstance()
             .createProperty(assemble, p.name(), p.cardinality(), p.description(), p.direction());
     for (Select s : p.selections()) {
       Selection sel =
           NavajoFactory.getInstance()
               .createSelection(assemble, s.name(), s.value(), s.selected());
       result.addSelection(sel);
     }
   } else {
     result =
         NavajoFactory.getInstance()
             .createProperty(
                 assemble,
                 p.name(),
                 p.type() == null ? Property.STRING_PROPERTY : p.type(),
                 null,
                 p.length(),
                 p.description(),
                 p.direction());
     result.setAnyValue(p.value());
     if (p.type() != null) {
       result.setType(p.type());
     }
   }
   return result;
 }
Example #2
0
  @SuppressWarnings({"unchecked"})
  private Navajo getRssNavajo(Channel c, String service) throws NavajoException {
    Navajo n = NavajoFactory.getInstance().createNavajo();
    Header h = NavajoFactory.getInstance().createHeader(n, service, "unknown", "unknown", -1);
    n.addHeader(h);
    Message channelMessage = NavajoFactory.getInstance().createMessage(n, "Channel");
    n.addMessage(channelMessage);
    addProperty(channelMessage, "Title", c.getTitle(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "Link", c.getLink(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "Description", c.getDescription(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "Language", c.getLanguage(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "Copyright", c.getCopyright(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "PubDate", c.getPubDate(), Property.STRING_PROPERTY);
    addProperty(channelMessage, "Ttl", c.getTtl(), Property.STRING_PROPERTY);
    addImage(channelMessage, c.getImage());

    Message m = NavajoFactory.getInstance().createMessage(n, "Rss", Message.MSG_TYPE_ARRAY);
    n.addMessage(m);
    Collection<Item> s = c.getItems();
    for (Iterator<Item> iterator = s.iterator(); iterator.hasNext(); ) {
      Item i = iterator.next();
      addItem(m, i);
    }
    return n;
  }
Example #3
0
 public static void main(String[] args) throws Exception {
   Navajo doc = NavajoFactory.getInstance().createNavajo();
   Message m = NavajoFactory.getInstance().createMessage(doc, "Input");
   doc.addMessage(m);
   doc.write(System.err);
   TestNavajoConfig tnc = new TestNavajoConfig();
   tnc.writeConfig("aap.xml", doc);
   Navajo read = NavajoFactory.getInstance().createNavajo(tnc.getConfig("aap.xml"));
   read.write(System.err);
 }
Example #4
0
 private Navajo createInitNavajo() throws NavajoException {
   Navajo n = NavajoFactory.getInstance().createNavajo();
   Header h = NavajoFactory.getInstance().createHeader(n, "InitRss", "unknown", "unknown", -1);
   n.addHeader(h);
   Message m = NavajoFactory.getInstance().createMessage(n, "Rss");
   n.addMessage(m);
   Property p =
       NavajoFactory.getInstance()
           .createProperty(n, "URL", Property.STRING_PROPERTY, "", 0, null, Property.DIR_IN);
   m.addProperty(p);
   Method mm = NavajoFactory.getInstance().createMethod(n, "Rss", null);
   n.addMethod(mm);
   return n;
 }
Example #5
0
  private void addImage(Message channelMessage, Image image) throws NavajoException {
    if (image == null) {
      return;
    }
    Navajo n = channelMessage.getRootDoc();
    Message imageMessage = NavajoFactory.getInstance().createMessage(n, "Image");
    channelMessage.addMessage(imageMessage);
    addProperty(imageMessage, "Title", image.getTitle(), Property.STRING_PROPERTY);
    addProperty(imageMessage, "Link", image.getLink(), Property.STRING_PROPERTY);
    addProperty(imageMessage, "Description", image.getDescription(), Property.STRING_PROPERTY);
    addProperty(imageMessage, "Width", image.getWidth(), Property.INTEGER_PROPERTY);
    addProperty(imageMessage, "Height", image.getWidth(), Property.INTEGER_PROPERTY);

    addProperty(imageMessage, "Url", image.getUrl(), Property.STRING_PROPERTY);
    addProperty(imageMessage, "ImageData", image.getUrl(), Property.STRING_PROPERTY);
    URL u;
    try {
      u = new URL(image.getUrl().getText());
      InputStream is = u.openStream();
      imageMessage.getProperty("ImageData").setAnyValue(new Binary(is));
      is.close();
    } catch (MalformedURLException e) {
      logger.error("Error: ", e);
    } catch (IOException e) {
      logger.error("Error: ", e);
    }
  }
Example #6
0
 @Override
 public final Navajo doSimpleSend(Navajo out, String method) throws ClientException {
   if (out == null) {
     out = NavajoFactory.getInstance().createNavajo();
   }
   return doSimpleSend(out, method, 0);
 }
Example #7
0
 private Property createBinaryProperty(String name, Binary value) {
   Property result =
       NavajoFactory.getInstance()
           .createProperty(assemble, name, Property.BINARY_PROPERTY, null, 0, "", Property.DIR_IN);
   result.setAnyValue(value);
   return result;
 }
Example #8
0
 private Message getMessage(Message parent, String name) throws NavajoException {
   Message m = null;
   m = NavajoFactory.getInstance().createMessage(callingAccess.getOutputDoc(), name);
   if (parent != null) {
     parent.addMessage(m);
   } else {
     callingAccess.getOutputDoc().addMessage(m);
   }
   return m;
 }
Example #9
0
  private void addItem(Message m, Item i) throws NavajoException {
    Navajo n = m.getRootDoc();

    Message itemMessage =
        NavajoFactory.getInstance().createMessage(n, "Rss", Message.MSG_TYPE_ARRAY_ELEMENT);
    m.addMessage(itemMessage);
    addProperty(itemMessage, "Title", i.getTitle(), Property.STRING_PROPERTY);
    addProperty(itemMessage, "Link", i.getLink(), Property.STRING_PROPERTY);
    addProperty(itemMessage, "Description", i.getDescription(), Property.MEMO_PROPERTY);
    addProperty(itemMessage, "Author", i.getAuthor(), Property.STRING_PROPERTY);
    addProperty(itemMessage, "PubDate", i.getPubDate(), Property.STRING_PROPERTY);
    addProperty(itemMessage, "Source", i.getSource(), Property.STRING_PROPERTY);
  }
 public void store() throws MappableException, UserException {
   File f = new File(pathPrefix);
   if (!f.exists()) {
     if (!f.mkdirs()) {
       throw new MappableException("Could not open directory, and could also not create it.");
     }
   }
   Navajo n = NavajoFactory.getInstance().createNavajo();
   Message filesMessage =
       NavajoFactory.getInstance().createMessage(n, "Files", Message.MSG_TYPE_ARRAY);
   try {
     n.addMessage(filesMessage);
   } catch (NavajoException ex2) {
     ex2.printStackTrace();
   }
   File[] files;
   if (fileNameFilter != null) {
     FilenameFilter ff =
         new FilenameFilter() {
           public boolean accept(File f, String s) {
             return s.endsWith(fileNameFilter);
           }
         };
     files = f.listFiles(ff);
   } else {
     files = f.listFiles();
   }
   for (int i = 0; i < files.length; i++) {
     try {
       Message m = createFileMessage(filesMessage, files[i], descriptionPath);
       if (m != null) {
         filesMessage.addMessage(m);
       }
     } catch (NavajoException ex1) {
       ex1.printStackTrace();
     }
   }
   access.setOutputDoc(n);
 }
Example #11
0
  public static void main(String[] args) throws Exception {
    InputStream is = GetPart.class.getResourceAsStream("testdata.xml");
    Navajo nn = NavajoFactory.getInstance().createNavajo(is);
    is.close();
    nn.write(System.err);
    Binary b = (Binary) nn.getProperty("MailBox/Mail@0/Content").getTypedValue();
    logger.info("Length: " + b.getLength() + " type: " + b.guessContentType());
    GetMailNavajo gp = new GetMailNavajo();
    gp.reset();
    gp.insertOperand(b);
    gp.insertOperand(0);
    Navajo result = (Navajo) gp.evaluate();
    result.write(System.err);
    //		logger.info("Length of part 0: "+result.getLength());

  }
Example #12
0
  private void addProperty(Message m, String name, BasicElement e, String type, String attribute)
      throws NavajoException {
    if (e == null) {
      return;
    }
    Navajo n = m.getRootDoc();
    String text = null;
    if (attribute == null) {
      text = e.getText();
    } else {
      text = e.getAttribute(attribute);
    }

    Property p =
        NavajoFactory.getInstance().createProperty(n, name, type, text, 0, null, Property.DIR_OUT);
    m.addProperty(p);
  }
  @Test
  public void testEvaluateExpression() throws Exception {

    DispatcherFactory df = new DispatcherFactory(new TestDispatcher(new TestNavajoConfig()));

    FunctionInterface fi = fff.getInstance(cl, "EvaluateExpression");
    fi.reset();
    Navajo doc = createTestNavajo();
    Header h = NavajoFactory.getInstance().createHeader(doc, "aap", "noot", "mies", -1);
    doc.addHeader(h);

    fi.setInMessage(doc);

    fi.insertOperand("true");

    Object o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
  }
Example #14
0
 protected void generateConnectionError(Navajo n, int id, String description) {
   try {
     Message conditionError =
         NavajoFactory.getInstance().createMessage(n, "ConditionErrors", Message.MSG_TYPE_ARRAY);
     n.addMessage(conditionError);
     Message conditionErrorElt = NavajoFactory.getInstance().createMessage(n, "ConditionErrors");
     conditionError.addMessage(conditionErrorElt);
     Property p1 =
         NavajoFactory.getInstance()
             .createProperty(
                 n, "Id", Property.INTEGER_PROPERTY, id + "", 10, "Id", Property.DIR_OUT);
     Property p2 =
         NavajoFactory.getInstance()
             .createProperty(
                 n,
                 "Description",
                 Property.INTEGER_PROPERTY,
                 description,
                 10,
                 "Omschrijving",
                 Property.DIR_OUT);
     Property p3 =
         NavajoFactory.getInstance()
             .createProperty(
                 n,
                 "FailedExpression",
                 Property.INTEGER_PROPERTY,
                 "",
                 10,
                 "FailedExpression",
                 Property.DIR_OUT);
     Property p4 =
         NavajoFactory.getInstance()
             .createProperty(
                 n,
                 "EvaluatedExpression",
                 Property.INTEGER_PROPERTY,
                 "",
                 10,
                 "EvaluatedExpression",
                 Property.DIR_OUT);
     conditionErrorElt.addProperty(p1);
     conditionErrorElt.addProperty(p2);
     conditionErrorElt.addProperty(p3);
     conditionErrorElt.addProperty(p4);
   } catch (NavajoException ex) {
     logger.error("Error: ", ex);
   }
 }
  @Test
  public void testExecuteScript() throws Exception {

    DispatcherFactory df = new DispatcherFactory(new TestDispatcher(new TestNavajoConfig()));

    FunctionInterface fi = fff.getInstance(cl, "ExecuteScript");
    fi.reset();
    Navajo doc = createTestNavajo();
    Header h = NavajoFactory.getInstance().createHeader(doc, "aap", "noot", "mies", -1);
    doc.addHeader(h);

    fi.setInMessage(doc);

    fi.insertOperand("<tsl/>");

    try {
      Object o = fi.evaluateWithTypeChecking();
    } catch (Exception cnfe) {
    }
  }
  private Navajo createTestNavajo() throws Exception {
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message array = NavajoFactory.getInstance().createMessage(doc, "Aap");
    array.setType(Message.MSG_TYPE_ARRAY);
    Message array1 = NavajoFactory.getInstance().createMessage(doc, "Aap");
    array.addElement(array1);
    doc.addMessage(array);
    Property p =
        NavajoFactory.getInstance()
            .createProperty(doc, "Noot", Property.INTEGER_PROPERTY, "10", 10, "", "in");
    array1.addProperty(p);

    Message single = NavajoFactory.getInstance().createMessage(doc, "Single");
    doc.addMessage(single);
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "Selectie", "1", "", "in");
    p2.addSelection(NavajoFactory.getInstance().createSelection(doc, "key", "value", true));
    single.addProperty(p2);
    Property p3 =
        NavajoFactory.getInstance()
            .createProperty(doc, "Vuur", Property.INTEGER_PROPERTY, "10", 10, "", "out");
    single.addProperty(p3);

    return doc;
  }
 private final void setupProp(MessageTable mm, Operand val, int column) {
   if (val != null) {
     Property p = null;
     try {
       p =
           NavajoFactory.getInstance()
               .createProperty(
                   null,
                   mm.getColumnId(column),
                   val.type,
                   "",
                   0,
                   mm.getColumnName(column),
                   Property.DIR_OUT);
       p.setAnyValue(val.value);
     } catch (NavajoException ex1) {
       logger.error("Error detected", ex1);
     }
     myPropComponent.setProperty(p);
     // myPropComponent
     // logger.debug("TYPE: "+p.getType()+" name: "+p.getName()+" value: "+p.getValue());
   }
 }
Example #18
0
  @Override
  public Object evaluate() throws TMLExpressionException {
    Binary b = (Binary) getOperand(0);
    DataSource ds = new BinaryDataSource(b);
    Navajo result = NavajoFactory.getInstance().createNavajo();
    Message mail = NavajoFactory.getInstance().createMessage(result, "Mail");
    result.addMessage(mail);
    Message parts =
        NavajoFactory.getInstance().createMessage(result, "Parts", Message.MSG_TYPE_ARRAY);
    mail.addMessage(parts);
    try {
      MimeMultipart mmp = getMultipartMessage(ds);
      if (mmp != null) {
        for (int i = 0; i < mmp.getCount(); i++) {
          BodyPart bp = mmp.getBodyPart(i);
          String type = bp.getContentType();
          Binary partBinary = new Binary(bp.getInputStream(), false);

          if (i == 0) {
            //					Property content = NavajoFactory.getInstance().createProperty(result, "Content",
            // Property.BINARY_PROPERTY, null, 0,"",Property.DIR_OUT);
            Property mimeType =
                NavajoFactory.getInstance()
                    .createProperty(
                        result,
                        "ContentType",
                        Property.STRING_PROPERTY,
                        type,
                        0,
                        "",
                        Property.DIR_OUT);
            //					mail.addProperty(content);
            mail.addProperty(mimeType);
            //					content.setAnyValue(partBinary);
            partBinary.setMimeType(type);
          }
          Message element =
              parts.addElement(
                  NavajoFactory.getInstance()
                      .createMessage(result, "Parts", Message.MSG_TYPE_ARRAY_ELEMENT));
          Property content =
              NavajoFactory.getInstance()
                  .createProperty(
                      result, "Content", Property.BINARY_PROPERTY, null, 0, "", Property.DIR_OUT);
          element.addProperty(content);
          Property mimeType =
              NavajoFactory.getInstance()
                  .createProperty(
                      result,
                      "ContentType",
                      Property.STRING_PROPERTY,
                      type,
                      0,
                      "",
                      Property.DIR_OUT);
          element.addProperty(mimeType);
          content.setAnyValue(partBinary);
          partBinary.setMimeType(type);
          content.addSubType("browserMime=" + type);
        }
      } else {
        String type = b.guessContentType();
        MimeMessage mm =
            new MimeMessage(Session.getDefaultInstance(new Properties()), b.getDataAsStream());

        //				mm.get

        Binary body = new Binary(mm.getInputStream(), false);
        //				mm.getC
        Message element =
            parts.addElement(
                NavajoFactory.getInstance()
                    .createMessage(result, "Parts", Message.MSG_TYPE_ARRAY_ELEMENT));
        Property content =
            NavajoFactory.getInstance()
                .createProperty(
                    result, "Content", Property.BINARY_PROPERTY, null, 0, "", Property.DIR_OUT);
        element.addProperty(content);
        Property mimeType =
            NavajoFactory.getInstance()
                .createProperty(
                    result, "ContentType", Property.STRING_PROPERTY, type, 0, "", Property.DIR_OUT);
        Property messageMimeType =
            NavajoFactory.getInstance()
                .createProperty(
                    result, "ContentType", Property.STRING_PROPERTY, type, 0, "", Property.DIR_OUT);
        mail.addProperty(messageMimeType);
        element.addProperty(mimeType);
        content.setAnyValue(body);
        b.setMimeType(mm.getContentType());
        content.addSubType("browserMime=" + type);
      }
      return result;
    } catch (MessagingException e) {
      logger.error("Error: ", e);
    } catch (IOException e) {
      logger.error("Error: ", e);
    }
    return null;
  }
Example #19
0
  @Override
  public final Navajo doSimpleSend(Navajo out, String method, Integer retries)
      throws ClientException {

    if (bearerToken == null) {
      if (username == null) {
        throw new ClientException(1, 1, "No username set!");
      }
      if (password == null) {
        throw new ClientException(1, 1, "No password set!");
      }
    }
    if (getCurrentHost() == null) {
      throw new ClientException(1, 1, "No host set!");
    }

    // NOTE: prefix persistence key with method, because same Navajo object
    // could be used as a request
    // for multiple methods!

    // ============ compared services ===================

    /** Make sure that same Navajo is not used simultaneously. */
    synchronized (out) {
      // ====================================================

      Header header = out.getHeader();
      String callingService = null;
      if (header == null) {
        header = NavajoFactory.getInstance().createHeader(out, method, username, password, -1);
        out.addHeader(header);
      } else {
        callingService = header.getRPCName();
        header.setRPCName(method);
        header.setRPCUser(username);
        header.setRPCPassword(password);
      }
      // ALWAY SET REQUEST ID AT THIS POINT.
      if (header.getRequestId() != null && header.getRequestId().equals("42")) {
        System.err.println("ENCOUNTERED TEST!!!");
      } else {
        header.setRequestId(Guid.create());
      }
      String sessionToken = getSessionTokenProvider().getSessionToken();
      header.setHeaderAttribute("clientToken", sessionToken);
      header.setHeaderAttribute("clientInfo", getSystemInfoProvider().toString());

      for (String key : navajoHeaders.keySet()) {
        header.setHeaderAttribute(key, navajoHeaders.get(key));
      }
      // ========= Adding globalMessages

      long clientTime = 0;
      try {
        if (out.getHeader() != null) {
          processPiggybackData(out.getHeader());
        }

        Navajo n = null;

        long timeStamp = System.currentTimeMillis();

        n = doTransaction(out, allowCompression, retries, 0);

        if (n.getHeader() != null) {
          n.getHeader().setHeaderAttribute("sourceScript", callingService);
          clientTime = (System.currentTimeMillis() - timeStamp);
          n.getHeader().setHeaderAttribute("clientTime", "" + clientTime);
          String tot = n.getHeader().getHeaderAttribute("serverTime");
          long totalTime = -1;
          if (tot != null && !"".equals(tot)) {
            totalTime = Long.parseLong(tot);
            n.getHeader().setHeaderAttribute("transferTime", "" + (clientTime - totalTime));
          }
          Map<String, String> headerAttributes = n.getHeader().getHeaderAttributes();
          Map<String, String> pbd = new HashMap<String, String>(headerAttributes);
          pbd.put("type", "performanceStats");
          pbd.put("service", method);
          synchronized (piggyBackData) {
            piggyBackData.add(pbd);
          }
        } else {
          logger.info("Null header in input message?");
        }

        return n;
      } catch (ClientException e) {
        throw e;
      } catch (Exception e) {
        logger.error("Error: ", e);
        throw new ClientException(-1, -1, e.getMessage(), e);
      }
    }
  }
Example #20
0
  private Observable<Navajo> processNavajoEvent(
      NavajoStreamEvent n, Subscriber<? super Navajo> subscriber) {
    switch (n.type()) {
      case NAVAJO_STARTED:
        createHeader((NavajoHead) n.body());
        return Observable.<Navajo>empty();

      case MESSAGE_STARTED:
        Message prMessage = null;
        if (!messageStack.isEmpty()) {
          prMessage = messageStack.peek();
        }
        String mode = (String) n.attribute("mode");

        Message msg =
            NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_SIMPLE);
        msg.setMode(mode);
        if (prMessage == null) {
          assemble.addMessage(msg);
        } else {
          prMessage.addMessage(msg);
        }
        messageStack.push(msg);
        tagStack.push(n.path());
        return Observable.<Navajo>empty();
      case MESSAGE:
        Message msgParent = messageStack.pop();
        tagStack.pop();
        Msg mm = (Msg) n.body();
        List<Prop> msgProps = mm.properties();
        for (Prop e : msgProps) {
          msgParent.addProperty(createTmlProperty(e));
        }
        for (Entry<String, Binary> e : pushBinaries.entrySet()) {
          msgParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
        }
        pushBinaries.clear();
        return Observable.<Navajo>empty();
      case ARRAY_STARTED:
        tagStack.push(n.path());
        String path = currentPath();
        AtomicInteger cnt = arrayCounts.get(path);
        if (cnt == null) {
          cnt = new AtomicInteger();
          arrayCounts.put(path, cnt);
        }
        //			cnt.incrementAndGet();
        Message parentMessage = null;
        if (!messageStack.isEmpty()) {
          parentMessage = messageStack.peek();
        }

        Message arr =
            NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_ARRAY);
        if (parentMessage == null) {
          assemble.addMessage(arr);
        } else {
          parentMessage.addMessage(arr);
        }
        messageStack.push(arr);
        return Observable.<Navajo>empty();
      case ARRAY_DONE:
        String apath = currentPath();
        arrayCounts.remove(apath);
        this.messageStack.pop();
        return Observable.<Navajo>empty();
      case ARRAY_ELEMENT_STARTED:
        String arrayElementName = tagStack.peek();
        String arrayPath = currentPath();
        AtomicInteger currentCount = arrayCounts.get(arrayPath);
        String ind = "@" + currentCount.getAndIncrement();
        tagStack.push(ind);
        arrayPath = currentPath();
        Message newElt =
            NavajoFactory.getInstance()
                .createMessage(assemble, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT);
        Message arrParent = messageStack.peek();
        arrParent.addElement(newElt);
        messageStack.push(newElt);
        return Observable.<Navajo>empty();
      case ARRAY_ELEMENT:
        tagStack.pop();
        Message elementParent = messageStack.pop();
        Msg msgElement = (Msg) n.body();
        List<Prop> elementProps = msgElement.properties();
        for (Prop e : elementProps) {
          elementParent.addProperty(createTmlProperty(e));
        }
        for (Entry<String, Binary> e : pushBinaries.entrySet()) {
          elementParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
        }
        pushBinaries.clear();

        return Observable.<Navajo>empty();

      case MESSAGE_DEFINITION_STARTED:
        // TODO
        return Observable.<Navajo>empty();
      case MESSAGE_DEFINITION:
        // TODO
        //			tagStack.push(n.path());
        //			deferredMessages.get(stripIndex(n.path())).setDefinitionMessage((Message) n.body());
        return Observable.<Navajo>empty();
      case NAVAJO_DONE:
        if (subscriber != null) {
          subscriber.onNext(assemble);
        }
        return Observable.<Navajo>just(assemble);

      case BINARY_STARTED:
        try {
          String name = n.path();
          this.currentBinary = new Binary();
          this.currentBinary.startPushRead();
          this.pushBinaries.put(name, currentBinary);
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }

      case BINARY_CONTENT:
        try {
          if (this.currentBinary == null) {
            // whoops;
          }
          this.currentBinary.pushContent((String) n.body());
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }

      case BINARY_DONE:
        try {
          this.currentBinary.finishPushContent();
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }
      default:
        return Observable.<Navajo>empty();
    }
  }
Example #21
0
public class NavajoStreamCollector {

  private Navajo assemble = NavajoFactory.getInstance().createNavajo();

  //	private final Map<String,Message> deferredMessages = new HashMap<>();
  //	private final List<Stack<String>> deferredPaths = new ArrayList<>();
  private final Map<String, AtomicInteger> arrayCounts = new HashMap<>();

  private final Stack<Message> messageStack = new Stack<Message>();

  private Stack<String> tagStack = new Stack<>();

  private final Map<String, Binary> pushBinaries = new HashMap<>();

  private Binary currentBinary;

  public NavajoStreamCollector() {}

  // TODO use just the operators

  public Observable<Navajo> feed(final NavajoStreamEvent streamEvents) {
    Observable<Navajo> processNavajoEvent = processNavajoEvent(streamEvents, null);
    return processNavajoEvent;
  }

  public void feed(final NavajoStreamEvent streamEvents, Subscriber<? super Navajo> subscriber) {
    processNavajoEvent(streamEvents, subscriber);
  }

  private String currentPath() {
    StringBuilder sb = new StringBuilder();
    for (String path : tagStack) {
      sb.append(path);
      sb.append('/');
    }
    int len = sb.length();
    if (sb.charAt(len - 1) == '/') {
      sb.deleteCharAt(len - 1);
    }
    return sb.toString();
  }

  private Observable<Navajo> processNavajoEvent(
      NavajoStreamEvent n, Subscriber<? super Navajo> subscriber) {
    switch (n.type()) {
      case NAVAJO_STARTED:
        createHeader((NavajoHead) n.body());
        return Observable.<Navajo>empty();

      case MESSAGE_STARTED:
        Message prMessage = null;
        if (!messageStack.isEmpty()) {
          prMessage = messageStack.peek();
        }
        String mode = (String) n.attribute("mode");

        Message msg =
            NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_SIMPLE);
        msg.setMode(mode);
        if (prMessage == null) {
          assemble.addMessage(msg);
        } else {
          prMessage.addMessage(msg);
        }
        messageStack.push(msg);
        tagStack.push(n.path());
        return Observable.<Navajo>empty();
      case MESSAGE:
        Message msgParent = messageStack.pop();
        tagStack.pop();
        Msg mm = (Msg) n.body();
        List<Prop> msgProps = mm.properties();
        for (Prop e : msgProps) {
          msgParent.addProperty(createTmlProperty(e));
        }
        for (Entry<String, Binary> e : pushBinaries.entrySet()) {
          msgParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
        }
        pushBinaries.clear();
        return Observable.<Navajo>empty();
      case ARRAY_STARTED:
        tagStack.push(n.path());
        String path = currentPath();
        AtomicInteger cnt = arrayCounts.get(path);
        if (cnt == null) {
          cnt = new AtomicInteger();
          arrayCounts.put(path, cnt);
        }
        //			cnt.incrementAndGet();
        Message parentMessage = null;
        if (!messageStack.isEmpty()) {
          parentMessage = messageStack.peek();
        }

        Message arr =
            NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_ARRAY);
        if (parentMessage == null) {
          assemble.addMessage(arr);
        } else {
          parentMessage.addMessage(arr);
        }
        messageStack.push(arr);
        return Observable.<Navajo>empty();
      case ARRAY_DONE:
        String apath = currentPath();
        arrayCounts.remove(apath);
        this.messageStack.pop();
        return Observable.<Navajo>empty();
      case ARRAY_ELEMENT_STARTED:
        String arrayElementName = tagStack.peek();
        String arrayPath = currentPath();
        AtomicInteger currentCount = arrayCounts.get(arrayPath);
        String ind = "@" + currentCount.getAndIncrement();
        tagStack.push(ind);
        arrayPath = currentPath();
        Message newElt =
            NavajoFactory.getInstance()
                .createMessage(assemble, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT);
        Message arrParent = messageStack.peek();
        arrParent.addElement(newElt);
        messageStack.push(newElt);
        return Observable.<Navajo>empty();
      case ARRAY_ELEMENT:
        tagStack.pop();
        Message elementParent = messageStack.pop();
        Msg msgElement = (Msg) n.body();
        List<Prop> elementProps = msgElement.properties();
        for (Prop e : elementProps) {
          elementParent.addProperty(createTmlProperty(e));
        }
        for (Entry<String, Binary> e : pushBinaries.entrySet()) {
          elementParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
        }
        pushBinaries.clear();

        return Observable.<Navajo>empty();

      case MESSAGE_DEFINITION_STARTED:
        // TODO
        return Observable.<Navajo>empty();
      case MESSAGE_DEFINITION:
        // TODO
        //			tagStack.push(n.path());
        //			deferredMessages.get(stripIndex(n.path())).setDefinitionMessage((Message) n.body());
        return Observable.<Navajo>empty();
      case NAVAJO_DONE:
        if (subscriber != null) {
          subscriber.onNext(assemble);
        }
        return Observable.<Navajo>just(assemble);

      case BINARY_STARTED:
        try {
          String name = n.path();
          this.currentBinary = new Binary();
          this.currentBinary.startPushRead();
          this.pushBinaries.put(name, currentBinary);
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }

      case BINARY_CONTENT:
        try {
          if (this.currentBinary == null) {
            // whoops;
          }
          this.currentBinary.pushContent((String) n.body());
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }

      case BINARY_DONE:
        try {
          this.currentBinary.finishPushContent();
          return Observable.<Navajo>empty();
        } catch (IOException e1) {
          return Observable.error(e1);
        }
      default:
        return Observable.<Navajo>empty();
    }
  }

  private Property createBinaryProperty(String name, Binary value) {
    Property result =
        NavajoFactory.getInstance()
            .createProperty(assemble, name, Property.BINARY_PROPERTY, null, 0, "", Property.DIR_IN);
    result.setAnyValue(value);
    return result;
  }

  private void createHeader(NavajoHead head) {
    Header h =
        NavajoFactory.getInstance()
            .createHeader(assemble, head.name(), head.username(), head.password(), -1);
    assemble.addHeader(h);
  }

  private Property createTmlProperty(Prop p) {
    Property result;
    if (Property.SELECTION_PROPERTY.equals(p.type())) {
      result =
          NavajoFactory.getInstance()
              .createProperty(assemble, p.name(), p.cardinality(), p.description(), p.direction());
      for (Select s : p.selections()) {
        Selection sel =
            NavajoFactory.getInstance()
                .createSelection(assemble, s.name(), s.value(), s.selected());
        result.addSelection(sel);
      }
    } else {
      result =
          NavajoFactory.getInstance()
              .createProperty(
                  assemble,
                  p.name(),
                  p.type() == null ? Property.STRING_PROPERTY : p.type(),
                  null,
                  p.length(),
                  p.description(),
                  p.direction());
      result.setAnyValue(p.value());
      if (p.type() != null) {
        result.setType(p.type());
      }
    }
    return result;
  }
}
Example #22
0
  protected void performComponentMethod(
      final String name, final TipiComponentMethod compMeth, TipiEvent event) {
    if ("loadDefinition".equals(name)) {
      try {
        if (resourceCodeBase == null) {
          stc.getContext().setGenericResourceLoader(myContext.getGenericResourceLoader());
        }
        if (tipiCodeBase == null) {
          stc.getContext().setTipiResourceLoader(myContext.getTipiResourceLoader());
        }
        Operand oo = compMeth.getEvaluatedParameter("location", event);
        String loc = (String) oo.value;
        parseLocation(loc, getParentExtension());
      } catch (Exception e) {
        logger.error("Error: ", e);
      }
    }
    if ("switch".equals(name)) {
      try {
        if (resourceCodeBase == null) {
          stc.getContext().setGenericResourceLoader(myContext.getGenericResourceLoader());
        }
        if (tipiCodeBase == null) {
          stc.getContext().setTipiResourceLoader(myContext.getTipiResourceLoader());
        }
        Operand oo = compMeth.getEvaluatedParameter("definition", event);
        String nameVal = (String) oo.value;

        switchToDefinition(nameVal);
      } catch (Exception e) {
        logger.error("Error: ", e);
      }
    }
    if ("addStartupProperty".equals(name)) {
      try {
        Operand nameOperand = compMeth.getEvaluatedParameter("propertyName", event);
        String nameVal = (String) nameOperand.value;
        Operand valueOperand = compMeth.getEvaluatedParameter("value", event);
        String vakueVal = (String) valueOperand.value;
        logger.debug("Adding: " + nameVal + " value: " + vakueVal);
        stc.getContext().setSystemProperty(nameVal, vakueVal);
        // ((Container) getContainer()).add((Component)
        // stc.getContext().getTopLevel(), BorderLayout.CENTER);
      } catch (Exception e) {
        logger.error("Error: ", e);
      }
    }
    if ("loadNavajo".equals(name)) {
      try {
        Operand navajoOperand = compMeth.getEvaluatedParameter("navajo", event);
        Navajo navajo = (Navajo) navajoOperand.value;
        Operand methodOperand = compMeth.getEvaluatedParameter("method", event);
        String method = (String) methodOperand.value;
        loadNavajo(navajo, method);
        // ((Container) getContainer()).add((Component)
        // stc.getContext().getTopLevel(), BorderLayout.CENTER);
      } catch (Exception e) {
        logger.error("Error: ", e);
      }
    }
    if ("loadAllNavajo".equals(name)) {
      try {
        Operand messageOperand = compMeth.getEvaluatedParameter("message", event);
        Message arrayMessage = (Message) messageOperand.value;
        List<Message> elements = arrayMessage.getAllMessages();
        for (int i = 0; i < elements.size(); i++) {
          Message current = elements.get(i);
          Property ob = current.getProperty("Navajo");
          Binary b = (Binary) ob.getTypedValue();
          Navajo n = NavajoFactory.getInstance().createNavajo(b.getDataAsStream());
          loadNavajo(n, n.getHeader().getRPCName());
        }
      } catch (Exception e) {
        logger.error("Error: ", e);
      }
    }
  }
Example #23
0
 private void createHeader(NavajoHead head) {
   Header h =
       NavajoFactory.getInstance()
           .createHeader(assemble, head.name(), head.username(), head.password(), -1);
   assemble.addHeader(h);
 }
  private Message createFileMessage(Message parent, File entry, String pathToDescription)
      throws NavajoException {
    Message m =
        NavajoFactory.getInstance()
            .createMessage(parent.getRootDoc(), parent.getName(), Message.MSG_TYPE_ARRAY_ELEMENT);
    Property filePath =
        NavajoFactory.getInstance()
            .createProperty(
                parent.getRootDoc(),
                "File",
                Property.STRING_PROPERTY,
                entry.toString(),
                0,
                "",
                Property.DIR_OUT);
    Property fileFullPath =
        NavajoFactory.getInstance()
            .createProperty(
                parent.getRootDoc(),
                "FilePath",
                Property.STRING_PROPERTY,
                entry.getAbsoluteFile().toString(),
                0,
                "",
                Property.DIR_OUT);
    Property description =
        NavajoFactory.getInstance()
            .createProperty(
                parent.getRootDoc(),
                "Description",
                Property.STRING_PROPERTY,
                "No description",
                0,
                "",
                Property.DIR_OUT);
    Property fileSize =
        NavajoFactory.getInstance()
            .createProperty(
                parent.getRootDoc(),
                "Size",
                Property.INTEGER_PROPERTY,
                "" + entry.length(),
                0,
                "",
                Property.DIR_OUT);

    if (pathToDescription != null || conditionPath != null || !columns.isEmpty()) {
      try {

        logger.debug("Reading description. Might be slow");
        logger.debug("Entry: " + entry);

        FileInputStream fis = new FileInputStream(entry);
        Navajo n = NavajoFactory.getInstance().createNavajo(fis);
        fis.close();
        Property desc = n.getProperty(pathToDescription);

        if (conditionPath != null) {
          logger.debug("Evaluating: " + conditionPath);
          Operand o = Expression.evaluate(conditionPath, n);
          logger.debug("Result: " + o.value);
          Boolean b = (Boolean) o.value;
          if (!b.booleanValue()) {
            return null;
          }
        }

        for (int i = 0; i < columns.size(); i++) {
          String current = columns.get(i);
          Property p = n.getProperty(current);
          if (p != null) {
            //          Property q = (Property)p.clone();
            Property q =
                NavajoFactory.getInstance()
                    .createProperty(
                        parent.getRootDoc(),
                        "Column" + i,
                        p.getType(),
                        p.getValue(),
                        p.getLength(),
                        p.getDescription(),
                        p.getDescription());
            //          q.setName("Column"+i);
            m.addProperty(q);
          }
        }
        if (desc != null) {
          description.setValue(desc.getValue());
        }

      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    m.addProperty(filePath);
    m.addProperty(fileFullPath);
    m.addProperty(description);
    m.addProperty(fileSize);

    return m;
  }
Example #25
0
 public Navajo readConfig(String s) throws IOException {
   Navajo config = NavajoFactory.getInstance().createNavajo(getConfig(s));
   return config;
 }
Example #26
0
  public static final Navajo constructFromRequest(HttpServletRequest request)
      throws NavajoException {

    Navajo result = NavajoFactory.getInstance().createNavajo();
    Enumeration<String> all = request.getParameterNames();

    // Construct TML document from request parameters.
    while (all.hasMoreElements()) {
      String parameter = all.nextElement().toString();
      if (parameter.indexOf("/") != -1) {
        StringTokenizer typedParameter = new StringTokenizer(parameter, "|");
        String propertyName = typedParameter.nextToken();
        String type =
            (typedParameter.hasMoreTokens()
                ? typedParameter.nextToken()
                : Property.STRING_PROPERTY);
        String value = request.getParameter(parameter);

        Message msg =
            com.dexels.navajo.mapping.MappingUtils.getMessageObject(
                parameter, null, false, result, false, "", -1);
        String propName =
            com.dexels.navajo.mapping.MappingUtils.getStrippedPropertyName(propertyName);
        Property prop = null;

        if (propName.indexOf(":") == -1) {
          prop =
              NavajoFactory.getInstance()
                  .createProperty(result, propName, type, value, 0, "", Property.DIR_IN);
          msg.addProperty(prop);
        } else {
          StringTokenizer selProp = new StringTokenizer(propName, ":");
          propertyName = selProp.nextToken();
          selProp.nextToken();

          prop = msg.getProperty(propertyName);
          if (prop == null) {
            prop =
                NavajoFactory.getInstance()
                    .createProperty(result, propertyName, "+", "", Property.DIR_IN);
            msg.addProperty(prop);
          } else {
            prop.setType(Property.SELECTION_PROPERTY);
            prop.setCardinality("+");
          }

          StringTokenizer allValues = new StringTokenizer(value, ",");
          while (allValues.hasMoreTokens()) {
            String val = allValues.nextToken();
            Selection sel = NavajoFactory.getInstance().createSelection(result, val, val, true);
            prop.addSelection(sel);
          }
        }
      }
    }

    String service = request.getParameter("service");
    String type = request.getParameter("type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if (username == null) {
      username = "******";
      password = "";
    }

    if ((type == null) || (type.equals(""))) {
      type = "xml";
    }

    if (password == null) {
      password = "";
    }
    long expirationInterval = -1;
    String expiration = request.getParameter("expiration");

    if ((expiration == null) || (expiration.equals(""))) {
      expirationInterval = -1;
    } else {
      try {
        expirationInterval = Long.parseLong(expiration);
      } catch (Exception e) {
        // System.out.println("invalid expiration interval: " +
        // expiration);
      }
    }

    Header h =
        NavajoFactory.getInstance()
            .createHeader(result, service, username, password, expirationInterval);
    result.addHeader(h);
    return result;
  }
Example #27
0
  /**
   * Handle a request.
   *
   * @param request
   * @param response
   * @throws IOException
   * @throws ServletException
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    MDC.clear();
    Date created = new java.util.Date();
    long start = created.getTime();

    String sendEncoding = request.getHeader("Accept-Encoding");
    String recvEncoding = request.getHeader("Content-Encoding");

    MDC.put("Accept-Encoding", sendEncoding);
    MDC.put("Content-Encoding", recvEncoding);
    DispatcherInterface dis = null;
    BufferedReader r = null;
    BufferedWriter out = null;
    try {

      Navajo in = null;

      if (streamingMode) {
        if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
          r =
              new BufferedReader(
                  new java.io.InputStreamReader(new InflaterInputStream(request.getInputStream())));
        } else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
          r =
              new BufferedReader(
                  new java.io.InputStreamReader(
                      new java.util.zip.GZIPInputStream(request.getInputStream()), "UTF-8"));
        } else {
          r = new BufferedReader(request.getReader());
        }
        in = NavajoFactory.getInstance().createNavajo(r);
        r.close();
        r = null;
      } else {
        logger.info(
            "Warning: Using non-streaming mode for "
                + request.getRequestURI()
                + ", file written: "
                + logfileIndex
                + ", total size: "
                + bytesWritten);
        InputStream is = request.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        copyResource(bos, is);
        is.close();
        bos.close();
        byte[] bytes = bos.toByteArray();
        try {
          if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
            r =
                new BufferedReader(
                    new java.io.InputStreamReader(
                        new InflaterInputStream(new ByteArrayInputStream(bytes))));
          } else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
            r =
                new BufferedReader(
                    new java.io.InputStreamReader(
                        new java.util.zip.GZIPInputStream(new ByteArrayInputStream(bytes)),
                        "UTF-8"));
          } else {
            r = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes)));
          }
          in = NavajoFactory.getInstance().createNavajo(r);
          if (in == null) {
            throw new Exception("Invalid Navajo");
          }
          r.close();
          r = null;
        } catch (Throwable t) {
          // Write request to temp file.
          File f = DispatcherFactory.getInstance().getTempDir();

          if (f != null) {
            bytesWritten += bytes.length;
            logfileIndex++;
            FileOutputStream fos = new FileOutputStream(new File(f, "request-" + logfileIndex));
            copyResource(fos, new ByteArrayInputStream(bytes));
            fos.close();
            PrintWriter fw =
                new PrintWriter(new FileWriter(new File(f, "exception-" + logfileIndex)));
            t.printStackTrace(fw);
            fw.flush();
            fw.close();
          }

          dumHttp(request, logfileIndex, f);
          throw new ServletException(t);
        }
      }

      long stamp = System.currentTimeMillis();
      int pT = (int) (stamp - start);

      if (in == null) {
        throw new ServletException("Invalid request.");
      }

      Header header = in.getHeader();
      if (header == null) {
        throw new ServletException("Empty Navajo header.");
      }

      dis = DispatcherFactory.getInstance();
      if (dis == null) {
        System.err.println(
            "SERIOUS: No dispatcher found. The navajo context did not initialize properly, check the logs to find out why!");
        return;
      }
      // Check for certificate.
      Object certObject = request.getAttribute("javax.servlet.request.X509Certificate");

      // Call Dispatcher with parsed TML document as argument.
      ClientInfo clientInfo =
          new ClientInfo(
              request.getRemoteAddr(),
              "unknown",
              recvEncoding,
              pT,
              (recvEncoding != null
                  && (recvEncoding.equals(COMPRESS_GZIP) || recvEncoding.equals(COMPRESS_JZLIB))),
              (sendEncoding != null
                  && (sendEncoding.equals(COMPRESS_GZIP) || sendEncoding.equals(COMPRESS_JZLIB))),
              request.getContentLength(),
              created);

      Navajo outDoc = handleTransaction(dis, in, certObject, clientInfo);

      response.setContentType("text/xml; charset=UTF-8");
      response.setHeader("Accept-Ranges", "none");
      response.setHeader("Connection", "close");

      if (recvEncoding != null && recvEncoding.equals(COMPRESS_JZLIB)) {
        response.setHeader("Content-Encoding", COMPRESS_JZLIB);
        out =
            new BufferedWriter(
                new OutputStreamWriter(
                    new DeflaterOutputStream(response.getOutputStream()), "UTF-8"));
      } else if (recvEncoding != null && recvEncoding.equals(COMPRESS_GZIP)) {
        response.setHeader("Content-Encoding", COMPRESS_GZIP);
        out =
            new BufferedWriter(
                new OutputStreamWriter(
                    new java.util.zip.GZIPOutputStream(response.getOutputStream()), "UTF-8"));
      } else {
        out = new BufferedWriter(response.getWriter());
      }

      outDoc.write(out);
      out.flush();
      out.close();

      if (in.getHeader() != null
          && outDoc.getHeader() != null
          && !Dispatcher.isSpecialwebservice(in.getHeader().getRPCName())) {
        statLogger.info(
            "("
                + dis.getApplicationId()
                + "): "
                + new java.util.Date()
                + ": "
                + outDoc.getHeader().getHeaderAttribute("accessId")
                + ":"
                + in.getHeader().getRPCName()
                + "("
                + in.getHeader().getRPCUser()
                + "):"
                + (System.currentTimeMillis() - start)
                + " ms. (st="
                + (outDoc.getHeader().getHeaderAttribute("serverTime")
                    + ",rpt="
                    + outDoc.getHeader().getHeaderAttribute("requestParseTime")
                    + ",at="
                    + outDoc.getHeader().getHeaderAttribute("authorisationTime")
                    + ",pt="
                    + outDoc.getHeader().getHeaderAttribute("processingTime")
                    + ",tc="
                    + outDoc.getHeader().getHeaderAttribute("threadCount")
                    + ",cpu="
                    + outDoc.getHeader().getHeaderAttribute("cpuload")
                    + ")"
                    + " ("
                    + sendEncoding
                    + "/"
                    + recvEncoding
                    + ")"));
      }

      out = null;

    } catch (Throwable e) {
      logger.error("Error: ", e);
      dumHttp(request, -1, null);
      if (e instanceof FatalException) {
        FatalException fe = (FatalException) e;
        if (fe.getMessage().equals("500.13")) {
          // Server too busy.
          throw new ServletException("500.13", e);
        }
      }
      throw new ServletException(e);
    } finally {
      dis = null;
      if (r != null) {
        try {
          r.close();
        } catch (Exception e) {
          // NOT INTERESTED.
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (Exception e) {
          // NOT INTERESTED.
        }
      }
    }
  }
Example #28
0
 protected static Header constructHeader(
     Navajo tbMessage, String service, String username, String password, long expirationInterval) {
   return NavajoFactory.getInstance()
       .createHeader(tbMessage, service, username, password, expirationInterval);
 }